Belle II Software development
RunManager.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <simulation/kernel/RunManager.h>
10#include <simulation/kernel/RandomEngine.h>
11#include <framework/logging/Logger.h>
12
13#include <framework/pcore/ProcHandler.h>
14
15#include <CLHEP/Random/Random.h>
16
17#include <G4LogicalVolumeStore.hh>
18#include <G4PhysicalVolumeStore.hh>
19#include <G4SolidStore.hh>
20#include <G4RegionStore.hh>
21#include <G4GeometryManager.hh>
22
23using namespace std;
24using namespace Belle2;
25using namespace Belle2::Simulation;
26
28
30{
31 if (!m_instance) m_instance = new RunManager();
32 return *m_instance;
33}
34
36{
38 //Set the random number engine
39 CLHEP::HepRandom::setTheEngine(m_randomEngine);
40
41 SetGeometryToBeOptimized(true);
42 //Set geometry to be Initialized because there won't be any before the beginRun
43 //FIXME: reverted to run independent
44 //geometryInitialized = true;
45 G4RunManager::Initialize();
46}
47
48
49void RunManager::beginRun(int runNumber)
50{
51 //Initialize Geometry FIXME: done run independent at the moment
52 //InitializeGeometry();
53 //Check if all the necessary initializations have already be done.
54 if (ConfirmBeamOnCondition()) {
55 //Set run number
56 SetRunIDCounter(runNumber);
57
58 //Finish initialization
59 ConstructScoringWorlds();
60 RunInitialization();
61 } else {
62 B2FATAL("The Geant4 kernel was not initialized properly ! The method 'ConfirmBeamOnCondition()' failed. ");
63 }
64}
65
66
67void RunManager::processEvent(int evtNumber)
68{
69 //Geant4 simulation
70 currentEvent = GenerateEvent(evtNumber);
71 eventManager->ProcessOneEvent(currentEvent);
72 AnalyzeEvent(currentEvent);
73 UpdateScoring();
74 StackPreviousEvent(currentEvent);
75 currentEvent = NULL;
76}
77
78
80{
81 RunTermination();
82}
83
84
86{
87 delete m_randomEngine;
88
89 if (m_instance == nullptr) return;
90
91 for (G4AssemblyVolume* assemblyVolume : m_AssemblyVolumes) delete assemblyVolume;
92 m_AssemblyVolumes.clear();
93
95 //Attention, ugly:
96 //Deleting the runmanager will cause Geant4 to open the geometry and clean up
97 //optimizations. This will modify the geometry and hurt parallel processing
98 //memory usage so we do not want to do this, the operating system
99 //will free the memory anyway.
100 //So what we do if we are in one of the child processes is to empty the
101 //list of pointers in the G4LogicalVolumeStore. This means Geant4 will
102 //not find the volumes and will not delete the optimizations. We also clear
103 //all the other stores which contain geometry information just to be sure
104 G4LogicalVolumeStore::GetInstance()->clear();
105 G4PhysicalVolumeStore::GetInstance()->clear();
106 G4SolidStore::GetInstance()->clear();
107 G4RegionStore::GetInstance()->clear();
108 //And we have to open the geometry to avoid warnings from the Stores which
109 //is now a trivial operation
110 G4GeometryManager::GetInstance()->OpenGeometry();
111 } else {
112 //If we are the framework process (a.k.a the parent or only process) we do
113 //the full cleanup. We could live with the same approach as for child
114 //processes but then valgrind will probably show huge memory leaks
115 delete m_instance;
116 }
117 m_instance = nullptr;
118}
119
120
121//============================================================================
122// Private methods
123//============================================================================
124
125RunManager::RunManager() : G4RunManager()
126{
127 m_AssemblyVolumes.clear();
128}
129
130
132{
133}
static bool isOutputProcess()
Return true if the process is an output process.
Definition: ProcHandler.cc:232
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Definition: ProcHandler.cc:226
Interface class to make Geant4 use the Belle2 RandomGenerator.
Definition: RandomEngine.h:22
The run manager controls the flow of the Geant4 program and manages the event loop(s) within a run.
Definition: RunManager.h:32
virtual ~RunManager()
The RunManager destructor.
Definition: RunManager.cc:131
void beginRun(int runNumber)
Prepares Geant4 for a new run.
Definition: RunManager.cc:49
void destroy()
Destroys the RunManager at the end of the simulation.
Definition: RunManager.cc:85
void processEvent(int evtNumber)
Process a single event in Geant4.
Definition: RunManager.cc:67
std::vector< G4AssemblyVolume * > m_AssemblyVolumes
Vector of pointers to G4AssemblyVolumes.
Definition: RunManager.h:87
void endRun()
Terminates a Geant4 run.
Definition: RunManager.cc:79
void Initialize()
Initialize the Kernel.
Definition: RunManager.cc:35
RandomEngine * m_randomEngine
Pointer to RandomEngine to avoid memory leak by creating it in the constructor and deleting it in the...
Definition: RunManager.h:90
static RunManager * m_instance
Pointer that saves the instance of this class.
Definition: RunManager.h:84
static RunManager & Instance()
Static method to get a reference to the RunManager instance.
Definition: RunManager.cc:29
RunManager()
The constructor is hidden to avoid that someone creates an instance of this class.
Definition: RunManager.cc:125
Abstract base class for different kinds of events.
STL namespace.