actions
Loading...
Searching...
No Matches
gRunAction.cc
Go to the documentation of this file.
1// gemc
2#include "gRunAction.h"
3#include "gRun.h"
5
6// geant4
7#include "G4Threading.hh"
8#include "G4MTRunManager.hh"
9
10
11// Constructor for workers: stores shared services and logs thread identity.
12GRunAction::GRunAction(std::shared_ptr<GOptions> gopt, std::shared_ptr<gdynamicdigitization::dRoutinesMap> digi_map) :
14 goptions(gopt),
15 digitization_routines_map(digi_map) {
16 auto desc= std::to_string(G4Threading::G4GetThreadId());
17
19}
20
21
22// Executed after BeamOn(): create the thread-local run container.
23G4Run* GRunAction::GenerateRun() {
24 log->debug(NORMAL, FUNCTION_NAME);
25
26 return new GRun(goptions, digitization_routines_map);
27}
28
29// Invoked at the beginning of BeamOn (before physics tables are computed).
30void GRunAction::BeginOfRunAction(const G4Run* aRun) {
31
32 int thread_id = G4Threading::G4GetThreadId();
33 int run = aRun->GetRunID();
34 int neventsThisRun = aRun->GetNumberOfEventToBeProcessed();
35
36 // Lazily define the per-thread streamer map for worker threads only.
37 if (!IsMaster() && gstreamer_map == nullptr) {
38 log->info(0, " Defining gstreamers for thread id ", thread_id);
39 gstreamer_map = gstreamer::gstreamersMapPtr(goptions, thread_id);
40 }
41
42 // (Re)-open streamer connections for this run on worker threads.
43 // (Re)-open streamer connections for this run on worker threads.
44 if (!IsMaster()) {
45 if (gstreamer_map == nullptr) {
46 log->error(1, FUNCTION_NAME, " gstreamer_map is null in thread ", thread_id, " - cannot open connections.");
47 } else {
48 for (auto& [name, gstreamer] : *gstreamer_map) {
49 if (!gstreamer->openConnection()) {
50 log->error(ERR_STREAMERMAP_NOT_EXISTING, "Failed to open connection for GStreamer ", name, " in thread ", thread_id);
51 }
52 }
53 }
54 }
55
56
57 std::string what_am_i = IsMaster() ? "Master" : "Worker";
58
59 log->info(2, FUNCTION_NAME, " ", what_am_i, " [", thread_id, "], for run ", run, ", events to be processed: ", neventsThisRun);
60}
61
62// Invoked at the very end of the run processing: close worker-thread streamer connections.
63void GRunAction::EndOfRunAction(const G4Run* aRun) {
64
65 // const GRun* theRun = static_cast<const GRun*>(aRun);
66 int thread_id = G4Threading::G4GetThreadId();
67 int run = aRun->GetRunID();
68 std::string what_am_i = IsMaster() ? "Master" : "Worker";
69
70 if (!IsMaster()) {
71 if (gstreamer_map == nullptr) {
72 log->error(ERR_STREAMERMAP_NOT_EXISTING, FUNCTION_NAME, " gstreamer_map is null in thread ", thread_id, " - cannot close connections.");
73 } else {
74 for (const auto& [name, gstreamer] : *gstreamer_map) {
75 log->info(2, FUNCTION_NAME, " ", what_am_i, " [", thread_id, "], for run ", run,
76 " closing connection for gstreamer ", name);
77 if (!gstreamer->closeConnection()) {
78 log->error(1, "Failed to close connection for GStreamer ", name, " in thread ", thread_id);
79 }
80 }
81 }
82 }
83
84}
85
86// (Legacy/experimental streaming logic remains commented out below.)
87
88
89// TODO: 2 more is too much we need some calculation here
90// int nFramesToCreate = neventsThisRun * eventDuration / frameDuration + 2;
91
92// if (stream) {
93// if (frameStreamVerbosity >= GVERBOSITY_SUMMARY) {
94// cout << SROLOGHEADER << " current nframes in the buffer: " << frameRunData.size() << ", new frames to create: " << nFramesToCreate;
95// cout << ", last frame id created: " << lastFrameCreated << endl;
96// }
97//
98// for (int f = lastFrameCreated; f < lastFrameCreated + nFramesToCreate; f++) {
99// GFrameDataCollectionHeader* gframeHeader = new GFrameDataCollectionHeader(f + 1, frameDuration, verbosity);
100// GFrameDataCollection* frameData = new GFrameDataCollection(gframeHeader, verbosity);
101// frameRunData.push_back(frameData);
102// }
103//
104// lastFrameCreated += nFramesToCreate;
105// if (frameStreamVerbosity >= GVERBOSITY_SUMMARY) {
106// cout << SROLOGHEADER << nFramesToCreate << " new frames, buffer size is now " << frameRunData.size();
107// cout << ", last frame id created: " << lastFrameCreated << endl;
108// }
109// }
110
111
112// looping over run data and filling frameRunData
113// need to remember last event number here
114// if (stream) {
115// for (auto eventDataCollection : theRun->getRunData()) {
116// int absoluteEventNumber = eventIndex + eventDataCollection->getEventNumber();
117//
118// // filling frameRunData with this eventDataCollection
119// for (auto [detectorName, gdataCollection] : *eventDataCollection->getDataCollectionMap()) {
120// for (auto hitDigitizedData : *gdataCollection->getDigitizedData()) {
121// int timeAtelectronic = hitDigitizedData->getTimeAtElectronics();
122// if (timeAtelectronic != TIMEATELECTRONICSNOTDEFINED) {
123// int frameIndex = eventFrameIndex(absoluteEventNumber, timeAtelectronic);
124// frameRunData[frameIndex]->addIntegralPayload(formPayload(hitDigitizedData), verbosity);
125// }
126// }
127// }
128// }
129// }
130
131
132// now flushing all frames past eventIndex
133
134// if (stream) {
135// // updating eventIndex
136// eventIndex += neventsThisRun;
137//
138// for (auto [factoryName, streamerFactory] : *gstreamerFactoryMap) {
139// if (streamerFactory->getStreamType() == "stream" && frameRunData.size() > 0) {
140// // need to look for additional frame to flush
141// int nFramesToFlush = nFramesToCreate - 2;
142//
143// if (frameStreamVerbosity >= GVERBOSITY_SUMMARY) { cout << SROLOGHEADER << "number of frames to flush: " << nFramesToFlush << endl; }
144// for (auto fid = 0; fid < nFramesToFlush; fid++) {
145// logSummary("Streaming frame id <" + to_string(frameRunData.front()->getFrameID()) + " using streamer factory >" + factoryName + "<");
146// streamerFactory->publishFrameRunData(goptions, frameRunData.front());
147// delete frameRunData.front();
148// frameRunData.erase(frameRunData.begin());
149// }
150// }
151// }
152// }
153
154
155// determine the frame ID based on event number, eventDuration, frameDuration and number of threads
156// add frameData to frameRunData if it's not present
157// int GRunAction::eventFrameIndex(int eventNumber, double timeAtElectronics) {
158// int absoluteHitTime = eventNumber * eventDuration + timeAtElectronics;
159// int frameID = absoluteHitTime / frameDuration + 1;
160// int frameIndex = -1;
161//
162// // cout << "eventNumber: " << eventNumber << ", absoluteHitTime: " << absoluteHitTime << ", frameID: " << frameID << endl;
163//
164// for (size_t f = 0; f < frameRunData.size(); f++) { if (frameRunData[f]->getFrameID() == frameID) { frameIndex = (int)f; } }
165// // cout << "eventNumber: " << eventNumber << ", absoluteHitTime: " << absoluteHitTime << ", frameIndex: " << frameIndex << endl;
166//
167// return frameIndex;
168// }
169
170// vector<int> GRunAction::formPayload(GDigitizedData* digitizedData) {
171// vector<int> payload;
172//
173// int crate = digitizedData->getIntObservable(CRATESTRINGID);
174// int slot = digitizedData->getIntObservable(SLOTSTRINGID);
175// int channel = digitizedData->getIntObservable(CHANNELSTRINGID);
176// int q = digitizedData->getIntObservable(CHARGEATELECTRONICS);
177// int time = digitizedData->getIntObservable(TIMEATELECTRONICS);
178//
179// payload.push_back(crate);
180// payload.push_back(slot);
181// payload.push_back(channel);
182// payload.push_back(q);
183// payload.push_back(time);
184//
185// return payload;
186// }
187//
188// bool GRunAction::findFrameID(int fid) {
189// for (auto frame : frameRunData) { if (frame->getFrameID() == fid) { return true; } }
190// return false;
191// }
std::shared_ptr< GLogger > log
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
GRunAction(std::shared_ptr< GOptions > gopts, std::shared_ptr< gdynamicdigitization::dRoutinesMap > digi_map)
Constructs the run action.
Definition gRunAction.cc:12
Thread-local run object created for each Geant4 run.
Definition gRun.h:53
Declares GRunAction, responsible for run lifecycle hooks and run object creation.
constexpr const char * GRUNACTION_LOGGER
Definition gRunAction.h:20
Declares GRun, the per-thread run container.
#define ERR_STREAMERMAP_NOT_EXISTING
#define FUNCTION_NAME
CONSTRUCTOR
std::shared_ptr< const gstreamersMap > gstreamersMapPtr(const std::shared_ptr< GOptions > &gopts, int thread_id)