11#include " gEventAction.h"
2+ #include " actions/gactionConventions.h"
23
34// geant4
45#include " G4EventManager.hh"
@@ -11,45 +12,66 @@ GEventAction::GEventAction(const std::shared_ptr<GOptions>& gopt, GRunAction* ru
1112 GBase(gopt, EVENTACTION_LOGGER),
1213 goptions(gopt),
1314 run_action(run_a) {
15+ // Constructor: store shared config and a non-owning pointer to the run action for this thread.
1416 auto desc = " GEventAction " + std::to_string (G4Threading::G4GetThreadId ());
1517 log->debug (CONSTRUCTOR, FUNCTION_NAME, desc);
1618}
1719
1820void GEventAction::BeginOfEventAction ([[maybe_unused]] const G4Event* event) {
21+ // Begin-of-event hook: logs event id and thread id for tracing.
1922 int thread_id = G4Threading::G4GetThreadId ();
2023 int eventID = event->GetEventID ();
2124
2225 log->debug (CONSTRUCTOR, FUNCTION_NAME, " event id " , eventID, " in thread " , thread_id);
2326}
2427
2528void GEventAction::EndOfEventAction ([[maybe_unused]] const G4Event* event) {
29+ // End-of-event hook: collect hit collections, digitize hits, and publish the event to streamers.
2630 G4HCofThisEvent* HCsThisEvent = event->GetHCofThisEvent ();
2731 if (!HCsThisEvent) return ;
32+ if (!run_action) {
33+ log->error (ERR_GRUNACTION_NOT_EXISTING, FUNCTION_NAME, " run_action is null - cannot access digitization routines or streamers." );
34+ }
2835
2936 int thread_id = G4Threading::G4GetThreadId ();
3037 int eventID = event->GetEventID ();
3138
39+ // Create the event data container that will receive digitized data and truth information.
3240 auto gevent_header = std::make_unique<GEventHeader>(goptions, eventID, thread_id);
3341 auto eventDataCollection = std::make_shared<GEventDataCollection>(goptions, std::move (gevent_header));
3442
35- // looping over all collections
43+ // Loop over all hit collections produced by sensitive detectors in this event.
3644 for (G4int hci = 0 ; hci < HCsThisEvent->GetNumberOfCollections (); hci++) {
3745 auto thisGHC = (GHitsCollection*)HCsThisEvent->GetHC (hci);
3846
3947 if (thisGHC) {
4048 std::string hcSDName = thisGHC->GetSDname ();
4149 auto digi_map = run_action->get_digitization_routines_map ();
50+ if (!digi_map) {
51+ log->error (ERR_GDIGIMAP_NOT_EXISTING, FUNCTION_NAME, " no digitization routines map available for collection " , hcSDName,
52+ " in thread " , thread_id);
53+ }
4254
4355 log->info (2 , FUNCTION_NAME, " worker " , thread_id,
4456 " for event number " , eventID,
4557 " for collection number " , hci + 1 ,
4658 " collection name: " , hcSDName);
4759
48- auto digitization_routine = digi_map->at (hcSDName);
60+ // Select the digitization routine by hit collection name, then publish through all streamers.
61+ auto it = digi_map->find (hcSDName);
62+ if (it == digi_map->end ()) {
63+ log->error (ERR_GDIGIMAP_NOT_EXISTING, FUNCTION_NAME, " no digitization routine registered for collection " , hcSDName,
64+ " in thread " , thread_id);
65+ }
66+ auto digitization_routine = it->second ;
67+
4968 auto gstreamers_map = run_action->get_streamer_map ();
69+ if (!gstreamers_map) {
70+ log->error (ERR_STREAMERMAP_NOT_EXISTING, FUNCTION_NAME, " no gstreamer map available in thread " , thread_id);
71+ }
5072
5173 if (digitization_routine != nullptr ) {
52- // looping over hits in this collection
74+ // Loop over hits in this collection and append produced data to the event container.
5375 for (size_t hitIndex = 0 ; hitIndex < thisGHC->GetSize (); hitIndex++) {
5476 auto thisHit = (GHit*)thisGHC->GetHit (hitIndex);
5577 // PRAGMA TODO: switch these on/off with options
@@ -60,7 +82,7 @@ void GEventAction::EndOfEventAction([[maybe_unused]] const G4Event* event) {
6082 }
6183
6284 for (const auto & [name, gstreamer] : *gstreamers_map) {
63- // publish the event to the gstreamer
85+ // Publish the event to the gstreamer.
6486 gstreamer->publishEventData (eventDataCollection);
6587 }
6688 }
0 commit comments