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