LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
DumpMCTracks_module.cc
Go to the documentation of this file.
1 
9 // LArSoft libraries
12 
13 // framework libraries
19 #include "fhiclcpp/ParameterSet.h"
20 #include "fhiclcpp/types/Atom.h"
22 
23 // C/C++ standard libraries
24 #include <string>
25 #include <vector>
26 #include <algorithm> // std::max()
27 
28 
29 namespace sim {
30  class DumpMCTracks;
31 } // namespace sim
32 
33 namespace {
34  using namespace fhicl;
35 
37  struct Config {
38  using Name = fhicl::Name;
39  using Comment = fhicl::Comment;
40 
41  fhicl::Atom<art::InputTag> InputTracks {
42  Name("InputTracks"),
43  Comment("data product with the MC tracks to be dumped")
44  };
45 
46  fhicl::Atom<std::string> OutputCategory {
47  Name("OutputCategory"),
48  Comment("name of the output stream (managed by the message facility)"),
49  "DumpMCTracks" /* default value */
50  };
51 
52  }; // struct Config
53 
54 
56  std::string OriginDescription(simb::Origin_t origin) {
57  switch (origin) {
58  case simb::kUnknown: return "unknown";
59  case simb::kBeamNeutrino: return "beam neutrino";
60  case simb::kCosmicRay: return "cosmic rays";
61  case simb::kSuperNovaNeutrino: return "supernova neutrinos";
62  case simb::kSingleParticle: return "single particles thrown at the detector";
63  // default case is deliberately missing,
64  // so that on new additions in nutools the compiler will complain
65  } // switch
67  << "Unexpected origin type #" << ((int) origin) << "\n";
68  } // OriginDescription()
69 
70 
72  template <typename Stream>
73  void PrintMCStep(Stream&& out, sim::MCStep const& step) {
74  out << "("
75  << step.X() << ", " << step.Y() << ", " << step.Z() << ") cm, t="
76  << step.T() << " ns; momentum ("
77  << step.Px() << ", " << step.Py() << ", " << step.Pz() << "; "
78  << step.E() << ") MeV/c";
79  } // PrintMCStep()
80 
81 
82 } // local namespace
83 
84 
86  public:
87  // type to enable module parameters description by art
89 
91  explicit DumpMCTracks(Parameters const& config);
92 
93  // Plugins should not be copied or assigned.
94  DumpMCTracks(DumpMCTracks const&) = delete;
95  DumpMCTracks(DumpMCTracks &&) = delete;
96  DumpMCTracks& operator = (DumpMCTracks const&) = delete;
98 
99 
100  // Operates on the event
101  virtual void analyze(art::Event const& event) override;
102 
103 
117  template <typename Stream>
118  void DumpMCTrack(
119  Stream&& out, sim::MCTrack const& track,
120  std::string indent = "", bool bIndentFirst = true
121  ) const;
122 
123 
124  private:
125 
127  std::string fOutputCategory;
128 
129 }; // class sim::DumpMCTracks
130 
131 
132 //------------------------------------------------------------------------------
133 //--- module implementation
134 //---
135 //------------------------------------------------------------------------------
137  : EDAnalyzer(config)
138  , fInputTracks(config().InputTracks())
139  , fOutputCategory(config().OutputCategory())
140 {}
141 
142 //------------------------------------------------------------------------------
143 template <typename Stream>
145  Stream&& out, sim::MCTrack const& track,
146  std::string indent /* = "" */, bool bIndentFirst /* = true */
147 ) const {
148  if (bIndentFirst) out << indent;
149  out
150  << "from GEANT track ID=" << track.TrackID()
151  << " PDG ID=" << track.PdgCode()
152  << " from " << OriginDescription(track.Origin())
153  << " via '" << track.Process() << "'";
154  out << "\n" << indent
155  << " starting at ";
156  ::PrintMCStep(out, track.Start());
157  out << "\n" << indent
158  << " ending at ";
159  ::PrintMCStep(out, track.End());
160 
161  std::vector<std::vector<double>> const& dQdx = track.dQdx(); // dQdx[MCStep][plane]
162  std::vector<double> const& dEdx = track.dEdx(); // dEdx[MCStep]
163  size_t const nQSteps = dQdx.size(), nESteps = dEdx.size();
164  size_t const nSteps = std::max(nQSteps, nESteps);
165  out << "\n" << indent;
166  if (nSteps > 0) {
167  out
168  << "energy information for " << nSteps
169  << " steps (dE/dX in MeV/cm, then dQ/dx per plane):";
170  for (size_t iStep = 0; iStep < nSteps; ++iStep) {
171  out << "\n" << indent
172  << " [#" << iStep << "] dE/dx=";
173  if (iStep < nESteps) out << dEdx[iStep];
174  else out << "<N/A>";
175  out << "; dQ/dx:";
176  if (iStep < nQSteps) {
177  std::vector<double> const& step_dQdx = dQdx[iStep]; // dQdx[plane]
178  for (size_t iPlane = 0; iPlane < step_dQdx.size(); ++iPlane) {
179  out << " [#" << iPlane << "] " << step_dQdx[iPlane];
180  } // for plane
181  }
182  else out << "<N/A>";
183  } // for iStep
184  }
185  else out << "no energy or charge information available";
186 
187  out << "\n" << indent
188  << "mother ID=" << track.MotherTrackID()
189  << " PDG ID=" << track.MotherPdgCode()
190  << " via '" << track.MotherProcess() << "'";
191  out << "\n" << indent
192  << " starting at ";
193  ::PrintMCStep(out, track.MotherStart());
194  out << "\n" << indent
195  << " ending at ";
196  ::PrintMCStep(out, track.MotherEnd());
197 
198  out << "\n" << indent
199  << "ancestor ID=" << track.AncestorTrackID()
200  << " PDG ID=" << track.AncestorPdgCode()
201  << " via '" << track.AncestorProcess() << "'";
202  out << "\n" << indent
203  << " starting at ";
204  ::PrintMCStep(out, track.AncestorStart());
205  out << "\n" << indent
206  << " ending at ";
207  ::PrintMCStep(out, track.AncestorEnd());
208 
209 } // sim::DumpMCTracks::DumpMCTrack()
210 
211 
212 //------------------------------------------------------------------------------
214 
215  // get the particles from the event
216  auto const& Tracks
217  = *(event.getValidHandle<std::vector<sim::MCTrack>>(fInputTracks));
218 
220  << "Event " << event.id() << ": data product '"
221  << fInputTracks.encode() << "' contains "
222  << Tracks.size() << " MCTrack objects";
223 
224  unsigned int iTrack = 0;
226  for (sim::MCTrack const& track: Tracks) {
227 
228  // a bit of a header
229  log << "\n[#" << (iTrack++) << "] ";
230  DumpMCTrack(log, track, " ", false);
231 
232  } // for
233  log << "\n";
234 
235 } // sim::DumpMCTracks::analyze()
236 
237 
238 //------------------------------------------------------------------------------
240 
241 //------------------------------------------------------------------------------
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
simb::Origin_t Origin() const
Definition: MCTrack.h:40
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
const MCStep & MotherEnd() const
Definition: MCTrack.h:53
unsigned int AncestorTrackID() const
Definition: MCTrack.h:56
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
Int_t max
Definition: plot.C:27
double Px() const
Definition: MCStep.h:46
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
DumpMCTracks & operator=(DumpMCTracks const &)=delete
single particles thrown at the detector
Definition: MCTruth.h:24
parameter set interface
std::string encode() const
Definition: InputTag.cc:36
std::string indent(std::size_t const i)
double Z() const
Definition: MCStep.h:44
double Py() const
Definition: MCStep.h:47
double Y() const
Definition: MCStep.h:43
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
Class def header for mctrack data container.
const MCStep & AncestorStart() const
Definition: MCTrack.h:58
Monte Carlo Simulation.
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
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:23
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.
Float_t track
Definition: plot.C:34
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:230
Cosmic rays.
Definition: MCTruth.h:22
Event finding and building.
Beam neutrinos.
Definition: MCTruth.h:21