29 #include "G4LorentzVector.hh"
30 #include "G4PrimaryVertex.hh"
31 #include "G4PrimaryParticle.hh"
32 #include "G4Trajectory.hh"
33 #include "G4Geantino.hh"
34 #include "G4ChargedGeantino.hh"
35 #include "G4ParticleTable.hh"
36 #include "G4StateManager.hh"
37 #include "G4TransportationManager.hh"
38 #include "G4UImanager.hh"
39 #include "G4ScoringManager.hh"
41 #include "G4SDManager.hh"
42 #include "G4VUserPhysicsList.hh"
43 #include "G4VModularPhysicsList.hh"
44 #include "G4ParallelWorldPhysics.hh"
51 static std::once_flag initializeOnceFlag;
52 static std::once_flag finalizeOnceFlag;
56 const std::string&
name,
73 m_runTimer =
new G4Timer();
74 m_eventTimer =
new G4Timer();
79 if (m_recordFlux) G4ScoringManager::GetScoringManager();
83 std::call_once(initializeOnceFlag, &iGeant4::G4LegacyTransportTool::initializeOnce,
this);
86 ATH_MSG_ERROR(
"Failure in iGeant4::G4LegacyTransportTool::initializeOnce: " <<
e.what());
87 return StatusCode::FAILURE;
98 return StatusCode::SUCCESS;
107 if (m_g4RunManagerHelper.retrieve().isFailure()) {
108 throw std::runtime_error(
"Could not initialize G4RunManagerHelper!");
111 m_pRunMgr = m_g4RunManagerHelper ? m_g4RunManagerHelper->g4RunManager() :
nullptr;
113 throw std::runtime_error(
"G4RunManagerHelper::g4RunManager() returned nullptr.");
116 if(m_physListSvc.retrieve().isFailure()) {
117 throw std::runtime_error(
"Could not initialize ATLAS PhysicsListSvc!");
119 m_physListSvc->SetPhysicsList();
121 m_pRunMgr->SetRecordFlux( m_recordFlux, std::make_unique<ISFFluxRecorder>() );
122 m_pRunMgr->SetLogLevel(
int(
msg().
level()) );
123 m_pRunMgr->SetDetGeoSvc( m_detGeoSvc.typeAndName() );
124 m_pRunMgr->SetFastSimMasterTool(m_fastSimTool.typeAndName() );
125 m_pRunMgr->SetPhysListSvc(m_physListSvc.typeAndName() );
126 m_pRunMgr->SetQuietMode( m_quietMode );
127 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
128 std::make_unique<G4AtlasActionInitialization>(&*m_userActionSvc);
129 m_pRunMgr->SetUserInitialization(actionInitialization.release());
131 G4UImanager *ui = G4UImanager::GetUIpointer();
133 if (!m_libList.empty()) {
134 ATH_MSG_INFO(
"G4AtlasAlg specific libraries requested ") ;
135 std::string temp=
"/load "+m_libList;
136 ui->ApplyCommand(temp);
139 if (!m_physList.empty()) {
140 ATH_MSG_INFO(
"requesting a specific physics list "<< m_physList) ;
141 std::string temp=
"/Physics/GetPhysicsList "+m_physList;
142 ui->ApplyCommand(temp);
145 if (!m_fieldMap.empty()) {
146 ATH_MSG_INFO(
"requesting a specific field map "<< m_fieldMap) ;
147 ATH_MSG_INFO(
"the field is initialized straight away") ;
148 std::string temp=
"/MagneticField/Select "+m_fieldMap;
149 ui->ApplyCommand(temp);
150 ui->ApplyCommand(
"/MagneticField/Initialize");
154 ATH_MSG_DEBUG(
"G4 Command: Trying at the end of initializeOnce()");
155 for (
const auto& g4command : m_g4commands) {
156 int returnCode = ui->ApplyCommand( g4command );
157 commandLog(returnCode, g4command);
161 auto* rm = G4RunManager::GetRunManager();
163 throw std::runtime_error(
"Run manager retrieval has failed");
167 if(!m_useMT && rm->ConfirmBeamOnCondition()) {
168 rm->RunInitialization();
171 ATH_MSG_INFO(
"retireving the Detector Geometry Service" );
172 if(m_detGeoSvc.retrieve().isFailure()) {
173 throw std::runtime_error(
"Could not initialize ATLAS DetectorGeometrySvc!");
176 if(m_userLimitsSvc.retrieve().isFailure()) {
177 throw std::runtime_error(
"Could not initialize ATLAS UserLimitsSvc!");
180 if (m_activateParallelGeometries) {
181 G4VModularPhysicsList* thePhysicsList=
dynamic_cast<G4VModularPhysicsList*
>(m_physListSvc->GetPhysicsList());
182 if (!thePhysicsList) {
183 throw std::runtime_error(
"Failed dynamic_cast!! this is not a G4VModularPhysicsList!");
185 #if G4VERSION_NUMBER >= 1010
186 std::vector<std::string>& parallelWorldNames=m_detGeoSvc->GetParallelWorldNames();
187 for (
auto&
it: parallelWorldNames) {
188 thePhysicsList->RegisterPhysics(
new G4ParallelWorldPhysics(
it,
true));
199 ATH_MSG_VERBOSE(
"++++++++++++ ISF G4 G4LegacyTransportTool finalized ++++++++++++");
206 ATH_MSG_ERROR(
"Failure in iGeant4::G4LegacyTransportTool::finalizeOnce: " <<
e.what());
207 return StatusCode::FAILURE;
212 float runTime=m_runTimer->GetUserElapsed()+m_runTimer->GetSystemElapsed();
213 float avgTimePerEvent=(m_nrOfEntries>1) ? m_accumulatedEventTime/(m_nrOfEntries-1.) : runTime;
214 float sigma=( m_nrOfEntries>2) ? std::sqrt((m_accumulatedEventTimeSq/
float(m_nrOfEntries-1)-
215 avgTimePerEvent*avgTimePerEvent)/
float(m_nrOfEntries-2)) : 0;
218 " End of run - time spent is "<<std::setprecision(4) <<
220 " Average time per event was "<<std::setprecision(4) <<
221 avgTimePerEvent <<
" +- "<< std::setprecision(4) <<
sigma<<
endmsg<<
223 "*****************************************");
226 return StatusCode::SUCCESS;
234 m_pRunMgr->RunTermination();
249 StatusCode success = this->simulateVector(ctx, ispVector, secondaries, mcEventCollection);
261 G4Event* inputEvent = m_inputConverter->ISF_to_G4Event(ctx,
particles, genEvent(mcEventCollection));
264 return StatusCode::FAILURE;
268 bool abort = m_pRunMgr->ProcessEvent(inputEvent);
275 return StatusCode::FAILURE;
290 Slot& slot = *m_slots;
302 ATH_MSG_VERBOSE(
"Found secondaries: " << searchResult->second.size() );
303 secondaries.splice(
end(secondaries), std::move(searchResult->second) );
308 return StatusCode::SUCCESS;
319 rngWrapper->
setSeed( m_randomStreamName, ctx );
320 G4Random::setTheEngine(rngWrapper->
getEngine(ctx));
322 ATH_CHECK(m_senDetTool->BeginOfAthenaEvent());
325 if (m_doTiming) m_eventTimer->Start();
328 G4SDManager::GetSDMpointer()->PrepareNewEvent();
330 return StatusCode::SUCCESS;
356 m_eventTimer->Stop();
358 double eventTime=m_eventTimer->GetUserElapsed()+m_eventTimer->GetSystemElapsed();
359 if (m_nrOfEntries>1) {
360 m_accumulatedEventTime +=eventTime;
361 m_accumulatedEventTimeSq+=eventTime*eventTime;
364 float avgTimePerEvent=(m_nrOfEntries>1) ? m_accumulatedEventTime/(m_nrOfEntries-1.) : eventTime;
365 float sigma=(m_nrOfEntries>2) ? std::sqrt((m_accumulatedEventTimeSq/
float(m_nrOfEntries-1)-
366 avgTimePerEvent*avgTimePerEvent)/
float(m_nrOfEntries-2)) : 0.;
368 ATH_MSG_INFO(
"\t Run:Event "<<ctx.eventID().run_number()<<
":"<<ctx.eventID().event_number() <<
"\t ("<<m_nrOfEntries<<
"th event for this worker) took " << std::setprecision(4) <<
369 eventTime <<
" s. New average " << std::setprecision(4) <<
370 avgTimePerEvent<<
" +- "<<std::setprecision(4) <<
sigma);
373 ATH_CHECK(m_senDetTool->EndOfAthenaEvent());
374 ATH_CHECK(m_fastSimTool->EndOfAthenaEvent());
376 return StatusCode::SUCCESS;
383 if(!mcEventCollection) {
385 if (evtStore()->contains<McEventCollection>(m_mcEventCollectionName)) {
386 if (evtStore()->
retrieve( mcEventCollection, m_mcEventCollectionName).isFailure()) {
387 ATH_MSG_ERROR(
"Unable to retrieve McEventCollection with name=" << m_mcEventCollectionName
392 ATH_MSG_WARNING(
"Fallback. Sucessfully retrieved McEventCollection with name=" << m_mcEventCollectionName);
395 else {
return nullptr; }
398 return mcEventCollection->
back();
405 case 0: {
ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Command Succeeded"); }
break;
406 case 100: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Command Not Found!"); }
break;
408 auto* stateManager = G4StateManager::GetStateManager();
409 ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Illegal Application State (" <<
410 stateManager->GetStateString(stateManager->GetCurrentState()) <<
")!");
412 case 300: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Range!"); }
break;
413 case 400: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Unreadable!"); }
break;
414 case 500: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Candidates!"); }
break;
415 case 600: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Alias Not Found!"); }
break;
416 default: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Unknown Status!"); }
break;