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/ganalysis/gAnalysisAccumulator.h>
13#include <gemc/gdynamicDigitization/gdynamicdigitization.h>
14#include <gemc/gstreamer/gstreamer.h>
15#include <gemc/gdata/run/gRunDataCollection.h>
17
18
25
26constexpr const char* GRUNACTION_LOGGER = "grunaction";
27
33namespace grunaction {
40} // namespace grunaction
41
42
67class GRunAction : public GBase<GRunAction>, public G4UserRunAction
68{
69public:
77 explicit GRunAction(std::shared_ptr<GOptions> gopts,
78 std::shared_ptr<gdynamicdigitization::dRoutinesMap> digi_map,
79 std::shared_ptr<GAnalysisAccumulator> analysis_accumulator = nullptr);
80
81 ~GRunAction() override = default;
82
83 // The run action manages thread-local and process-wide state and is therefore
84 // intentionally non-copyable and non-movable.
85 GRunAction(const GRunAction&) = delete;
86 GRunAction& operator=(const GRunAction&) = delete;
89
98 [[nodiscard]] auto get_digitization_routines_map() const
99 -> std::shared_ptr<gdynamicdigitization::dRoutinesMap> {
100 return digitization_routines_map;
101 }
102
112 [[nodiscard]] auto get_streamer_threads_map() const
113 -> std::shared_ptr<const gstreamer::gstreamersMap> {
114 if (!gstreamer_threads_map) {
115 log->error(ERR_STREAMERMAP_NOT_EXISTING, FUNCTION_NAME, " no gstreamer thread map available");
116 }
117 return gstreamer_threads_map;
118 }
119
120 [[nodiscard]] bool has_streamer_threads_map() const {
121 return gstreamer_threads_map != nullptr;
122 }
123
125 [[nodiscard]] bool analysis_enabled() const { return analysis_shard != nullptr; }
126
128 void record_analysis_digitized(const std::string& detector, const GDigitizedData& data) {
129 if (analysis_shard != nullptr) {
130 analysis_shard->recordDigitized(analysis_run_number, detector, data);
131 }
132 }
133
135 void record_analysis_true(const std::string& detector, const GTrueInfoData& data) {
136 if (analysis_shard != nullptr) {
137 analysis_shard->recordTrueInformation(analysis_run_number, detector, data);
138 }
139 }
140
151 void collect_event_data_collections(const std::string& hcSDName,
152 std::unique_ptr<GDigitizedData> digi_data) {
153 if (run_data == nullptr) {
155 " run_data is null - cannot collect run-level payload for collection ", hcSDName);
156 }
157
158 run_data->collect_event_data_collections(
159 hcSDName,
160 std::move(digi_data));
161 }
162
170 if (run_data == nullptr) {
172 " run_data is null - cannot increment processed events.");
173 }
174
175 auto& header = run_data->getHeader();
176 if (header == nullptr) {
178 " run_data header is null - cannot increment processed events.");
179 }
180
181 header->increment_events_processed();
182 }
183
191 if (run_data == nullptr) {
193 " run_data is null - cannot increment payload events.");
194 }
195
196 auto& header = run_data->getHeader();
197 if (header == nullptr) {
199 " run_data header is null - cannot increment payload events.");
200 }
201
202 header->increment_events_with_payload();
203 }
204
205private:
206 using CompletedRunData = std::vector<std::unique_ptr<GRunDataCollection>>;
207
214 void stash_worker_run_data();
215
224 [[nodiscard]] CompletedRunData take_completed_worker_run_data();
225
234 G4Run* GenerateRun() override;
235
255 void BeginOfRunAction(const G4Run* run) override;
256
271 void EndOfRunAction(const G4Run* run) override;
272
278 void publish_run_data(const std::shared_ptr<GRunDataCollection>& run_data) const;
279
283 std::shared_ptr<GOptions> goptions;
284
288 std::shared_ptr<gdynamicdigitization::dRoutinesMap> digitization_routines_map;
289
291 std::shared_ptr<GAnalysisAccumulator> analysis_accumulator;
292
294 std::unique_ptr<GAnalysisShard> analysis_shard;
295
297 int analysis_run_number = -1;
298
302 std::shared_ptr<const gstreamer::gstreamersMap> gstreamer_threads_map;
303
307 std::shared_ptr<const gstreamer::gstreamersMap> gstreamer_run_map;
308
315 std::unique_ptr<GRunDataCollection> run_data;
316
320 bool need_a_thread_streamer = false;
321
325 bool need_a_run_streamer = false;
326
330 static std::mutex completed_run_data_mutex;
331
338 static CompletedRunData completed_worker_run_data;
339
358 void normalize_run_data(const std::shared_ptr<GRunDataCollection>& run_data) const;
359
360 // coming from digitization routine
361 std::unordered_map<std::string, std::vector<std::string>> to_normalize;
362};
363
364
365// vector of frame data in the run (local thread, merged in GRun::Merge in the global thread)
366// std::vector<GFrameDataCollection*> frameRunData;
367
368//
369// double frameDuration = UNINITIALIZEDNUMBERQUANTITY; // frame length in nanoseconds
370// double eventDuration = UNINITIALIZEDNUMBERQUANTITY; // event duration in nanoseconds
371// int eventIndex = 0; // added to the absolute event number, increases with each run
372// int lastFrameCreated = 0; // keeping track of the last frame created
373//
374// // determine the frame ID based on event number, eventDuration, frameDuration
375// // TODO: this should be in gdata?
376// int eventFrameIndex(int eventNumber, double timeAtElectronics);
377// bool findFrameID(int fid);
378//
379// // decide whether to write or not to stream the frame based on event number, eventDuration, frameDuration and number of threads
380// // streaming the frame also deletes it from frameRunData
381// // TODO: this should be in gdata?
382// bool shouldWriteFrameID(int eventNumber, long int frameID);
383//
384// // TODO: this should be in gdata?
385// std::vector<int> formPayload(GDigitizedData* digitizedData);
386
387
388// in the constructur we had:
389
390// frameDuration = 64000;
391// eventDuration = gutilities::getG4Number(goptions->getScalarString("eventTimeSize"));
392
393// stream = gopt->getSwitch("stream");
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
GRunAction & operator=(GRunAction &&)=delete
void record_analysis_digitized(const std::string &detector, const GDigitizedData &data)
Discover digitized variables in one accepted record and add them to this thread's shard.
Definition gRunAction.h:128
void increment_run_events_with_payload()
Increments the number of events that produced run-mode payload.
Definition gRunAction.h:190
void increment_run_events_processed()
Increments the number of events processed by the current thread for this run.
Definition gRunAction.h:169
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:98
bool has_streamer_threads_map() const
Definition gRunAction.h:120
bool analysis_enabled() const
Return whether this run action has a GUI Analyzer shard.
Definition gRunAction.h:125
void record_analysis_true(const std::string &detector, const GTrueInfoData &data)
Discover true-information variables in one record and add them to this thread's shard.
Definition gRunAction.h:135
GRunAction & operator=(const GRunAction &)=delete
GRunAction(std::shared_ptr< GOptions > gopts, std::shared_ptr< gdynamicdigitization::dRoutinesMap > digi_map, std::shared_ptr< GAnalysisAccumulator > analysis_accumulator=nullptr)
Constructs the run action.
Definition gRunAction.cc:16
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:151
~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:112
constexpr const char * GRUNACTION_LOGGER
Definition gRunAction.h:26
Defines error codes used by the GEMC actions module.
#define ERR_GRUNACTION_NOT_EXISTING
#define ERR_STREAMERMAP_NOT_EXISTING
run
#define FUNCTION_NAME
Namespace containing helpers related to run-action configuration.
Definition gRunAction.h:33
GOptions defineOptions()
Returns the options associated with the run-action logger scope.
Definition gRunAction.h:39