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  RunInitialization();
52  // Setup physics decay channels.
53  // Normally done in InitializeEventLoop, from DoEventLoop, from BeamOn.
54  GetMTMasterRunManagerKernel()->SetUpDecayChannels();
55  // Setup UI commands
56  PrepareCommandsStack();
57 }
58 
59 
60 void G4AtlasMTRunManager::InitializeGeometry()
61 {
62  ATH_MSG_INFO("InitializeGeometry");
63 
64  // Retrieve detector geo service
65  if (m_detGeoSvc.retrieve().isFailure()) {
66  ATH_MSG_ERROR("Could not retrieve the DetectorGeometrySvc");
67  G4ExceptionDescription description;
68  description << "InitializeGeometry: Failed to retrieve IDetectorGeometrySvc.";
69  G4Exception("G4AtlasMTRunManager", "CouldNotRetrieveDetGeoSvc",
70  FatalException, description);
71  abort(); // to keep Coverity happy
72  }
73 
74  // Create/assign detector construction
75  SetUserInitialization( m_detGeoSvc->GetDetectorConstruction() );
76  if (userDetector) {
77  G4RunManager::InitializeGeometry();
78  }
79  else {
80  // Shouldn't we abort here?
81  ATH_MSG_WARNING("User Detector not set!!! Geometry NOT initialized!!!");
82  }
83 
84  // Setup the sensitive detectors on master.
85  /*ISvcLocator* svcLocator = Gaudi::svcLocator(); // from Bootstrap
86  if (svcLocator->service("SensitiveDetectorSvc", m_senDetSvc).isFailure()){
87  ATH_MSG_ERROR ( "Could not retrieve the SD service" );
88  throw "CouldNotRetrieveSDService";
89  }*/
90 }
91 
92 void G4AtlasMTRunManager::InitializePhysics()
93 {
94  ATH_MSG_INFO("InitializePhysics");
95  kernel->InitializePhysics();
96 
99  physicsInitialized = true;
100 
101  // Grab the physics list tool and set the extra options
102  if (m_physListSvc.retrieve().isFailure()) {
103  ATH_MSG_ERROR("Could not retrieve the physics list tool");
104  G4ExceptionDescription description;
105  description << "InitializePhysics: Failed to retrieve IPhysicsListSvc.";
106  G4Exception("G4AtlasMTRunManager", "CouldNotRetrievePLTool",
107  FatalException, description);
108  abort(); // to keep Coverity happy
109  }
110  m_physListSvc->SetPhysicsOptions();
111 
112  // Setup the fast simulations
113  const std::string methodName = "G4AtlasMTRunManager::InitializePhysics";
114  if(m_fastSimTool.retrieve().isFailure()) {
115  throw GaudiException("Could not retrieve FastSims master tool",
116  methodName, StatusCode::FAILURE);
117  }
118  if(m_fastSimTool->initializeFastSims().isFailure()) {
119  throw GaudiException("Failed to initialize FastSims for master thread",
120  methodName, StatusCode::FAILURE);
121  }
122 
123  // TODO: parallel worlds stuff here
124 }
125 
126 
127 // I suspect a lot of this could just be delegated to the base class.
128 // I wonder if something there breaks in Athena..
129 void G4AtlasMTRunManager::RunTermination()
130 {
131  // vanilla G4 calls a different method... why?
132  CleanUpPreviousEvents();
133  previousEvents->clear();
134 
135  if(userRunAction) { userRunAction->EndOfRunAction(currentRun); }
136 
137  delete currentRun;
138  currentRun = nullptr;
139  runIDCounter++;
140 
141  ATH_MSG_INFO( "Changing the state..." );
142  G4StateManager* stateManager = G4StateManager::GetStateManager();
143  stateManager->SetNewState(G4State_Idle);
144 
145  ATH_MSG_INFO( "Opening the geometry back up" );
146  G4GeometryManager::GetInstance()->OpenGeometry();
147 
148  ATH_MSG_INFO( "Terminating the run... State is " <<
149  stateManager->GetStateString( stateManager->GetCurrentState() ) );
150  kernel->RunTermination();
151  ATH_MSG_INFO( "All done..." );
152 
153  userRunAction = nullptr;
154  userEventAction = nullptr;
155  userSteppingAction = nullptr;
156  userStackingAction = nullptr;
157  userTrackingAction = nullptr;
158  // physicsList = nullptr;
159  userDetector = nullptr;
160  userPrimaryGeneratorAction = nullptr;
161 }
162 
163 #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