10#include "G4EventManager.hh"
11#include "G4GDMLParser.hh"
12#include "G4TrackingManager.hh"
13#include "G4PhysicalVolumeStore.hh"
14#include "G4GeometryManager.hh"
41 if(!m_simplifiedGeoPath.empty()) {
43 if (geoFile.empty()) {
44 ATH_MSG_FATAL(
"Could not find simplified geometry file: " << m_simplifiedGeoPath);
45 return StatusCode::FAILURE;
48 parser.Read(geoFile,
false);
53 ATH_MSG_INFO(
"- Using ISF TruthRecordSvc : " << m_truthRecordSvc.typeAndName());
55 ATH_MSG_INFO(
"- Using ISF GeoIDSvc : " << m_geoIDSvc.typeAndName());
63 ATH_MSG_INFO(
"Waiting on G4RunTool to be ready for run");
67 m_g4RunTool->WaitBeginRun();
77 ATH_CHECK(m_inputTruthCollectionKey.initialize());
78 ATH_CHECK(m_outputTruthCollectionKey.initialize());
82 if (!m_truthPreselectionTool.empty()) {
83 ATH_CHECK(m_truthPreselectionTool.retrieve());
86 if (!m_qspatcher.empty()) {
91 return StatusCode::SUCCESS;
98 static std::atomic<unsigned int> n_Event=0;
103 if (n_Event<=10 || (n_Event%100) == 0) {
113 catch(
const std::exception& e) {
114 ATH_MSG_ERROR(
"Failure in G4RunAlg::releaseGeoModel: " << e.what());
115 return StatusCode::FAILURE;
119 const EventContext& ctx = Gaudi::Hive::currentContext();
126 if (!inputTruthCollection.
isValid()) {
127 ATH_MSG_FATAL(
"Unable to read input GenEvent collection " << inputTruthCollection.
name() <<
" in store " << inputTruthCollection.
store());
128 return StatusCode::FAILURE;
130 ATH_MSG_DEBUG(
"Found input GenEvent collection " << inputTruthCollection.
name() <<
" in store " << inputTruthCollection.
store());
133 std::unique_ptr<McEventCollection> shadowTruth{};
135 outputTruthCollection = std::make_unique<McEventCollection>();
137 shadowTruth = std::make_unique<McEventCollection>(*inputTruthCollection);
138 for (HepMC::GenEvent* currentGenEvent : *shadowTruth ) {
145 outputTruthCollection->push_back(outputEvent.release());
150 outputTruthCollection = std::make_unique<McEventCollection>(*inputTruthCollection);
152 shadowTruth = std::make_unique<McEventCollection>();
155 for (HepMC::GenEvent* currentGenEvent : *outputTruthCollection ) {
161 ATH_MSG_DEBUG(
"Recorded output GenEvent collection " << outputTruthCollection.
name() <<
" in store " << outputTruthCollection.
store());
163 const int largestGeneratedParticleBC = (outputTruthCollection->empty()) ?
HepMC::UNDEFINED_ID
165 const int largestGeneratedVertexBC = (outputTruthCollection->empty()) ?
HepMC::UNDEFINED_ID
170 auto prepare_event = [
this, &outputTruthCollection, &shadowTruth, largestGeneratedParticleBC, largestGeneratedVertexBC](G4Event& event, std::unique_ptr<AtlasG4SyncEventUserInfo> g4eventInfo) -> StatusCode {
173 event.SetEventID(g4eventInfo->AthenaEventID());
174 event.SetUserInformation(g4eventInfo.release());
176 *outputTruthCollection, event, *shadowTruth));
177 return StatusCode::SUCCESS;
180 auto eventInfo = std::make_unique<AtlasG4SyncEventUserInfo>(rngWrapper->
getEngine(ctx), std::move(prepare_event), ctx);
183 std::shared_ptr<HitCollectionMap> hitCollections = eventInfo->GetHitCollectionMap();
189 auto syncInterface = eventInfo->SyncInterface();
191 ATH_MSG_DEBUG(
"Pushing Athena event " << ctx.eventID().event_number() <<
" onto event buffer");
195 syncInterface->WaitStatusDone();
197 if (syncInterface->EventAborted()) {
202 setFilterPassed(
false);
208 "Failed to retrieve xAOD::EventInfo while trying to update the "
210 return StatusCode::FAILURE;
227 for (HepMC::GenEvent* currentGenEvent : *outputTruthCollection ) {
232 return StatusCode::SUCCESS;
239 SmartIF<IGeoModelSvc> geoModel{Gaudi::svcLocator()->service(
"GeoModelSvc")};
244 if (geoModel->clear().isFailure()) {
248 ATH_MSG_INFO(
" ----> GeoModelSvc::clear() succeeded " );
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ALWAYS(x)
#define ATH_MSG_WARNING(x)
static std::once_flag releaseGeoModelOnceFlag
StatusCode G4RunAlg::initialize ATLAS_NOT_THREAD_SAFE()
Install fatal handler with default options.
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
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.
Gaudi::Property< bool > m_releaseGeoModel
ToolHandle< ISF::IGenEventFilter > m_truthPreselectionTool
Tool for filtering out quasi-stable particle daughters.
ServiceHandle< Simulation::IZeroLifetimePatcher > m_qspatcher
Quasi-Stable Particle Simulation Patcher.
ServiceHandle< IAthRNGSvc > m_rndmGenSvc
Random number service.
PublicToolHandle< IG4RunTool > m_g4RunTool
G4Atlas Tool for thread management and data interface.
void releaseGeoModel()
Releases the GeoModel geometry from memory once it has been used to build the G4 geometry and is no-l...
Gaudi::Property< bool > m_useShadowEvent
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
PublicToolHandle< ISensitiveDetectorMasterTool > m_senDetTool
Sensitive Detector Master Tool.
ServiceHandle< G4UA::IUserActionSvc > m_userActionSvc
User Action Service.
ServiceHandle< ISF::ITruthSvc > m_truthRecordSvc
Central Truth Service.
virtual StatusCode execute() override
Simulate one Athena event.
Gaudi::Property< bool > m_killAbortedEvents
Gaudi::Property< std::string > m_randomStreamName
PublicToolHandle< IFastSimulationMasterTool > m_fastSimTool
Fast Simulation Master Tool.
SG::WriteHandleKey< McEventCollection > m_outputTruthCollectionKey
ServiceHandle< ISF::IInputConverter > m_inputConverter
Service to convert ISF_Particles into a G4Event.
SG::ReadHandleKey< McEventCollection > m_inputTruthCollectionKey
Gaudi::Property< bool > m_flagAbortedEvents
virtual bool isValid() override final
Can the handle be successfully dereferenced?
std::string store() const
Return the name of the store holding the object we are proxying.
const std::string & name() const
Return the StoreGate ID for the referenced object.
Singleton class for creating truth incidents.
void SetISFGeoIDSvc(ISF::IGeoIDSvc *geoIDSvc)
Define which ISF GeoIDSvc to use.
void SetISFTruthSvc(ISF::ITruthSvc *truthSvc)
Define which ISF TruthService to use.
@ Core
Core flags describing the event.
@ Error
The sub-detector issued an error.
int maxGeneratedVertexBarcode(const HepMC::GenEvent *genEvent)
Get the maximal absolute value of barcode of vertex present in the event. Returns a negative number.
constexpr int UNDEFINED_ID
int maxGeneratedParticleBarcode(const HepMC::GenEvent *genEvent)
Get the maximal value of barcode of particle present in the event.