DD4hep  1.36.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  CollectionOwner primaries(nullptr, [](EVENT::LCCollection*){});
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) {
72  return ret;
73  }
74  int NHEP = primaries->getNumberOfElements();
75  // check if there is at least one particle
76  if ( NHEP == 0 ) return EVENT_READER_NO_PRIMARIES;
77 
78  mcpcoll.resize(NHEP,0);
79  for(int i=0; i<NHEP; ++i ) {
80  EVENT::MCParticle* p = dynamic_cast<EVENT::MCParticle*>(primaries->getElementAt(i));
81  mcparts[p] = i;
82  mcpcoll[i] = p;
83  }
84 
85  // build collection of MCParticles
86  for(int i=0; i<NHEP; ++i ) {
87  EVENT::MCParticle* mcp = mcpcoll[i];
89  const double *mom = mcp->getMomentum();
90  const double *vsx = mcp->getVertex();
91  const double *vex = mcp->getEndpoint();
92  const float *spin = mcp->getSpin();
93  const int *color = mcp->getColorFlow();
94  const int pdg = mcp->getPDG();
95  p->pdgID = pdg;
96  p->charge = int(mcp->getCharge()*3.0);
97  p->psx = mom[0]*CLHEP::GeV;
98  p->psy = mom[1]*CLHEP::GeV;
99  p->psz = mom[2]*CLHEP::GeV;
100  p->time = mcp->getTime()*CLHEP::ns;
101  p->properTime = mcp->getTime()*CLHEP::ns;
102  p->vsx = vsx[0]*CLHEP::mm;
103  p->vsy = vsx[1]*CLHEP::mm;
104  p->vsz = vsx[2]*CLHEP::mm;
105  p->vex = vex[0]*CLHEP::mm;
106  p->vey = vex[1]*CLHEP::mm;
107  p->vez = vex[2]*CLHEP::mm;
108  p->process = 0;
109  p->spin[0] = spin[0];
110  p->spin[1] = spin[1];
111  p->spin[2] = spin[2];
112  p->colorFlow[0] = color[0];
113  p->colorFlow[1] = color[1];
114  p->mass = mcp->getMass()*CLHEP::GeV;
115  const EVENT::MCParticleVec &par = mcp->getParents(), &dau=mcp->getDaughters();
116  for(int num=dau.size(),k=0; k<num; ++k)
117  p->daughters.insert(GET_ENTRY(mcparts,dau[k]));
118  for(int num=par.size(),k=0; k<num; ++k)
119  p->parents.insert(GET_ENTRY(mcparts,par[k]));
120 
121  PropertyMask status(p->status);
122  int genStatus = mcp->getGeneratorStatus();
123  // Copy raw generator status
124  p->genStatus = genStatus&G4PARTICLE_GEN_STATUS_MASK;
125  if(m_inputAction) {
126  // in some tests we do not set up the inputAction
127  m_inputAction->setGeneratorStatus(genStatus, status);
128  }
129 
130  //fg: we simply add all particles without parents as with their own vertex.
131  // This might include the incoming beam particles, e.g. in
132  // the case of lcio files written with Whizard2, which is slightly odd,
133  // however should be treated correctly in Geant4InputHandling.cpp.
134  // We no longer make an attempt to identify the incoming particles
135  // based on the generator status, as this varies widely with different
136  // generators.
137 
138  if ( p->parents.size() == 0 ) {
139 
140  Geant4Vertex* vtx = new Geant4Vertex ;
141  vertices.emplace_back( vtx );
142  vtx->x = p->vsx;
143  vtx->y = p->vsy;
144  vtx->z = p->vsz;
145  vtx->time = p->time;
146 
147  vtx->out.insert(p->id) ;
148  }
149 
150  if ( mcp->isCreatedInSimulation() ) status.set(G4PARTICLE_SIM_CREATED);
151  if ( mcp->isBackscatter() ) status.set(G4PARTICLE_SIM_BACKSCATTER);
152  if ( mcp->vertexIsNotEndpointOfParent() ) status.set(G4PARTICLE_SIM_PARENT_RADIATED);
153  if ( mcp->isDecayedInTracker() ) status.set(G4PARTICLE_SIM_DECAY_TRACKER);
154  if ( mcp->isDecayedInCalorimeter() ) status.set(G4PARTICLE_SIM_DECAY_CALO);
155  if ( mcp->hasLeftDetector() ) status.set(G4PARTICLE_SIM_LEFT_DETECTOR);
156  if ( mcp->isStopped() ) status.set(G4PARTICLE_SIM_STOPPED);
157  if ( mcp->isOverlay() ) status.set(G4PARTICLE_SIM_OVERLAY);
158  particles.emplace_back(p);
159  }
160  return EVENT_READER_OK;
161 }
162 
dd4hep::sim::G4PARTICLE_SIM_CREATED
@ G4PARTICLE_SIM_CREATED
Definition: Geant4Particle.h:86
dd4hep::sim::Geant4Particle::vsz
double vsz
Definition: Geant4Particle.h:125
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:92
dd4hep::sim::Geant4Vertex::y
double y
Definition: Geant4Vertex.h:53
dd4hep::sim::Geant4Particle::mass
double mass
Particle mass.
Definition: Geant4Particle.h:133
dd4hep::sim::Geant4Particle::vey
double vey
Definition: Geant4Particle.h:127
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:109
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:116
dd4hep::sim::Geant4Particle::psz
double psz
Definition: Geant4Particle.h:129
dd4hep::sim::G4PARTICLE_GEN_STATUS_MASK
@ G4PARTICLE_GEN_STATUS_MASK
Definition: Geant4Particle.h:84
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::sim::G4PARTICLE_SIM_BACKSCATTER
@ G4PARTICLE_SIM_BACKSCATTER
Definition: Geant4Particle.h:87
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:90
dd4hep::sim::Geant4Particle::vez
double vez
Definition: Geant4Particle.h:127
dd4hep::sim::G4PARTICLE_SIM_DECAY_CALO
@ G4PARTICLE_SIM_DECAY_CALO
Definition: Geant4Particle.h:88
dd4hep::sim::Geant4Vertex::x
double x
Vertex data.
Definition: Geant4Vertex.h:53
dd4hep::sim::Geant4Particle::colorFlow
int colorFlow[2]
Definition: Geant4Particle.h:118
dd4hep::sim::Geant4Particle::spin
float spin[3]
Definition: Geant4Particle.h:122
dd4hep::sim::Geant4Particle::genStatus
unsigned short genStatus
Definition: Geant4Particle.h:119
dd4hep::sim::Geant4Particle::status
int status
Definition: Geant4Particle.h:117
dd4hep::sim::G4PARTICLE_SIM_OVERLAY
@ G4PARTICLE_SIM_OVERLAY
Definition: Geant4Particle.h:93
dd4hep::sim::Geant4Particle::daughters
Particles daughters
The list of daughters of this MC particle.
Definition: Geant4Particle.h:141
dd4hep::sim::G4PARTICLE_SIM_DECAY_TRACKER
@ G4PARTICLE_SIM_DECAY_TRACKER
Definition: Geant4Particle.h:89
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:135
dd4hep::sim::Geant4Particle::process
const G4VProcess * process
Reference to the G4VProcess, which created this track.
Definition: Geant4Particle.h:146
dd4hep::sim::Geant4EventReader::m_inputAction
Geant4InputAction * m_inputAction
The input action context.
Definition: Geant4InputAction.h:85
dd4hep::sim::LCIOEventReader::CollectionOwner
std::unique_ptr< EVENT::LCCollection, void(*)(EVENT::LCCollection *)> CollectionOwner
Definition: LCIOEventReader.h:40
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:127
dd4hep::sim::Geant4Particle::vsx
double vsx
The starting vertex.
Definition: Geant4Particle.h:125
dd4hep::sim::Geant4Particle::psx
double psx
The track momentum at the start vertex.
Definition: Geant4Particle.h:129
dd4hep::sim::LCIOEventReader::readParticleCollection
virtual EventReaderStatus readParticleCollection(int event_number, CollectionOwner &particles)=0
Factories.h
dd4hep::sim::G4PARTICLE_SIM_LEFT_DETECTOR
@ G4PARTICLE_SIM_LEFT_DETECTOR
Definition: Geant4Particle.h:91
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: EDM4hepFileReader.cpp:46
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:182
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:139
dd4hep::sim::Geant4Particle::vsy
double vsy
Definition: Geant4Particle.h:125
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:120
PropertyMask
dd4hep::detail::ReferenceBitMask< int > PropertyMask
Definition: EDM4hepFileReader.cpp:43
dd4hep::sim::Geant4EventReader::Vertices
std::vector< Vertex * > Vertices
Definition: Geant4InputAction.h:66
dd4hep::sim::Geant4Particle::psy
double psy
Definition: Geant4Particle.h:129
Printout.h
Geant4Context.h
dd4hep::sim::Geant4Particle::properTime
double properTime
Proper time.
Definition: Geant4Particle.h:137