Belle II Software  release-08-01-10
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 
23 using namespace std;
24 using namespace Belle2;
25 using namespace Belle2::Simulation;
26 
27 RunManager* RunManager::m_instance = NULL;
28 
29 RunManager& RunManager::Instance()
30 {
31  if (!m_instance) m_instance = new RunManager();
32  return *m_instance;
33 }
34 
35 void RunManager::Initialize()
36 {
37  m_randomEngine = new RandomEngine();
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 
49 void 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 
67 void 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 
79 void RunManager::endRun()
80 {
81  RunTermination();
82 }
83 
84 
85 void RunManager::destroy()
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 
94  if (ProcHandler::parallelProcessingUsed() and !ProcHandler::isOutputProcess()) {
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 
125 RunManager::RunManager() : G4RunManager()
126 {
127  m_AssemblyVolumes.clear();
128 }
129 
130 
132 {
133 }
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
std::vector< G4AssemblyVolume * > m_AssemblyVolumes
Vector of pointers to G4AssemblyVolumes.
Definition: RunManager.h:87
Abstract base class for different kinds of events.