LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
DumpMCShowers_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 <iomanip>
25 #include <string>
26 
27 namespace sim {
28  class DumpMCShowers;
29 } // namespace sim
30 
31 namespace {
32  using namespace fhicl;
33 
35  struct Config {
36  using Name = fhicl::Name;
37  using Comment = fhicl::Comment;
38 
39  fhicl::Atom<art::InputTag> InputShowers{
40  Name("InputShowers"),
41  Comment("data product with the MC showers to be dumped")};
42 
43  fhicl::Atom<std::string> OutputCategory{
44  Name("OutputCategory"),
45  Comment("name of the output stream (managed by the message facility)"),
46  "DumpMCShowers" /* default value */
47  };
48 
49  fhicl::Atom<unsigned int> DaughtersPerLine{
50  Name("DaughtersPerLine"),
51  Comment("daughter IDs to print on each output line"),
52  12 /* default value */
53  };
54 
55  }; // struct Config
56 
58  std::string OriginDescription(simb::Origin_t origin)
59  {
60  switch (origin) {
61  case simb::kUnknown: return "unknown";
62  case simb::kBeamNeutrino: return "beam neutrino";
63  case simb::kCosmicRay: return "cosmic rays";
64  case simb::kSuperNovaNeutrino: return "supernova neutrinos";
66  return "single particles thrown at the detector";
67  // default case is deliberately missing,
68  // so that on new additions in nutools the compiler will complain
69  } // switch
71  << "Unexpected origin type #" << ((int)origin) << "\n";
72  } // OriginDescription()
73 
75  template <typename Stream>
76  void PrintMCStep(Stream&& out, sim::MCStep const& step)
77  {
78  out << "(" << step.X() << ", " << step.Y() << ", " << step.Z() << ") cm, t=" << step.T()
79  << " ns; momentum (" << step.Px() << ", " << step.Py() << ", " << step.Pz() << "; "
80  << step.E() << ") MeV/c";
81  } // PrintMCStep()
82 
83 } // local namespace
84 
86 public:
87  // type to enable module parameters description by art
89 
91  explicit DumpMCShowers(Parameters const& config);
92 
93  // Plugins should not be copied or assigned.
94  DumpMCShowers(DumpMCShowers const&) = delete;
95  DumpMCShowers(DumpMCShowers&&) = delete;
96  DumpMCShowers& operator=(DumpMCShowers const&) = delete;
98 
99  // Operates on the event
100  virtual void analyze(art::Event const& event) override;
101 
115  template <typename Stream>
116  void DumpMCShower(Stream&& out,
117  sim::MCShower const& shower,
118  std::string indent = "",
119  bool bIndentFirst = true) const;
120 
121 private:
123  std::string fOutputCategory;
124  unsigned int fDaughtersPerLine;
125 
126 }; // class sim::DumpMCShowers
127 
128 //------------------------------------------------------------------------------
129 //--- module implementation
130 //---
131 //------------------------------------------------------------------------------
133  : EDAnalyzer(config)
134  , fInputShowers(config().InputShowers())
135  , fOutputCategory(config().OutputCategory())
136  , fDaughtersPerLine(config().DaughtersPerLine())
137 {}
138 
139 //------------------------------------------------------------------------------
140 template <typename Stream>
142  sim::MCShower const& shower,
143  std::string indent /* = "" */,
144  bool bIndentFirst /* = true */
145 ) const
146 {
147  if (bIndentFirst) out << indent;
148  out << "from GEANT track ID=" << shower.TrackID() << " PDG ID=" << shower.PdgCode() << " from "
149  << OriginDescription(shower.Origin()) << " via '" << shower.Process() << "'";
150  out << "\n" << indent << " starting at ";
151  ::PrintMCStep(out, shower.Start());
152  out << "\n" << indent << " ending at ";
153  ::PrintMCStep(out, shower.End());
154 
155  TVector3 const& startDir = shower.StartDir();
156  out << "\n"
157  << indent << "pointing toward (" << startDir.X() << ", " << startDir.Y() << ", "
158  << startDir.Z() << ") cm";
159  std::vector<double> const& charges = shower.Charge();
160  std::vector<double> const& dQdx = shower.dQdx();
161  size_t const nQPlanes = dQdx.size(), nChPlanes = charges.size();
162  size_t const nPlanes = std::max(nQPlanes, nChPlanes);
163  out << "\n" << indent;
164  if (nPlanes > 0) {
165  out << "dE/dx=" << shower.dEdx() << " MeV/cm and dQ/dx (charge) on " << nPlanes << " planes:";
166  for (size_t iPlane = 0; iPlane < nPlanes; ++iPlane) {
167  out << " [#" << iPlane << "] ";
168  if (iPlane < dQdx.size())
169  out << dQdx[iPlane];
170  else
171  out << "<N/A>";
172  if (iPlane < charges.size())
173  out << " (" << charges[iPlane] << ")";
174  else
175  out << "<N/A>";
176  } // for plane
177  }
178  else
179  out << "no energy or charge information available";
180 
181  std::vector<unsigned int> const& daughters = shower.DaughterTrackID();
182  out << "\n" << indent << "combined energy deposition information: ";
183  ::PrintMCStep(out, shower.DetProfile());
184  out << "\n" << indent << daughters.size() << " daughters, ID:";
185  for (size_t i = 0; i < daughters.size(); ++i) {
186  if ((i % fDaughtersPerLine) == 0) out << "\n" << indent << " ";
187  out << " " << std::setw(8) << daughters[i];
188  } // for
189 
190  out << "\n"
191  << indent << "mother ID=" << shower.MotherTrackID() << " PDG ID=" << shower.MotherPdgCode()
192  << " via '" << shower.MotherProcess() << "'";
193  out << "\n" << indent << " starting at ";
194  ::PrintMCStep(out, shower.MotherStart());
195  out << "\n" << indent << " ending at ";
196  ::PrintMCStep(out, shower.MotherEnd());
197 
198  out << "\n"
199  << indent << "ancestor ID=" << shower.AncestorTrackID()
200  << " PDG ID=" << shower.AncestorPdgCode() << " via '" << shower.AncestorProcess() << "'";
201  out << "\n" << indent << " starting at ";
202  ::PrintMCStep(out, shower.AncestorStart());
203  out << "\n" << indent << " ending at ";
204  ::PrintMCStep(out, shower.AncestorEnd());
205 
206 } // sim::DumpMCShowers::DumpMCShower()
207 
208 //------------------------------------------------------------------------------
210 {
211 
212  // get the particles from the event
213  auto const& Showers = *(event.getValidHandle<std::vector<sim::MCShower>>(fInputShowers));
214 
216  << "Event " << event.id() << ": data product '" << fInputShowers.encode() << "' contains "
217  << Showers.size() << " MCShower objects";
218 
219  unsigned int iShower = 0;
221  for (sim::MCShower const& shower : Showers) {
222 
223  // a bit of a header
224  log << "\n[#" << (iShower++) << "] ";
225  DumpMCShower(log, shower, " ", false);
226 
227  } // for
228  log << "\n";
229 
230 } // sim::DumpMCShowers::analyze()
231 
232 //------------------------------------------------------------------------------
234 
235 //------------------------------------------------------------------------------
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
const MCStep & End() const
Definition: MCShower.h:54
art::InputTag fInputShowers
name of MCShower&#39;s data product
unsigned int TrackID() const
Definition: MCShower.h:51
std::string fOutputCategory
name of the stream for output
DumpMCShowers(Parameters const &config)
Configuration-checking constructor.
enum simb::_ev_origin Origin_t
event origin types
int PdgCode() const
Definition: MCShower.h:50
DumpMCShowers & operator=(DumpMCShowers const &)=delete
double T() const
Definition: MCStep.h:41
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.cc:6
std::string encode() const
Definition: InputTag.cc:97
Class def header for mcstep data container.
const std::vector< unsigned int > & DaughterTrackID() const
Definition: MCShower.h:70
const TVector3 & StartDir() const
Definition: MCShower.h:79
simb::Origin_t Origin() const
Definition: MCShower.h:48
double Px() const
Definition: MCStep.h:42
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
int MotherPdgCode() const
Definition: MCShower.h:56
const std::string & AncestorProcess() const
Definition: MCShower.h:64
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
unsigned int fDaughtersPerLine
number of daughter IDs printed per line
double Py() const
Definition: MCStep.h:43
double Y() const
Definition: MCStep.h:39
const MCStep & AncestorStart() const
Definition: MCShower.h:65
const std::string & MotherProcess() const
Definition: MCShower.h:58
Monte Carlo Simulation.
double dEdx() const
Definition: MCShower.h:78
unsigned int AncestorTrackID() const
Definition: MCShower.h:63
const MCStep & AncestorEnd() const
Definition: MCShower.h:66
const MCStep & DetProfile() const
Definition: MCShower.h:68
const MCStep & Start() const
Definition: MCShower.h:53
double Charge(size_t plane) const
Definition: MCShower.cxx:46
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
virtual void analyze(art::Event const &event) override
const MCStep & MotherEnd() const
Definition: MCShower.h:60
void DumpMCShower(Stream &&out, sim::MCShower const &shower, std::string indent="", bool bIndentFirst=true) const
Dumps the content of the specified particle in the output stream.
Supernova neutrinos.
Definition: MCTruth.h:25
double E() const
Definition: MCStep.h:45
Class def header for MCShower data container.
unsigned int MotherTrackID() const
Definition: MCShower.h:57
const std::string & Process() const
Definition: MCShower.h:52
double Pz() const
Definition: MCStep.h:44
const MCStep & MotherStart() const
Definition: MCShower.h:59
double X() const
Definition: MCStep.h:38
int AncestorPdgCode() const
Definition: MCShower.h:62
double dQdx(size_t plane) const
Definition: MCShower.cxx:55
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