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.
46  if (m_quietMode) { SetVerboseLevel(0); } // HACK
48 }
49 
50 
52 {
53  ATH_MSG_DEBUG( "InitializeGeometry()" );
54  if (m_detGeoSvc.retrieve().isFailure()) {
55  ATH_MSG_ERROR ( "Could not retrieve the DetectorGeometrySvc" );
56  G4ExceptionDescription description;
57  description << "InitializeGeometry: Failed to retrieve IDetectorGeometrySvc.";
58  G4Exception("G4AtlasRunManager", "CouldNotRetrieveDetGeoSvc", FatalException, description);
59  abort(); // to keep Coverity happy
60  }
61 
62  // Create/assign detector construction
63  G4RunManager::SetUserInitialization(m_detGeoSvc->GetDetectorConstruction());
64  if (userDetector) {
65  G4RunManager::InitializeGeometry();
66  }
67  else {
68  ATH_MSG_WARNING( " User Detector not set!!! Geometry NOT initialized!!!" );
69  }
70  return;
71 }
72 
73 
75 {
76  ATH_MSG_DEBUG( "G4AtlasRunManager::EndEvent" );
77 }
78 
79 
81 {
82  ATH_MSG_INFO( "InitializePhysics()" );
83  kernel->InitializePhysics();
84  physicsInitialized = true;
85 
86  // Grab the physics list tool and set the extra options
87  if (m_physListSvc.retrieve().isFailure()) {
88  ATH_MSG_ERROR ( "Could not retrieve the physics list tool" );
89  G4ExceptionDescription description;
90  description << "InitializePhysics: Failed to retrieve IPhysicsListSvc.";
91  G4Exception("G4AtlasRunManager", "CouldNotRetrievePLTool", FatalException, description);
92  abort(); // to keep Coverity happy
93  }
94  m_physListSvc->SetPhysicsOptions();
95 
96  // Fast simulations last
97  if (m_fastSimTool.retrieve().isFailure()) {
98  ATH_MSG_ERROR ( "Could not retrieve the FastSim master tool" );
99  G4ExceptionDescription description;
100  description << "InitializePhysics: Failed to retrieve IFastSimulationMasterTool.";
101  G4Exception("G4AtlasRunManager", "CouldNotRetrieveFastSimMaster", FatalException, description);
102  abort(); // to keep Coverity happy
103  }
104  if(m_fastSimTool->initializeFastSims().isFailure()) {
105  G4ExceptionDescription description;
106  description << "InitializePhysics: Call to IFastSimulationMasterTool::initializeFastSims failed.";
107  G4Exception("G4AtlasRunManager", "FailedToInitializeFastSims", FatalException, description);
108  abort(); // to keep Coverity happy
109  }
110 
111  if (m_recordFlux) {
112  m_fluxRecorder->InitializeFluxRecording();
113  }
114 
115  return;
116 }
117 
119 {
120 
121  G4StateManager* stateManager = G4StateManager::GetStateManager();
122  stateManager->SetNewState(G4State_GeomClosed);
123 
124  currentEvent = event;
125 
126  eventManager->ProcessOneEvent(currentEvent);
127  if (currentEvent->IsAborted()) {
128  ATH_MSG_WARNING( "G4AtlasRunManager::ProcessEvent: Event Aborted at Detector Simulation level" );
129  currentEvent = nullptr;
130  return true;
131  }
132 
133  if (m_recordFlux) { m_fluxRecorder->RecordFlux(currentEvent); }
134 
135  this->StackPreviousEvent(currentEvent);
136  bool abort = currentEvent->IsAborted();
137  currentEvent = nullptr;
138 
139  return abort;
140 }
141 
143 {
144  ATH_MSG_DEBUG( " G4AtlasRunManager::RunTermination() " );
145  if (m_recordFlux) {
146  m_fluxRecorder->WriteFluxInformation();
147  }
148 
149  this->CleanUpPreviousEvents();
150  previousEvents->clear();
151 
152  if (userRunAction) {
153  userRunAction->EndOfRunAction(currentRun);
154  }
155 
156  delete currentRun;
157  currentRun = nullptr;
158  runIDCounter++;
159 
160  ATH_MSG_VERBOSE( "Changing the state..." );
161  G4StateManager* stateManager = G4StateManager::GetStateManager();
162  stateManager->SetNewState(G4State_Idle);
163 
164  ATH_MSG_VERBOSE( "Opening the geometry back up" );
165  G4GeometryManager::GetInstance()->OpenGeometry();
166 
167  ATH_MSG_VERBOSE( "Terminating the run... State is " << stateManager->GetStateString( stateManager->GetCurrentState() ) );
168  kernel->RunTermination();
169  ATH_MSG_VERBOSE( "All done..." );
170 
171  userRunAction = nullptr;
172  userEventAction = nullptr;
173  userSteppingAction = nullptr;
174  userStackingAction = nullptr;
175  userTrackingAction = nullptr;
176  userDetector = nullptr;
177  userPrimaryGeneratorAction = nullptr;
178 
179  return;
180 }
181 
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:142
G4AtlasRunManager::m_quietMode
bool m_quietMode
Quiet Mode for production.
Definition: G4AtlasRunManager.h:104
G4AtlasRunManager::m_physListSvc
ServiceHandle< IPhysicsListSvc > m_physListSvc
Definition: G4AtlasRunManager.h:91
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:97
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:118
G4AtlasRunManager::m_recordFlux
bool m_recordFlux
Definition: G4AtlasRunManager.h:88
G4AtlasRunManager::m_detGeoSvc
ServiceHandle< IDetectorGeometrySvc > m_detGeoSvc
Definition: G4AtlasRunManager.h:93
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:80
G4AtlasRunManager::m_fastSimTool
ToolHandle< IFastSimulationMasterTool > m_fastSimTool
Definition: G4AtlasRunManager.h:90
G4AtlasRunManager::InitializeGeometry
void InitializeGeometry() override final
Definition: G4AtlasRunManager.cxx:51
description
std::string description
glabal timer - how long have I taken so far?
Definition: hcg.cxx:88
G4AtlasRunManager::EndEvent
void EndEvent()
Definition: G4AtlasRunManager.cxx:74