DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Geant4InputAction.cpp
Go to the documentation of this file.
1 // $Id: $
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // @author P.Kostka (main author)
12 // @author M.Frank (code reshuffeling into new DDG4 scheme)
13 //
14 //====================================================================
15 
16 // Framework include files
17 #include "DD4hep/Memory.h"
18 #include "DD4hep/Plugins.h"
19 #include "DDG4/Geant4Primary.h"
20 #include "DDG4/Geant4Context.h"
21 #include "DDG4/Geant4InputAction.h"
22 
23 #include "G4Event.hh"
24 
25 using namespace std;
26 using namespace DD4hep::Simulation;
28 
30 Geant4EventReader::Geant4EventReader(const std::string& nam)
31  : m_name(nam), m_directAccess(false), m_currEvent(0)
32 {
33 }
34 
37 }
38 
41  if ( hasDirectAccess() ) {
42  ++m_currEvent;
43  return EVENT_READER_OK;
44  }
45  std::vector<Particle*> particles;
47  ++m_currEvent;
48  EventReaderStatus sc = readParticles(m_currEvent,vertex,particles);
49  for_each(particles.begin(),particles.end(),deleteObject<Particle>);
50  return sc;
51 }
52 
55 Geant4EventReader::moveToEvent(int event_number) {
56  if ( event_number >= INT_MIN ) {
57  return EVENT_READER_OK; // Logic below does not work as expected.
58  } // This shortcuts it!
59  // APS: would have been nice to know what exactly doesn't work...
60  if ( m_currEvent == event_number ) {
61  return EVENT_READER_OK;
62  }
63  else if ( hasDirectAccess() ) {
64  m_currEvent = event_number;
65  return EVENT_READER_OK;
66  }
67  else if ( event_number<m_currEvent ) {
68  return EVENT_READER_ERROR;
69  }
70  else {
71  for(int i=m_currEvent; i<event_number;++i)
72  skipEvent();
73  m_currEvent = event_number;
74  return EVENT_READER_OK;
75  }
76  return EVENT_READER_ERROR;
77 }
78 
81  : Geant4GeneratorAction(ctxt,nam), m_reader(0), m_currentEventNumber(0)
82 {
83  declareProperty("Input", m_input);
84  declareProperty("Sync", m_firstEvent=0);
85  declareProperty("Mask", m_mask = 0);
86  declareProperty("MomentumScale", m_momScale = 1.0);
87  declareProperty("HaveAbort", m_abort = true);
88  m_needsControl = true;
89 }
90 
93 }
94 
96 string Geant4InputAction::issue(int i) const {
97  stringstream str;
98  str << "Geant4InputAction[" << name() << "]: Event " << i << " ";
99  return str.str();
100 }
101 
104  Vertex& prim_vertex,
105  std::vector<Particle*>& particles)
106 {
107  int evid = evt_number + m_firstEvent;
108  if ( 0 == m_reader ) {
109  if ( m_input.empty() ) {
110  except("InputAction: No input file declared!");
111  }
112  string err;
113  TypeName tn = TypeName::split(m_input,"|");
114  try {
115  m_reader = PluginService::Create<Geant4EventReader*>(tn.first,tn.second);
116  if ( 0 == m_reader ) {
117  PluginDebug dbg;
118  m_reader = PluginService::Create<Geant4EventReader*>(tn.first,tn.second);
119  abortRun(issue(evid)+"Error creating reader plugin.",
120  "Failed to create file reader of type %s. Cannot open dataset %s",
121  tn.first.c_str(),tn.second.c_str());
123  }
124  }
125  catch(const exception& e) {
126  err = e.what();
127  }
128  if ( !err.empty() ) {
129  abortRun(issue(evid)+err,"Error when creating reader for file %s",m_input.c_str());
131  }
132  }
133  int status = m_reader->moveToEvent(evid);
134  if ( Geant4EventReader::EVENT_READER_OK != status ) {
135  string msg = issue(evid)+"Error when moving to event - may be end of file.";
136  if ( m_abort ) {
137  abortRun(msg,"Error when reading file %s",m_input.c_str());
138  return status;
139  }
140  error(msg.c_str());
141  except("Error when reading file %s.", m_input.c_str());
142  return status;
143  }
144  status = m_reader->readParticles(evid, prim_vertex, particles);
145  if ( Geant4EventReader::EVENT_READER_OK != status ) {
146  string msg = issue(evid)+"Error when moving to event - may be end of file.";
147  if ( m_abort ) {
148  abortRun(msg,"Error when reading file %s",m_input.c_str());
149  return status;
150  }
151  error(msg.c_str());
152  except("Error when reading file %s.", m_input.c_str());
153  }
154  return status;
155 }
156 
158 void Geant4InputAction::operator()(G4Event* event) {
159  vector<Particle*> primaries;
160  Geant4Event& evt = context()->event();
163  int result;
164 
165  vertex->x = 0;
166  vertex->y = 0;
167  vertex->z = 0;
168  vertex->time = 0;
169  result = readParticles(m_currentEventNumber, *(vertex.get()), primaries);
170 
171  event->SetEventID(m_firstEvent + m_currentEventNumber);
173 
174  if ( result != Geant4EventReader::EVENT_READER_OK ) { // handle I/O error, but how?
175  return;
176  }
177 
179  prim->add(m_mask, inter);
180  // check if there is at least one particle
181  if ( primaries.empty() ) return;
182 
183  print("+++ Particle interaction with %d generator particles ++++++++++++++++++++++++",
184  int(primaries.size()));
185  Geant4Vertex* vtx = vertex.get();
186  inter->vertices.insert(make_pair(m_mask,vertex.release())); // Move vertex ownership
187  // build collection of MCParticles
188  for(size_t i=0; i<primaries.size(); ++i ) {
189  Geant4ParticleHandle p(primaries[i]);
190  const double mom_scale = m_momScale;
191  PropertyMask status(p->status);
192  p->psx = mom_scale*p->psx;
193  p->psy = mom_scale*p->psy;
194  p->psz = mom_scale*p->psz;
195 
196  if ( p->parents.size() == 0 ) {
198  vtx->in.insert(p->id); // Beam particles and primary quarks etc.
199  else
200  vtx->out.insert(p->id); // Stuff, to be given to Geant4 together with daughters
201  }
202  inter->particles.insert(make_pair(p->id,p));
204  }
205 }
DD4hep::ReferenceBitMask< int > PropertyMask
PrintLevel outputLevel() const
Access the output level.
Definition: Geant4Action.h:287
VertexMap vertices
The map of primary vertices for the particles.
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
User event context for DDG4.
bool hasDirectAccess() const
Flag if direct event access (by event sequence number) is supported (Default: false) ...
virtual void operator()(G4Event *event)
Callback to generate primary particles.
Helper class to handle strings of the format "type/name".
Definition: Geant4Action.h:62
double m_momScale
Property: Momentum downscaler for debugging.
std::string m_input
Property: input file.
Geant4EventReader * m_reader
Event reader object.
Particles out
The list of outgoing particles.
Definition: Geant4Vertex.h:56
Class modelling a single interaction with multiple primary vertices and particles.
Definition: Geant4Primary.h:96
Particles in
The list of incoming particles.
Definition: Geant4Vertex.h:58
Out version of the std auto_ptr implementation base either on auto_ptr or unique_ptr.
Definition: Memory.h:43
return e
Definition: Volumes.cpp:297
void error(const char *fmt,...) const
Support of error messages.
Data structure to manipulate a bitmask held by reference and represented by an integer.
Definition: Primitives.h:526
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:261
Class modelling a complete primary event with multiple interactions.
Data structure to store the MC vertex information.
Definition: Geant4Vertex.h:46
const std::string & name() const
Access name of the action.
Definition: Geant4Action.h:271
void print(const char *fmt,...) const
Support for messages with variable output level using output level.
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
virtual ~Geant4EventReader()
Default destructor.
Geant4Vertex * vertex(EventStream &info, int i)
virtual ~Geant4InputAction()
Default destructor.
Particles parents
The list of daughters of this MC particle.
Helper to debug plugin manager calls.
Definition: Plugins.h:77
int m_currentEventNumber
current event number without initially skipped events
ParticleMap particles
The map of particles participating in this primary interaction.
virtual EventReaderStatus skipEvent()
Skip event. To be implemented for sequential sources.
EventReaderStatus
Status codes of the event reader object. Anything with NOT low-bit set is an error.
std::string issue(int i) const
helper to report Geant4 exceptions
T * extension(bool alert=true)
Access to type safe extension object. Exception is thrown if the object is invalid.
Concrete implementation of the Geant4 generator action base class.
virtual EventReaderStatus moveToEvent(int event_number)
Move to the indicated event number.
UriReader * m_reader
Pointer to URI reader.
bool m_needsControl
Default property: Flag to create control instance.
Definition: Geant4Action.h:101
int m_currEvent
Current event number.
virtual EventReaderStatus readParticles(int event_number, Vertex &primary_vertex, Particles &particles)=0
Read an event and fill a vector of MCParticles.
Data structure to access derived MC particle information.
static TypeName split(const std::string &type_name)
Split string pair according to default delimiter ('/')
void dumpWithMomentumAndVertex(int level, const std::string &src, const char *tag) const
Output type 3:+++ <tag> ID: 0 e- status:00000014 type: 11 Vertex:(+0.00e+00,+0.00e+00,+0.00e+00) [mm] time: +0.00e+00 [ns] #Par: 0 #Dau: 4.
Generic context to extend user, run and event information.
void abortRun(const std::string &exception, const char *fmt,...) const
Abort Geant4 Run by throwing a G4Exception with type RunMustBeAborted.
void add(int id, Geant4PrimaryInteraction *interaction)
Add a new interaction object to the event.
int m_mask
Property; interaction mask.
Geant4InputAction(Geant4Context *context, const std::string &name)
Standard constructor.
bool m_abort
Flag to call abortEvent in case of failure (default: true)
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:358
bool isSet(const T &m) const
Definition: Primitives.h:544
int readParticles(int event_number, Vertex &primary_vertex, Particles &particles)
Read an event and return a LCCollectionVec of MCParticles.