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#include <G4AssemblyVolume.hh>
23
24using namespace std;
25using namespace Belle2;
26using namespace Belle2::Simulation;
27
29
35
37{
39 //Set the random number engine
40 CLHEP::HepRandom::setTheEngine(m_randomEngine);
41
42 SetGeometryToBeOptimized(true);
43 //Set geometry to be Initialized because there won't be any before the beginRun
44 //FIXME: reverted to run independent
45 //geometryInitialized = true;
46 G4RunManager::Initialize();
47}
48
49
50void RunManager::beginRun(int runNumber)
51{
52 //Initialize Geometry FIXME: done run independent at the moment
53 //InitializeGeometry();
54 //Check if all the necessary initializations have already be done.
55 if (ConfirmBeamOnCondition()) {
56 //Set run number
57 SetRunIDCounter(runNumber);
58
59 //Finish initialization
60 ConstructScoringWorlds();
61 RunInitialization();
62 } else {
63 B2FATAL("The Geant4 kernel was not initialized properly ! The method 'ConfirmBeamOnCondition()' failed. ");
64 }
65}
66
67
68void RunManager::processEvent(int evtNumber)
69{
70 //Geant4 simulation
71 currentEvent = GenerateEvent(evtNumber);
72 eventManager->ProcessOneEvent(currentEvent);
73 AnalyzeEvent(currentEvent);
74 UpdateScoring();
75 StackPreviousEvent(currentEvent);
76 currentEvent = NULL;
77}
78
79
81{
82 RunTermination();
83}
84
85
87{
88 delete m_randomEngine;
89
90 if (m_instance == nullptr) return;
91
92 for (G4AssemblyVolume* assemblyVolume : m_AssemblyVolumes) delete assemblyVolume;
93 m_AssemblyVolumes.clear();
94
96 //Attention, ugly:
97 //Deleting the runmanager will cause Geant4 to open the geometry and clean up
98 //optimizations. This will modify the geometry and hurt parallel processing
99 //memory usage so we do not want to do this, the operating system
100 //will free the memory anyway.
101 //So what we do if we are in one of the child processes is to empty the
102 //list of pointers in the G4LogicalVolumeStore. This means Geant4 will
103 //not find the volumes and will not delete the optimizations. We also clear
104 //all the other stores which contain geometry information just to be sure
105 G4LogicalVolumeStore::GetInstance()->clear();
106 G4PhysicalVolumeStore::GetInstance()->clear();
107 G4SolidStore::GetInstance()->clear();
108 G4RegionStore::GetInstance()->clear();
109 //And we have to open the geometry to avoid warnings from the Stores which
110 //is now a trivial operation
111 G4GeometryManager::GetInstance()->OpenGeometry();
112 } else {
113 //If we are the framework process (a.k.a the parent or only process) we do
114 //the full cleanup. We could live with the same approach as for child
115 //processes but then valgrind will probably show huge memory leaks
116 delete m_instance;
117 }
118 m_instance = nullptr;
119}
120
121
122//============================================================================
123// Private methods
124//============================================================================
125
126RunManager::RunManager() : G4RunManager()
127{
128 m_AssemblyVolumes.clear();
129}
130
131
static bool isOutputProcess()
Return true if the process is an output process.
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Interface class to make Geant4 use the Belle2 RandomGenerator.
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.
void beginRun(int runNumber)
Prepares Geant4 for a new run.
Definition RunManager.cc:50
void destroy()
Destroys the RunManager at the end of the simulation.
Definition RunManager.cc:86
void processEvent(int evtNumber)
Process a single event in Geant4.
Definition RunManager.cc:68
std::vector< G4AssemblyVolume * > m_AssemblyVolumes
Vector of pointers to G4AssemblyVolumes.
Definition RunManager.h:87
void endRun()
Terminates a Geant4 run.
Definition RunManager.cc:80
void Initialize()
Initialize the Kernel.
Definition RunManager.cc:36
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:30
RunManager()
The constructor is hidden to avoid that someone creates an instance of this class.
Abstract base class for different kinds of events.
STL namespace.