ATLAS Offline Software
Loading...
Searching...
No Matches
G4AtlasDetectorConstructionTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Include files
6#include <type_traits>
7
8#include "G4GeometryManager.hh"
9#include "G4LogicalVolumeStore.hh"
10#include "G4PhysicalVolumeStore.hh"
11#include "G4Version.hh"
12
13// local
15
16//-----------------------------------------------------------------------------
17// Implementation file for class : G4AtlasDetectorConstructionTool
18//
19// 2014-10-03: Andrea Dell'Acqua
20//-----------------------------------------------------------------------------
21
22
23//=================================
24// Standard constructor, initializes variables
25//=================================
27 const std::string& nam,const IInterface* parent )
28 : base_class( type, nam , parent )
29{
30}
31
32//=================================
33// Athena method overrides
34//=================================
36{
37 ATH_MSG_DEBUG( "Initializing Geometry configuration tools " );
38 for (auto it: m_configurationTools)
39 {
40 ATH_CHECK( it.retrieve() );
41 ATH_CHECK( it->preGeometryConfigure() );
42 }
43
44 ATH_MSG_DEBUG( "Initializing World detectors in " << name() );
45 ATH_CHECK( m_detTool.retrieve() );
46
47 ATH_MSG_DEBUG( "Initializing sensitive detectors in " << name() );
48 ATH_CHECK( m_senDetTool.retrieve() );
49
50 ATH_MSG_DEBUG( "Initializing fastsim in " << name() );
51 ATH_CHECK( m_fastSimTool.retrieve() );
52
53 ATH_MSG_DEBUG( "Setting up G4 physics regions" );
54 for (auto& it: m_regionCreators)
55 {
56 ATH_CHECK( it.retrieve() );
57 }
58
60 {
61 ATH_MSG_DEBUG( "Setting up G4 parallel worlds" );
62 for (auto& it: m_parallelWorlds)
63 {
64 ATH_CHECK( it.retrieve() );
65 }
66 }
67
68 ATH_MSG_DEBUG( "Setting up field managers" );
69 ATH_CHECK( m_fieldManagers.retrieve() );
70
71 ATH_CHECK( m_G4CaloTransportTool.retrieve( DisableTool{ m_G4CaloTransportTool.empty() } ) );
72
73 return StatusCode::SUCCESS;
74}
75
80
82 -> UPDetectorConstruction {
83 static_assert(std::has_virtual_destructor_v<G4VUserDetectorConstruction>,
84 "G4VUserDetectorConstruction must have a virtual destructor");
85 return {
87 [](G4VUserDetectorConstruction* ptr) { delete ptr; }};
88}
89
90//=================================
91// G4VUserDetectorConstruction method overrides
92//=================================
95 // The master run manager deletes its detector construction after joining
96 // all workers and before deleting the Geant4 kernel. Clean the shared
97 // physical volumes in that safe teardown window, including when run-manager
98 // destruction is caused by an early return or exception.
99 G4GeometryManager::GetInstance()->OpenGeometry();
100 G4PhysicalVolumeStore::GetInstance()->Clean();
101}
102
103G4VPhysicalVolume*
105 ATH_MSG_DEBUG("Detectors " << m_detConstructionTool->m_detTool.name()
106 << " being set as World");
107 m_detConstructionTool->m_detTool->SetAsWorld();
108 m_detConstructionTool->m_detTool->Build();
109
110 ATH_MSG_DEBUG( "Setting up G4 physics regions" );
111 for (auto& it : m_detConstructionTool->m_regionCreators) {
112 it->Construct();
113 }
114
115 if (m_detConstructionTool->m_activateParallelWorlds) {
116 ATH_MSG_DEBUG( "Setting up G4 parallel worlds" );
117 for (auto& it : m_detConstructionTool->m_parallelWorlds) {
118 m_detConstructionTool->m_parallelWorldNames.push_back(it.name());
119 this->RegisterParallelWorld(it->GetParallelWorld());
120 }
121 }
122
123 ATH_MSG_DEBUG( "Running geometry post-configuration tools" );
124 for (auto it : m_detConstructionTool->m_configurationTools) {
125 StatusCode sc = it->postGeometryConfigure();
126 if (!sc.isSuccess())
127 {
128 ATH_MSG_FATAL( "Unable to run post-geometry configuration for " << it->name() );
129 }
130 }
131
132 // Build world volume and rebuild LV/PV stores if Geant4 is 11 or newer
133 // - Rebuild necessary because Athena may install LV/PV notifiers that change
134 // volume names, which invalidates store maps.
135 G4VPhysicalVolume* wv = m_detConstructionTool->m_detTool->GetWorldVolume();
136#if G4VERSION_NUMBER > 1079
137 G4LogicalVolumeStore::GetInstance()->SetMapValid(false);
138 G4LogicalVolumeStore::GetInstance()->UpdateMap();
139 G4PhysicalVolumeStore::GetInstance()->SetMapValid(false);
140 G4PhysicalVolumeStore::GetInstance()->UpdateMap();
141#endif
142
143 return wv;
144}
145
148 ATH_MSG_DEBUG( "Setting up sensitive detectors" );
149 if (m_detConstructionTool->m_senDetTool->initializeSDs().isFailure()) {
150 ATH_MSG_FATAL("Failed to initialize SDs for worker thread");
151 }
152
153 if(!m_detConstructionTool->m_fastSimTool->initializeFastSims().isSuccess()) {
154 ATH_MSG_FATAL("Failed to initialize Fast Simulation Tool for worker thread");
155 return;
156 }
157
158 ATH_MSG_DEBUG( "Setting up field managers" );
159 for (auto& fm : m_detConstructionTool->m_fieldManagers) {
160 StatusCode sc = fm->initializeField();
161 if (!sc.isSuccess())
162 {
163 ATH_MSG_FATAL( "Unable to initialise field with " << fm->name() );
164 return;
165 }
166 }
167
168 if (m_detConstructionTool->m_G4CaloTransportTool.isEnabled()) {
169 ATH_MSG_DEBUG("Setting up G4CaloTransportTool");
170 if (m_detConstructionTool->m_G4CaloTransportTool->initializePropagator()
171 .isFailure()) {
172 ATH_MSG_FATAL("Failed to initialize G4CaloTransportTool for worker thread.");
173 return;
174 }
175 }
176
177 return;
178}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_FATAL(x,...)
static Double_t sc
G4VUserDetectorConstruction object passed to the G4 run manager Thid shouldn't live longer than the p...
ToolHandleArray< IRegionCreator > m_regionCreators
G4AtlasDetectorConstructionTool(const std::string &type, const std::string &name, const IInterface *parent)
Standard constructor.
virtual UPDetectorConstruction GetDetectorConstruction() override final
implements the following IDetectorConstructionTool methods
virtual std::vector< std::string > & GetParallelWorldNames() override final
ToolHandleArray< IG4GeometryConfigurationTool > m_configurationTools
ToolHandleArray< IFieldManagerTool > m_fieldManagers
virtual StatusCode initialize() override final
Initialize method.
PublicToolHandle< IG4CaloTransportTool > m_G4CaloTransportTool
ToolHandleArray< IParallelWorldTool > m_parallelWorlds
PublicToolHandle< ISensitiveDetectorMasterTool > m_senDetTool
ToolHandle< IDetectorGeometryTool > m_detTool
PublicToolHandle< IFastSimulationMasterTool > m_fastSimTool