10#include "G4EventManager.hh"
11#include "G4GDMLParser.hh"
12#include "G4TrackingManager.hh"
13#include "G4PhysicalVolumeStore.hh"
14#include "G4GeometryManager.hh"
40 if(!m_simplifiedGeoPath.empty()) {
42 if (geoFile.empty()) {
43 ATH_MSG_FATAL(
"Could not find simplified geometry file: " << m_simplifiedGeoPath);
44 return StatusCode::FAILURE;
47 parser.Read(geoFile,
false);
52 ATH_MSG_INFO(
"- Using ISF TruthRecordSvc : " << m_truthRecordSvc.typeAndName());
56 ATH_MSG_INFO(
"Waiting on G4RunTool to be ready for run");
70 ATH_CHECK(m_inputTruthCollectionKey.initialize());
71 ATH_CHECK(m_outputTruthCollectionKey.initialize());
75 if (!m_truthPreselectionTool.empty()) {
76 ATH_CHECK(m_truthPreselectionTool.retrieve());
79 if (!m_qspatcher.empty()) {
84 return StatusCode::SUCCESS;
91 static std::atomic<unsigned int> n_Event=0;
96 if (n_Event<=10 || (n_Event%100) == 0) {
106 catch(
const std::exception& e) {
107 ATH_MSG_ERROR(
"Failure in G4RunAlg::releaseGeoModel: " << e.what());
108 return StatusCode::FAILURE;
118 if (!inputTruthCollection.
isValid()) {
119 ATH_MSG_FATAL(
"Unable to read input GenEvent collection " << inputTruthCollection.
name() <<
" in store " << inputTruthCollection.
store());
120 return StatusCode::FAILURE;
122 ATH_MSG_DEBUG(
"Found input GenEvent collection " << inputTruthCollection.
name() <<
" in store " << inputTruthCollection.
store());
125 std::unique_ptr<McEventCollection> shadowTruth{};
127 outputTruthCollection = std::make_unique<McEventCollection>();
129 shadowTruth = std::make_unique<McEventCollection>(*inputTruthCollection);
137 outputTruthCollection->push_back(outputEvent.release());
142 outputTruthCollection = std::make_unique<McEventCollection>(*inputTruthCollection);
144 shadowTruth = std::make_unique<McEventCollection>();
153 ATH_MSG_DEBUG(
"Recorded output GenEvent collection " << outputTruthCollection.
name() <<
" in store " << outputTruthCollection.
store());
155 const int largestGeneratedParticleBC = (outputTruthCollection->empty()) ?
HepMC::UNDEFINED_ID
157 const int largestGeneratedVertexBC = (outputTruthCollection->empty()) ?
HepMC::UNDEFINED_ID
160 EventOutcome eventOutcome = EventOutcome::Success;
164 auto prepare_event = [
this, &outputTruthCollection, &shadowTruth, largestGeneratedParticleBC, largestGeneratedVertexBC](G4Event& event) -> StatusCode {
168 *outputTruthCollection, event, *shadowTruth));
169 return StatusCode::SUCCESS;
172 auto eventInfo = std::make_unique<AtlasG4SyncEventUserInfo>(rngWrapper->
getEngine(ctx), std::move(prepare_event), ctx);
175 std::shared_ptr<HitCollectionMap> hitCollections = eventInfo->GetHitCollectionMap();
181 auto syncInterface = eventInfo->SyncInterface();
183 ATH_MSG_DEBUG(
"Pushing Athena event " << ctx.eventID().event_number() <<
" onto event buffer");
187 syncInterface->WaitStatusDone();
188 eventOutcome = syncInterface->Outcome();
190 if (eventOutcome == EventOutcome::Aborted) {
201 "Failed to retrieve xAOD::EventInfo while trying to update the "
203 return StatusCode::FAILURE;
225 if (eventOutcome == EventOutcome::PreparationFailed) {
227 return StatusCode::FAILURE;
229 if (eventOutcome == EventOutcome::RunTerminated) {
230 ATH_MSG_ERROR(
"Geant4 terminated before completing this event");
231 return StatusCode::FAILURE;
234 return StatusCode::SUCCESS;
241 SmartIF<IGeoModelSvc> geoModel{Gaudi::svcLocator()->service(
"GeoModelSvc")};
246 if (geoModel->clear().isFailure()) {
250 ATH_MSG_INFO(
" ----> GeoModelSvc::clear() succeeded " );
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_ALWAYS(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_INFO(x,...)
#define ATH_MSG_FATAL(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.
virtual void setFilterPassed(bool state, const EventContext &ctx) const
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(const EventContext &ctx) 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.
@ 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.
HepMC3::GenEvent GenEvent