DD4hep  1.38.0
Detector Description Toolkit for High Energy Physics
Geant4EventSeed.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 : A.Sailer
11 //
12 //=========================================================================
13 
14 // Class include file
15 #include "Geant4EventSeed.h"
16 
17 // Framework include files
18 #include <DD4hep/InstanceCount.h>
19 #include <DD4hep/Printout.h>
20 
21 #include <DDG4/EventParameters.h>
22 #include <DDG4/Geant4EventAction.h>
24 #include <DDG4/Geant4Random.h>
25 #include <DDG4/Factories.h>
26 
27 #include <CLHEP/Random/EngineFactory.h>
28 
29 //Geant includes
30 #include <G4Run.hh>
31 #include <G4Event.hh>
32 #include <G4EventManager.hh>
33 #include <G4StackManager.hh>
34 
35 using namespace dd4hep::sim;
36 
38 Geant4EventSeed::Geant4EventSeed(Geant4Context* c, const std::string& typ) : Geant4RunAction(c, typ),
39  m_initialSeed(0),
40  m_runID(0),
41  m_type(typ),
42  m_initialised(false)
43 {
45  // Re-seed after GeneratePrimaries so file-based generators' SetEventID is visible.
48 }
49 
53 }
54 
56 void Geant4EventSeed::begin(const G4Run* run) {
57 
58  if(not m_initialised){
59  m_initialised = true;
61  }
62 
63  m_runID = run->GetRunID();
64 
65  dd4hep::printout( dd4hep::INFO, m_type, "Get RunID: runID=%u", m_runID );
66 
67 }
68 
70 void Geant4EventSeed::beginEvent(const G4Event* evt) {
71  dd4hep::printout(dd4hep::DEBUG, m_type, "EventSeed:: At beginEvent");
72  setSeed(evt, true);
73 }
74 
76 void Geant4EventSeed::setSeedForPrimaries(const G4Event* evt) {
77  dd4hep::printout(dd4hep::DEBUG, m_type, "EventSeed:: At generatePrimaries");
78  // since this is before we read input files we cannot expect parameters to be present
79  setSeed(evt, false);
80 }
81 
83 void Geant4EventSeed::setSeed(const G4Event* evt, bool checkForEventParameters) {
84 
86 
87  //Trying to use event and run id from the Geant4 event, unless we have them also in the EventParameters
88  //Both are kept local: m_runID is owned by the begin-of-run callback and must not be overwritten here,
89  //otherwise the value would leak into the calls that asked not to look at the EventParameters
90  unsigned int eventID = evt->GetEventID();
91  unsigned int runID = m_runID;
92 
93  if(checkForEventParameters) {
94  //Get EventParameters from the context
95  EventParameters* parameters = context()->event().extension<EventParameters>(false);
96  if(parameters) {
97  //Unset numbers are negative in the EventParameters, keep the Geant4 values in that case
98  if(parameters->eventNumber() < 0) {
99  dd4hep::printout(dd4hep::DEBUG, m_type,
100  "EventSeed::setSeed: eventParameters have unset eventNumber=%d, keeping eventID=%u",
101  parameters->eventNumber(), eventID);
102  } else {
103  eventID = parameters->eventNumber();
104  }
105  if(parameters->runNumber() < 0) {
106  dd4hep::printout(dd4hep::DEBUG, m_type,
107  "EventSeed::setSeed: eventParameters have unset runNumber=%d, keeping runID=%u",
108  parameters->runNumber(), runID);
109  } else {
110  runID = parameters->runNumber();
111  }
112  dd4hep::printout(dd4hep::DEBUG, m_type,
113  "EventSeed::setSeed: Found eventParameters: eventID=%u, runID=%u",
114  eventID, runID);
115  } else {
116  dd4hep::printout(dd4hep::DEBUG, m_type,
117  "EventSeed::setSeed: Did not find eventParameters");
118  }
119  }
120 
121  unsigned int newSeed = hash( m_initialSeed, eventID, runID );
122  dd4hep::printout(dd4hep::INFO, m_type,
123  "EventSeed::setSeed: eventID=%u, runID=%u initialSeed=%u, newSeed=%u",
124  eventID, runID, m_initialSeed, newSeed );
125 
126  rndm->setSeed(newSeed);
127 
128  if (dd4hep::printLevel() <= dd4hep::DEBUG) {
129  rndm->showStatus();
130  }
131 
132 }
133 
dd4hep::sim::Geant4Random::instance
static Geant4Random * instance(bool throw_exception=true)
Access the main Geant4 random generator instance. Must be created before used!
Definition: Geant4Random.cpp:114
dd4hep::sim::Geant4Random::engine
CLHEP::HepRandomEngine * engine()
CLHEP random number engine (valid after initialization only)
Definition: Geant4Random.h:116
dd4hep::sim::Geant4Action::generatorAction
Geant4GeneratorActionSequence & generatorAction() const
Access to the main generator action sequence from the kernel object.
Definition: Geant4Action.cpp:303
dd4hep::sim::Geant4EventSeed::begin
void begin(const G4Run *)
begin-of-run callback
Definition: Geant4EventSeed.cpp:56
Geant4EventAction.h
dd4hep::sim::Geant4EventSeed::~Geant4EventSeed
virtual ~Geant4EventSeed()
Default destructor.
Definition: Geant4EventSeed.cpp:51
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::sim::Geant4EventSeed::setSeedForPrimaries
void setSeedForPrimaries(const G4Event *)
generatePrimaries callback
Definition: Geant4EventSeed.cpp:76
dd4hep::sim::Geant4Context::event
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
Definition: Geant4Context.cpp:84
dd4hep::sim::Geant4EventSeed
Plugin class to set the event seed for each event.
Definition: Geant4EventSeed.h:47
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::sim::Geant4EventSeed::Geant4EventSeed
Geant4EventSeed(Geant4Context *, const std::string &)
Standard constructor with initializing arguments.
Definition: Geant4EventSeed.cpp:38
dd4hep::sim::Geant4Random::setSeed
virtual void setSeed(long seed)
Should initialise the status of the algorithm according to seed.
Definition: Geant4Random.cpp:200
dd4hep::sim::Geant4Event::extension
T * extension(bool alert=true)
Access to type safe extension object. Exception is thrown if the object is invalid.
Definition: Geant4Context.h:151
dd4hep::sim::Geant4GeneratorActionSequence::callAtBegin
void callAtBegin(Q *p, void(T::*f)(const G4Event *))
Register callback to happen before primary particle. Types Q and T must be polymorph!
Definition: Geant4GeneratorAction.h:144
dd4hep::sim::Geant4Action::eventAction
Geant4EventActionSequence & eventAction() const
Access to the main event action sequence from the kernel object.
Definition: Geant4Action.cpp:283
dd4hep::sim::Geant4EventSeed::m_initialSeed
unsigned int m_initialSeed
Definition: Geant4EventSeed.h:50
EventParameters.h
Geant4Random.h
dd4hep::sim::Geant4EventSeed::setSeed
void setSeed(const G4Event *, bool checkForParameters=true)
set the seed function, called from other callbacks)
Definition: Geant4EventSeed.cpp:83
dd4hep::sim::Geant4EventActionSequence::callAtBegin
void callAtBegin(Q *p, void(T::*f)(const G4Event *))
Register begin-of-event callback.
Definition: Geant4EventAction.h:164
dd4hep::sim::Geant4Random::showStatus
virtual void showStatus() const
Should dump the current engine status on the screen.
Definition: Geant4Random.cpp:231
Geant4StackingAction.h
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4EventSeed::beginEvent
void beginEvent(const G4Event *)
begin-of-event callback: seeds based on pre-assigned Geant4 event ID
Definition: Geant4EventSeed.cpp:70
dd4hep::sim::Geant4Random
Mini interface to THE random generator of the application.
Definition: Geant4Random.h:59
Factories.h
Geant4EventSeed.h
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: EDM4hepFileReader.cpp:46
dd4hep::sim::Geant4EventSeed::m_runID
unsigned int m_runID
Definition: Geant4EventSeed.h:51
dd4hep::sim::EventParameters
Event extension to pass input event data to output event.
Definition: EventParameters.h:28
dd4hep::sim::Geant4RunAction
Concrete basic implementation of the Geant4 run action base class.
Definition: Geant4RunAction.h:45
dd4hep::sim::hash
unsigned int hash(unsigned int initialSeed, unsigned int eventNumber, unsigned int runNumber)
calculate hash from initialSeed, eventID and runID
Definition: Geant4EventSeed.h:201
dd4hep::sim::Geant4EventSeed::m_type
std::string m_type
Definition: Geant4EventSeed.h:52
InstanceCount.h
dd4hep::sim::EventParameters::eventNumber
int eventNumber() const
Get the event number.
Definition: EventParameters.h:40
Printout.h
dd4hep::sim::EventParameters::runNumber
int runNumber() const
Get the run number.
Definition: EventParameters.h:38
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270
dd4hep::sim::Geant4EventSeed::m_initialised
bool m_initialised
Definition: Geant4EventSeed.h:53