Belle II Software  release-05-02-19
RunManager.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2014 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Andreas Moll, Guofu Cao, Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <simulation/kernel/RunManager.h>
12 #include <simulation/kernel/RandomEngine.h>
13 #include <framework/logging/Logger.h>
14 
15 #include <framework/pcore/ProcHandler.h>
16 
17 #include <CLHEP/Random/Random.h>
18 
19 #include <G4LogicalVolumeStore.hh>
20 #include <G4PhysicalVolumeStore.hh>
21 #include <G4SolidStore.hh>
22 #include <G4RegionStore.hh>
23 #include <G4GeometryManager.hh>
24 
25 using namespace std;
26 using namespace Belle2;
27 using namespace Belle2::Simulation;
28 
29 RunManager* RunManager::m_instance = NULL;
30 
31 RunManager& RunManager::Instance()
32 {
33  if (!m_instance) m_instance = new RunManager();
34  return *m_instance;
35 }
36 
37 void RunManager::Initialize()
38 {
39  //Set the random number engine
40  CLHEP::HepRandom::setTheEngine(new 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 
50 void 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 
68 void 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 
80 void RunManager::endRun()
81 {
82  RunTermination();
83 }
84 
85 
86 void RunManager::destroy()
87 {
88  if (m_instance == nullptr) return;
89 
90  for (G4AssemblyVolume* assemblyVolume : m_AssemblyVolumes) delete assemblyVolume;
91  m_AssemblyVolumes.clear();
92 
93  if (ProcHandler::parallelProcessingUsed() and !ProcHandler::isOutputProcess()) {
94  //Attention, ugly:
95  //Deleting the runmanager will cause Geant4 to open the geometry and clean up
96  //optimizations. This will modify the geometry and hurt parallel processing
97  //memory usage so we do not want to do this, the operating system
98  //will free the memory anyway.
99  //So what we do if we are in one of the child processes is to empty the
100  //list of pointers in the G4LogicalVolumeStore. This means Geant4 will
101  //not find the volumes and will not delete the optimizations. We also clear
102  //all the other stores which contain geometry information just to be sure
103  G4LogicalVolumeStore::GetInstance()->clear();
104  G4PhysicalVolumeStore::GetInstance()->clear();
105  G4SolidStore::GetInstance()->clear();
106  G4RegionStore::GetInstance()->clear();
107  //And we have to open the geometry to avoid warnings from the Stores which
108  //is now a trivial operation
109  G4GeometryManager::GetInstance()->OpenGeometry();
110  } else {
111  //If we are the framework process (a.k.a the parent or only process) we do
112  //the full cleanup. We could live with the same approach as for child
113  //processes but then valgrind will probably show huge memory leaks
114  delete m_instance;
115  }
116  m_instance = nullptr;
117 }
118 
119 
120 //============================================================================
121 // Private methods
122 //============================================================================
123 
124 RunManager::RunManager() : G4RunManager()
125 {
126  m_AssemblyVolumes.clear();
127 }
128 
129 
131 {
132 }
Belle2::Simulation::RunManager::m_AssemblyVolumes
std::vector< G4AssemblyVolume * > m_AssemblyVolumes
Vector of pointers to G4AssemblyVolumes.
Definition: RunManager.h:95
Belle2::Simulation::RunManager
The run manager controls the flow of the Geant4 program and manages the event loop(s) within a run.
Definition: RunManager.h:40
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Simulation::RunManager::~RunManager
virtual ~RunManager()
The RunManager destructor.
Definition: RunManager.cc:130
Belle2::RandomEngine
Interface class to make Geant4 use the Belle2 RandomGenerator.
Definition: RandomEngine.h:24