SLIC  6.1.1
Simulation for the Linear Collider (SLIC) - Geant4 simulation application
EventSourceManager.cc
Go to the documentation of this file.
1 #include "EventSourceManager.hh"
2 
3 // SLIC
4 #include "FileUtil.hh"
5 #include "EventAction.hh"
6 #include "RunManager.hh"
7 #include "SlicApplication.hh"
8 #include "GPSEventSource.hh"
9 #include "LcioEventSource.hh"
11 #include "StdHepEventSource.hh"
12 #include "LHEEventSource.hh"
13 
14 // LCDD
15 #include "lcdd/util/StringUtil.hh"
16 
17 namespace slic {
18 
19 std::string EventSourceManager::STDHEP = std::string("stdhep");
20 std::string EventSourceManager::LCIO = std::string("lcio");
21 std::string EventSourceManager::GPS = std::string("gps");
22 std::string EventSourceManager::PARTICLE_GUN = std::string("gun");
23 std::string EventSourceManager::LHE = std::string("lhe");
24 std::string EventSourceManager::UNKNOWN = std::string("unknown");
25 
27  Module("EventSourceManager"), m_currentEventSource(0), m_filename(""), m_fileIsSet(false),
28  m_newFilename(false), m_ngen(0), m_nskip(0), m_newSource(true), m_sourceType(eUnknown),
29  m_lorentzTransformationAngle(0.), m_zSmearingParam(0.), m_enablePrintEvent(false) {
30 
31  // messenger with generator command macro bindings
33 
34  // Always create a ParticleGun generator.
36 
38 }
39 
41 }
42 
43 const std::string& EventSourceManager::getFilename() {
44  return m_filename;
45 }
46 
47 void EventSourceManager::setFilename(const std::string& f) {
48  m_filename = f;
49  m_fileIsSet = true;
50  m_newFilename = true;
51 
52  /* Get a file extension from the input file. */
53  std::string fext = FileUtil::extension(m_filename);
54 
55  /* For known file extensions, get a source type. */
57 
58  /* Setup a (possibly) new source. */
59  setupEventSource(est);
60 }
61 
63  ESourceType est = eUnknown;
64 
65  /* LCIO file extension */
66  if (fext == "slcio") {
67  est = eLCIO;
68  }
69  /* StdHep files, which may have .xdr file ext */
70  else if (fext == "stdhep" || fext == "xdr") {
71  est = eStdHep;
72  /* LHE files */
73  } else if (fext == "lhe") {
74  est = eLHE;
75  } else {
76  log() << LOG::error << "WARNING: File <" << m_filename << "> does not have a known file extension." << LOG::done;
77  }
78  return est;
79 }
80 
83  m_ngen = 0;
84 }
85 
86 void EventSourceManager::setSkipEvents(unsigned int s) {
87  m_nskip = s;
88 }
89 
91  return m_nskip;
92 }
93 
94 void EventSourceManager::setSourceType(const std::string& s) {
96 }
97 
99  m_sourceType = egt;
100  m_newSource = true;
101 }
102 
104  if (egt == eStdHep) {
105  return STDHEP;
106  } else if (egt == eLCIO) {
107  return LCIO;
108  } else if (egt == eGPS) {
109  return GPS;
110  } else if (egt == eParticleGun) {
111  return PARTICLE_GUN;
112  } else if (egt == eLHE) {
113  return LHE;
114  }
115 
116  return UNKNOWN;
117 }
118 
120  std::string sl = StringUtil::toLower(s);
121 
122  ESourceType egt = eUnknown;
123 
124  if (sl == STDHEP) {
125  egt = eStdHep;
126  } else if (sl == GPS) {
127  egt = eGPS;
128  } else if (sl == PARTICLE_GUN) {
129  egt = eParticleGun;
130  } else if (sl == LCIO) {
131  egt = eLCIO;
132  } else if (sl == LHE) {
133  egt = eLHE;
134  }
135 
136  return egt;
137 }
138 
140  return m_sourceType;
141 }
142 
145 }
146 
149 }
150 
152  log() << LOG::okay << LOG::head << "Generated " << m_ngen << " events." << LOG::done;
153 }
154 
156  return m_ngen;
157 }
158 
160  m_ngen += 1;
161 }
162 
163 void EventSourceManager::generate(G4Event* evt) {
164  if (!isEOF()) {
166  } else {
167  log() << LOG::error << "No more input events from file <" << m_filename << ">." << LOG::done;
168  }
170 }
171 
175 }
176 
178  if (m_currentEventSource) {
179  delete m_currentEventSource;
181  }
182 }
183 
185  EventSource* src = 0;
186  if (st == eLCIO) {
187  src = new LcioEventSource(getFilename());
188  } else if (st == eStdHep) {
189  src = new StdHepEventSource(getFilename());
190  } else if (st == eGPS) {
191  src = new GPSEventSource();
192  } else if (st == eParticleGun) {
193  return m_particleGunSource;
194  } else if (st == eLHE) {
195  return new slic::LHEEventSource(getFilename());
196  } else if (st == eUnknown) {
197  log() << LOG::error << "The event source type <" << getSourceNameFromType(st) << " is flagged as unknown!" << LOG::done;
198  } else {
199  log() << LOG::error << "Invalid ESourceType." << LOG::done;
200  }
201 
202  return src;
203 }
204 
206  // Is it a known source type?
207  if (st != eUnknown) {
208  // It is a new type of source?
209  if (isNewSource(st)) {
210  // Set new source type.
211  m_sourceType = st;
212 
213  // Delete current source.
215 
216  // Create new source.
218 
219 #ifdef SLIC_LOG
220  log().okay("Created event generator <" + getCurrentSourceName() + ">");
221 #endif
222  } else {
223  log().warning("Type of new event source is same as old; keeping old source.");
224  }
225  } else {
226  G4Exception("", "", JustWarning, "Event Source type is unknown.");
227  }
228 }
229 
230 void EventSourceManager::setupEventSource(const std::string& s) {
232  if (st != eUnknown) {
233  setupEventSource(st);
234  } else {
235  log().error("The name <" + s + "> is not a valid event source. New source was NOT created.");
236  }
237 }
238 
240  return m_currentEventSource;
241 }
242 
243 void EventSourceManager::beginEvent(const G4Event* anEvent) {
244  // Start the event timer, as generation occurs before EventAction::BeginOfEventAction().
246 
247  // Begin event action of current source.
249 }
250 
251 void EventSourceManager::endEvent(const G4Event* anEvent) {
252  m_currentEventSource->endEvent(anEvent);
253 
254  if (m_enablePrintEvent) {
256  }
257 }
258 
259 void EventSourceManager::beginRun(const G4Run* aRun) {
260 
261 #ifdef SLIC_LOG
262  log() << LOG::debug << LOG::name << "beginRun" << LOG::done;
263 #endif
264 
265  // Setup a new event source if neccessary.
267 
268  // Call the beginRun() function of the current event source.
269  if (m_currentEventSource != 0) {
271 
272  // Filename is now old so source won't reset.
273  m_newFilename = false;
274 
275  // Reset event counter.
276  m_ngen = 0;
277  } else {
278  log() << LOG::fatal << "No event generator was setup." << LOG::done;
280  }
281 }
282 
284  if (m_newSource) {
286  m_newSource = false;
287  }
288 }
289 
290 void EventSourceManager::endRun(const G4Run* aRun) {
292 }
293 
295  return (m_sourceType == eStdHep || m_sourceType == eLCIO);
296 }
297 
299  bool eof = false;
300  if (isFileSource()) {
302  if (src) {
303  eof = src->isEOF();
304  }
305  }
306  return eof;
307 }
308 
310  return m_newSource;
311 }
312 
314  return (est != eUnknown && est != m_sourceType);
315 }
316 
318  return m_newFilename;
319 }
320 
322  EventSourceWithInputFile* esif = 0;
323  if (isFileSource()) {
324  esif = dynamic_cast<EventSourceWithInputFile*>(m_currentEventSource);
325  }
326  return esif;
327 }
328 
329 G4GeneralParticleSource* EventSourceManager::getGPS() {
330  GPSEventSource* src = dynamic_cast<GPSEventSource*>(m_currentEventSource);
331 
332  G4GeneralParticleSource* gps = 0;
333  if (src) {
334  gps = src->getGPS();
335  }
336 
337  return gps;
338 }
339 
342 
343  G4ParticleGun* gun = 0;
344  if (src) {
345  gun = src->getParticleGun();
346  }
347 
348  return gun;
349 }
350 
352  generate(evt);
353 }
354 
355 void EventSourceManager::setLorentzTransformationAngle(const G4double lorentzTransformationAngle) {
356 
357  m_lorentzTransformationAngle = lorentzTransformationAngle;
358 #ifdef SLIC_LOG
359  log() << LOG::always << "Set Lorentz transformation angle to " << G4BestUnit(m_lorentzTransformationAngle, "Angle") << LOG::done;
360 #endif
361 
362 }
363 
364 }
void startEventTimer()
Definition: EventAction.cc:84
static EventAction * getEventAction()
Definition: EventAction.cc:70
static std::string PARTICLE_GUN
void endRun(const G4Run *run)
const std::string & getCurrentSourceName()
const std::string & getSourceNameFromType(ESourceType egt) const
void setFilename(const std::string &fileName)
ESourceType getSourceTypeFromName(const std::string &name) const
const std::string & getFilename()
void setLorentzTransformationAngle(const G4double angle)
GeneratorMessenger * m_messenger
void generateNextEvent(G4Event *evt)
G4GeneralParticleSource * getGPS()
void setEventSource(EventSource *eventSource)
G4ParticleGun * getParticleGun()
void beginRun(const G4Run *run)
ESourceType getSourceTypeFromFileExtension(const std::string &ext)
EventSource * createEventSource(ESourceType st)
void beginEvent(const G4Event *event)
void generate(G4Event *event)
void endEvent(const G4Event *event)
void setSkipEvents(unsigned int n)
EventSourceWithInputFile * getFileSource()
void setSourceType(ESourceType est)
An EventSource with an associated input file, such as StdHep or LCIO event files.
The virtual base class for a physics event source such as a StdHep file.
Definition: EventSource.hh:19
virtual void endRun(const G4Run *run)
Definition: EventSource.cc:37
virtual void generate(G4Event *event)=0
virtual void beginRun(const G4Run *run)
Definition: EventSource.cc:33
virtual void printCurrentEvent()=0
virtual void beginEvent(const G4Event *event)
Definition: EventSource.cc:25
virtual void reset()
Definition: EventSource.cc:16
virtual void endEvent(const G4Event *event)
Definition: EventSource.cc:29
static std::string extension(std::string filename)
Definition: FileUtil.cc:63
Wrapper for Geant4's general particle source.
G4GeneralParticleSource * getGPS()
Geant4 commands related to event generation.
Top-level controller of LHE input.
Generates events from the MCParticle collection of an LCIO file.
LogStream & okay()
Definition: LogStream.hh:246
void setOutputLevel(const LOG::ELevel &outputLevel)
Definition: LogStream.hh:135
LogStream & error()
Definition: LogStream.hh:262
LogStream & warning()
Definition: LogStream.hh:254
Base class for slic code modules that provides common functionality, such as logging to an output str...
Definition: Module.hh:25
LogStream & log()
Definition: Module.hh:99
Wrapper for the Geant4 particle gun.
static RunManager * instance()
Definition: RunManager.hh:38
Top-level controller of StdHep input.
@ okay
Definition: LogStream.hh:33
@ debug
Definition: LogStream.hh:33
@ fatal
Definition: LogStream.hh:33
@ always
Definition: LogStream.hh:33
@ error
Definition: LogStream.hh:33
@ head
Definition: LogStream.hh:26
@ name
Definition: LogStream.hh:26
@ done
Definition: LogStream.hh:26