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 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
127 std::make_unique<G4AtlasActionInitialization>(&*m_userActionSvc);
128 m_pRunMgr->SetUserInitialization(actionInitialization.release());
130 G4UImanager *ui = G4UImanager::GetUIpointer();
132 if (!m_libList.empty()) {
133 ATH_MSG_INFO(
"G4AtlasAlg specific libraries requested ") ;
134 std::string temp=
"/load "+m_libList;
135 ui->ApplyCommand(temp);
138 if (!m_physList.empty()) {
139 ATH_MSG_INFO(
"requesting a specific physics list "<< m_physList) ;
140 std::string temp=
"/Physics/GetPhysicsList "+m_physList;
141 ui->ApplyCommand(temp);
144 if (!m_fieldMap.empty()) {
145 ATH_MSG_INFO(
"requesting a specific field map "<< m_fieldMap) ;
146 ATH_MSG_INFO(
"the field is initialized straight away") ;
147 std::string temp=
"/MagneticField/Select "+m_fieldMap;
148 ui->ApplyCommand(temp);
149 ui->ApplyCommand(
"/MagneticField/Initialize");
153 ATH_MSG_DEBUG(
"G4 Command: Trying at the end of initializeOnce()");
154 for (
const auto& g4command : m_g4commands) {
155 int returnCode = ui->ApplyCommand( g4command );
160 auto* rm = G4RunManager::GetRunManager();
162 throw std::runtime_error(
"Run manager retrieval has failed");
166 if(!m_useMT && rm->ConfirmBeamOnCondition()) {
167 rm->RunInitialization();
170 ATH_MSG_INFO(
"retireving the Detector Geometry Service" );
171 if(m_detGeoSvc.retrieve().isFailure()) {
172 throw std::runtime_error(
"Could not initialize ATLAS DetectorGeometrySvc!");
175 if(m_userLimitsSvc.retrieve().isFailure()) {
176 throw std::runtime_error(
"Could not initialize ATLAS UserLimitsSvc!");
179 if (m_activateParallelGeometries) {
180 G4VModularPhysicsList* thePhysicsList=
dynamic_cast<G4VModularPhysicsList*
>(m_physListSvc->GetPhysicsList());
181 if (!thePhysicsList) {
182 throw std::runtime_error(
"Failed dynamic_cast!! this is not a G4VModularPhysicsList!");
184 #if G4VERSION_NUMBER >= 1010
185 std::vector<std::string>& parallelWorldNames=m_detGeoSvc->GetParallelWorldNames();
186 for (
auto&
it: parallelWorldNames) {
187 thePhysicsList->RegisterPhysics(
new G4ParallelWorldPhysics(
it,
true));
198 ATH_MSG_VERBOSE(
"++++++++++++ ISF G4 G4LegacyTransportTool finalized ++++++++++++");
205 ATH_MSG_ERROR(
"Failure in iGeant4::G4LegacyTransportTool::finalizeOnce: " <<
e.what());
206 return StatusCode::FAILURE;
211 float runTime=m_runTimer->GetUserElapsed()+m_runTimer->GetSystemElapsed();
212 float avgTimePerEvent=(m_nrOfEntries>1) ? m_accumulatedEventTime/(m_nrOfEntries-1.) : runTime;
213 float sigma=( m_nrOfEntries>2) ? std::sqrt((m_accumulatedEventTimeSq/
float(m_nrOfEntries-1)-
214 avgTimePerEvent*avgTimePerEvent)/
float(m_nrOfEntries-2)) : 0;
217 " End of run - time spent is "<<std::setprecision(4) <<
219 " Average time per event was "<<std::setprecision(4) <<
220 avgTimePerEvent <<
" +- "<< std::setprecision(4) <<
sigma<<
endmsg<<
222 "*****************************************");
225 return StatusCode::SUCCESS;
233 m_pRunMgr->RunTermination();
248 StatusCode success = this->simulateVector(ispVector, secondaries, mcEventCollection);
260 G4Event* inputEvent = m_inputConverter->ISF_to_G4Event(
particles, genEvent(mcEventCollection));
263 return StatusCode::FAILURE;
267 bool abort = m_pRunMgr->ProcessEvent(inputEvent);
274 return StatusCode::FAILURE;
289 Slot& slot = *m_slots;
301 ATH_MSG_VERBOSE(
"Found secondaries: " << searchResult->second.size() );
302 secondaries.splice(
end(secondaries), std::move(searchResult->second) );
307 return StatusCode::SUCCESS;
318 rngWrapper->
setSeed( m_randomStreamName, ctx );
319 G4Random::setTheEngine(rngWrapper->
getEngine(ctx));
321 ATH_CHECK(m_senDetTool->BeginOfAthenaEvent());
324 if (m_doTiming) m_eventTimer->Start();
327 G4SDManager::GetSDMpointer()->PrepareNewEvent();
329 return StatusCode::SUCCESS;
355 m_eventTimer->Stop();
357 double eventTime=m_eventTimer->GetUserElapsed()+m_eventTimer->GetSystemElapsed();
358 if (m_nrOfEntries>1) {
359 m_accumulatedEventTime +=eventTime;
360 m_accumulatedEventTimeSq+=eventTime*eventTime;
363 float avgTimePerEvent=(m_nrOfEntries>1) ? m_accumulatedEventTime/(m_nrOfEntries-1.) : eventTime;
364 float sigma=(m_nrOfEntries>2) ? std::sqrt((m_accumulatedEventTimeSq/
float(m_nrOfEntries-1)-
365 avgTimePerEvent*avgTimePerEvent)/
float(m_nrOfEntries-2)) : 0.;
367 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) <<
368 eventTime <<
" s. New average " << std::setprecision(4) <<
369 avgTimePerEvent<<
" +- "<<std::setprecision(4) <<
sigma);
372 ATH_CHECK(m_senDetTool->EndOfAthenaEvent());
373 ATH_CHECK(m_fastSimTool->EndOfAthenaEvent());
375 return StatusCode::SUCCESS;
382 if(!mcEventCollection) {
384 if (evtStore()->contains<McEventCollection>(m_mcEventCollectionName)) {
385 if (evtStore()->
retrieve( mcEventCollection, m_mcEventCollectionName).isFailure()) {
386 ATH_MSG_ERROR(
"Unable to retrieve McEventCollection with name=" << m_mcEventCollectionName
391 ATH_MSG_WARNING(
"Fallback. Sucessfully retrieved McEventCollection with name=" << m_mcEventCollectionName);
394 else {
return nullptr; }
397 return mcEventCollection->
back();
404 case 0: {
ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Command Succeeded"); }
break;
405 case 100: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Command Not Found!"); }
break;
407 auto* stateManager = G4StateManager::GetStateManager();
408 ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Illegal Application State (" <<
409 stateManager->GetStateString(stateManager->GetCurrentState()) <<
")!");
411 case 300: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Range!"); }
break;
412 case 400: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Unreadable!"); }
break;
413 case 500: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Candidates!"); }
break;
414 case 600: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Alias Not Found!"); }
break;
415 default: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Unknown Status!"); }
break;