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/Geant4EventAction.h>
23 #include <DDG4/Geant4Random.h>
24 #include <DDG4/Factories.h>
25 
26 #include <CLHEP/Random/EngineFactory.h>
27 
28 //Geant includes
29 #include <G4Run.hh>
30 #include <G4Event.hh>
31 #include <G4EventManager.hh>
32 #include <G4StackManager.hh>
33 
34 using namespace dd4hep::sim;
35 
37 Geant4EventSeed::Geant4EventSeed(Geant4Context* c, const std::string& typ) : Geant4RunAction(c, typ),
38  m_initialSeed(0),
39  m_runID(0),
40  m_type(typ),
41  m_initialised(false)
42 {
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 
73 
74  unsigned int eventID = evt->GetEventID();
75  unsigned int newSeed = hash( m_initialSeed, eventID, m_runID );
76 
77  dd4hep::printout( dd4hep::INFO, m_type,
78  "At beginEvent: eventID=%u, runID=%u initialSeed=%u, newSeed=%u" ,
79  evt->GetEventID(), m_runID, m_initialSeed, newSeed );
80 
81  rndm->setSeed( newSeed );
82 
83  if ( dd4hep::printLevel() <= dd4hep::DEBUG ) {
84  rndm->showStatus();
85  }
86 
87 }
88 
90 void Geant4EventSeed::prepareEvent(G4StackManager* /* stackMgr */) {
91 
92  const G4Event* evt = G4EventManager::GetEventManager()->GetConstCurrentEvent();
93  if ( !evt ) return;
94 
96 
97  unsigned int eventID = evt->GetEventID();
98  unsigned int newSeed = hash( m_initialSeed, eventID, m_runID );
99 
100  dd4hep::printout( dd4hep::INFO, m_type,
101  "At prepareEvent: eventID=%u, runID=%u initialSeed=%u, newSeed=%u",
102  eventID, m_runID, m_initialSeed, newSeed );
103 
104  rndm->setSeed( newSeed );
105 
106 }
107 
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::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::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
Plugin class to set the event seed for each event.
Definition: Geant4EventSeed.h:46
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:37
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::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:49
Geant4Random.h
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:70
dd4hep::sim::Geant4EventSeed::prepareEvent
void prepareEvent(G4StackManager *)
prepare-stacking callback: re-seeds after GeneratePrimaries using the final event ID
Definition: Geant4EventSeed.cpp:90
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:50
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:196
dd4hep::sim::Geant4EventSeed::m_type
std::string m_type
Definition: Geant4EventSeed.h:51
dd4hep::sim::Geant4StackingActionSequence::callAtPrepare
void callAtPrepare(T *p, void(T::*f)(G4StackManager *))
Register end-of-event callback. Types Q and T must be polymorph!
Definition: Geant4StackingAction.h:155
InstanceCount.h
dd4hep::sim::Geant4Action::stackingAction
Geant4StackingActionSequence & stackingAction() const
Access to the main stacking action sequence from the kernel object.
Definition: Geant4Action.cpp:298
Printout.h
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4EventSeed::m_initialised
bool m_initialised
Definition: Geant4EventSeed.h:52