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