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/gstreamer/factories/SRO/gSROFactory.h>
18#include <gemc/gdata/run/gRunDataCollection.h>
20
21
28
29constexpr const char* GRUNACTION_LOGGER = "grunaction";
30
36namespace grunaction {
43} // namespace grunaction
44
45
70class GRunAction : public GBase<GRunAction>, public G4UserRunAction
71{
72public:
81 explicit GRunAction(std::shared_ptr<GOptions> gopts,
82 std::shared_ptr<gdynamicdigitization::dRoutinesMap> digi_map,
83 std::shared_ptr<GAnalysisAccumulator> analysis_accumulator = nullptr,
84 std::shared_ptr<GSROFactory> sro_factory = nullptr);
85
86 ~GRunAction() override = default;
87
88 [[nodiscard]] const std::shared_ptr<GSROFactory>& get_sro_factory() const { return sro_factory; }
89
90 // The run action manages thread-local and process-wide state and is therefore
91 // intentionally non-copyable and non-movable.
92 GRunAction(const GRunAction&) = delete;
93 GRunAction& operator=(const GRunAction&) = delete;
96
105 [[nodiscard]] auto get_digitization_routines_map() const
106 -> std::shared_ptr<gdynamicdigitization::dRoutinesMap> {
107 return digitization_routines_map;
108 }
109
119 [[nodiscard]] auto get_streamer_threads_map() const
120 -> std::shared_ptr<const gstreamer::gstreamersMap> {
121 if (!gstreamer_threads_map) {
122 log->error(gaction::ERR_STREAMERMAP_NOT_EXISTING, FUNCTION_NAME, " no gstreamer thread map available");
123 }
124 return gstreamer_threads_map;
125 }
126
127 [[nodiscard]] bool has_streamer_threads_map() const {
128 return gstreamer_threads_map != nullptr;
129 }
130
132 [[nodiscard]] bool analysis_enabled() const { return analysis_shard != nullptr; }
133
135 void record_analysis_digitized(const std::string& detector, const GDigitizedData& data) {
136 if (analysis_shard != nullptr) {
137 analysis_shard->recordDigitized(requiredAnalysisRunNumber(), detector, data);
138 }
139 }
140
142 void record_analysis_true(const std::string& detector, const GTrueInfoData& data) {
143 if (analysis_shard != nullptr) {
144 analysis_shard->recordTrueInformation(requiredAnalysisRunNumber(), detector, data);
145 }
146 }
147
158 void collect_event_data_collections(const std::string& hcSDName,
159 std::unique_ptr<GDigitizedData> digi_data) {
160 if (run_data == nullptr) {
162 " run_data is null - cannot collect run-level payload for collection ", hcSDName);
163 }
164
165 run_data->collect_event_data_collections(
166 hcSDName,
167 std::move(digi_data));
168 }
169
177 if (run_data == nullptr) {
179 " run_data is null - cannot increment processed events.");
180 }
181
182 auto& header = run_data->getHeader();
183 if (header == nullptr) {
185 " run_data header is null - cannot increment processed events.");
186 }
187
188 header->increment_events_processed();
189 }
190
198 if (run_data == nullptr) {
200 " run_data is null - cannot increment payload events.");
201 }
202
203 auto& header = run_data->getHeader();
204 if (header == nullptr) {
206 " run_data header is null - cannot increment payload events.");
207 }
208
209 header->increment_events_with_payload();
210 }
211
212private:
213 using CompletedRunData = std::vector<std::unique_ptr<GRunDataCollection>>;
214
221 void stash_worker_run_data();
222
231 [[nodiscard]] CompletedRunData take_completed_worker_run_data();
232
241 G4Run* GenerateRun() override;
242
262 void BeginOfRunAction(const G4Run* run) override;
263
278 void EndOfRunAction(const G4Run* run) override;
279
285 void publish_run_data(const std::shared_ptr<GRunDataCollection>& run_data) const;
286
290 std::shared_ptr<GOptions> goptions;
291
295 std::shared_ptr<gdynamicdigitization::dRoutinesMap> digitization_routines_map;
296
298 std::shared_ptr<GAnalysisAccumulator> analysis_accumulator;
299
301 std::shared_ptr<GSROFactory> sro_factory;
302
304 std::unique_ptr<GAnalysisShard> analysis_shard;
305
307 std::optional<int> analysis_run_number;
308
309 [[nodiscard]] int requiredAnalysisRunNumber() const {
310 if (!analysis_run_number) {
311 throw std::logic_error("Analyzer data recorded before the simulation run number was published");
312 }
313 return *analysis_run_number;
314 }
315
319 std::shared_ptr<const gstreamer::gstreamersMap> gstreamer_threads_map;
320
324 std::shared_ptr<const gstreamer::gstreamersMap> gstreamer_run_map;
325
332 std::unique_ptr<GRunDataCollection> run_data;
333
337 bool need_a_thread_streamer = false;
338
342 bool need_a_run_streamer = false;
343
347 static std::mutex completed_run_data_mutex;
348
355 static CompletedRunData completed_worker_run_data;
356
375 void normalize_run_data(const std::shared_ptr<GRunDataCollection>& run_data) const;
376
377 // coming from digitization routine
378 std::unordered_map<std::string, std::vector<std::string>> to_normalize;
379};
380
381
382// vector of frame data in the run (local thread, merged in GRun::Merge in the global thread)
383// std::vector<GFrameDataCollection*> frameRunData;
384
385//
386// int eventIndex = 0; // added to the absolute event number, increases with each run
387// int lastFrameCreated = 0; // keeping track of the last frame created
388//
389// // determine the frame ID based on event number, eventDuration, frameDuration
390// // TODO: this should be in gdata?
391// int eventFrameIndex(int eventNumber, double timeAtElectronics);
392// bool findFrameID(int fid);
393//
394// // decide whether to write or not to stream the frame based on event number, eventDuration, frameDuration and number of threads
395// // streaming the frame also deletes it from frameRunData
396// // TODO: this should be in gdata?
397// bool shouldWriteFrameID(int eventNumber, long int frameID);
398//
399// // TODO: this should be in gdata?
400// std::vector<int> formPayload(GDigitizedData* digitizedData);
401
402
403// in the constructur we had:
404
405// frameDuration = 64000;
406// eventDuration = gparticle::getEventTimeWidth(goptions);
407
408// 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:135
void increment_run_events_with_payload()
Increments the number of events that produced run-mode payload.
Definition gRunAction.h:197
GRunAction(std::shared_ptr< GOptions > gopts, std::shared_ptr< gdynamicdigitization::dRoutinesMap > digi_map, std::shared_ptr< GAnalysisAccumulator > analysis_accumulator=nullptr, std::shared_ptr< GSROFactory > sro_factory=nullptr)
Constructs the run action.
Definition gRunAction.cc:16
void increment_run_events_processed()
Increments the number of events processed by the current thread for this run.
Definition gRunAction.h:176
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:105
bool has_streamer_threads_map() const
Definition gRunAction.h:127
bool analysis_enabled() const
Return whether this run action has a GUI Analyzer shard.
Definition gRunAction.h:132
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:142
GRunAction & operator=(const GRunAction &)=delete
const std::shared_ptr< GSROFactory > & get_sro_factory() const
Definition gRunAction.h:88
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:158
~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:119
constexpr const char * GRUNACTION_LOGGER
Definition gRunAction.h:29
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:36
GOptions defineOptions()
Returns the options associated with the run-action logger scope.
Definition gRunAction.h:42