All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
14 #include "art/Framework/Core/EDAnalyzer.h"
15 #include "art/Framework/Core/ModuleMacros.h"
16 #include "art/Framework/Principal/Event.h"
17 #include "art/Framework/Principal/Handle.h"
18 #include "canvas/Utilities/InputTag.h"
19 #include "fhiclcpp/types/Atom.h"
20 #include "messagefacility/MessageLogger/MessageLogger.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
64  throw art::Exception(art::errors::LogicError)
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 
83 class sim::DumpMCTracks: public art::EDAnalyzer {
84  public:
85  // type to enable module parameters description by art
86  using Parameters = art::EDAnalyzer::Table<Config>;
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;
95  DumpMCTracks& operator = (DumpMCTracks &&) = 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 //------------------------------------------------------------------------------
211 void sim::DumpMCTracks::analyze(art::Event const& event) {
212 
213  // get the particles from the event
214  auto const& Tracks
215  = *(event.getValidHandle<std::vector<sim::MCTrack>>(fInputTracks));
216 
217  mf::LogVerbatim(fOutputCategory)
218  << "Event " << event.id() << ": data product '"
219  << fInputTracks.encode() << "' contains "
220  << Tracks.size() << " MCTrack objects";
221 
222  unsigned int iTrack = 0;
223  mf::LogVerbatim log(fOutputCategory);
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 //------------------------------------------------------------------------------
237 DEFINE_ART_MODULE(sim::DumpMCTracks)
238 
239 //------------------------------------------------------------------------------
process_name opflashCryo1 flashfilter analyze
simb::Origin_t Origin() const
Definition: MCTrack.h:40
Definition: Data.h:32
const std::string & AncestorProcess() const
Definition: MCTrack.h:57
art::InputTag fInputTracks
name of MCTrack&#39;s data product
const MCStep & MotherEnd() const
Definition: MCTrack.h:53
unsigned int AncestorTrackID() const
Definition: MCTrack.h:56
process_name use argoneut_mc_hitfinder track
double T() const
Definition: MCStep.h:45
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
Class def header for mcstep data container.
unsigned int MotherTrackID() const
Definition: MCTrack.h:50
double Px() const
Definition: MCStep.h:46
double Z() const
Definition: MCStep.h:44
const Cut kCosmicRay
BEGIN_PROLOG vertical distance to the surface Name
double Py() const
Definition: MCStep.h:47
double Y() const
Definition: MCStep.h:43
Class def header for mctrack data container.
const MCStep & AncestorStart() const
Definition: MCTrack.h:58
float dEdx(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, const TCSlice &slc, TP3D &tp3d)
Definition: PFPUtils.cxx:2687
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
const MCStep & MotherStart() const
Definition: MCTrack.h:52
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
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
art::EDAnalyzer::Table< Config > Parameters
const MCStep & AncestorEnd() const
Definition: MCTrack.h:59
bnb BNB Stream
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:227