DumpMCShowers_module.cc
Go to the documentation of this file.
1 /**
2  * @file DumpMCShowers_module.cc
3  * @brief Module dumping MCShower 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 <iomanip>
25 #include <algorithm> // std::max()
26 
27 
28 namespace sim {
29  class DumpMCShowers;
30 } // namespace sim
31 
32 namespace {
33  using namespace fhicl;
34 
35  /// Collection of configuration parameters for the module
36  struct Config {
37  using Name = fhicl::Name;
38  using Comment = fhicl::Comment;
39 
40  fhicl::Atom<art::InputTag> InputShowers {
41  Name("InputShowers"),
42  Comment("data product with the MC showers to be dumped")
43  };
44 
45  fhicl::Atom<std::string> OutputCategory {
46  Name("OutputCategory"),
47  Comment("name of the output stream (managed by the message facility)"),
48  "DumpMCShowers" /* default value */
49  };
50 
51  fhicl::Atom<unsigned int> DaughtersPerLine {
52  Name("DaughtersPerLine"),
53  Comment("daughter IDs to print on each output line"),
54  12 /* default value */
55  };
56 
57  }; // struct Config
58 
59 
60  /// Returns a string describing the type of origin
61  std::string OriginDescription(simb::Origin_t origin) {
62  switch (origin) {
63  case simb::kUnknown: return "unknown";
64  case simb::kBeamNeutrino: return "beam neutrino";
65  case simb::kCosmicRay: return "cosmic rays";
66  case simb::kSuperNovaNeutrino: return "supernova neutrinos";
67  case simb::kSingleParticle: return "single particles thrown at the detector";
68  // default case is deliberately missing,
69  // so that on new additions in nutools the compiler will complain
70  } // switch
72  << "Unexpected origin type #" << ((int) origin) << "\n";
73  } // OriginDescription()
74 
75 
76  /// Prints a MC step into a stream
77  template <typename Stream>
78  void PrintMCStep(Stream&& out, sim::MCStep const& step) {
79  out << "("
80  << step.X() << ", " << step.Y() << ", " << step.Z() << ") cm, t="
81  << step.T() << " ns; momentum ("
82  << step.Px() << ", " << step.Py() << ", " << step.Pz() << "; "
83  << step.E() << ") MeV/c";
84  } // PrintMCStep()
85 
86 
87 } // local namespace
88 
89 
91  public:
92  // type to enable module parameters description by art
94 
95  /// Configuration-checking constructor
96  explicit DumpMCShowers(Parameters const& config);
97 
98  // Plugins should not be copied or assigned.
99  DumpMCShowers(DumpMCShowers const&) = delete;
100  DumpMCShowers(DumpMCShowers &&) = delete;
101  DumpMCShowers& operator = (DumpMCShowers const&) = delete;
103 
104 
105  // Operates on the event
106  virtual void analyze(art::Event const& event) override;
107 
108 
109  /**
110  * @brief Dumps the content of the specified particle in the output stream
111  * @tparam Stream the type of output stream
112  * @param out the output stream
113  * @param particle the particle to be dumped
114  * @param indent base indentation string (default: none)
115  * @param bIndentFirst if first output line should be indented (default: yes)
116  *
117  * The indent string is prepended to every line of output, with the possible
118  * exception of the first one, in case bIndentFirst is true.
119  *
120  * The output starts on the current line, and the last line is NOT broken.
121  */
122  template <typename Stream>
123  void DumpMCShower(
124  Stream&& out, sim::MCShower const& shower,
125  std::string indent = "", bool bIndentFirst = true
126  ) const;
127 
128 
129  private:
130 
131  art::InputTag fInputShowers; ///< name of MCShower's data product
132  std::string fOutputCategory; ///< name of the stream for output
133  unsigned int fDaughtersPerLine; ///< number of daughter IDs printed per line
134 
135 }; // class sim::DumpMCShowers
136 
137 
138 //------------------------------------------------------------------------------
139 //--- module implementation
140 //---
141 //------------------------------------------------------------------------------
143  : EDAnalyzer(config)
144  , fInputShowers(config().InputShowers())
145  , fOutputCategory(config().OutputCategory())
146  , fDaughtersPerLine(config().DaughtersPerLine())
147 {}
148 
149 //------------------------------------------------------------------------------
150 template <typename Stream>
152  Stream&& out, sim::MCShower const& shower,
153  std::string indent /* = "" */, bool bIndentFirst /* = true */
154 ) const {
155  if (bIndentFirst) out << indent;
156  out
157  << "from GEANT track ID=" << shower.TrackID()
158  << " PDG ID=" << shower.PdgCode()
159  << " from " << OriginDescription(shower.Origin())
160  << " via '" << shower.Process() << "'";
161  out << "\n" << indent
162  << " starting at ";
163  ::PrintMCStep(out, shower.Start());
164  out << "\n" << indent
165  << " ending at ";
166  ::PrintMCStep(out, shower.End());
167 
168  TVector3 const& startDir = shower.StartDir();
169  out << "\n" << indent
170  << "pointing toward ("
171  << startDir.X() << ", " << startDir.Y() << ", " << startDir.Z() << ") cm";
172  std::vector<double> const& charges = shower.Charge();
173  std::vector<double> const& dQdx = shower.dQdx();
174  size_t const nQPlanes = dQdx.size(), nChPlanes = charges.size();
175  size_t const nPlanes = std::max(nQPlanes, nChPlanes);
176  out << "\n" << indent;
177  if (nPlanes > 0) {
178  out
179  << "dE/dx=" << shower.dEdx() << " MeV/cm and dQ/dx (charge) on "
180  << nPlanes << " planes:";
181  for (size_t iPlane = 0; iPlane < nPlanes; ++iPlane) {
182  out << " [#" << iPlane << "] ";
183  if (iPlane < dQdx.size()) out << dQdx[iPlane];
184  else out << "<N/A>";
185  if (iPlane < charges.size()) out << " (" << charges[iPlane] << ")";
186  else out << "<N/A>";
187  } // for plane
188  }
189  else out << "no energy or charge information available";
190 
191  std::vector<unsigned int> const& daughters = shower.DaughterTrackID();
192  out << "\n" << indent
193  << "combined energy deposition information: ";
194  ::PrintMCStep(out, shower.DetProfile());
195  out << "\n" << indent
196  << daughters.size() << " daughters, ID:";
197  for (size_t i = 0; i < daughters.size(); ++i) {
198  if ((i % fDaughtersPerLine) == 0) out << "\n" << indent << " ";
199  out << " " << std::setw(8) << daughters[i];
200  } // for
201 
202  out << "\n" << indent
203  << "mother ID=" << shower.MotherTrackID()
204  << " PDG ID=" << shower.MotherPdgCode()
205  << " via '" << shower.MotherProcess() << "'";
206  out << "\n" << indent
207  << " starting at ";
208  ::PrintMCStep(out, shower.MotherStart());
209  out << "\n" << indent
210  << " ending at ";
211  ::PrintMCStep(out, shower.MotherEnd());
212 
213  out << "\n" << indent
214  << "ancestor ID=" << shower.AncestorTrackID()
215  << " PDG ID=" << shower.AncestorPdgCode()
216  << " via '" << shower.AncestorProcess() << "'";
217  out << "\n" << indent
218  << " starting at ";
219  ::PrintMCStep(out, shower.AncestorStart());
220  out << "\n" << indent
221  << " ending at ";
222  ::PrintMCStep(out, shower.AncestorEnd());
223 
224 } // sim::DumpMCShowers::DumpMCShower()
225 
226 
227 //------------------------------------------------------------------------------
229 
230  // get the particles from the event
231  auto const& Showers
232  = *(event.getValidHandle<std::vector<sim::MCShower>>(fInputShowers));
233 
235  << "Event " << event.id() << ": data product '"
236  << fInputShowers.encode() << "' contains "
237  << Showers.size() << " MCShower objects";
238 
239  unsigned int iShower = 0;
241  for (sim::MCShower const& shower: Showers) {
242 
243  // a bit of a header
244  log << "\n[#" << (iShower++) << "] ";
245  DumpMCShower(log, shower, " ", false);
246 
247  } // for
248  log << "\n";
249 
250 } // sim::DumpMCShowers::analyze()
251 
252 
253 //------------------------------------------------------------------------------
255 
256 //------------------------------------------------------------------------------
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
const MCStep & End() const
Definition: MCShower.h:56
art::InputTag fInputShowers
name of MCShower&#39;s data product
unsigned int TrackID() const
Definition: MCShower.h:53
std::string string
Definition: nybbler.cc:12
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
ChannelGroupService::Name Name
int PdgCode() const
Definition: MCShower.h:52
DumpMCShowers & operator=(DumpMCShowers const &)=delete
double T() const
Definition: MCStep.h:45
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
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:72
const TVector3 & StartDir() const
Definition: MCShower.h:82
typename config_impl< T >::type Config
Definition: ModuleMacros.h:52
simb::Origin_t Origin() const
Definition: MCShower.h:50
double Px() const
Definition: MCStep.h:46
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
int MotherPdgCode() const
Definition: MCShower.h:58
const std::string & AncestorProcess() const
Definition: MCShower.h:66
static Config * config
Definition: config.cpp:1054
single particles thrown at the detector
Definition: MCTruth.h:26
double Z() const
Definition: MCStep.h:44
unsigned int fDaughtersPerLine
number of daughter IDs printed per line
double Py() const
Definition: MCStep.h:47
double Y() const
Definition: MCStep.h:43
const MCStep & AncestorStart() const
Definition: MCShower.h:67
static int max(int a, int b)
const std::string & MotherProcess() const
Definition: MCShower.h:60
Code to link reconstructed objects back to the MC truth information.
double dEdx() const
Definition: MCShower.h:81
unsigned int AncestorTrackID() const
Definition: MCShower.h:65
const MCStep & AncestorEnd() const
Definition: MCShower.h:68
const MCStep & DetProfile() const
Definition: MCShower.h:70
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
const MCStep & Start() const
Definition: MCShower.h:55
double Charge(size_t plane) const
Definition: MCShower.cxx:49
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
virtual void analyze(art::Event const &event) override
#define Comment
const MCStep & MotherEnd() const
Definition: MCShower.h:62
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:49
Class def header for MCShower data container.
unsigned int MotherTrackID() const
Definition: MCShower.h:59
const std::string & Process() const
Definition: MCShower.h:54
double Pz() const
Definition: MCStep.h:48
const MCStep & MotherStart() const
Definition: MCShower.h:61
double X() const
Definition: MCStep.h:42
int AncestorPdgCode() const
Definition: MCShower.h:64
double dQdx(size_t plane) const
Definition: MCShower.cxx:58
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