ATLAS Offline Software
G4AtlasMTRunManager.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Hide multi-threading classes from builds without G4MT
6 #include "G4Types.hh"
7 #ifdef G4MULTITHREADED
8 
10 
11 #include "G4StateManager.hh"
12 #include "G4GeometryManager.hh"
13 #include "G4UserRunAction.hh"
14 #include "G4Run.hh"
15 #include "G4LogicalVolumeStore.hh"
16 #include "G4MTRunManagerKernel.hh"
17 
18 #include "G4IonTable.hh"
19 #include "G4ParticleTable.hh"
20 #include "G4CascadeInterface.hh"
21 
22 #include "GaudiKernel/ISvcLocator.h"
23 #include "GaudiKernel/Bootstrap.h"
24 
25 G4AtlasMTRunManager::G4AtlasMTRunManager()
26  : G4MTRunManager()
27  , AthMessaging("G4AtlasMTRunManager")
28  , m_detGeoSvc("DetectorGeometrySvc", "G4AtlasMTRunManager")
29  , m_physListSvc("PhysicsListSvc", "G4AtlasMTRunManager")
30  , m_fastSimTool("FastSimulationMasterTool")
31 {}
32 
33 
34 G4AtlasMTRunManager* G4AtlasMTRunManager::GetG4AtlasMTRunManager ATLAS_NOT_THREAD_SAFE ()
35 {
36  static G4AtlasMTRunManager* thisManager = nullptr;
37  if (!thisManager) { thisManager = new G4AtlasMTRunManager; }
38  return thisManager;
39 }
40 
41 
43 {
44  ATH_MSG_INFO("Initialize");
45  // Set up geometry and physics in base class.
47  // Construct scoring worlds
48  ConstructScoringWorlds();
49  // Run initialization in G4RunManager.
50  // Normally done in BeamOn.
51  if (m_quietMode) { SetVerboseLevel(0); } // HACK
52  RunInitialization();
53  // Setup physics decay channels.
54  // Normally done in InitializeEventLoop, from DoEventLoop, from BeamOn.
55  GetMTMasterRunManagerKernel()->SetUpDecayChannels();
56  // Setup UI commands
57  PrepareCommandsStack();
58 }
59 
60 
61 void G4AtlasMTRunManager::InitializeGeometry()
62 {
63  ATH_MSG_INFO("InitializeGeometry");
64 
65  // Retrieve detector geo service
66  if (m_detGeoSvc.retrieve().isFailure()) {
67  ATH_MSG_ERROR("Could not retrieve the DetectorGeometrySvc");
68  G4ExceptionDescription description;
69  description << "InitializeGeometry: Failed to retrieve IDetectorGeometrySvc.";
70  G4Exception("G4AtlasMTRunManager", "CouldNotRetrieveDetGeoSvc",
71  FatalException, description);
72  abort(); // to keep Coverity happy
73  }
74 
75  // Create/assign detector construction
76  SetUserInitialization( m_detGeoSvc->GetDetectorConstruction() );
77  if (userDetector) {
78  G4RunManager::InitializeGeometry();
79  }
80  else {
81  // Shouldn't we abort here?
82  ATH_MSG_WARNING("User Detector not set!!! Geometry NOT initialized!!!");
83  }
84 }
85 
86 void G4AtlasMTRunManager::InitializePhysics()
87 {
88  ATH_MSG_INFO("InitializePhysics");
89  kernel->InitializePhysics();
90 
93  physicsInitialized = true;
94 
95  // Grab the physics list tool and set the extra options
96  if (m_physListSvc.retrieve().isFailure()) {
97  ATH_MSG_ERROR("Could not retrieve the physics list tool");
98  G4ExceptionDescription description;
99  description << "InitializePhysics: Failed to retrieve IPhysicsListSvc.";
100  G4Exception("G4AtlasMTRunManager", "CouldNotRetrievePLTool",
101  FatalException, description);
102  abort(); // to keep Coverity happy
103  }
104  m_physListSvc->SetPhysicsOptions();
105 
106  // Setup the fast simulations
107  const std::string methodName = "G4AtlasMTRunManager::InitializePhysics";
108  if(m_fastSimTool.retrieve().isFailure()) {
109  throw GaudiException("Could not retrieve FastSims master tool",
110  methodName, StatusCode::FAILURE);
111  }
112  if(m_fastSimTool->initializeFastSims().isFailure()) {
113  throw GaudiException("Failed to initialize FastSims for master thread",
114  methodName, StatusCode::FAILURE);
115  }
116 
117  // TODO: parallel worlds stuff here
118 }
119 
120 
121 // I suspect a lot of this could just be delegated to the base class.
122 // I wonder if something there breaks in Athena..
123 void G4AtlasMTRunManager::RunTermination()
124 {
125  // vanilla G4 calls a different method... why?
126  CleanUpPreviousEvents();
127  previousEvents->clear();
128 
129  if(userRunAction) { userRunAction->EndOfRunAction(currentRun); }
130 
131  delete currentRun;
132  currentRun = nullptr;
133  runIDCounter++;
134 
135  ATH_MSG_INFO( "Changing the state..." );
136  G4StateManager* stateManager = G4StateManager::GetStateManager();
137  stateManager->SetNewState(G4State_Idle);
138 
139  ATH_MSG_INFO( "Opening the geometry back up" );
140  G4GeometryManager::GetInstance()->OpenGeometry();
141 
142  ATH_MSG_INFO( "Terminating the run... State is " <<
143  stateManager->GetStateString( stateManager->GetCurrentState() ) );
144  kernel->RunTermination();
145  ATH_MSG_INFO( "All done..." );
146 
147  userRunAction = nullptr;
148  userEventAction = nullptr;
149  userSteppingAction = nullptr;
150  userStackingAction = nullptr;
151  userTrackingAction = nullptr;
152  // physicsList = nullptr;
153  userDetector = nullptr;
154  userPrimaryGeneratorAction = nullptr;
155 }
156 
157 #endif // G4MULTITHREADED
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
G4AtlasMTRunManager.h
ATLAS_NOT_THREAD_SAFE
#define ATLAS_NOT_THREAD_SAFE
getNoisyStrip() Find noisy strips from hitmaps and write out into xml/db formats
Definition: checker_macros.h:212
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
jobOptions.Initialize
Initialize
Definition: jobOptions.pA.py:28
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
description
std::string description
glabal timer - how long have I taken so far?
Definition: hcg.cxx:88