MCDumpers.h
Go to the documentation of this file.
1 /**
2  * @file garsoft/Utilities/MCDumpers.h
3  * @brief Utility functions to print MC truth information.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date November 2, 2017
6  *
7  * Functions dumping Monte Carlo data product objects (`sim::dump` namespace).
8  *
9  */
10 
11 #ifndef GARSOFT_MCDUMPERS_MCDUMPERS_H
12 #define GARSOFT_MCDUMPERS_MCDUMPERS_H
13 
14 #include "MCDumperUtils.h"
15 #include "DumpUtils.h" // gar::dump namespace
16 #include "Utilities/geo_vectors_utils_TVector.h" // geo::vect::dump
17 
18 // nutools libraries
23 
24 // ROOT libraries
25 #include "TLorentzVector.h"
26 
27 // C/C++ standard libraries
28 #include <string>
29 #include <utility> // std::forward()
30 
31 
32 namespace sim {
33 
34  /// Functions to dump Monte Carlo object information into a stream.
35  namespace dump {
36 
37  //--------------------------------------------------------------------------
38  //@{
39  /**
40  * @brief Dumps the content of the specified particle in the output stream.
41  * @tparam Stream the type of output stream
42  * @param out the output stream
43  * @param particle the particle to be dumped
44  * @param indent base indentation string (default: none)
45  * @param firstIndent indentation of first line (default: as `indent`)
46  *
47  * The `indent` string is prepended to every line of output, except for
48  * the first one, where `firstIndent` is used.
49  *
50  * The output starts on the current line, and the last line is NOT broken.
51  */
52  template <typename Stream>
53  void DumpMCParticle(
54  Stream&& out, simb::MCParticle const& particle,
55  std::string indent, std::string firstIndent
56  );
57 
58  template <typename Stream>
59  void DumpMCParticle
60  (Stream&& out, simb::MCParticle const& particle, std::string indent = "")
61  { DumpMCParticle(std::forward<Stream>(out), particle, indent, indent); }
62 
63  //@}
64 
65 
66  //--------------------------------------------------------------------------
67  //@{
68  /**
69  * @brief Dumps the specified particle trajectory into the output stream.
70  * @tparam Stream the type of output stream
71  * @param out the output stream
72  * @param trajectory the particle trajectory to be dumped
73  * @param pointsPerLine number of points dumped per line (default: all)
74  * @param indent base indentation string (default: none)
75  *
76  * All points of the specified Monte Carlo `trajectory` are printed
77  * on screen, `pointsPerLine` on each line.
78  * The points are printed starting on a new line, and each line is applied
79  * the same indentation string (`indent`).
80  * As an exception, if `pointsPerLine` is not specified, all points are
81  * printed on the current line and `indent` is ignored.
82  *
83  * The last line of the output is NOT broken.
84  */
85  template <typename Stream>
87  Stream&& out, simb::MCTrajectory const& trajectory,
88  unsigned int pointsPerLine, std::string indent
89  );
90 
91  template <typename Stream>
93  (Stream&& out, simb::MCTrajectory const& trajectory)
94  {
95  DumpMCParticleTrajectory(std::forward<Stream>(out), trajectory, 0U, "");
96  }
97 
98  // @}
99 
100 
101  //--------------------------------------------------------------------------
102  // @{
103  /**
104  * @brief Dumps the content of the specified neutrino in the output stream.
105  * @tparam Stream the type of output stream
106  * @param out the output stream
107  * @param neutrino the neutrino to be dumped
108  * @param indent base indentation string (default: none)
109  * @param firstIndent string to be used for indentation of the first line
110  * (default: as `indent`)
111  *
112  * The `indent` string is prepended to every line of output, except for
113  * the first one, where `firstIndent` is used.
114  *
115  * The output starts on the current line, and the last line is NOT broken.
116  */
117  template <typename Stream>
118  void DumpMCNeutrino(
119  Stream&& out, simb::MCNeutrino const& neutrino,
120  std::string indent, std::string firstIndent
121  );
122 
123  template <typename Stream>
124  void DumpMCNeutrino
125  (Stream&& out, simb::MCNeutrino const& neutrino, std::string indent = "")
126  { DumpMCNeutrino(std::forward<Stream>(out), neutrino, indent, indent); }
127 
128  // @}
129 
130 
131  //--------------------------------------------------------------------------
132  // @{
133  /**
134  * @brief Dumps the content of the specified MC truth in the output stream.
135  * @tparam Stream the type of output stream
136  * @param out the output stream
137  * @param truth the truth information to be dumped
138  * @param pointsPerLine (_optional_) number of trajectory points dumped per
139  * line (default: `0`, no points shown)
140  * @param indent base indentation string (default: none)
141  * @param firstIndent string to be used for indentation of the first line
142  * (default: same as `indent`)
143  *
144  * The `indent` string is prepended to every line of output, except for
145  * the first one, where `firstIndent` is used.
146  *
147  * The argument `pointsPerLine` regulates the dump of trajectory points from
148  * all the particles in the record (except the ones stored in the neutrino
149  * object). Setting it to `0`, or leaving it out, will suppress the dump of
150  * particle trajectories completely. There is no option to reduce the number
151  * of printed trajectory points: it's just all or none.
152  *
153  * The output starts on the current line, and the last line is NOT broken.
154  */
155  template <typename Stream>
156  void DumpMCTruth(
157  Stream&& out, simb::MCTruth const& truth, unsigned int pointsPerLine,
158  std::string indent, std::string firstIndent
159  );
160 
161  template <typename Stream>
163  Stream&& out, simb::MCTruth const& truth, unsigned int pointsPerLine,
164  std::string indent = ""
165  )
166  {
168  (std::forward<Stream>(out), truth, pointsPerLine, indent, indent);
169  }
170 
171  template <typename Stream>
173  Stream&& out, simb::MCTruth const& truth,
174  std::string indent, std::string firstIndent
175  )
176  { DumpMCTruth(std::forward<Stream>(out), truth, 0, indent, firstIndent); }
177 
178  template <typename Stream>
180  Stream&& out, simb::MCTruth const& truth, std::string indent = ""
181  )
182  { DumpMCTruth(std::forward<Stream>(out), truth, indent, indent); }
183 
184  // @}
185 
186 
187  //--------------------------------------------------------------------------
188  // @{
189  /**
190  * @brief Dumps the content of the GENIE truth in the output stream.
191  * @tparam Stream the type of output stream
192  * @param out the output stream
193  * @param truth the truth information to be dumped
194  * @param indent base indentation string (default: none)
195  * @param firstIndent string to be used for indentation of the first line
196  * (default: as `indent`)
197  *
198  * The `indent` string is prepended to every line of output, except for
199  * the first one, where `firstIndent` is used.
200  *
201  * The output starts on the current line, and the last line is NOT broken.
202  */
203  template <typename Stream>
204  void DumpGTruth(
205  Stream&& out, simb::GTruth const& truth,
206  std::string indent, std::string firstIndent
207  );
208 
209  template <typename Stream>
210  void DumpGTruth
211  (Stream&& out, simb::GTruth const& truth, std::string indent = "")
212  { DumpGTruth(std::forward<Stream>(out), truth, indent, indent); }
213 
214  // @}
215 
216  //--------------------------------------------------------------------------
217 
218  } // namespace dump
219 
220 } // namespace sim
221 
222 
223 //------------------------------------------------------------------------------
224 //--- template implementation
225 //------------------------------------------------------------------------------
226 template <typename Stream>
228  Stream&& out, simb::MCParticle const& particle,
229  std::string indent, std::string firstIndent
230 ) {
231  out << firstIndent
232  << "ID=" << particle.TrackId() << ": " << ParticleName(particle.PdgCode())
233  << " mass=" << particle.Mass() << " GeV/c2 "
234  << " status=" << particle.StatusCode()
235  << " (" << ParticleStatusName(particle.StatusCode()) << ")"
236  ;
237  if (particle.Weight() != 1.0) out << " weight=" << particle.Weight();
238  if (particle.Rescatter()) {
239  out << " rescattered (" << particle.Rescatter()
240  << ") at vertex " << particle.GetGvtx();
241  }
242  out << "\n" << indent << "created via "
243  << (particle.Process().empty()? "magics": particle.Process());
244  if (particle.Mother() == 0) out << " by the gods";
245  else out << " from ID=" << particle.Mother();
246 
247  const unsigned int nDaughters = particle.NumberDaughters();
248  const unsigned int nPoints = particle.NumberTrajectoryPoints();
249  if (nPoints > 0) {
250  TLorentzVector const& start = particle.Position();
251  TLorentzVector const& start_mom = particle.Momentum();
252  out << " at " << start << " cm with momentum " << start_mom << " GeV/c";
253  }
254  if (particle.Polarization().Mag2() != 0.) {
255  out
256  << " with polarization " << gar::dump::vector3D(particle.Polarization());
257  }
258  // does this particle seem to stop? (by decay, or because of extended traj.)
259  if ((nPoints > 1) || (nDaughters > 0)) {
260  out << "\n" << indent << ((nDaughters > 0)? "ends": "stops") << " by "
261  << (particle.EndProcess().empty()? "magics": particle.EndProcess());
262  if (nPoints > 1) {
263  TLorentzVector const& stop = particle.EndPosition();
264  TLorentzVector const& stop_mom = particle.EndMomentum();
265  out << " at " << stop << " cm with momentum " << stop_mom << " GeV/c";
266  }
267  if (nDaughters > 0) {
268  out << " into ";
269  if (nDaughters == 1)
270  out << "particle ID=" << particle.FirstDaughter();
271  else {
272  out << nDaughters << " particles from ID=" << particle.FirstDaughter()
273  << " to ID=" << particle.LastDaughter();
274  }
275  } // if daughters
276  } // if it stops
277  if (nPoints > 1) {
278  simb::MCTrajectory const& traj = particle.Trajectory();
279  out << "\n" << indent << "comes with a trajectory " << traj.TotalLength()
280  << " cm long in " << nPoints << " points";
281  } // if has points
282 
283 } // sim::dump::DumpMCParticle()
284 
285 
286 //------------------------------------------------------------------------------
287 template <typename Stream>
289  Stream&& out, simb::MCTrajectory const& trajectory,
290  unsigned int pointsPerLine, std::string indent
291 ) {
292  unsigned int page = 0;
293  for (auto const& pair: trajectory) {
294  if ((pointsPerLine > 0) && (page-- == 0)) {
295  out << "\n" << indent << " ";
296  page = pointsPerLine - 1;
297  }
298  else out << " -- ";
299 
300  TLorentzVector const& pos = pair.first;
301  out << pos;
302  } // for trajectory points
303 
304 } // sim::dump::DumpMCParticleTrajectory()
305 
306 
307 //------------------------------------------------------------------------------
308 template <typename Stream>
310  Stream&& out, simb::MCNeutrino const& nu,
311  std::string indent, std::string firstIndent
312 ) {
313 
314  out << firstIndent
315  << "from " << TruthCCNCname(nu.CCNC())
317  << ", mode: " << nu.Mode() << " (" << TruthReactionMode(nu.Mode()) << ")"
318  << '\n' << indent
319  << "target: " << nu.Target() << " (" << ParticleName(nu.Target()) << ")"
320  ;
321  if (nu.HitNuc() != 0) {
322  out << ", hit nucleon: " << nu.HitNuc()
323  << " (" << ParticleName(nu.HitNuc()) << ")";
324  }
325  if (nu.HitQuark() != 0) {
326  out << ", hit quark: " << nu.HitQuark()
327  << " (" << ParticleName(nu.HitQuark()) << ")";
328  }
329  out
330  << '\n' << indent
331  << "x=" << nu.X() << " y=" << nu.Y() << " w=" << nu.W()
332  << " Q^2=" << nu.QSqr() << " GeV^2; theta=" << nu.Theta()
333  << " rad pT=" << nu.Pt() << " GeV/c"
334  ;
335  out << '\n' << indent << "neutrino: ";
336  DumpMCParticle(std::forward<Stream>(out), nu.Nu(), indent + " ", "");
337  out << '\n' << indent << "outgoing lepton: ";
338  DumpMCParticle(std::forward<Stream>(out), nu.Lepton(), indent + " ", "");
339 
340 } // sim::dump::DumpMCNeutrino()
341 
342 
343 //------------------------------------------------------------------------------
344 template <typename Stream>
346  Stream&& out, simb::MCTruth const& truth, unsigned int pointsPerLine,
347  std::string indent, std::string firstIndent
348 ) {
349  unsigned int const nParticles = truth.NParticles();
350  out << firstIndent
351  << nParticles << " particles from "
352  << TruthOriginName(truth.Origin());
353  if (truth.NeutrinoSet()) {
354  out << '\n' << indent << "neutrino information: ";
356  (std::forward<Stream>(out), truth.GetNeutrino(), indent + " ", "");
357  }
358  for (unsigned int i = 0; i < nParticles; ++i) {
359  out << '\n' << indent << "[#" << i << "] ";
360  simb::MCParticle const& particle = truth.GetParticle(i);
361  DumpMCParticle(std::forward<Stream>(out), particle, indent + " ", "");
362 
363  const unsigned int nPoints = particle.NumberTrajectoryPoints();
364  if ((nPoints > 0) && (pointsPerLine > 0)) {
365  out << ":";
367  std::forward<Stream>(out), particle.Trajectory(),
368  pointsPerLine, indent + " "
369  );
370  } // if has points
371  } // for all particles
372 
373 } // sim::dump::DumpMCTruth()
374 
375 
376 //------------------------------------------------------------------------------
377 template <typename Stream>
379  Stream&& out, simb::GTruth const& truth,
380  std::string indent, std::string firstIndent
381 ) {
382 
383  unsigned int const nCharged
384  = truth.fNumPiPlus + truth.fNumPiMinus + truth.fNumProton;
385  unsigned int const nNeutral = truth.fNumPi0 + truth.fNumNeutron;
386  unsigned int const nPions
387  = truth.fNumPiPlus + truth.fNumPiMinus + truth.fNumPi0;
388  unsigned int const nNucleons = truth.fNumProton + truth.fNumNeutron;
389  unsigned int const nTotalParticles = nCharged + nNeutral;
390 
391  out << firstIndent
392  << "interaction code: " << truth.fGint
393  << ", neutrino scattering code: " << truth.fGscatter
394  << " at " << truth.fVertex
395  << "\n" << indent
396  << "probe: " << ParticleName(truth.fProbePDG)
397  << " with cp=" << truth.fProbeP4
398  << " hit nucleon with cp=" << truth.fHitNucP4 << " GeV"
399  << " (" << (truth.fIsSeaQuark? "": "not a ") << "sea quark)"
400  << " in target: " << ParticleName(truth.ftgtPDG)
401  << " (Z: " << truth.ftgtZ << ", A: " << truth.ftgtA << ")"
402  << "\n" << indent
403  << "event interaction weight (genie internal): " << truth.fweight
404  << ", interaction probability: " << truth.fprobability
405  << ", cross section: " << truth.fXsec
406  << ", differential cross section: " << truth.fDiffXsec
407  << "\n" << indent
408  << "particles after reaction, before FSI: "
409  << truth.fNumPiPlus << " pi+"
410  << ", " << truth.fNumPiMinus << " pi-"
411  << ", " << truth.fNumPi0 << " pi0"
412  << ", " << truth.fNumProton << " p/pbar"
413  << ", " << truth.fNumNeutron << " n/nbar"
414  << "\n" << indent
415  << " total " << nTotalParticles << " particles after reaction before FSI"
416  ": " << nCharged << "/" << nNeutral << " charged/neutral"
417  ", " << nPions << " pions, " << nNucleons << " nucleons"
418  << "\n" << indent << "process "
419  << (truth.fIsCharm? "with": "without") << " charmed hadron";
420  if (truth.fResNum == -1) out << ", no resonance";
421  else out << ", resonance: #" << truth.fResNum;
422  out
423  << "\n" << indent
424  << "internal (on shell) genie kinematics: Q^2: " << truth.fgQ2 << " GeV^2"
425  << " q^2: " << truth.fgq2 << " GeV^2"
426  << ", w: " << truth.fgW << " GeV^2"
427  << ", t: " << truth.fgT << " GeV^2"
428  << ", x: " << truth.fgX
429  << ", y: " << truth.fgY
430  << "\n" << indent
431  << "FShadSyst: " << truth.fFShadSystP4
432  ;
433 
434 } // sim::DumpGTruth::DumpTruth()
435 
436 
437 //------------------------------------------------------------------------------
438 
439 
440 #endif // GARSOFT_MCDUMPERS_MCDUMPERS_H
double fgW
Definition: GTruth.h:64
int fGint
interaction code
Definition: GTruth.h:56
unsigned int NumberTrajectoryPoints() const
Definition: MCParticle.h:218
const TVector3 & Polarization() const
Definition: MCParticle.h:214
const TLorentzVector & Position(const int i=0) const
Definition: MCParticle.h:219
int PdgCode() const
Definition: MCParticle.h:212
int CCNC() const
Definition: MCNeutrino.h:148
double fgq2
Definition: GTruth.h:63
double QSqr() const
Definition: MCNeutrino.h:157
double Theta() const
angle between incoming and outgoing leptons, in radians
Definition: MCNeutrino.cxx:63
const simb::MCNeutrino & GetNeutrino() const
Definition: MCTruth.h:77
double fgX
Definition: GTruth.h:66
int ftgtA
Definition: GTruth.h:46
void DumpGTruth(Stream &&out, simb::GTruth const &truth, std::string indent, std::string firstIndent)
Dumps the content of the GENIE truth in the output stream.
Definition: MCDumpers.h:378
Trajectory class.
std::string TruthInteractionTypeName(int type)
const TLorentzVector & EndPosition() const
Definition: MCParticle.h:225
double Weight() const
Definition: MCParticle.h:254
int HitQuark() const
Definition: MCNeutrino.h:153
int FirstDaughter() const
Definition: MCParticle.h:250
std::string string
Definition: nybbler.cc:12
const simb::MCTrajectory & Trajectory() const
Definition: MCParticle.h:253
int Mother() const
Definition: MCParticle.h:213
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:146
simb::Origin_t Origin() const
Definition: MCTruth.h:74
double Mass() const
Definition: MCParticle.h:239
double Pt() const
transverse momentum of interaction, in GeV/c
Definition: MCNeutrino.cxx:74
int fNumNeutron
number of neutrons after reaction, before FSI
Definition: GTruth.h:81
void DumpMCParticleTrajectory(Stream &&out, simb::MCTrajectory const &trajectory, unsigned int pointsPerLine, std::string indent)
Dumps the specified particle trajectory into the output stream.
Definition: MCDumpers.h:288
int HitNuc() const
Definition: MCNeutrino.h:152
Specializations of geo_vectors_utils.h for ROOT old vector types.
int ftgtZ
Definition: GTruth.h:45
double fXsec
cross section of interaction
Definition: GTruth.h:30
int fNumPiPlus
number of pi pluses after reaction, before FSI
Definition: GTruth.h:83
int StatusCode() const
Definition: MCParticle.h:211
int fNumPiMinus
number of pi minuses after reaction, before FSI
Definition: GTruth.h:84
std::string TruthCCNCname(int ccnc)
int NParticles() const
Definition: MCTruth.h:75
auto vector3D(Vector3D const &v)
Returns a manipulator which will print the specified vector.
Definition: DumpUtils.h:301
std::string Process() const
Definition: MCParticle.h:215
TLorentzVector GetGvtx() const
Definition: MCParticle.h:245
Particle class.
int NumberDaughters() const
Definition: MCParticle.h:217
int TrackId() const
Definition: MCParticle.h:210
TLorentzVector fProbeP4
Definition: GTruth.h:41
int fResNum
resonance number
Definition: GTruth.h:89
int fNumProton
number of protons after reaction, before FSI
Definition: GTruth.h:80
int InteractionType() const
Definition: MCNeutrino.h:150
double fprobability
interaction probability
Definition: GTruth.h:29
std::string TruthReactionMode(int mode)
Returns the "mode" of the reaction (a lesser version of interaction type).
const simb::MCParticle & Lepton() const
Definition: MCNeutrino.h:147
double W() const
Definition: MCNeutrino.h:154
int fProbePDG
Definition: GTruth.h:39
std::string EndProcess() const
Definition: MCParticle.h:216
double Y() const
Definition: MCNeutrino.h:156
int fGscatter
neutrino scattering code
Definition: GTruth.h:55
int fNumPi0
number of pi0 after reaction, before FSI
Definition: GTruth.h:82
std::string ParticleName(int pigid)
Returns a string with the name of particle the specified with PDG ID.
void DumpMCTruth(Stream &&out, simb::MCTruth const &truth, unsigned int pointsPerLine, std::string indent, std::string firstIndent)
Dumps the content of the specified MC truth in the output stream.
Definition: MCDumpers.h:345
bool fIsCharm
did the interaction produce a charmed hadron?
Definition: GTruth.h:76
double fweight
event interaction weight (genie internal)
Definition: GTruth.h:28
double X() const
Definition: MCNeutrino.h:155
Code to link reconstructed objects back to the MC truth information.
TLorentzVector fHitNucP4
Definition: GTruth.h:51
Collection of utilities for dumping data on screen.
Definition: DumperBase.h:30
const simb::MCParticle & GetParticle(int i) const
Definition: MCTruth.h:76
int Target() const
Definition: MCNeutrino.h:151
std::string TruthOriginName(simb::Origin_t origin)
Returns a string representing the specified process origin.
double TotalLength() const
int ftgtPDG
PDG of Target Nucleus, nucleon only if free.
Definition: GTruth.h:47
double fgQ2
Definition: GTruth.h:62
int LastDaughter() const
Definition: MCParticle.h:251
const TLorentzVector & Momentum(const int i=0) const
Definition: MCParticle.h:220
TLorentzVector fFShadSystP4
generated final state hadronic system (LAB frame)
Definition: GTruth.h:73
int Rescatter() const
Definition: MCParticle.h:252
double fgT
Definition: GTruth.h:65
void DumpMCNeutrino(Stream &&out, simb::MCNeutrino const &neutrino, std::string indent, std::string firstIndent)
Dumps the content of the specified neutrino in the output stream.
Definition: MCDumpers.h:309
bool NeutrinoSet() const
Definition: MCTruth.h:78
Event generator information.
Definition: MCTruth.h:32
Event generator information.
Definition: MCNeutrino.h:18
int Mode() const
Definition: MCNeutrino.h:149
bool fIsSeaQuark
Definition: GTruth.h:50
std::string ParticleStatusName(int code)
Describes the status of a particle (simb::MCParticle::StatusCode()).
TLorentzVector fVertex
Definition: GTruth.h:26
const TLorentzVector & EndMomentum() const
Definition: MCParticle.h:240
double fgY
a common running variable to be recorded
Definition: GTruth.h:67
double fDiffXsec
differential cross section of interaction
Definition: GTruth.h:31
void DumpMCParticle(Stream &&out, simb::MCParticle const &particle, std::string indent, std::string firstIndent)
Dumps the content of the specified particle in the output stream.
Definition: MCDumpers.h:227