ATLAS Offline Software
G4AtlasRunManager.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 #include "G4GeometryManager.hh"
9 #include "G4LogicalVolumeStore.hh"
10 #include "G4ParallelWorldScoringProcess.hh"
11 #include "G4RegionStore.hh"
12 #include "G4Run.hh"
13 #include "G4ScoringManager.hh"
14 #include "G4StateManager.hh"
15 #include "G4TransportationManager.hh"
16 #include "G4UImanager.hh"
17 #include "G4UserRunAction.hh"
18 #include "G4Version.hh"
19 
20 #include <string>
21 
23  : G4RunManager()
24  , AthMessaging("G4AtlasRunManager")
25  , m_recordFlux(false)
26  , m_fastSimTool("FastSimulationMasterTool")
27  , m_physListSvc("PhysicsListSvc", "G4AtlasRunManager")
28  , m_detGeoSvc("DetectorGeometrySvc", "G4AtlasRunManager")
29  , m_volumeSmartlessLevel({})
30 { }
31 
32 
33 G4AtlasRunManager* G4AtlasRunManager::GetG4AtlasRunManager ATLAS_NOT_THREAD_SAFE ()
34 {
35  static G4AtlasRunManager *thisManager = nullptr;
36  if (!thisManager) { thisManager = new G4AtlasRunManager; } // Leaked
37  return thisManager;
38 }
39 
40 
42 {
43  // ADA 11/28.2018: switch initialization order to meet ISF requirements
44  // Call the base class Initialize method. This will call
45  // InitializeGeometry and InitializePhysics.
47 }
48 
49 
51 {
52  ATH_MSG_DEBUG( "InitializeGeometry()" );
53  if (m_detGeoSvc.retrieve().isFailure()) {
54  ATH_MSG_ERROR ( "Could not retrieve the DetectorGeometrySvc" );
55  G4ExceptionDescription description;
56  description << "InitializeGeometry: Failed to retrieve IDetectorGeometrySvc.";
57  G4Exception("G4AtlasRunManager", "CouldNotRetrieveDetGeoSvc", FatalException, description);
58  abort(); // to keep Coverity happy
59  }
60 
61  // Create/assign detector construction
62  G4RunManager::SetUserInitialization(m_detGeoSvc->GetDetectorConstruction());
63  if (userDetector) {
64  G4RunManager::InitializeGeometry();
65  }
66  else {
67  ATH_MSG_WARNING( " User Detector not set!!! Geometry NOT initialized!!!" );
68  }
69  return;
70 }
71 
72 
74 {
75  ATH_MSG_DEBUG( "G4AtlasRunManager::EndEvent" );
76 }
77 
78 
80 {
81  ATH_MSG_INFO( "InitializePhysics()" );
82  kernel->InitializePhysics();
83  physicsInitialized = true;
84 
85  // Grab the physics list tool and set the extra options
86  if (m_physListSvc.retrieve().isFailure()) {
87  ATH_MSG_ERROR ( "Could not retrieve the physics list tool" );
88  G4ExceptionDescription description;
89  description << "InitializePhysics: Failed to retrieve IPhysicsListSvc.";
90  G4Exception("G4AtlasRunManager", "CouldNotRetrievePLTool", FatalException, description);
91  abort(); // to keep Coverity happy
92  }
93  m_physListSvc->SetPhysicsOptions();
94 
95  // Fast simulations last
96  if (m_fastSimTool.retrieve().isFailure()) {
97  ATH_MSG_ERROR ( "Could not retrieve the FastSim master tool" );
98  G4ExceptionDescription description;
99  description << "InitializePhysics: Failed to retrieve IFastSimulationMasterTool.";
100  G4Exception("G4AtlasRunManager", "CouldNotRetrieveFastSimMaster", FatalException, description);
101  abort(); // to keep Coverity happy
102  }
103  if(m_fastSimTool->initializeFastSims().isFailure()) {
104  G4ExceptionDescription description;
105  description << "InitializePhysics: Call to IFastSimulationMasterTool::initializeFastSims failed.";
106  G4Exception("G4AtlasRunManager", "FailedToInitializeFastSims", FatalException, description);
107  abort(); // to keep Coverity happy
108  }
109 
110  if (m_recordFlux) {
111  m_fluxRecorder->InitializeFluxRecording();
112  }
113 
114  return;
115 }
116 
118 {
119 
120  G4StateManager* stateManager = G4StateManager::GetStateManager();
121  stateManager->SetNewState(G4State_GeomClosed);
122 
123  currentEvent = event;
124 
125  eventManager->ProcessOneEvent(currentEvent);
126  if (currentEvent->IsAborted()) {
127  ATH_MSG_WARNING( "G4AtlasRunManager::ProcessEvent: Event Aborted at Detector Simulation level" );
128  currentEvent = nullptr;
129  return true;
130  }
131 
132  if (m_recordFlux) { m_fluxRecorder->RecordFlux(currentEvent); }
133 
134  this->StackPreviousEvent(currentEvent);
135  bool abort = currentEvent->IsAborted();
136  currentEvent = nullptr;
137 
138  return abort;
139 }
140 
142 {
143  ATH_MSG_DEBUG( " G4AtlasRunManager::RunTermination() " );
144  if (m_recordFlux) {
145  m_fluxRecorder->WriteFluxInformation();
146  }
147 
148  this->CleanUpPreviousEvents();
149  previousEvents->clear();
150 
151  if (userRunAction) {
152  userRunAction->EndOfRunAction(currentRun);
153  }
154 
155  delete currentRun;
156  currentRun = nullptr;
157  runIDCounter++;
158 
159  ATH_MSG_VERBOSE( "Changing the state..." );
160  G4StateManager* stateManager = G4StateManager::GetStateManager();
161  stateManager->SetNewState(G4State_Idle);
162 
163  ATH_MSG_VERBOSE( "Opening the geometry back up" );
164  G4GeometryManager::GetInstance()->OpenGeometry();
165 
166  ATH_MSG_VERBOSE( "Terminating the run... State is " << stateManager->GetStateString( stateManager->GetCurrentState() ) );
167  kernel->RunTermination();
168  ATH_MSG_VERBOSE( "All done..." );
169 
170  userRunAction = nullptr;
171  userEventAction = nullptr;
172  userSteppingAction = nullptr;
173  userStackingAction = nullptr;
174  userTrackingAction = nullptr;
175  userDetector = nullptr;
176  userPrimaryGeneratorAction = nullptr;
177 
178  return;
179 }
180 
G4AtlasRunManager::Initialize
void Initialize() override final
Definition: G4AtlasRunManager.cxx:41
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
G4AtlasRunManager::RunTermination
void RunTermination() override final
G4 function called at end of run.
Definition: G4AtlasRunManager.cxx:141
G4AtlasRunManager::m_physListSvc
ServiceHandle< IPhysicsListSvc > m_physListSvc
Definition: G4AtlasRunManager.h:87
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
G4AtlasRunManager
ATLAS custom singleton run manager.
Definition: G4AtlasRunManager.h:30
G4AtlasRunManager::G4AtlasRunManager
G4AtlasRunManager()
Pure singleton private constructor.
Definition: G4AtlasRunManager.cxx:22
G4AtlasRunManager.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
G4AtlasRunManager::m_fluxRecorder
std::unique_ptr< IFluxRecorder > m_fluxRecorder
Interface to flux recording.
Definition: G4AtlasRunManager.h:93
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
G4AtlasRunManager::ProcessEvent
bool ProcessEvent(G4Event *event)
Does the work of simulating an ATLAS event.
Definition: G4AtlasRunManager.cxx:117
G4AtlasRunManager::m_recordFlux
bool m_recordFlux
Definition: G4AtlasRunManager.h:84
G4AtlasRunManager::m_detGeoSvc
ServiceHandle< IDetectorGeometrySvc > m_detGeoSvc
Definition: G4AtlasRunManager.h:89
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
ATLAS_NOT_THREAD_SAFE
G4AtlasRunManager *G4AtlasRunManager::GetG4AtlasRunManager ATLAS_NOT_THREAD_SAFE()
Install fatal handler with default options.
Definition: G4AtlasRunManager.cxx:33
jobOptions.Initialize
Initialize
Definition: jobOptions.pA.py:28
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
G4AtlasRunManager::InitializePhysics
void InitializePhysics() override final
Definition: G4AtlasRunManager.cxx:79
G4AtlasRunManager::m_fastSimTool
ToolHandle< IFastSimulationMasterTool > m_fastSimTool
Definition: G4AtlasRunManager.h:86
G4AtlasRunManager::InitializeGeometry
void InitializeGeometry() override final
Definition: G4AtlasRunManager.cxx:50
description
std::string description
glabal timer - how long have I taken so far?
Definition: hcg.cxx:88
G4AtlasRunManager::EndEvent
void EndEvent()
Definition: G4AtlasRunManager.cxx:73