ATLAS Offline Software
Loading...
Searching...
No Matches
SyncPrimaryGeneratorAction.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "GaudiKernel/StatusCode.h"
7
9
10#include "G4RunManager.hh"
11#include "G4Event.hh"
12#include "G4SystemOfUnits.hh"
13#include "Randomize.hh"
14
15#include <exception>
16
17namespace G4UA
18{
19
21 : G4VUserPrimaryGeneratorAction(), m_g4RunTool(g4RunTool)
22{
23}
24
26{
27 // get an event from the shared queue
28 if(auto inputEventInfo = m_g4RunTool->GetEvent()) {
29 // setup rng engine seeded by Athena
30 G4Random::setTheEngine(inputEventInfo->HepRandomEngine());
31
32 // Transfer ownership before calling the event factory so that Athena can
33 // still be notified if event preparation fails.
34 auto* eventInfo = inputEventInfo.get();
35 anEvent->SetEventID(eventInfo->AthenaEventID());
36 anEvent->SetUserInformation(inputEventInfo.release());
37
38 bool eventPreparationFailed = false;
39 try {
40 // convert Genevent to G4Event
41 eventPreparationFailed =
42 eventInfo->EventFactory()(*anEvent).isFailure();
43 if(eventPreparationFailed) {
44 G4cerr << "Failed to prepare G4Event from Athena event" << G4endl;
45 }
46 }
47 catch(const std::exception& error) {
48 eventPreparationFailed = true;
49 G4cerr << "Exception while preparing G4Event from Athena event: "
50 << error.what() << G4endl;
51 }
52 catch(...) {
53 eventPreparationFailed = true;
54 G4cerr << "Unknown exception while preparing G4Event from Athena event"
55 << G4endl;
56 }
57
58 if(eventPreparationFailed) {
59 eventInfo->SetEventPreparationFailed();
60 // AbortCurrentEvent cannot be called until Geant4 enters EventProc.
61 // SyncEventAction will abort this event at BeginOfEventAction.
62 }
63 }
64 // If GetEvent returns nullptr, the event queue has been closed and the run
65 // should be aborted.
66 // Because GeneratePrimaries runs before Geant4 enters EventProc, AbortRun
67 // must be called in BeginOfEventAction.
68}
69
70} // namespace G4UA
virtual void GeneratePrimaries(G4Event *) override
Provides an interface to interact with the Geant4 run.
Definition IG4RunTool.h:22