14 #include <G4GlobalConfig.hh>
15 #include <G4ParticleTable.hh>
24 #include <G4ParticleTable.hh>
26 #include <HepMC3/FourVector.h>
27 #include <HepMC3/GenEvent.h>
28 #include <HepMC3/GenParticle.h>
29 #include <HepMC3/GenVertex.h>
30 #include <HepMC3/Units.h>
40 template<
class T>
inline int GET_ENTRY(
const std::map<T,int>& mcparts, T part) {
41 auto ip = mcparts.find(part);
42 if (ip == mcparts.end()) {
43 throw std::runtime_error(
"Unknown particle identifier look-up!");
50 HEPMC3EventReader::HEPMC3EventReader(
const string& fileName):
Geant4EventReader(fileName) {}
55 HepMC3::GenEvent genEvent;
56 std::map<SGenParticle, int> mcparts;
57 std::vector<SGenParticle> mcpcoll;
62 int NHEP = genEvent.particles().size();
66 mcpcoll.resize(NHEP,0);
67 for(
int i=0; i<NHEP; ++i ) {
68 auto p = genEvent.particles().at(i);
70 mcpcoll[i] = std::move(p);
74 std::vector<std::map<int, int>> colorFlow(2, std::map<int, int>());
75 std::map<std::string, std::string> eventAttributes {};
76 for(
auto const& attr: genEvent.attributes()){
77 for(
auto const& inAttr: attr.second){
79 colorFlow[0][inAttr.first] = std::atoi(inAttr.second->unparsed_string().c_str());
80 }
else if(attr.first ==
m_flow2){
81 colorFlow[1][inAttr.first] = std::atoi(inAttr.second->unparsed_string().c_str());
86 double mom_unit = (genEvent.momentum_unit() == HepMC3::Units::MomentumUnit::GEV) ? CLHEP::GeV : CLHEP::MeV;
87 double len_unit = (genEvent.length_unit() == HepMC3::Units::LengthUnit::MM) ? CLHEP::mm : CLHEP::cm;
89 for(
int i=0; i<NHEP; ++i ) {
90 auto const& mcp = mcpcoll[i];
92 auto const& mom = mcp->momentum();
93 auto const& vsx = mcp->production_vertex()->position();
94 auto const& vex = (mcp->end_vertex()) ? mcp->end_vertex()->position() : HepMC3::FourVector();
95 const float spin[] = {0.0, 0.0, 0.0};
96 const int color[] = {colorFlow[0][mcp->id()], colorFlow[1][mcp->id()]};
97 const int pdg = mcp->pid();
100 p->
psx = mom.get_component(0) * mom_unit;
101 p->
psy = mom.get_component(1) * mom_unit;
102 p->
psz = mom.get_component(2) * mom_unit;
103 p->
time = vsx.get_component(3) * len_unit / CLHEP::c_light;
104 p->
properTime = vsx.get_component(3) * len_unit / CLHEP::c_light;
105 p->
vsx = vsx.get_component(0) * len_unit;
106 p->
vsy = vsx.get_component(1) * len_unit;
107 p->
vsz = vsx.get_component(2) * len_unit;
108 p->
vex = vex.get_component(0) * len_unit;
109 p->
vey = vex.get_component(1) * len_unit;
110 p->
vez = vex.get_component(2) * len_unit;
112 p->
spin[0] = spin[0];
113 p->
spin[1] = spin[1];
114 p->
spin[2] = spin[2];
117 p->
mass = mcp->generated_mass() * mom_unit;
118 auto const &par = mcp->parents(), &dau=mcp->children();
119 for(
int num=dau.size(), k=0; k<num; ++k)
120 p->
daughters.insert(GET_ENTRY(mcparts,dau[k]));
121 for(
int num=par.size(), k=0; k<num; ++k)
122 p->
parents.insert(GET_ENTRY(mcparts,par[k]));
125 int genStatus = mcp->status();
130 if ( p->
parents.size() == 0 ) {
138 vertices.emplace_back( vtx );
140 vtx->
x = vex.get_component(0);
141 vtx->
y = vex.get_component(1);
142 vtx->
z = vex.get_component(2);
143 vtx->
time = vex.get_component(3) * len_unit / CLHEP::c_light;
145 vtx->
out.insert(p->
id) ;
148 particles.emplace_back(p);