30#include "G4ChargedGeantino.hh"
32#include "G4Geantino.hh"
33#include "G4LorentzVector.hh"
34#include "G4ParallelWorldPhysics.hh"
35#include "G4ParticleTable.hh"
36#include "G4PrimaryParticle.hh"
37#include "G4PrimaryVertex.hh"
38#include "G4SDManager.hh"
39#include "G4ScoringManager.hh"
40#include "G4StateManager.hh"
42#include "G4Trajectory.hh"
43#include "G4TransportationManager.hh"
44#include "G4UImanager.hh"
45#include "G4VModularPhysicsList.hh"
46#include "G4VUserPhysicsList.hh"
56 const std::string& name,
57 const IInterface* parent )
58 :
ISF::BaseSimulatorG4Tool(
type, name, parent)
83 std::call_once(
initializeOnceFlag, &iGeant4::G4LegacyTransportTool::initializeOnce,
this);
85 catch(
const std::exception& e) {
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();
120 ATH_MSG_INFO(
"retireving the Detector Construction tool" );
121 if(m_detConstruction.retrieve().isFailure()) {
122 throw std::runtime_error(
"Could not initialize ATLAS DetectorConstruction!");
125 m_pRunMgr->SetRecordFlux( m_recordFlux, std::make_unique<ISFFluxRecorder>() );
126 m_pRunMgr->SetLogLevel(
int(
msg().level()) );
127 m_pRunMgr->SetDetConstructionTool( m_detConstruction.get() );
128 m_pRunMgr->SetPhysListSvc(m_physListSvc.typeAndName() );
129 m_pRunMgr->SetQuietMode( m_quietMode );
130 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
131 std::make_unique<G4AtlasActionInitialization>(&*m_userActionSvc);
132 m_pRunMgr->SetUserInitialization(actionInitialization.release());
134 G4UImanager *ui = G4UImanager::GetUIpointer();
136 if (!m_libList.empty()) {
137 ATH_MSG_INFO(
"G4AtlasAlg specific libraries requested ") ;
138 std::string temp=
"/load "+m_libList;
139 ui->ApplyCommand(temp);
142 if (!m_physList.empty()) {
143 ATH_MSG_INFO(
"requesting a specific physics list "<< m_physList) ;
144 std::string temp=
"/Physics/GetPhysicsList "+m_physList;
145 ui->ApplyCommand(temp);
148 if (!m_fieldMap.empty()) {
149 ATH_MSG_INFO(
"requesting a specific field map "<< m_fieldMap) ;
150 ATH_MSG_INFO(
"the field is initialized straight away") ;
151 std::string temp=
"/MagneticField/Select "+m_fieldMap;
152 ui->ApplyCommand(temp);
153 ui->ApplyCommand(
"/MagneticField/Initialize");
157 ATH_MSG_DEBUG(
"G4 Command: Trying at the end of initializeOnce()");
158 for (
const auto& g4command : m_g4commands) {
159 int returnCode = ui->ApplyCommand( g4command );
160 commandLog(returnCode, g4command);
164 auto* rm = G4RunManager::GetRunManager();
166 throw std::runtime_error(
"Run manager retrieval has failed");
170 if(!m_useMT && rm->ConfirmBeamOnCondition()) {
171 rm->RunInitialization();
174 ATH_MSG_INFO(
"Initializing " << m_physicsInitializationTools.size() <<
" physics initialization tools");
175 for(
auto& physicsTool : m_physicsInitializationTools) {
176 if (physicsTool->initializePhysics().isFailure()) {
177 throw std::runtime_error(
"Failed to initialize physics with tool " + physicsTool.name());
181 if(m_userLimitsSvc.retrieve().isFailure()) {
182 throw std::runtime_error(
"Could not initialize ATLAS UserLimitsSvc!");
185 if (m_activateParallelGeometries) {
186 G4VModularPhysicsList* thePhysicsList=
dynamic_cast<G4VModularPhysicsList*
>(m_physListSvc->GetPhysicsList());
187 if (!thePhysicsList) {
188 throw std::runtime_error(
"Failed dynamic_cast!! this is not a G4VModularPhysicsList!");
190#if G4VERSION_NUMBER >= 1010
191 std::vector<std::string>& parallelWorldNames=m_detConstruction->GetParallelWorldNames();
192 for (
auto& it: parallelWorldNames) {
193 thePhysicsList->RegisterPhysics(
new G4ParallelWorldPhysics(it,
true));
204 ATH_MSG_VERBOSE(
"++++++++++++ ISF G4 G4LegacyTransportTool finalized ++++++++++++");
210 catch(
const std::exception& e) {
211 ATH_MSG_ERROR(
"Failure in iGeant4::G4LegacyTransportTool::finalizeOnce: " << e.what());
212 return StatusCode::FAILURE;
220 avgTimePerEvent*avgTimePerEvent)/
float(
m_nrOfEntries-2)) : 0;
223 " End of run - time spent is "<<std::setprecision(4) <<
225 " Average time per event was "<<std::setprecision(4) <<
226 avgTimePerEvent <<
" +- "<< std::setprecision(4) << sigma<<
endmsg<<
228 "*****************************************");
231 return StatusCode::SUCCESS;
248 McEventCollection* mcEventCollection, std::shared_ptr<HitCollectionMap> hitCollections) {
257 StatusCode success = this->
simulateVector(ctx, ispVector, secondaries,
258 mcEventCollection, hitCollections);
269 McEventCollection* mcEventCollection, std::shared_ptr<HitCollectionMap> hitCollections,
272 ATH_MSG_DEBUG (name() <<
".simulateVector(...) : Received a vector of " << particles.size() <<
" particles for simulation.");
276 auto eventInfo = std::make_unique<AtlasG4EventUserInfo>(ctx);
277 eventInfo->SetHitCollectionMap(hitCollections);
279 auto inputEvent = std::make_unique<G4Event>(ctx.eventID().event_number());
280 inputEvent->SetUserInformation(eventInfo.release());
286 return m_pRunMgr->ProcessEvent(inputEvent.release());
294 return StatusCode::FAILURE;
309 Slot& slot = *m_slots;
312 for (
auto* cisp : particles) {
321 ATH_MSG_VERBOSE(
"Found secondaries: " << searchResult->second.size() );
322 secondaries.splice( end(secondaries), std::move(searchResult->second) );
327 return StatusCode::SUCCESS;
339 G4Random::setTheEngine(rngWrapper->
getEngine(ctx));
347 G4SDManager::GetSDMpointer()->PrepareNewEvent();
349 return StatusCode::SUCCESS;
385 avgTimePerEvent*avgTimePerEvent)/
float(
m_nrOfEntries-2)) : 0.;
387 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) <<
388 eventTime <<
" s. New average " << std::setprecision(4) <<
389 avgTimePerEvent<<
" +- "<<std::setprecision(4) << sigma);
396 return StatusCode::SUCCESS;
403 if(!mcEventCollection) {
415 else {
return nullptr; }
418 return mcEventCollection->
back();
425 case 0: {
ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Command Succeeded"); }
break;
426 case 100: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Command Not Found!"); }
break;
428 auto* stateManager = G4StateManager::GetStateManager();
429 ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Illegal Application State (" <<
430 stateManager->GetStateString(stateManager->GetCurrentState()) <<
")!");
432 case 300: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Range!"); }
break;
433 case 400: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Unreadable!"); }
break;
434 case 500: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Candidates!"); }
break;
435 case 600: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Alias Not Found!"); }
break;
436 default: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Unknown Status!"); }
break;
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
static std::once_flag initializeOnceFlag
static std::once_flag finalizeOnceFlag
Define macros for attributes used to control the static checker.
#define ATLAS_NOT_THREAD_SAFE
getNoisyStrip() Find noisy strips from hitmaps and write out into xml/db formats
A wrapper class for event-slot-local random engines.
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
const T * back() const
Access the last element in the collection as an rvalue.
Small wrapper around hit collection map to facilitate accessing the hit collection.
The generic ISF particle definition,.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
bool contains(const std::string &s, const std::string ®x)
does a string contain the substring
std::list< ISF::ISFParticle * > ISFParticleContainer
generic ISFParticle container (not necessarily a std::list!)
std::vector< ISF::ISFParticle * > ISFParticleVector
ISFParticle vector.