gdynamicDigitization
Loading...
Searching...
No Matches
gdynamicdigitization.h
Go to the documentation of this file.
1#pragma once
2
3#include "greadoutSpecs.h"
4#include <gemc/gfactory/gfactory_options.h>
5
6// gemc
7#include <gemc/gfactory/gfactory.h>
8#include <gemc/gtouchable/gtouchable.h>
9#include <gemc/ghit/ghit.h>
10#include <gemc/gdata/gDigitizedData.h>
11#include <gemc/gdata/gTrueInfoData.h>
12#include <gemc/gtranslationTable/gtranslationTable.h>
14
15// c++
16#include <utility>
17#include <vector>
18#include <bitset>
19#include <map>
20#include <string>
21#include <optional>
22
23// geant4
24#include "G4Step.hh"
25
41public:
50 explicit GTouchableModifiers(const std::vector<std::string> &touchableNames);
51
52private:
61 std::map<std::string, std::vector<double> > modifierWeightsMap;
62
71 std::map<std::string, std::vector<double> > modifierWeightsAndTimesMap;
72
73public:
81 void insertIdAndWeight(const std::string &touchableName, int idValue, double weight);
82
91 void insertIdWeightAndTime(const std::string &touchableName, int idValue, double weight, double time);
92
102 void assignOverallWeight(const std::string &touchableName, double totalWeight);
103
113 [[nodiscard]] inline bool isWeightsOnly() const { return !modifierWeightsMap.empty(); }
114
124 inline std::vector<double> getModifierWeightsVector(const std::string &touchableName) {
125 return modifierWeightsMap[touchableName];
126 }
127
137 inline std::vector<double> getModifierWeightsAndTimeVector(const std::string &touchableName) {
138 return modifierWeightsAndTimesMap[touchableName];
139 }
140};
141
147
148inline constexpr const char *to_string(CollectionMode mode) {
149 switch (mode) {
150 case event: return "event";
151 case run: return "run";
152 case frame: return "frame";
153 }
154 return "unknown";
155}
156
173class GDynamicDigitization : public GBase<GDynamicDigitization> {
174public:
180 explicit GDynamicDigitization(const std::shared_ptr<GOptions> &g) : GBase(g, GDIGITIZATION_LOGGER) {
181 recordZeroEdep = g->getSwitch("recordZeroEdep");
182 }
183
185 ~GDynamicDigitization() override = default;
186
187 [[nodiscard]] virtual CollectionMode collection_mode() const { return CollectionMode::event; }
188
199 [[nodiscard]] double processStepTime(const std::shared_ptr<GTouchable> &gTouchID,
200 [[maybe_unused]] G4Step *thisStep) {
203 return processStepTimeImpl(gTouchID, thisStep);
204 }
205
215 [[nodiscard]] virtual double processStepTimeImpl(const std::shared_ptr<GTouchable> &gTouchID,
216 [[maybe_unused]] G4Step *thisStep);
217
218 [[nodiscard]] std::vector<std::string> variables_to_normalize() {
220
222
224 }
225
226
227 [[nodiscard]] virtual std::vector<std::string> variables_to_normalizeImpl() {
228 std::vector<std::string> variables;
229 return variables;
230 }
231
232
244 [[nodiscard]] std::vector<std::shared_ptr<GTouchable> > processTouchable(
245 std::shared_ptr<GTouchable> gtouchable, G4Step *thisStep) {
248 return processTouchableImpl(std::move(gtouchable), thisStep);
249 }
250
258 [[nodiscard]] virtual std::vector<std::shared_ptr<GTouchable> > processTouchableImpl(
259 std::shared_ptr<GTouchable> gtouchable, G4Step *thisStep);
260
271 [[nodiscard]] std::vector<std::shared_ptr<GTouchable> > processGTouchableModifiers(
272 const std::shared_ptr<GTouchable> &gTouchID, const GTouchableModifiers &gmods) {
275 return processGTouchableModifiersImpl(gTouchID, gmods);
276 }
277
287 virtual std::vector<std::shared_ptr<GTouchable> > processGTouchableModifiersImpl(
288 [[maybe_unused]] const std::shared_ptr<GTouchable> &gTouchID,
289 [[maybe_unused]] const GTouchableModifiers &gmods);
290
300 [[nodiscard]] std::unique_ptr<GTrueInfoData> collectTrueInformation(GHit *ghit, size_t hitn) {
302 log->info(2, "GDynamicDigitization::collect true information for hit number ", hitn, " with size ",
303 ghit->nsteps(), " steps");
304 return collectTrueInformationImpl(ghit, hitn);
305 }
306
314 [[nodiscard]] virtual std::unique_ptr<GTrueInfoData> collectTrueInformationImpl(GHit *ghit, size_t hitn);
315
325 [[nodiscard]] std::unique_ptr<GDigitizedData> digitizeHit(GHit *ghit, [[maybe_unused]] size_t hitn) {
327 log->info(2, "GDynamicDigitization::digitize hit number ", hitn, " with size ", ghit->nsteps(), " steps");
328 return digitizeHitImpl(ghit, hitn);
329 }
330
340 [[nodiscard]] virtual std::unique_ptr<GDigitizedData> digitizeHitImpl(
341 [[maybe_unused]] GHit *ghit, [[maybe_unused]] size_t hitn) { return nullptr; }
342
352 [[nodiscard]] bool loadConstants([[maybe_unused]] int runno, [[maybe_unused]] std::string const &variation) {
354 log->debug(NORMAL, "GDynamicDigitization::load constants");
355 return loadConstantsImpl(runno, variation);
356 }
357
367 virtual bool loadConstantsImpl([[maybe_unused]] int runno, [[maybe_unused]] std::string const &variation) {
368 return true;
369 }
370
380 [[nodiscard]] bool loadTT([[maybe_unused]] int runno, [[maybe_unused]] std::string const &variation) {
382 log->debug(NORMAL, "GDynamicDigitization::load Translation Table for run ", runno, " with variation ",
383 variation);
384 return loadTTImpl(runno, variation);
385 }
386
396 virtual bool loadTTImpl([[maybe_unused]] int runno, [[maybe_unused]] std::string const &variation) { return true; }
397
410 void chargeAndTimeAtHardware(int time, int q, const GHit *ghit, GDigitizedData &gdata);
411
419 [[nodiscard]] bool defineReadoutSpecs() {
421 log->debug(NORMAL, "GDynamicDigitization::define readout specs");
422 return defineReadoutSpecsImpl();
423 };
424
432 virtual bool defineReadoutSpecsImpl() = 0;
433
435 std::shared_ptr<const GReadoutSpecs> readoutSpecs;
436
438 std::shared_ptr<const GTranslationTable> translationTable;
439
449 static GDynamicDigitization *instantiate(const dlhandle h, std::shared_ptr<GOptions> g) {
450 if (!h) return nullptr;
451 using fptr = GDynamicDigitization* (*)(std::shared_ptr<GOptions>);
452
453 auto sym = dlsym(h, "GDynamicDigitizationFactory");
454 if (!sym) return nullptr;
455
456 auto func = reinterpret_cast<fptr>(sym);
457 return func(g);
458 }
459
469 virtual bool decisionToSkipHit(double energy) {
470 if (energy == 0 && !recordZeroEdep) {
471 return true;
472 }
473 return false;
474 }
475
486 virtual bool decisionToSkipHit(double energy, [[maybe_unused]] const G4Step* thisStep) {
487 return decisionToSkipHit(energy);
488 }
489
498 // TODO: REMOVE THIS EVERYWHERE also remove check_if_log_defined
499 void set_loggers(const std::shared_ptr<GOptions> &g) { gopts = g; }
500
509 void setDigitizationVariation(const std::string &v) { digitization_variation = v; }
510
516 [[nodiscard]] std::string getDigitizationVariation() const { return digitization_variation; }
517
518private:
520 bool recordZeroEdep = false;
521
524 std::string digitization_variation = "default";
525
526protected:
528 std::shared_ptr<GOptions> gopts;
529
535 void check_if_log_defined() const {
536 if (gopts == nullptr) {
537 std::cerr << KRED
538 << "Fatal Error: GDynamicDigitization: goption is not set for this plugin or one of the loggers is null."
539 << std::endl;
540 std::cerr << "The set_loggers function needs to be called." << std::endl;
541 std::cerr << "For example: dynamicRoutines[\"ctof\"]->set_loggers(gopts);" << std::endl;
542 std::cerr << RST << std::endl;
543 exit(1);
544 }
545 }
546};
547
549 using dRoutinesMap = std::unordered_map<std::string, std::shared_ptr<GDynamicDigitization> >;
550
558 inline std::shared_ptr<GDynamicDigitization> load_dynamicRoutine(const std::string &plugin_name,
559 const std::shared_ptr<GOptions> &gopts) {
560 GManager manager(gopts);
562 }
563
574 inline std::shared_ptr<const dRoutinesMap> dynamicRoutinesMap(const std::vector<std::string> &plugin_names,
575 const std::shared_ptr<GOptions> &gopts) {
576 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, GDIGITIZATION_LOGGER);
577 GManager manager(gopts);
578
579 auto routines = std::make_shared<dRoutinesMap>();
580
581 for (const auto &plugin: plugin_names) {
582 routines->emplace(plugin, manager.LoadAndRegisterObjectFromLibrary<GDynamicDigitization>(plugin, gopts));
583 log->info(0, "dynamicRoutinesMap[", plugin, "]: ", (*routines)[plugin]);
584 }
585
586 return routines;
587 }
588} // namespace gdynamicdigitization
std::shared_ptr< GLogger > log
Abstract base class for dynamically loaded digitization plugins.
virtual CollectionMode collection_mode() const
std::shared_ptr< GOptions > gopts
Options used by the digitization plugin instance.
virtual bool defineReadoutSpecsImpl()=0
Implementation hook to define readout specs.
std::vector< std::string > variables_to_normalize()
bool loadConstants(int runno, std::string const &variation)
Loads digitization constants (calibration/configuration).
~GDynamicDigitization() override=default
Virtual destructor.
virtual std::unique_ptr< GTrueInfoData > collectTrueInformationImpl(GHit *ghit, size_t hitn)
Implementation hook for true-information collection.
GDynamicDigitization(const std::shared_ptr< GOptions > &g)
Constructs the digitization base object.
bool loadTT(int runno, std::string const &variation)
Loads the translation table (identity -> electronics address).
double processStepTime(const std::shared_ptr< GTouchable > &gTouchID, G4Step *thisStep)
Computes the time associated with a simulation step for electronics binning.
virtual std::vector< std::shared_ptr< GTouchable > > processGTouchableModifiersImpl(const std::shared_ptr< GTouchable > &gTouchID, const GTouchableModifiers &gmods)
Implementation hook for touchable modifier application.
void check_if_log_defined() const
Ensures options/logging are configured before plugin methods run.
void setDigitizationVariation(const std::string &v)
Sets the variation used when this routine loads constants / translation tables.
virtual double processStepTimeImpl(const std::shared_ptr< GTouchable > &gTouchID, G4Step *thisStep)
Implementation hook for step time computation.
std::unique_ptr< GTrueInfoData > collectTrueInformation(GHit *ghit, size_t hitn)
Collects standardized “true hit information” into a GTrueInfoData record.
std::shared_ptr< const GTranslationTable > translationTable
Translation table is typically loaded during initialization and treated as immutable.
std::vector< std::shared_ptr< GTouchable > > processTouchable(std::shared_ptr< GTouchable > gtouchable, G4Step *thisStep)
Processes a touchable based on the current step and readout specs.
static GDynamicDigitization * instantiate(const dlhandle h, std::shared_ptr< GOptions > g)
Dynamically instantiates a plugin instance from a dynamic library.
virtual std::vector< std::shared_ptr< GTouchable > > processTouchableImpl(std::shared_ptr< GTouchable > gtouchable, G4Step *thisStep)
Implementation hook for touchable processing.
std::shared_ptr< const GReadoutSpecs > readoutSpecs
Readout specs are created during initialization and treated as immutable.
virtual std::vector< std::string > variables_to_normalizeImpl()
virtual bool loadTTImpl(int runno, std::string const &variation)
Implementation hook for translation table loading.
void set_loggers(const std::shared_ptr< GOptions > &g)
Sets the options pointer required by the digitization base.
virtual std::unique_ptr< GDigitizedData > digitizeHitImpl(GHit *ghit, size_t hitn)
Implementation hook for hit digitization.
std::string getDigitizationVariation() const
Returns the variation passed to loadConstants() / loadTT().
virtual bool decisionToSkipHit(double energy)
Decides whether a hit should be skipped based on deposited energy.
std::unique_ptr< GDigitizedData > digitizeHit(GHit *ghit, size_t hitn)
Digitizes a hit into a GDigitizedData record.
void chargeAndTimeAtHardware(int time, int q, const GHit *ghit, GDigitizedData &gdata)
Adds hardware-level time/charge and address fields to a digitized record.
virtual bool loadConstantsImpl(int runno, std::string const &variation)
Implementation hook for constant loading.
std::vector< std::shared_ptr< GTouchable > > processGTouchableModifiers(const std::shared_ptr< GTouchable > &gTouchID, const GTouchableModifiers &gmods)
Applies touchable modifiers.
virtual bool decisionToSkipHit(double energy, const G4Step *thisStep)
Decides whether a step should be skipped before hit creation.
bool defineReadoutSpecs()
Initializes readout specifications.
size_t nsteps() const
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
std::shared_ptr< T > LoadAndRegisterObjectFromLibrary(std::string_view name, const std::shared_ptr< GOptions > &gopts)
Helper container for representing touchable “modifier” information.
void insertIdAndWeight(const std::string &touchableName, int idValue, double weight)
Inserts a new (id, weight) pair for the specified touchable.
std::vector< double > getModifierWeightsVector(const std::string &touchableName)
Returns the weights vector for a touchable.
std::vector< double > getModifierWeightsAndTimeVector(const std::string &touchableName)
Returns the weights-and-times vector for a touchable.
bool isWeightsOnly() const
Returns whether the container currently reports weights-only modifiers.
void insertIdWeightAndTime(const std::string &touchableName, int idValue, double weight, double time)
Inserts a new (id, weight, time) triplet for the specified touchable.
void assignOverallWeight(const std::string &touchableName, double totalWeight)
Normalizes modifier weights by dividing by a provided total.
GTouchableModifiers(const std::vector< std::string > &touchableNames)
Constructs the container and declares supported touchable names.
void * dlhandle
constexpr const char * to_string(CollectionMode mode)
Options for the GDynamicDigitization module.
constexpr const char * GDIGITIZATION_LOGGER
Logger name used by this module.
#define FUNCTION_NAME
#define SFUNCTION_NAME
NORMAL
Readout electronics specifications used by time-binning and hit organization.
const std::string plugin_name
#define KRED
#define RST
std::shared_ptr< GDynamicDigitization > load_dynamicRoutine(const std::string &plugin_name, const std::shared_ptr< GOptions > &gopts)
Loads a single dynamic routine and returns it.
std::unordered_map< std::string, std::shared_ptr< GDynamicDigitization > > dRoutinesMap
std::shared_ptr< const dRoutinesMap > dynamicRoutinesMap(const std::vector< std::string > &plugin_names, const std::shared_ptr< GOptions > &gopts)
Loads multiple dynamic routines and returns an immutable shared map.