DD4hep  1.37.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 {
46  // Re-seed after GeneratePrimaries so file-based generators' SetEventID is visible.
49 }
50 
54 }
55 
57 void Geant4EventSeed::begin(const G4Run* run) {
58 
59  if(not m_initialised){
60  m_initialised = true;
62  }
63 
64  m_runID = run->GetRunID();
65 
66  dd4hep::printout( dd4hep::INFO, m_type, "Get RunID: runID=%u", m_runID );
67 
68 }
69 
71 void Geant4EventSeed::beginEvent(const G4Event* evt) {
72  dd4hep::printout(dd4hep::DEBUG, m_type, "EventSeed:: At beginEvent");
73  setSeed(evt, true);
74 }
75 
77 void Geant4EventSeed::setSeedForPrimaries(const G4Event* evt) {
78  dd4hep::printout(dd4hep::DEBUG, m_type, "EventSeed:: At generatePrimaries");
79  // since this is before we read input files we cannot expect parameters to be present
80  setSeed(evt, false);
81 }
82 
84 void Geant4EventSeed::setSeed(const G4Event* evt, bool checkForEventParameters) {
85 
87 
88  //Trying to use event and run id from the Geant4 event, unless we have them also in the EventParameters
89  //Both are kept local: m_runID is owned by the begin-of-run callback and must not be overwritten here,
90  //otherwise the value would leak into the calls that asked not to look at the EventParameters
91  unsigned int eventID = evt->GetEventID();
92  unsigned int runID = m_runID;
93 
94  if(checkForEventParameters) {
95  //Get EventParameters from the context
96  EventParameters* parameters = context()->event().extension<EventParameters>(false);
97  if(parameters) {
98  //Unset numbers are negative in the EventParameters, keep the Geant4 values in that case
99  if(parameters->eventNumber() < 0) {
100  dd4hep::printout(dd4hep::DEBUG, m_type,
101  "EventSeed::setSeed: eventParameters have unset eventNumber=%d, keeping eventID=%u",
102  parameters->eventNumber(), eventID);
103  } else {
104  eventID = parameters->eventNumber();
105  }
106  if(parameters->runNumber() < 0) {
107  dd4hep::printout(dd4hep::DEBUG, m_type,
108  "EventSeed::setSeed: eventParameters have unset runNumber=%d, keeping runID=%u",
109  parameters->runNumber(), runID);
110  } else {
111  runID = parameters->runNumber();
112  }
113  dd4hep::printout(dd4hep::DEBUG, m_type,
114  "EventSeed::setSeed: Found eventParameters: eventID=%u, runID=%u",
115  eventID, runID);
116  } else {
117  dd4hep::printout(dd4hep::DEBUG, m_type,
118  "EventSeed::setSeed: Did not find eventParameters");
119  }
120  }
121 
122  unsigned int newSeed = hash( m_initialSeed, eventID, runID );
123  dd4hep::printout(dd4hep::INFO, m_type,
124  "EventSeed::setSeed: eventID=%u, runID=%u initialSeed=%u, newSeed=%u",
125  eventID, runID, m_initialSeed, newSeed );
126 
127  rndm->setSeed(newSeed);
128 
129  if (dd4hep::printLevel() <= dd4hep::DEBUG) {
130  rndm->showStatus();
131  }
132 
133 }
134 
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:57
Geant4EventAction.h
dd4hep::sim::Geant4EventSeed::~Geant4EventSeed
virtual ~Geant4EventSeed()
Default destructor.
Definition: Geant4EventSeed.cpp:52
dd4hep::sim::Geant4RunActionSequence::callAtBegin
void callAtBegin(Q *p, void(T::*f)(const G4Run *))
Register begin-of-run callback. Types Q and T must be polymorph!
Definition: Geant4RunAction.h:144
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:77
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:84
dd4hep::sim::Geant4EventActionSequence::callAtBegin
void callAtBegin(Q *p, void(T::*f)(const G4Event *))
Register begin-of-event callback.
Definition: Geant4EventAction.h:152
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:71
dd4hep::sim::Geant4Random
Mini interface to THE random generator of the application.
Definition: Geant4Random.h:59
dd4hep::sim::Geant4Action::runAction
Geant4RunActionSequence & runAction() const
Access to the main run action sequence from the kernel object.
Definition: Geant4Action.cpp:278
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