DumpMCTracks_module.cc
Go to the documentation of this file.
1 /**
2  * @file DumpMCTracks_module.cc
3  * @brief Module dumping MCTrack information on screen
4  * @date March 21st, 2016
5  * @author Gianluca Petrillo (petrillo@fnal.gov)
6  *
7  */
8 
9 // LArSoft libraries
12 
13 // framework libraries
19 #include "fhiclcpp/types/Atom.h"
21 
22 // C/C++ standard libraries
23 #include <string>
24 #include <algorithm> // std::max()
25 
26 
27 namespace sim {
28  class DumpMCTracks;
29 } // namespace sim
30 
31 namespace {
32  using namespace fhicl;
33 
34  /// Collection of configuration parameters for the module
35  struct Config {
36  using Name = fhicl::Name;
37  using Comment = fhicl::Comment;
38 
39  fhicl::Atom<art::InputTag> InputTracks {
40  Name("InputTracks"),
41  Comment("data product with the MC tracks to be dumped")
42  };
43 
44  fhicl::Atom<std::string> OutputCategory {
45  Name("OutputCategory"),
46  Comment("name of the output stream (managed by the message facility)"),
47  "DumpMCTracks" /* default value */
48  };
49 
50  }; // struct Config
51 
52 
53  /// Returns a string describing the type of origin
54  std::string OriginDescription(simb::Origin_t origin) {
55  switch (origin) {
56  case simb::kUnknown: return "unknown";
57  case simb::kBeamNeutrino: return "beam neutrino";
58  case simb::kCosmicRay: return "cosmic rays";
59  case simb::kSuperNovaNeutrino: return "supernova neutrinos";
60  case simb::kSingleParticle: return "single particles thrown at the detector";
61  // default case is deliberately missing,
62  // so that on new additions in nutools the compiler will complain
63  } // switch
65  << "Unexpected origin type #" << ((int) origin) << "\n";
66  } // OriginDescription()
67 
68 
69  /// Prints a MC step into a stream
70  template <typename Stream>
71  void PrintMCStep(Stream&& out, sim::MCStep const& step) {
72  out << "("
73  << step.X() << ", " << step.Y() << ", " << step.Z() << ") cm, t="
74  << step.T() << " ns; momentum ("
75  << step.Px() << ", " << step.Py() << ", " << step.Pz() << "; "
76  << step.E() << ") MeV/c";
77  } // PrintMCStep()
78 
79 
80 } // local namespace
81 
82 
84  public:
85  // type to enable module parameters description by art
87 
88  /// Configuration-checking constructor
89  explicit DumpMCTracks(Parameters const& config);
90 
91  // Plugins should not be copied or assigned.
92  DumpMCTracks(DumpMCTracks const&) = delete;
93  DumpMCTracks(DumpMCTracks &&) = delete;
94  DumpMCTracks& operator = (DumpMCTracks const&) = delete;
96 
97 
98  // Operates on the event
99  virtual void analyze(art::Event const& event) override;
100 
101 
102  /**
103  * @brief Dumps the content of the specified particle in the output stream
104  * @tparam Stream the type of output stream
105  * @param out the output stream
106  * @param particle the particle to be dumped
107  * @param indent base indentation string (default: none)
108  * @param bIndentFirst if first output line should be indented (default: yes)
109  *
110  * The indent string is prepended to every line of output, with the possible
111  * exception of the first one, in case bIndentFirst is true.
112  *
113  * The output starts on the current line, and the last line is NOT broken.
114  */
115  template <typename Stream>
116  void DumpMCTrack(
117  Stream&& out, sim::MCTrack const& track,
118  std::string indent = "", bool bIndentFirst = true
119  ) const;
120 
121 
122  private:
123 
124  art::InputTag fInputTracks; ///< name of MCTrack's data product
125  std::string fOutputCategory; ///< name of the stream for output
126 
127 }; // class sim::DumpMCTracks
128 
129 
130 //------------------------------------------------------------------------------
131 //--- module implementation
132 //---
133 //------------------------------------------------------------------------------
135  : EDAnalyzer(config)
136  , fInputTracks(config().InputTracks())
137  , fOutputCategory(config().OutputCategory())
138 {}
139 
140 //------------------------------------------------------------------------------
141 template <typename Stream>
143  Stream&& out, sim::MCTrack const& track,
144  std::string indent /* = "" */, bool bIndentFirst /* = true */
145 ) const {
146  if (bIndentFirst) out << indent;
147  out
148  << "from GEANT track ID=" << track.TrackID()
149  << " PDG ID=" << track.PdgCode()
150  << " from " << OriginDescription(track.Origin())
151  << " via '" << track.Process() << "'";
152  out << "\n" << indent
153  << " starting at ";
154  ::PrintMCStep(out, track.Start());
155  out << "\n" << indent
156  << " ending at ";
157  ::PrintMCStep(out, track.End());
158 
159  std::vector<std::vector<double>> const& dQdx = track.dQdx(); // dQdx[MCStep][plane]
160  std::vector<double> const& dEdx = track.dEdx(); // dEdx[MCStep]
161  size_t const nQSteps = dQdx.size(), nESteps = dEdx.size();
162  size_t const nSteps = std::max(nQSteps, nESteps);
163  out << "\n" << indent;
164  if (nSteps > 0) {
165  out
166  << "energy information for " << nSteps
167  << " steps (dE/dX in MeV/cm, then dQ/dx per plane):";
168  for (size_t iStep = 0; iStep < nSteps; ++iStep) {
169  out << "\n" << indent
170  << " [#" << iStep << "] dE/dx=";
171  if (iStep < nESteps) out << dEdx[iStep];
172  else out << "<N/A>";
173  out << "; dQ/dx:";
174  if (iStep < nQSteps) {
175  std::vector<double> const& step_dQdx = dQdx[iStep]; // dQdx[plane]
176  for (size_t iPlane = 0; iPlane < step_dQdx.size(); ++iPlane) {
177  out << " [#" << iPlane << "] " << step_dQdx[iPlane];
178  } // for plane
179  }
180  else out << "<N/A>";
181  } // for iStep
182  }
183  else out << "no energy or charge information available";
184 
185  out << "\n" << indent
186  << "mother ID=" << track.MotherTrackID()
187  << " PDG ID=" << track.MotherPdgCode()
188  << " via '" << track.MotherProcess() << "'";
189  out << "\n" << indent
190  << " starting at ";
191  ::PrintMCStep(out, track.MotherStart());
192  out << "\n" << indent
193  << " ending at ";
194  ::PrintMCStep(out, track.MotherEnd());
195 
196  out << "\n" << indent
197  << "ancestor ID=" << track.AncestorTrackID()
198  << " PDG ID=" << track.AncestorPdgCode()
199  << " via '" << track.AncestorProcess() << "'";
200  out << "\n" << indent
201  << " starting at ";
202  ::PrintMCStep(out, track.AncestorStart());
203  out << "\n" << indent
204  << " ending at ";
205  ::PrintMCStep(out, track.AncestorEnd());
206 
207 } // sim::DumpMCTracks::DumpMCTrack()
208 
209 
210 //------------------------------------------------------------------------------
212 
213  // get the particles from the event
214  auto const& Tracks
215  = *(event.getValidHandle<std::vector<sim::MCTrack>>(fInputTracks));
216 
218  << "Event " << event.id() << ": data product '"
219  << fInputTracks.encode() << "' contains "
220  << Tracks.size() << " MCTrack objects";
221 
222  unsigned int iTrack = 0;
224  for (sim::MCTrack const& track: Tracks) {
225 
226  // a bit of a header
227  log << "\n[#" << (iTrack++) << "] ";
228  DumpMCTrack(log, track, " ", false);
229 
230  } // for
231  log << "\n";
232 
233 } // sim::DumpMCTracks::analyze()
234 
235 
236 //------------------------------------------------------------------------------
238 
239 //------------------------------------------------------------------------------
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
simb::Origin_t Origin() const
Definition: MCTrack.h:40
std::string string
Definition: nybbler.cc:12
const std::string & AncestorProcess() const
Definition: MCTrack.h:57
enum simb::_ev_origin Origin_t
event origin types
art::InputTag fInputTracks
name of MCTrack&#39;s data product
ChannelGroupService::Name Name
const MCStep & MotherEnd() const
Definition: MCTrack.h:53
unsigned int AncestorTrackID() const
Definition: MCTrack.h:56
double T() const
Definition: MCStep.h:45
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
int AncestorPdgCode() const
Definition: MCTrack.h:55
const MCStep & End() const
Definition: MCTrack.h:45
const std::vector< std::vector< double > > & dQdx() const
Definition: MCTrack.h:46
std::string encode() const
Definition: InputTag.cc:97
Class def header for mcstep data container.
unsigned int MotherTrackID() const
Definition: MCTrack.h:50
typename config_impl< T >::type Config
Definition: ModuleMacros.h:52
double dEdx(float dqdx, float Efield)
Definition: doAna.cpp:21
double Px() const
Definition: MCStep.h:46
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
static Config * config
Definition: config.cpp:1054
DumpMCTracks & operator=(DumpMCTracks const &)=delete
single particles thrown at the detector
Definition: MCTruth.h:26
double Z() const
Definition: MCStep.h:44
double Py() const
Definition: MCStep.h:47
double Y() const
Definition: MCStep.h:43
static int max(int a, int b)
Class def header for mctrack data container.
const MCStep & AncestorStart() const
Definition: MCTrack.h:58
Code to link reconstructed objects back to the MC truth information.
void DumpMCTrack(Stream &&out, sim::MCTrack const &track, std::string indent="", bool bIndentFirst=true) const
Dumps the content of the specified particle in the output stream.
int PdgCode() const
Definition: MCTrack.h:41
std::string fOutputCategory
name of the stream for output
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
const MCStep & MotherStart() const
Definition: MCTrack.h:52
#define Comment
int MotherPdgCode() const
Definition: MCTrack.h:49
const std::string & Process() const
Definition: MCTrack.h:43
const std::string & MotherProcess() const
Definition: MCTrack.h:51
Supernova neutrinos.
Definition: MCTruth.h:25
double E() const
Definition: MCStep.h:49
double Pz() const
Definition: MCStep.h:48
const MCStep & Start() const
Definition: MCTrack.h:44
double X() const
Definition: MCStep.h:42
unsigned int TrackID() const
Definition: MCTrack.h:42
const std::vector< double > & dEdx() const
Definition: MCTrack.h:47
DumpMCTracks(Parameters const &config)
Configuration-checking constructor.
virtual void analyze(art::Event const &event) override
const MCStep & AncestorEnd() const
Definition: MCTrack.h:59
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:227
Cosmic rays.
Definition: MCTruth.h:24
Event finding and building.
Beam neutrinos.
Definition: MCTruth.h:23