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 <optional>
6#include <stdexcept>
7#include <vector>
8
9// geant4
10#include "G4UserRunAction.hh"
11
12// gemc
13#include <gemc/gbase/gbase.h>
14#include <gemc/ganalysis/gAnalysisAccumulator.h>
15#include <gemc/gdynamicDigitization/gdynamicdigitization.h>
16#include <gemc/gstreamer/gstreamer.h>
17#include <gemc/gdata/run/gRunDataCollection.h>
19
20
27
28constexpr const char* GRUNACTION_LOGGER = "grunaction";
29
35namespace grunaction {
42} // namespace grunaction
43
44
69class GRunAction : public GBase<GRunAction>, public G4UserRunAction
70{
71public:
79 explicit GRunAction(std::shared_ptr<GOptions> gopts,
80 std::shared_ptr<gdynamicdigitization::dRoutinesMap> digi_map,
81 std::shared_ptr<GAnalysisAccumulator> analysis_accumulator = nullptr);
82
83 ~GRunAction() override = default;
84
85 // The run action manages thread-local and process-wide state and is therefore
86 // intentionally non-copyable and non-movable.
87 GRunAction(const GRunAction&) = delete;
88 GRunAction& operator=(const GRunAction&) = delete;
91
100 [[nodiscard]] auto get_digitization_routines_map() const
101 -> std::shared_ptr<gdynamicdigitization::dRoutinesMap> {
102 return digitization_routines_map;
103 }
104
114 [[nodiscard]] auto get_streamer_threads_map() const
115 -> std::shared_ptr<const gstreamer::gstreamersMap> {
116 if (!gstreamer_threads_map) {
117 log->error(gaction::ERR_STREAMERMAP_NOT_EXISTING, FUNCTION_NAME, " no gstreamer thread map available");
118 }
119 return gstreamer_threads_map;
120 }
121
122 [[nodiscard]] bool has_streamer_threads_map() const {
123 return gstreamer_threads_map != nullptr;
124 }
125
127 [[nodiscard]] bool analysis_enabled() const { return analysis_shard != nullptr; }
128
130 void record_analysis_digitized(const std::string& detector, const GDigitizedData& data) {
131 if (analysis_shard != nullptr) {
132 analysis_shard->recordDigitized(requiredAnalysisRunNumber(), detector, data);
133 }
134 }
135
137 void record_analysis_true(const std::string& detector, const GTrueInfoData& data) {
138 if (analysis_shard != nullptr) {
139 analysis_shard->recordTrueInformation(requiredAnalysisRunNumber(), detector, data);
140 }
141 }
142
153 void collect_event_data_collections(const std::string& hcSDName,
154 std::unique_ptr<GDigitizedData> digi_data) {
155 if (run_data == nullptr) {
157 " run_data is null - cannot collect run-level payload for collection ", hcSDName);
158 }
159
160 run_data->collect_event_data_collections(
161 hcSDName,
162 std::move(digi_data));
163 }
164
172 if (run_data == nullptr) {
174 " run_data is null - cannot increment processed events.");
175 }
176
177 auto& header = run_data->getHeader();
178 if (header == nullptr) {
180 " run_data header is null - cannot increment processed events.");
181 }
182
183 header->increment_events_processed();
184 }
185
193 if (run_data == nullptr) {
195 " run_data is null - cannot increment payload events.");
196 }
197
198 auto& header = run_data->getHeader();
199 if (header == nullptr) {
201 " run_data header is null - cannot increment payload events.");
202 }
203
204 header->increment_events_with_payload();
205 }
206
207private:
208 using CompletedRunData = std::vector<std::unique_ptr<GRunDataCollection>>;
209
216 void stash_worker_run_data();
217
226 [[nodiscard]] CompletedRunData take_completed_worker_run_data();
227
236 G4Run* GenerateRun() override;
237
257 void BeginOfRunAction(const G4Run* run) override;
258
273 void EndOfRunAction(const G4Run* run) override;
274
280 void publish_run_data(const std::shared_ptr<GRunDataCollection>& run_data) const;
281
285 std::shared_ptr<GOptions> goptions;
286
290 std::shared_ptr<gdynamicdigitization::dRoutinesMap> digitization_routines_map;
291
293 std::shared_ptr<GAnalysisAccumulator> analysis_accumulator;
294
296 std::unique_ptr<GAnalysisShard> analysis_shard;
297
299 std::optional<int> analysis_run_number;
300
301 [[nodiscard]] int requiredAnalysisRunNumber() const {
302 if (!analysis_run_number) {
303 throw std::logic_error("Analyzer data recorded before the simulation run number was published");
304 }
305 return *analysis_run_number;
306 }
307
311 std::shared_ptr<const gstreamer::gstreamersMap> gstreamer_threads_map;
312
316 std::shared_ptr<const gstreamer::gstreamersMap> gstreamer_run_map;
317
324 std::unique_ptr<GRunDataCollection> run_data;
325
329 bool need_a_thread_streamer = false;
330
334 bool need_a_run_streamer = false;
335
339 static std::mutex completed_run_data_mutex;
340
347 static CompletedRunData completed_worker_run_data;
348
367 void normalize_run_data(const std::shared_ptr<GRunDataCollection>& run_data) const;
368
369 // coming from digitization routine
370 std::unordered_map<std::string, std::vector<std::string>> to_normalize;
371};
372
373
374// vector of frame data in the run (local thread, merged in GRun::Merge in the global thread)
375// std::vector<GFrameDataCollection*> frameRunData;
376
377//
378// int eventIndex = 0; // added to the absolute event number, increases with each run
379// int lastFrameCreated = 0; // keeping track of the last frame created
380//
381// // determine the frame ID based on event number, eventDuration, frameDuration
382// // TODO: this should be in gdata?
383// int eventFrameIndex(int eventNumber, double timeAtElectronics);
384// bool findFrameID(int fid);
385//
386// // decide whether to write or not to stream the frame based on event number, eventDuration, frameDuration and number of threads
387// // streaming the frame also deletes it from frameRunData
388// // TODO: this should be in gdata?
389// bool shouldWriteFrameID(int eventNumber, long int frameID);
390//
391// // TODO: this should be in gdata?
392// std::vector<int> formPayload(GDigitizedData* digitizedData);
393
394
395// in the constructur we had:
396
397// frameDuration = 64000;
398// eventDuration = gutilities::getG4Number(goptions->getRequiredScalarString("eventTimeSize"));
399
400// 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:130
void increment_run_events_with_payload()
Increments the number of events that produced run-mode payload.
Definition gRunAction.h:192
void increment_run_events_processed()
Increments the number of events processed by the current thread for this run.
Definition gRunAction.h:171
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:100
bool has_streamer_threads_map() const
Definition gRunAction.h:122
bool analysis_enabled() const
Return whether this run action has a GUI Analyzer shard.
Definition gRunAction.h:127
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:137
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:153
~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:114
constexpr const char * GRUNACTION_LOGGER
Definition gRunAction.h:28
Defines error codes used by the GEMC actions module.
run
#define FUNCTION_NAME
constexpr int ERR_STREAMERMAP_NOT_EXISTING
constexpr int ERR_GRUNACTION_NOT_EXISTING
Namespace containing helpers related to run-action configuration.
Definition gRunAction.h:35
GOptions defineOptions()
Returns the options associated with the run-action logger scope.
Definition gRunAction.h:41