DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4EventReaderGuineaPig.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 : M.Frank
11 //
12 //==========================================================================
13 
25 // Framework include files
26 #include <DDG4/Geant4InputAction.h>
27 
28 // C/C++ include files
29 #include <fstream>
30 #include <algorithm>
31 #include <sstream>
32 
34 namespace dd4hep {
35 
37  namespace sim {
38 
40 
51 
52  protected:
53  std::ifstream m_input;
54  int m_part_num ;
55 
56  public:
58  explicit Geant4EventReaderGuineaPig(const std::string& nam);
62  virtual EventReaderStatus readParticles(int event_number,
63  Vertices& vertices,
64  std::vector<Particle*>& particles) override ;
65  virtual EventReaderStatus moveToEvent(int event_number) override ;
66  virtual EventReaderStatus skipEvent() override { return EVENT_READER_OK; }
67  virtual EventReaderStatus setParameters( std::map< std::string, std::string > & parameters ) override ;
68  };
69  } /* End namespace sim */
70 } /* End namespace dd4hep */
71 
72 
73 // Framework include files
74 #include <DDG4/Factories.h>
75 #include <DD4hep/Printout.h>
76 #include <CLHEP/Units/SystemOfUnits.h>
77 #include <CLHEP/Units/PhysicalConstants.h>
78 
79 // C/C++ include files
80 #include <cerrno>
81 
82 using namespace dd4hep::sim;
83 typedef dd4hep::detail::ReferenceBitMask<int> PropertyMask;
84 
85 // Factory entry
87 
90 : Geant4EventReader(nam), m_input(), m_part_num(-1)
91 {
92  // Now open the input file:
93  m_input.open(nam.c_str(), std::ifstream::in);
94  if ( !m_input.good() ) {
95  std::string err = "+++ Geant4EventReaderGuineaPig: Failed to open input stream:"+nam+
96  " Error:"+std::string(strerror(errno));
97  throw std::runtime_error(err);
98  }
99 }
100 
102 Geant4EventReaderGuineaPig::~Geant4EventReaderGuineaPig() {
103  m_input.close();
104 }
105 
106 
108 Geant4EventReaderGuineaPig::setParameters( std::map< std::string, std::string > & parameters ) {
109 
110  _getParameterValue( parameters, "ParticlesPerEvent", m_part_num, -1);
111 
112  if( m_part_num < 0 )
113  printout(INFO,"EventReader","--- Will read all particles in pairs file into one event " );
114  else
115  printout(INFO,"EventReader","--- Will read %d particles per event from pairs file ", m_part_num );
116 
117  return EVENT_READER_OK;
118 }
119 
121 Geant4EventReaderGuineaPig::moveToEvent(int event_number) {
122 
123  printout(DEBUG,"EventReader"," move to event_number: %d , m_currEvent %d",
124  event_number,m_currEvent ) ;
125 
126  if( m_currEvent == 0 && event_number > 0 ){
127 
128  if( m_part_num < 1 ) {
129 
130  printout(ERROR,"EventReader","--- Cannot skip to event %d in GuineaPig file without parameter 'ParticlesPerEvent' being set ! ", event_number );
131 
132  return EVENT_READER_IO_ERROR;
133 
134  } else {
135 
136 
137  unsigned nSkipParticles = m_part_num * event_number ;
138 
139  printout(INFO,"EventReader","--- Will skip first %d events, i.e. %d particles ", event_number , nSkipParticles );
140 
141  // First check the input file status
142  if ( !m_input.good() || m_input.eof() ) {
143  return EVENT_READER_IO_ERROR;
144  }
145 
146  for (unsigned i = 0; i<nSkipParticles; ++i){
147  if (m_input.ignore(std::numeric_limits<std::streamsize>::max(), m_input.widen('\n'))){
148  //just skipping the line
149  }
150  else
151  return EVENT_READER_IO_ERROR ;
152  }
153  }
154  }
155  // else: nothing to do ...
156 
157  return EVENT_READER_OK;
158 }
159 
162 Geant4EventReaderGuineaPig::readParticles(int /* event_number */,
163  Vertices& vertices,
164  std::vector<Particle*>& particles) {
165 
166 
167  // if no number of particles per event set, we will read the whole file
168  if ( m_part_num < 0 )
169  m_part_num = std::numeric_limits<int>::max() ;
170 
171  // First check the input file status
172  if ( m_input.eof() ) {
173  return EVENT_READER_EOF;
174  }
175  else if ( !m_input.good() ) {
176  return EVENT_READER_IO_ERROR;
177  }
178 
179  double Energy;
180  double betaX;
181  double betaY;
182  double betaZ;
183  double posX;
184  double posY;
185  double posZ;
186 
187  // Loop over particles
188  for( int counter = 0; counter < m_part_num ; ++counter ){
189 
190  // need to check for NAN as not all ifstream implementations can handle this directly
191  std::string lineStr ;
192  std::getline( m_input, lineStr ) ;
193 
194  if( m_input.eof() ) {
195  if( counter==0 ) {
196  return EVENT_READER_IO_ERROR ; // reading first particle of event failed
197  } else{
198  ++m_currEvent;
199  return EVENT_READER_OK ; // simply EOF
200  }
201  }
202 
203  std::transform(lineStr.begin(), lineStr.end(), lineStr.begin(), ::tolower);
204  if( lineStr.find("nan") != std::string::npos){
205 
206  printout(WARNING,"EventReader","### Read line with 'nan' entries - particle will be ignored ! " ) ;
207  continue ;
208  }
209  std::stringstream m_input_str( lineStr ) ;
210 
211  m_input_str >> Energy
212  >> betaX >> betaY >> betaZ
213  >> posX >> posY >> posZ ;
214 
215 
216  // printf(" ------- %e %e %e %e %e %e %e \n", Energy,betaX, betaY,betaZ,posX,posY,posZ ) ;
217 
218  //
219  // Create a MCParticle and fill it from stdhep info
220  Particle* p = new Particle(counter);
221  PropertyMask status(p->status);
222 
223  // PDGID: If Energy positive (negative) particle is electron (positron)
224  // Remember: Geant4Particle charge is in units of 1/3 elementary/positron charge
225  p->pdgID = 11;
226  p->charge = -1 * 3;
227  if(Energy < 0.0) {
228  p->pdgID = -11;
229  p->charge = +1 * 3;
230  }
231 
232  // Momentum vector
233  p->pex = p->psx = betaX*TMath::Abs(Energy)*CLHEP::GeV;
234  p->pey = p->psy = betaY*TMath::Abs(Energy)*CLHEP::GeV;
235  p->pez = p->psz = betaZ*TMath::Abs(Energy)*CLHEP::GeV;
236 
237  // Mass
238  p->mass = 0.0005109989461*CLHEP::GeV;
239  //
240 
241 
242  // Creation time (note the units [1/c_light])
243  // ( not information in GuineaPig files )
244  p->time = 0.0;
245  p->properTime = 0.0;
246 
247 
248  // Vertex
249  p->vsx = posX*CLHEP::nm;
250  p->vsy = posY*CLHEP::nm;
251  p->vsz = posZ*CLHEP::nm;
252 
253  Vertex* vtx = new Vertex ;
254  vtx->x = p->vsx ;
255  vtx->y = p->vsy ;
256  vtx->z = p->vsz ;
257  vtx->time = p->time ;
258 
259  vtx->out.insert( p->id );
260 
261  //
262  // Generator status
263  // Simulator status 0 until simulator acts on it
264  p->status = 0;
265  status.set(G4PARTICLE_GEN_STABLE);
266 
267 
268  // Add the particle to the collection vector
269  particles.emplace_back(p);
270 
271  // create a new vertex for this particle
272  vertices.emplace_back(vtx);
273 
274 
275  } // End loop over particles
276 
277  ++m_currEvent;
278 
279  return EVENT_READER_OK;
280 
281 }
282 
dd4hep::sim::Geant4EventReader::Vertex
Geant4Vertex Vertex
Definition: Geant4InputAction.h:63
dd4hep::sim::Geant4EventReader::EVENT_READER_EOF
@ EVENT_READER_EOF
Definition: Geant4InputAction.h:75
dd4hep::sim::G4PARTICLE_GEN_STABLE
@ G4PARTICLE_GEN_STABLE
Definition: Geant4Particle.h:71
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::Geant4EventReaderGuineaPig::m_part_num
int m_part_num
Definition: Geant4EventReaderGuineaPig.cpp:54
dd4hep::sim::Geant4EventReaderGuineaPig::readParticles
virtual EventReaderStatus readParticles(int event_number, Vertices &vertices, std::vector< Particle * > &particles) override
Read an event and fill a vector of MCParticles.
DECLARE_GEANT4_EVENT_READER
#define DECLARE_GEANT4_EVENT_READER(name)
Plugin defintion to create event reader objects.
Definition: Factories.h:237
dd4hep::sim::Geant4EventReader::_getParameterValue
void _getParameterValue(std::map< std::string, std::string > &parameters, std::string const &parameterName, T &parameter, T defaultValue)
transform the string parameter value into the type of parameter
Definition: Geant4InputAction.h:92
dd4hep::sim::Geant4EventReaderGuineaPig::skipEvent
virtual EventReaderStatus skipEvent() override
Skip event. To be implemented for sequential sources.
Definition: Geant4EventReaderGuineaPig.cpp:66
dd4hep::sim::Geant4EventReaderGuineaPig::moveToEvent
virtual EventReaderStatus moveToEvent(int event_number) override
Move to the indicated event number.
dd4hep::sim::Geant4EventReader::m_currEvent
int m_currEvent
Current event number.
Definition: Geant4InputAction.h:83
dd4hep::sim::Geant4EventReader::Particle
Geant4Particle Particle
Definition: Geant4InputAction.h:64
dd4hep::sim::Geant4EventReaderGuineaPig::m_input
std::ifstream m_input
Definition: Geant4EventReaderGuineaPig.cpp:53
dd4hep::sim::Geant4EventReaderGuineaPig::Geant4EventReaderGuineaPig
Geant4EventReaderGuineaPig(const std::string &nam)
Initializing constructor.
dd4hep::sim::Geant4Vertex::x
double x
Vertex data.
Definition: Geant4Vertex.h:53
dd4hep::sim::Geant4EventReaderGuineaPig::setParameters
virtual EventReaderStatus setParameters(std::map< std::string, std::string > &parameters) override
pass parameters to the event reader object
dd4hep::sim::Geant4EventReaderGuineaPig::~Geant4EventReaderGuineaPig
virtual ~Geant4EventReaderGuineaPig()
Default destructor.
dd4hep::sim::Geant4EventReader
Basic geant4 event reader class. This interface/base-class must be implemented by concrete readers.
Definition: Geant4InputAction.h:60
Factories.h
dd4hep::sim::Geant4EventReader::EVENT_READER_IO_ERROR
@ EVENT_READER_IO_ERROR
Definition: Geant4InputAction.h:74
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
PropertyMask
dd4hep::detail::ReferenceBitMask< int > PropertyMask
Definition: Geant4EventReaderGuineaPig.cpp:83
Geant4EventReaderGuineaPig
Reader for ascii files with e+e- pairs created from GuineaPig.
Geant4InputAction.h
Vertices
Geant4InputAction::Vertices Vertices
Definition: Geant4InputAction.cpp:27
dd4hep::sim::Geant4EventReader::EVENT_READER_OK
@ EVENT_READER_OK
Definition: Geant4InputAction.h:70
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::Geant4EventReader::Vertices
std::vector< Vertex * > Vertices
Definition: Geant4InputAction.h:66
Printout.h