DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
LCIOEventReader.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 // @author P.Kostka (main author)
11 // @author M.Frank (code reshuffeling into new DDG4 scheme)
12 //
13 //====================================================================
14 
15 // Framework include files
16 #include "LCIOEventReader.h"
17 #include <DD4hep/Printout.h>
18 #include <DDG4/Geant4Primary.h>
19 #include <DDG4/Geant4Context.h>
20 #include <DDG4/Factories.h>
21 
22 #include <G4ParticleTable.hh>
23 #include <EVENT/MCParticle.h>
24 #include <EVENT/LCCollection.h>
25 
26 #include <G4Event.hh>
27 
28 using namespace std;
29 using namespace dd4hep;
30 using namespace dd4hep::sim;
31 typedef dd4hep::detail::ReferenceBitMask<int> PropertyMask;
32 
33 // Neede for backwards compatibility:
34 namespace dd4hep{namespace sim{typedef Geant4InputAction LCIOInputAction;}}
36 
37 
38 namespace {
39  inline int GET_ENTRY(const map<EVENT::MCParticle*,int>& mcparts, EVENT::MCParticle* part) {
40  map<EVENT::MCParticle*,int>::const_iterator ip=mcparts.find(part);
41  if ( ip == mcparts.end() ) {
42  throw runtime_error("Unknown particle identifier look-up!");
43  }
44  return (*ip).second;
45  }
46 }
47 
48 
50 LCIOEventReader::LCIOEventReader(const string& nam)
51  : Geant4EventReader(nam)
52 {
53 }
54 
57 }
58 
59 
63  Vertices& vertices,
64  vector<Particle*>& particles)
65 {
66  EVENT::LCCollection* primaries = 0;
67  map<EVENT::MCParticle*,int> mcparts;
68  vector<EVENT::MCParticle*> mcpcoll;
69  EventReaderStatus ret = readParticleCollection(event_number,&primaries);
70 
71  if ( ret != EVENT_READER_OK ) return ret;
72 
73  int NHEP = primaries->getNumberOfElements();
74  // check if there is at least one particle
75  if ( NHEP == 0 ) return EVENT_READER_NO_PRIMARIES;
76 
77  mcpcoll.resize(NHEP,0);
78  for(int i=0; i<NHEP; ++i ) {
79  EVENT::MCParticle* p = dynamic_cast<EVENT::MCParticle*>(primaries->getElementAt(i));
80  mcparts[p] = i;
81  mcpcoll[i] = p;
82  }
83 
84  // build collection of MCParticles
85  for(int i=0; i<NHEP; ++i ) {
86  EVENT::MCParticle* mcp = mcpcoll[i];
88  const double *mom = mcp->getMomentum();
89  const double *vsx = mcp->getVertex();
90  const double *vex = mcp->getEndpoint();
91  const float *spin = mcp->getSpin();
92  const int *color = mcp->getColorFlow();
93  const int pdg = mcp->getPDG();
94  p->pdgID = pdg;
95  p->charge = int(mcp->getCharge()*3.0);
96  p->psx = mom[0]*CLHEP::GeV;
97  p->psy = mom[1]*CLHEP::GeV;
98  p->psz = mom[2]*CLHEP::GeV;
99  p->time = mcp->getTime()*CLHEP::ns;
100  p->properTime = mcp->getTime()*CLHEP::ns;
101  p->vsx = vsx[0]*CLHEP::mm;
102  p->vsy = vsx[1]*CLHEP::mm;
103  p->vsz = vsx[2]*CLHEP::mm;
104  p->vex = vex[0]*CLHEP::mm;
105  p->vey = vex[1]*CLHEP::mm;
106  p->vez = vex[2]*CLHEP::mm;
107  p->process = 0;
108  p->spin[0] = spin[0];
109  p->spin[1] = spin[1];
110  p->spin[2] = spin[2];
111  p->colorFlow[0] = color[0];
112  p->colorFlow[1] = color[1];
113  p->mass = mcp->getMass()*CLHEP::GeV;
114  const EVENT::MCParticleVec &par = mcp->getParents(), &dau=mcp->getDaughters();
115  for(int num=dau.size(),k=0; k<num; ++k)
116  p->daughters.insert(GET_ENTRY(mcparts,dau[k]));
117  for(int num=par.size(),k=0; k<num; ++k)
118  p->parents.insert(GET_ENTRY(mcparts,par[k]));
119 
120  PropertyMask status(p->status);
121  int genStatus = mcp->getGeneratorStatus();
122  // Copy raw generator status
123  p->genStatus = genStatus&G4PARTICLE_GEN_STATUS_MASK;
124  m_inputAction->setGeneratorStatus(genStatus, status);
125 
126  //fg: we simply add all particles without parents as with their own vertex.
127  // This might include the incoming beam particles, e.g. in
128  // the case of lcio files written with Whizard2, which is slightly odd,
129  // however should be treated correctly in Geant4InputHandling.cpp.
130  // We no longer make an attempt to identify the incoming particles
131  // based on the generator status, as this varies widely with different
132  // generators.
133 
134  if ( p->parents.size() == 0 ) {
135 
136  Geant4Vertex* vtx = new Geant4Vertex ;
137  vertices.emplace_back( vtx );
138  vtx->x = p->vsx;
139  vtx->y = p->vsy;
140  vtx->z = p->vsz;
141  vtx->time = p->time;
142 
143  vtx->out.insert(p->id) ;
144  }
145 
146  if ( mcp->isCreatedInSimulation() ) status.set(G4PARTICLE_SIM_CREATED);
147  if ( mcp->isBackscatter() ) status.set(G4PARTICLE_SIM_BACKSCATTER);
148  if ( mcp->vertexIsNotEndpointOfParent() ) status.set(G4PARTICLE_SIM_PARENT_RADIATED);
149  if ( mcp->isDecayedInTracker() ) status.set(G4PARTICLE_SIM_DECAY_TRACKER);
150  if ( mcp->isDecayedInCalorimeter() ) status.set(G4PARTICLE_SIM_DECAY_CALO);
151  if ( mcp->hasLeftDetector() ) status.set(G4PARTICLE_SIM_LEFT_DETECTOR);
152  if ( mcp->isStopped() ) status.set(G4PARTICLE_SIM_STOPPED);
153  if ( mcp->isOverlay() ) status.set(G4PARTICLE_SIM_OVERLAY);
154  particles.emplace_back(p);
155  }
156  return EVENT_READER_OK;
157 }
158 
dd4hep::sim::G4PARTICLE_SIM_CREATED
@ G4PARTICLE_SIM_CREATED
Definition: Geant4Particle.h:85
dd4hep::sim::Geant4Particle::vsz
double vsz
Definition: Geant4Particle.h:124
dd4hep::sim::LCIOEventReader::readParticles
virtual EventReaderStatus readParticles(int event_number, Vertices &vertices, std::vector< Particle * > &particles)
Read an event and fill a vector of MCParticles.
Definition: LCIOEventReader.cpp:62
dd4hep::sim::G4PARTICLE_SIM_PARENT_RADIATED
@ G4PARTICLE_SIM_PARENT_RADIATED
Definition: Geant4Particle.h:91
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
Geant4InputAction
Basic geant4 event reader class. This interface/base-class must be implemented by concrete readers.
dd4hep::sim::Geant4Particle::id
int id
not persistent
Definition: Geant4Particle.h:108
dd4hep::sim::Geant4InputAction
Generic input action capable of using the Geant4EventReader class.
Definition: Geant4InputAction.h:157
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
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::sim::LCIOEventReader::readParticleCollection
virtual EventReaderStatus readParticleCollection(int event_number, EVENT::LCCollection **particles)=0
Read an event and return a LCCollectionVec of MCParticles.
dd4hep::sim::G4PARTICLE_SIM_BACKSCATTER
@ G4PARTICLE_SIM_BACKSCATTER
Definition: Geant4Particle.h:86
dd4hep::sim::LCIOInputAction
Geant4InputAction LCIOInputAction
Definition: LCIOEventReader.cpp:34
dd4hep::sim::LCIOEventReader::~LCIOEventReader
virtual ~LCIOEventReader()
Default destructor.
Definition: LCIOEventReader.cpp:56
LCIOEventReader.h
dd4hep::sim::Geant4EventReader::Particle
Geant4Particle Particle
Definition: Geant4InputAction.h:64
dd4hep::sim::G4PARTICLE_SIM_STOPPED
@ G4PARTICLE_SIM_STOPPED
Definition: Geant4Particle.h:89
dd4hep::sim::Geant4Particle::vez
double vez
Definition: Geant4Particle.h:126
dd4hep::sim::G4PARTICLE_SIM_DECAY_CALO
@ G4PARTICLE_SIM_DECAY_CALO
Definition: Geant4Particle.h:87
dd4hep::sim::Geant4Vertex::x
double x
Vertex data.
Definition: Geant4Vertex.h:53
dd4hep::sim::Geant4Particle::colorFlow
int colorFlow[2]
Definition: Geant4Particle.h:117
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::G4PARTICLE_SIM_OVERLAY
@ G4PARTICLE_SIM_OVERLAY
Definition: Geant4Particle.h:92
dd4hep::sim::Geant4Particle::daughters
Particles daughters
The list of daughters of this MC particle.
Definition: Geant4Particle.h:140
dd4hep::sim::G4PARTICLE_SIM_DECAY_TRACKER
@ G4PARTICLE_SIM_DECAY_TRACKER
Definition: Geant4Particle.h:88
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::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
Factories.h
dd4hep::sim::G4PARTICLE_SIM_LEFT_DETECTOR
@ G4PARTICLE_SIM_LEFT_DETECTOR
Definition: Geant4Particle.h:90
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
PropertyMask
dd4hep::detail::ReferenceBitMask< int > PropertyMask
Definition: HepMC3EventReader.cpp:37
std
Definition: Plugins.h:30
PropertyMask
dd4hep::detail::ReferenceBitMask< int > PropertyMask
Definition: LCIOEventReader.cpp:31
dd4hep::sim::Geant4EventReader::EVENT_READER_OK
@ EVENT_READER_OK
Definition: Geant4InputAction.h:70
Geant4Primary.h
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
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::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