actions
Loading...
Searching...
No Matches
gRunAction.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <mutex>
5#include <vector>
6
7// geant4
8#include "G4UserRunAction.hh"
9
10// gemc
11#include <gemc/gbase/gbase.h>
12#include <gemc/gdynamicDigitization/gdynamicdigitization.h>
13#include <gemc/gstreamer/gstreamer.h>
14#include <gemc/gdata/run/gRunDataCollection.h>
16
17
25constexpr const char* GRUNACTION_LOGGER = "grunaction";
26
32namespace grunaction {
39} // namespace grunaction
40
41
66class GRunAction : public GBase<GRunAction>, public G4UserRunAction
67{
68public:
75 explicit GRunAction(std::shared_ptr<GOptions> gopts,
76 std::shared_ptr<gdynamicdigitization::dRoutinesMap> digi_map);
77
78 ~GRunAction() override = default;
79
80 // The run action manages thread-local and process-wide state and is therefore
81 // intentionally non-copyable and non-movable.
82 GRunAction(const GRunAction&) = delete;
83 GRunAction& operator=(const GRunAction&) = delete;
86
95 [[nodiscard]] auto get_digitization_routines_map() const
96 -> std::shared_ptr<gdynamicdigitization::dRoutinesMap> {
97 return digitization_routines_map;
98 }
99
109 [[nodiscard]] auto get_streamer_threads_map() const
110 -> std::shared_ptr<const gstreamer::gstreamersMap> {
111 if (!gstreamer_threads_map) {
112 log->error(ERR_STREAMERMAP_NOT_EXISTING, FUNCTION_NAME, " no gstreamer thread map available");
113 }
114 return gstreamer_threads_map;
115 }
116
117 [[nodiscard]] bool has_streamer_threads_map() const {
118 return gstreamer_threads_map != nullptr;
119 }
120
131 void collect_event_data_collections(const std::string& hcSDName,
132 std::unique_ptr<GDigitizedData> digi_data) {
133 if (run_data == nullptr) {
135 " run_data is null - cannot collect run-level payload for collection ", hcSDName);
136 }
137
139 hcSDName,
140 std::move(digi_data));
141 }
142
150 if (run_data == nullptr) {
152 " run_data is null - cannot increment processed events.");
153 }
154
155 auto& header = run_data->getHeader();
156 if (header == nullptr) {
158 " run_data header is null - cannot increment processed events.");
159 }
160
161 header->increment_events_processed();
162 }
163
171 if (run_data == nullptr) {
173 " run_data is null - cannot increment payload events.");
174 }
175
176 auto& header = run_data->getHeader();
177 if (header == nullptr) {
179 " run_data header is null - cannot increment payload events.");
180 }
181
182 header->increment_events_with_payload();
183 }
184
185private:
186 using CompletedRunData = std::vector<std::unique_ptr<GRunDataCollection>>;
187
194 void stash_worker_run_data();
195
204 [[nodiscard]] CompletedRunData take_completed_worker_run_data();
205
214 G4Run* GenerateRun() override;
215
235 void BeginOfRunAction(const G4Run* run) override;
236
251 void EndOfRunAction(const G4Run* run) override;
252
258 void publish_run_data(const std::shared_ptr<GRunDataCollection>& run_data) const;
259
263 std::shared_ptr<GOptions> goptions;
264
268 std::shared_ptr<gdynamicdigitization::dRoutinesMap> digitization_routines_map;
269
273 std::shared_ptr<const gstreamer::gstreamersMap> gstreamer_threads_map;
274
278 std::shared_ptr<const gstreamer::gstreamersMap> gstreamer_run_map;
279
286 std::unique_ptr<GRunDataCollection> run_data;
287
291 bool need_a_thread_streamer = false;
292
296 bool need_a_run_streamer = false;
297
301 static std::mutex completed_run_data_mutex;
302
309 static CompletedRunData completed_worker_run_data;
310
329 void normalize_run_data(const std::shared_ptr<GRunDataCollection>& run_data) const;
330
331 // coming from digitization routine
332 std::unordered_map<std::string, std::vector<std::string>> to_normalize;
333};
334
335
336// vector of frame data in the run (local thread, merged in GRun::Merge in the global thread)
337// std::vector<GFrameDataCollection*> frameRunData;
338
339//
340// double frameDuration = UNINITIALIZEDNUMBERQUANTITY; // frame length in nanoseconds
341// double eventDuration = UNINITIALIZEDNUMBERQUANTITY; // event duration in nanoseconds
342// int eventIndex = 0; // added to the absolute event number, increases with each run
343// int lastFrameCreated = 0; // keeping track of the last frame created
344//
345// // determine the frame ID based on event number, eventDuration, frameDuration
346// // TODO: this should be in gdata?
347// int eventFrameIndex(int eventNumber, double timeAtElectronics);
348// bool findFrameID(int fid);
349//
350// // decide whether to write or not to stream the frame based on event number, eventDuration, frameDuration and number of threads
351// // streaming the frame also deletes it from frameRunData
352// // TODO: this should be in gdata?
353// bool shouldWriteFrameID(int eventNumber, long int frameID);
354//
355// // TODO: this should be in gdata?
356// std::vector<int> formPayload(GDigitizedData* digitizedData);
357
358
359// in the constructur we had:
360
361// frameDuration = 64000;
362// eventDuration = gutilities::getG4Number(goptions->getScalarString("eventTimeSize"));
363
364// stream = gopt->getSwitch("stream");
std::shared_ptr< GLogger > log
void error(int exit_code, Args &&... args) const
Handles run begin/end callbacks and creates the thread-local GRun object.
Definition gRunAction.h:67
GRunAction & operator=(GRunAction &&)=delete
void increment_run_events_with_payload()
Increments the number of events that produced run-mode payload.
Definition gRunAction.h:170
void increment_run_events_processed()
Increments the number of events processed by the current thread for this run.
Definition gRunAction.h:149
GRunAction(GRunAction &&)=delete
auto get_digitization_routines_map() const -> std::shared_ptr< gdynamicdigitization::dRoutinesMap >
Returns the shared digitization-routine map used by this run action.
Definition gRunAction.h:95
bool has_streamer_threads_map() const
Definition gRunAction.h:117
GRunAction & operator=(const GRunAction &)=delete
GRunAction(const GRunAction &)=delete
void collect_event_data_collections(const std::string &hcSDName, std::unique_ptr< GDigitizedData > digi_data)
Adds one run-mode digitized payload to the current thread run-data collection.
Definition gRunAction.h:131
~GRunAction() override=default
auto get_streamer_threads_map() const -> std::shared_ptr< const gstreamer::gstreamersMap >
Returns the worker-thread streamer map, if it has been instantiated.
Definition gRunAction.h:109
GRunAction(std::shared_ptr< GOptions > gopts, std::shared_ptr< gdynamicdigitization::dRoutinesMap > digi_map)
Constructs the run action.
Definition gRunAction.cc:16
void collect_event_data_collections(const std::string &sdName, std::unique_ptr< GDigitizedData > ddata)
auto getHeader() const -> const std::unique_ptr< GRunHeader > &
constexpr const char * GRUNACTION_LOGGER
Definition gRunAction.h:25
Defines error codes used by the GEMC actions module.
#define ERR_GRUNACTION_NOT_EXISTING
#define ERR_STREAMERMAP_NOT_EXISTING
#define FUNCTION_NAME
Namespace containing helpers related to run-action configuration.
Definition gRunAction.h:32
GOptions defineOptions()
Returns the options associated with the run-action logger scope.
Definition gRunAction.h:38