actions
Loading...
Searching...
No Matches
gEventAction.cc
Go to the documentation of this file.
1#include "gEventAction.h"
3
4// geant4
5#include "G4EventManager.hh"
6
7// gemc
9
10
11GEventAction::GEventAction(const std::shared_ptr<GOptions>& gopt, GRunAction* run_a) :
13 goptions(gopt),
14 run_action(run_a) {
15 // Constructor: store shared config and a non-owning pointer to the run action for this thread.
16 auto desc = "GEventAction " + std::to_string(G4Threading::G4GetThreadId());
18}
19
20void GEventAction::BeginOfEventAction([[maybe_unused]] const G4Event* event) {
21 // Begin-of-event hook: logs event id and thread id for tracing.
22 int thread_id = G4Threading::G4GetThreadId();
23 int eventID = event->GetEventID();
24
25 log->debug(CONSTRUCTOR, FUNCTION_NAME, " event id ", eventID, " in thread ", thread_id);
26}
27
28void GEventAction::EndOfEventAction([[maybe_unused]] const G4Event* event) {
29 // End-of-event hook: collect hit collections, digitize hits, and publish the event to streamers.
30 G4HCofThisEvent* HCsThisEvent = event->GetHCofThisEvent();
31 if (!HCsThisEvent) return;
32 if (!run_action) {
34 " run_action is null - cannot access digitization routines or streamers.");
35 }
36
37 int thread_id = G4Threading::G4GetThreadId();
38 int eventID = event->GetEventID();
39
40 // Create the event data container that will receive digitized data and truth information.
41 auto gevent_header = std::make_unique<GEventHeader>(goptions, eventID, thread_id);
42 auto eventDataCollection = std::make_shared<GEventDataCollection>(goptions, std::move(gevent_header));
43
44 // Loop over all hit collections produced by sensitive detectors in this event.
45 for (G4int hci = 0; hci < HCsThisEvent->GetNumberOfCollections(); hci++) {
46 auto thisGHC = (GHitsCollection*)HCsThisEvent->GetHC(hci);
47
48 if (thisGHC) {
49 std::string hcSDName = thisGHC->GetSDname();
50 auto digi_map = run_action->get_digitization_routines_map();
51 if (!digi_map) {
53 " no digitization routines map available for collection ", hcSDName,
54 " in thread ", thread_id);
55 }
56
57 log->info(2, FUNCTION_NAME, " worker ", thread_id,
58 " for event number ", eventID,
59 " for collection number ", hci + 1,
60 " collection name: ", hcSDName);
61
62 // Select the digitization routine by hit collection name, then publish through all streamers.
63 auto it = digi_map->find(hcSDName);
64 if (it == digi_map->end()) {
66 " no digitization routine registered for collection ", hcSDName,
67 " in thread ", thread_id);
68 }
69 auto digitization_routine = it->second;
70
71 auto gstreamers_map = run_action->get_streamer_map();
72 if (!gstreamers_map) {
73 log->error(ERR_STREAMERMAP_NOT_EXISTING, FUNCTION_NAME, " no gstreamer map available in thread ",
74 thread_id);
75 }
76
77 if (digitization_routine != nullptr) {
78 // Loop over hits in this collection and append produced data to the event container.
79 for (size_t hitIndex = 0; hitIndex < thisGHC->GetSize(); hitIndex++) {
80 auto thisHit = (GHit*)thisGHC->GetHit(hitIndex);
81 // PRAGMA TODO: switch these on/off with options
82 auto true_data = digitization_routine->collectTrueInformation(thisHit, hitIndex);
83 auto digi_data = digitization_routine->digitizeHit(thisHit, hitIndex);
84 eventDataCollection->addDetectorDigitizedData(hcSDName, std::move(digi_data));
85 eventDataCollection->addDetectorTrueInfoData(hcSDName, std::move(true_data));
86 }
87
88 for (const auto& [name, gstreamer] : *gstreamers_map) {
89 // Publish the event to the gstreamer.
90 gstreamer->publishEventData(eventDataCollection);
91 }
92 }
93 }
94 }
95}
std::shared_ptr< GLogger > log
void EndOfEventAction(const G4Event *event) override
Called by Geant4 at the end of an event.
void BeginOfEventAction(const G4Event *event) override
Called by Geant4 at the beginning of an event.
GEventAction(const std::shared_ptr< GOptions > &gopt, GRunAction *run_a)
Constructs the event action.
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
Handles run begin/end callbacks and creates the per-thread run object.
Definition gRunAction.h:54
auto get_streamer_map() const -> std::shared_ptr< const gstreamer::gstreamersMap >
Returns the per-thread streamer map, if it has been created.
Definition gRunAction.h:83
auto get_digitization_routines_map() const -> std::shared_ptr< gdynamicdigitization::dRoutinesMap >
Returns the shared digitization routines map.
Definition gRunAction.h:72
Declares GEventAction, responsible for per-event lifecycle hooks and event publication.
constexpr const char * EVENTACTION_LOGGER
#define ERR_GDIGIMAP_NOT_EXISTING
#define ERR_GRUNACTION_NOT_EXISTING
#define ERR_STREAMERMAP_NOT_EXISTING
G4THitsCollection< GHit > GHitsCollection
#define FUNCTION_NAME
CONSTRUCTOR