DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
HepMC3EventReader.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 //
11 //====================================================================
12 
13 // Geant4 include files
14 #include <G4GlobalConfig.hh>
15 #include <G4ParticleTable.hh>
16 
17 // Framework include files
18 #include "HepMC3EventReader.h"
19 #include <DD4hep/Printout.h>
20 #include <DDG4/Geant4Primary.h>
21 #include <DDG4/Geant4Context.h>
22 #include <DDG4/Factories.h>
23 
24 #include <G4ParticleTable.hh>
25 
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>
31 
32 #include <G4Event.hh>
33 
34 
36 using SGenParticle = std::shared_ptr<HepMC3::GenParticle>;
37 using PropertyMask = dd4hep::detail::ReferenceBitMask<int>;
38 
39 namespace {
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!");
44  }
45  return (*ip).second;
46  }
47 }
48 
50 HEPMC3EventReader::HEPMC3EventReader(const string& fileName): Geant4EventReader(fileName) {}
51 
54 HEPMC3EventReader::readParticles(int event_number, Vertices& vertices, Particles& particles) {
55  HepMC3::GenEvent genEvent;
56  std::map<SGenParticle, int> mcparts;
57  std::vector<SGenParticle> mcpcoll;
58  EventReaderStatus ret = readGenEvent(event_number, genEvent);
59 
60  if ( ret != EVENT_READER_OK ) return ret;
61 
62  int NHEP = genEvent.particles().size();
63  // check if there is at least one particle
64  if ( NHEP == 0 ) return EVENT_READER_NO_PRIMARIES;
65 
66  mcpcoll.resize(NHEP,0);
67  for(int i=0; i<NHEP; ++i ) {
68  auto p = genEvent.particles().at(i);
69  mcparts[p] = i;
70  mcpcoll[i] = std::move(p);
71  }
72 
73  //treat event attributes, flow[12]
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){
78  if(attr.first == m_flow1){
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());
82  }
83  }
84  }
85 
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;
88  // build collection of MCParticles
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}; //mcp->getSpin(); // FIXME
96  const int color[] = {colorFlow[0][mcp->id()], colorFlow[1][mcp->id()]};
97  const int pdg = mcp->pid();
98  p->pdgID = pdg;
99  p->charge = 0; // int(mcp->getCharge()*3.0); // FIXME
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;
111  p->process = 0;
112  p->spin[0] = spin[0];
113  p->spin[1] = spin[1];
114  p->spin[2] = spin[2];
115  p->colorFlow[0] = color[0];
116  p->colorFlow[1] = color[1];
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]));
123 
124  PropertyMask status(p->status);
125  int genStatus = mcp->status();
126  // Copy raw generator status
127  p->genStatus = genStatus&G4PARTICLE_GEN_STATUS_MASK;
128  m_inputAction->setGeneratorStatus(genStatus, status);
129 
130  if ( p->parents.size() == 0 ) {
131  // A particle without a parent in HepMC3 can only be (something like) a beam particle, and it is attached to the
132  // root vertex, by default (0,0,0,0) and equal for all parent-less particles. Therefore we can take the end vertex
133  // of the parentless particle as the start vertex for outgoing particles. Note that for a particle without end
134  // vertex (such as in a particle gun), it defaults to (0,0,0,0). This cannot be fixed, the information simply isn't
135  // in the HepMC file. Having a parent enforces a vertex, having no parent forbids a vertex.
136 
137  Geant4Vertex* vtx = new Geant4Vertex ;
138  vertices.emplace_back( vtx );
139 
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;
144 
145  vtx->out.insert(p->id) ;
146  }
147 
148  particles.emplace_back(p);
149  }
150  return EVENT_READER_OK;
151 }
dd4hep::sim::Geant4Particle::vsz
double vsz
Definition: Geant4Particle.h:124
dd4hep::sim::Geant4Vertex::y
double y
Definition: Geant4Vertex.h:53
dd4hep::sim::Geant4Particle::mass
double mass
Particle mass.
Definition: Geant4Particle.h:132
dd4hep::sim::Geant4Particle::vey
double vey
Definition: Geant4Particle.h:126
dd4hep::sim::Geant4EventReader::EventReaderStatus
EventReaderStatus
Status codes of the event reader object. Anything with NOT low-bit set is an error.
Definition: Geant4InputAction.h:68
dd4hep::sim::Geant4EventReader::Particles
std::vector< Particle * > Particles
Definition: Geant4InputAction.h:65
dd4hep::sim::Geant4Particle::id
int id
not persistent
Definition: Geant4Particle.h:108
SGenParticle
std::shared_ptr< HepMC3::GenParticle > SGenParticle
Definition: HepMC3EventReader.cpp:36
dd4hep::sim::HEPMC3EventReader
Definition: HepMC3EventReader.h:33
dd4hep::sim::Geant4Particle::pdgID
int pdgID
Definition: Geant4Particle.h:115
dd4hep::sim::Geant4Particle::psz
double psz
Definition: Geant4Particle.h:128
dd4hep::sim::G4PARTICLE_GEN_STATUS_MASK
@ G4PARTICLE_GEN_STATUS_MASK
Definition: Geant4Particle.h:83
dd4hep::sim::Geant4EventReader::Particle
Geant4Particle Particle
Definition: Geant4InputAction.h:64
dd4hep::sim::Geant4Particle::vez
double vez
Definition: Geant4Particle.h:126
dd4hep::sim::HEPMC3EventReader::m_flow2
std::string m_flow2
name of the GenEvent Attribute storing the color flow2
Definition: HepMC3EventReader.h:49
dd4hep::sim::Geant4Vertex::x
double x
Vertex data.
Definition: Geant4Vertex.h:53
dd4hep::sim::Geant4Particle::colorFlow
int colorFlow[2]
Definition: Geant4Particle.h:117
HepMC3EventReader.h
dd4hep::sim::Geant4Particle::spin
float spin[3]
Definition: Geant4Particle.h:121
dd4hep::sim::Geant4Particle::genStatus
unsigned short genStatus
Definition: Geant4Particle.h:118
dd4hep::sim::Geant4Particle::status
int status
Definition: Geant4Particle.h:116
dd4hep::sim::Geant4Particle::daughters
Particles daughters
The list of daughters of this MC particle.
Definition: Geant4Particle.h:140
dd4hep::sim::Geant4InputAction::setGeneratorStatus
void setGeneratorStatus(int generatorStatus, PropertyMask &status)
Convert the generator status into a common set of generator status bits.
dd4hep::sim::Geant4Particle::time
double time
Particle creation time.
Definition: Geant4Particle.h:134
dd4hep::sim::Geant4Particle::process
const G4VProcess * process
Reference to the G4VProcess, which created this track.
Definition: Geant4Particle.h:145
dd4hep::sim::HEPMC3EventReader::readGenEvent
virtual EventReaderStatus readGenEvent(int event_number, HepMC3::GenEvent &genEvent)=0
Read an event.
dd4hep::sim::Geant4EventReader::m_inputAction
Geant4InputAction * m_inputAction
The input action context.
Definition: Geant4InputAction.h:85
dd4hep::sim::Geant4EventReader
Basic geant4 event reader class. This interface/base-class must be implemented by concrete readers.
Definition: Geant4InputAction.h:60
dd4hep::sim::Geant4Vertex::out
Particles out
The list of outgoing particles.
Definition: Geant4Vertex.h:55
dd4hep::sim::Geant4Particle::vex
double vex
The end vertex.
Definition: Geant4Particle.h:126
dd4hep::sim::Geant4Particle::vsx
double vsx
The starting vertex.
Definition: Geant4Particle.h:124
dd4hep::sim::Geant4Particle::psx
double psx
The track momentum at the start vertex.
Definition: Geant4Particle.h:128
dd4hep::sim::HEPMC3EventReader::m_flow1
std::string m_flow1
name of the GenEvent Attribute storing the color flow1
Definition: HepMC3EventReader.h:47
Factories.h
PropertyMask
dd4hep::detail::ReferenceBitMask< int > PropertyMask
Definition: HepMC3EventReader.cpp:37
dd4hep::sim::Geant4EventReader::EVENT_READER_OK
@ EVENT_READER_OK
Definition: Geant4InputAction.h:70
Geant4Primary.h
dd4hep::sim::Geant4ParticleHandle
Data structure to access derived MC particle information.
Definition: Geant4Particle.h:181
dd4hep::sim::Geant4Vertex::time
double time
Definition: Geant4Vertex.h:53
dd4hep::sim::Geant4Particle::parents
Particles parents
The list of parents of this MC particle.
Definition: Geant4Particle.h:138
dd4hep::sim::Geant4Particle::vsy
double vsy
Definition: Geant4Particle.h:124
dd4hep::sim::Geant4Vertex::z
double z
Definition: Geant4Vertex.h:53
dd4hep::sim::HEPMC3EventReader::readParticles
virtual EventReaderStatus readParticles(int event_number, Vertices &vertices, Particles &particles)
Read an event and fill a vector of Particles and vertices.
Definition: HepMC3EventReader.cpp:54
dd4hep::sim::Geant4EventReader::EVENT_READER_NO_PRIMARIES
@ EVENT_READER_NO_PRIMARIES
Definition: Geant4InputAction.h:72
dd4hep::sim::Geant4Vertex
Data structure to store the MC vertex information.
Definition: Geant4Vertex.h:45
dd4hep::sim::Geant4Particle::charge
char charge
Definition: Geant4Particle.h:119
dd4hep::sim::Geant4EventReader::Vertices
std::vector< Vertex * > Vertices
Definition: Geant4InputAction.h:66
dd4hep::sim::Geant4Particle::psy
double psy
Definition: Geant4Particle.h:128
Printout.h
Geant4Context.h
dd4hep::sim::Geant4Particle::properTime
double properTime
Proper time.
Definition: Geant4Particle.h:136