9#include <analysis/modules/PrintMCParticles/PrintMCParticlesModule.h>
10#include <analysis/dataobjects/StringWrapper.h>
12#include <mdst/dataobjects/MCParticle.h>
14#include <framework/logging/LogConnectionConsole.h>
16#include <boost/format.hpp>
17#include <boost/algorithm/string.hpp>
19#include <TDatabasePDG.h>
20#include <Math/Vector3D.h>
35 std::string statusToString(
const MCParticle& mc)
37 static std::map<int, std::string> names {
48 std::vector<std::string> set;
49 for (
auto&& [status, name] : names) {
50 if (mc.hasStatus(status)) set.emplace_back(name);
52 return boost::join(set,
", ");
56 std::string g4ProcessToName(
int process)
58 static std::map<int, std::string> translation{
59 {1,
"CoulombScattering"},
61 {3,
"Bremsstrahlung"},
62 {4,
"PairProdByCharged"},
64 {6,
"AnnihilationToMuMu"},
65 {7,
"AnnihilationToHadrons"},
66 {8,
"NuclearStopping"},
67 {9,
"ElectronGeneralProcess"},
68 {10,
"MultipleScattering"},
70 {12,
"PhotoElectricEffect"},
71 {13,
"ComptonScattering"},
72 {14,
"GammaConversion"},
73 {15,
"GammaConversionToMuMu"},
74 {16,
"GammaGeneralProcess"},
75 {17,
"PositronGeneralProcess"},
76 {18,
"AnnihilationToTauTau"},
78 {22,
"Scintillation"},
79 {23,
"SynchrotronRadiation"},
80 {24,
"TransitionRadiation"},
81 {25,
"SurfaceReflection"},
82 {40,
"DarkBremsstrahlung"},
83 {49,
"MuonPairProdByCharged"},
84 {111,
"HadronElastic"},
85 {116,
"NeutronGeneral"},
86 {121,
"HadronInelastic"},
88 {132,
"MuAtomicCapture"},
90 {151,
"HadronAtRest"},
91 {152,
"LeptonAtRest"},
92 {161,
"ChargeExchange"},
95 {202,
"Decay_WithSpin"},
96 {203,
"Decay_PionMakeSpin"},
97 {210,
"Decay_Radioactive"},
98 {211,
"Decay_Unknown"},
99 {221,
"Decay_MuAtom "},
100 {231,
"Decay_External"},
101 {310,
"EMDissociation"},
103 if (
auto it = translation.find(process); it != translation.end()) {
106 return "[Unknown process]";
119 addParam(
"maxLevel",
m_maxLevel,
"Show only up to specified depth level, -1 means no limit", -1);
123 addParam(
"showStatus",
m_showStatus,
"Show extendend status information of the particle",
false);
141 m_output <<
"Content of MCParticle list" << std::endl;
145 std::vector<MCParticle*> first_gen;
147 if (mc.getMother() !=
nullptr)
continue;
148 first_gen.emplace_back(&mc);
165 particles.erase(std::remove_if(particles.begin(), particles.end(), [](
MCParticle * mc) {
166 return not mc->hasStatus(MCParticle::c_PrimaryParticle);
167 }), particles.end());
176 std::string colorStart[] = {
"",
""};
177 std::string colorEnd =
"";
180 colorStart[0] = anyExtraInfo ?
"\x1b[31;1m" :
"\x1b[31m";
181 colorStart[1] = anyExtraInfo ?
"\x1b[1m" :
"";
185 TDatabasePDG* pdb = TDatabasePDG::Instance();
188 const auto num_particles = particles.size();
189 size_t particle_index{0};
190 for (
const auto* mc : particles) {
192 const bool last = ++particle_index == num_particles;
193 m_output << indent << (last ?
"╰" :
"├");
196 TParticlePDG* pdef = pdb->GetParticle(mc->getPDG());
198 m_output <<
" (" << mc->getPDG() <<
")" << colorEnd;
201 auto daughters = mc->getDaughters();
206 const std::string newIndent = indent + (last ?
" " :
"│") +
" ";
208 const std::string propIndent =
"\n" + newIndent + (showDaughters ?
"│ " :
"");
210 if ((not showDaughters) and (not daughters.empty())) {
216 m_output << boost::format(
"mass=%.3g energy=%.3g charge=%d") % mc->getMass() % mc->getEnergy() % mc->getCharge();
220 const ROOT::Math::XYZVector& p = mc->getMomentum();
222 m_output << boost::format(
"p=(%.3g, %.3g, %.3g) |p|=%.3g") % p.X() % p.Y() % p.Z() % p.R();
225 const ROOT::Math::XYZVector& v = mc->getVertex();
227 m_output << boost::format(
"production vertex=(%.3g, %.3g, %.3g), time=%.3g") % v.X() % v.Y() % v.Z() % mc->getProductionTime();
231 m_output <<
"status flags=" << statusToString(*mc);
234 m_output <<
"creation process=" << g4ProcessToName(mc->getSecondaryPhysicsProcess());
237 m_output <<
"list index=" << mc->getIndex();
240 if (showDaughters and anyExtraInfo)
m_output << propIndent;
245 printTree(daughters, level + 1, newIndent);
248 if (anyExtraInfo and not last)
m_output << newIndent << std::endl;
static bool terminalSupportsColors(int fileDescriptor)
Returns true if the given file descriptor is a tty and supports colors.
A Class to store the Monte Carlo particle information.
@ c_IsFSRPhoton
bit 7: Particle is from final state radiation
@ c_Initial
bit 5: Particle is initial such as e+ or e- and not going to Geant4
@ c_IsPHOTOSPhoton
bit 8: Particle is an radiative photon from PHOTOS
@ c_PrimaryParticle
bit 0: Particle is primary particle.
@ c_LeftDetector
bit 2: Particle left the detector (the simulation volume).
@ c_IsVirtual
bit 4: Particle is virtual and not going to Geant4.
@ c_StableInGenerator
bit 1: Particle is stable, i.e., not decaying in the generator.
@ c_StoppedInDetector
bit 3: Particle was stopped in the detector (the simulation volume).
@ c_IsISRPhoton
bit 6: Particle is from initial state radiation
void setDescription(const std::string &description)
Sets the description of the module.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
std::stringstream m_output
Buffer to keep all the output while building the tree.
virtual void initialize() override
init.
bool m_showProperties
Show remaining properties.
virtual void event() override
Method is called for each event.
StoreArray< MCParticle > m_mcparticles
store array for the MCParticles
bool m_showStatus
Show extended status information.
int m_maxLevel
Show only up to specified depth level.
PrintMCParticlesModule()
Constructor.
bool m_showVertices
Show particle production vertices.
void filterPrimaryOnly(std::vector< MCParticle * > &particles) const
if m_onlyPrimary is set remove all non primary particles from the given vector inplace
StoreObjPtr< StringWrapper > m_stringWrapper
string wrapper to store the MCDecayString
void printTree(const std::vector< MCParticle * > &particles, int level=1, const std::string &indent="")
Loops recursively over the MCParticle list and prints information about each particle.
bool m_showMomenta
Show particle momenta.
std::string m_particleList
The name of the MCParticle collection.
bool m_suppressPrint
Suppress print the information.
bool m_onlyPrimaries
Print only primary particles.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.