gdynamicDigitization
Loading...
Searching...
No Matches
gdynamicdigitization.h
Go to the documentation of this file.
1#pragma once
2
3#include <gemc/gstreamer/sro/gSROData.h>
4
5#include "greadoutSpecs.h"
6#include <gemc/gfactory/gfactory_options.h>
7
8// gemc
9#include <gemc/gfactory/gfactory.h>
10#include <gemc/gtouchable/gtouchable.h>
11#include <gemc/ghit/ghit.h>
12#include <gemc/gdata/gDigitizedData.h>
13#include <gemc/gdata/gTrueInfoData.h>
14#include <gemc/gtranslationTable/gtranslationTable.h>
16
17// c++
18#include <utility>
19#include <vector>
20#include <bitset>
21#include <map>
22#include <string>
23#include <optional>
24
25// geant4
26#include "G4Step.hh"
27
43public:
52 explicit GTouchableModifiers(const std::vector<std::string> &touchableNames);
53
54private:
63 std::map<std::string, std::vector<double> > modifierWeightsMap;
64
73 std::map<std::string, std::vector<double> > modifierWeightsAndTimesMap;
74
75public:
83 void insertIdAndWeight(const std::string &touchableName, int idValue, double weight);
84
93 void insertIdWeightAndTime(const std::string &touchableName, int idValue, double weight, double time);
94
104 void assignOverallWeight(const std::string &touchableName, double totalWeight);
105
115 [[nodiscard]] inline bool isWeightsOnly() const { return !modifierWeightsMap.empty(); }
116
126 inline std::vector<double> getModifierWeightsVector(const std::string &touchableName) {
127 return modifierWeightsMap[touchableName];
128 }
129
139 inline std::vector<double> getModifierWeightsAndTimeVector(const std::string &touchableName) {
140 return modifierWeightsAndTimesMap[touchableName];
141 }
142};
143
149
150inline constexpr const char *to_string(CollectionMode mode) {
151 switch (mode) {
152 case event: return "event";
153 case run: return "run";
154 case frame: return "frame";
155 }
156 return "unknown";
157}
158
175class GDynamicDigitization : public GBase<GDynamicDigitization> {
176public:
182 explicit GDynamicDigitization(const std::shared_ptr<GOptions> &g) : GBase(g, GDIGITIZATION_LOGGER) {
183 recordZeroEdep = g->getSwitch("recordZeroEdep");
184 }
185
187 ~GDynamicDigitization() override = default;
188
189 [[nodiscard]] virtual CollectionMode collection_mode() const { return CollectionMode::event; }
190
201 [[nodiscard]] double processStepTime(const std::shared_ptr<GTouchable> &gTouchID,
202 [[maybe_unused]] G4Step *thisStep) {
204 log->debug(NORMAL, FUNCTION_NAME);
205 return processStepTimeImpl(gTouchID, thisStep);
206 }
207
217 [[nodiscard]] virtual double processStepTimeImpl(const std::shared_ptr<GTouchable> &gTouchID,
218 [[maybe_unused]] G4Step *thisStep);
219
220 [[nodiscard]] std::vector<std::string> variables_to_normalize() {
222
223 log->debug(NORMAL, FUNCTION_NAME);
224
226 }
227
228
229 [[nodiscard]] virtual std::vector<std::string> variables_to_normalizeImpl() {
230 std::vector<std::string> variables;
231 return variables;
232 }
233
234
246 [[nodiscard]] std::vector<std::shared_ptr<GTouchable> > processTouchable(
247 std::shared_ptr<GTouchable> gtouchable, G4Step *thisStep) {
249 log->debug(NORMAL, FUNCTION_NAME);
250 return processTouchableImpl(std::move(gtouchable), thisStep);
251 }
252
260 [[nodiscard]] virtual std::vector<std::shared_ptr<GTouchable> > processTouchableImpl(
261 std::shared_ptr<GTouchable> gtouchable, G4Step *thisStep);
262
273 [[nodiscard]] std::vector<std::shared_ptr<GTouchable> > processGTouchableModifiers(
274 const std::shared_ptr<GTouchable> &gTouchID, const GTouchableModifiers &gmods) {
276 log->debug(NORMAL, FUNCTION_NAME);
277 return processGTouchableModifiersImpl(gTouchID, gmods);
278 }
279
289 virtual std::vector<std::shared_ptr<GTouchable> > processGTouchableModifiersImpl(
290 [[maybe_unused]] const std::shared_ptr<GTouchable> &gTouchID,
291 [[maybe_unused]] const GTouchableModifiers &gmods);
292
302 [[nodiscard]] std::unique_ptr<GTrueInfoData> collectTrueInformation(GHit *ghit, size_t hitn) {
304 log->info(2, "GDynamicDigitization::collect true information for hit number ", hitn, " with size ",
305 ghit->nsteps(), " steps");
306 return collectTrueInformationImpl(ghit, hitn);
307 }
308
316 [[nodiscard]] virtual std::unique_ptr<GTrueInfoData> collectTrueInformationImpl(GHit *ghit, size_t hitn);
317
327 [[nodiscard]] std::unique_ptr<GDigitizedData> digitizeHit(GHit *ghit, [[maybe_unused]] size_t hitn) {
329 log->info(2, "GDynamicDigitization::digitize hit number ", hitn, " with size ", ghit->nsteps(), " steps");
330 return digitizeHitImpl(ghit, hitn);
331 }
332
342 [[nodiscard]] virtual std::unique_ptr<GDigitizedData> digitizeHitImpl(
343 [[maybe_unused]] GHit *ghit, [[maybe_unused]] size_t hitn) { return nullptr; }
344
353 virtual void stream_hit([[maybe_unused]] GHit* hit, [[maybe_unused]] std::size_t hit_index,
354 [[maybe_unused]] const GSROEventContext& event,
355 [[maybe_unused]] const GSROEmit& emit) const {}
356
366 [[nodiscard]] bool loadConstants([[maybe_unused]] int runno, [[maybe_unused]] std::string const &variation) {
368 log->debug(NORMAL, "GDynamicDigitization::load constants");
369 return loadConstantsImpl(runno, variation);
370 }
371
381 virtual bool loadConstantsImpl([[maybe_unused]] int runno, [[maybe_unused]] std::string const &variation) {
382 return true;
383 }
384
394 [[nodiscard]] bool loadTT([[maybe_unused]] int runno, [[maybe_unused]] std::string const &variation) {
396 log->debug(NORMAL, "GDynamicDigitization::load Translation Table for run ", runno, " with variation ",
397 variation);
398 return loadTTImpl(runno, variation);
399 }
400
410 virtual bool loadTTImpl([[maybe_unused]] int runno, [[maybe_unused]] std::string const &variation) { return true; }
411
424 void chargeAndTimeAtHardware(int time, int q, const GHit *ghit, GDigitizedData &gdata);
425
433 [[nodiscard]] bool defineReadoutSpecs() {
435 log->debug(NORMAL, "GDynamicDigitization::define readout specs");
436 return defineReadoutSpecsImpl();
437 };
438
446 virtual bool defineReadoutSpecsImpl() = 0;
447
449 std::shared_ptr<const GReadoutSpecs> readoutSpecs;
450
452 std::shared_ptr<const GTranslationTable> translationTable;
453
463 static GDynamicDigitization *instantiate(const dlhandle h, std::shared_ptr<GOptions> g) {
464 if (!h) return nullptr;
465 using fptr = GDynamicDigitization* (*)(std::shared_ptr<GOptions>);
466
467 auto sym = dlsym(h, "GDynamicDigitizationFactory");
468 if (!sym) return nullptr;
469
470 auto func = reinterpret_cast<fptr>(sym);
471 return func(g);
472 }
473
483 virtual bool decisionToSkipHit(double energy) {
484 if (energy == 0 && !recordZeroEdep) {
485 return true;
486 }
487 return false;
488 }
489
500 virtual bool decisionToSkipHit(double energy, [[maybe_unused]] const G4Step* thisStep) {
501 return decisionToSkipHit(energy);
502 }
503
514 [[nodiscard]] bool shouldStopTrackAfterHit(const G4Step* thisStep) const {
516 return shouldStopTrackAfterHitImpl(thisStep);
517 }
518
525 [[nodiscard]] virtual bool shouldStopTrackAfterHitImpl(
526 [[maybe_unused]] const G4Step* thisStep) const {
527 return false;
528 }
529
538 // TODO: REMOVE THIS EVERYWHERE also remove check_if_log_defined
539 void set_loggers(const std::shared_ptr<GOptions> &g) { gopts = g; }
540
549 void setDigitizationVariation(const std::string &v) { digitization_variation = v; }
550
556 [[nodiscard]] std::string getDigitizationVariation() const { return digitization_variation; }
557
568 void setHitRejectionPolicies(const std::string &systemName);
569
582 [[nodiscard]] bool apply_thresholds(GHit* ghit, GDigitizedData* digitizedData) {
583 if (!applyThresholds_ && !thresholds_are_intrinsic_impl()) { return false; }
584 return apply_thresholds_impl(ghit, digitizedData);
585 }
586
593 [[nodiscard]] virtual bool apply_thresholds_impl([[maybe_unused]] GHit* ghit,
594 [[maybe_unused]] GDigitizedData* digitizedData) {
595 return false;
596 }
597
604 [[nodiscard]] virtual bool thresholds_are_intrinsic_impl() const { return false; }
605
618 [[nodiscard]] bool apply_efficiency(GHit* ghit, GDigitizedData* digitizedData) {
619 if (!applyInefficiencies_ && !efficiencies_are_intrinsic_impl()) { return false; }
620 return apply_efficiency_impl(ghit, digitizedData);
621 }
622
629 [[nodiscard]] virtual bool apply_efficiency_impl([[maybe_unused]] GHit* ghit,
630 [[maybe_unused]] GDigitizedData* digitizedData) {
631 return false;
632 }
633
640 [[nodiscard]] virtual bool efficiencies_are_intrinsic_impl() const { return false; }
641
642private:
644 bool recordZeroEdep = false;
645
648 bool applyThresholds_ = false;
649 bool applyInefficiencies_ = false;
650
653 std::string digitization_variation = "default";
654
655protected:
657 std::shared_ptr<GOptions> gopts;
658
664 void check_if_log_defined() const {
665 if (gopts == nullptr) {
666 std::cerr << guts::KRED
667 << "Fatal Error: GDynamicDigitization: goption is not set for this plugin or one of the loggers is null."
668 << std::endl;
669 std::cerr << "The set_loggers function needs to be called." << std::endl;
670 std::cerr << "For example: dynamicRoutines[\"ctof\"]->set_loggers(gopts);" << std::endl;
671 std::cerr << guts::RST << std::endl;
672 exit(1);
673 }
674 }
675};
676
678 using dRoutinesMap = std::unordered_map<std::string, std::shared_ptr<GDynamicDigitization> >;
679
687 inline std::shared_ptr<GDynamicDigitization> load_dynamicRoutine(const std::string &plugin_name,
688 const std::shared_ptr<GOptions> &gopts) {
689 GManager manager(gopts);
691 }
692
703 inline std::shared_ptr<const dRoutinesMap> dynamicRoutinesMap(const std::vector<std::string> &plugin_names,
704 const std::shared_ptr<GOptions> &gopts) {
705 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, GDIGITIZATION_LOGGER);
706 GManager manager(gopts);
707
708 auto routines = std::make_shared<dRoutinesMap>();
709
710 for (const auto &plugin: plugin_names) {
711 routines->emplace(plugin, manager.LoadAndRegisterObjectFromLibrary<GDynamicDigitization>(plugin, gopts));
712 log->info(0, "dynamicRoutinesMap[", plugin, "]: ", (*routines)[plugin]);
713 }
714
715 return routines;
716 }
717} // namespace gdynamicdigitization
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
Abstract base class for dynamically loaded digitization plugins.
bool apply_thresholds(GHit *ghit, GDigitizedData *digitizedData)
Applies this system's ADC-threshold rejection to a digitized hit.
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.
virtual bool apply_thresholds_impl(GHit *ghit, GDigitizedData *digitizedData)
Plugin hook: is this digitized hit below the per-channel ADC threshold?
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.
bool apply_efficiency(GHit *ghit, GDigitizedData *digitizedData)
Applies this system's efficiency rejection to a digitized hit.
std::shared_ptr< const GReadoutSpecs > readoutSpecs
Readout specs are created during initialization and treated as immutable.
bool shouldStopTrackAfterHit(const G4Step *thisStep) const
Ask whether Geant4 should terminate the track after recording this step's hit.
virtual std::vector< std::string > variables_to_normalizeImpl()
virtual bool apply_efficiency_impl(GHit *ghit, GDigitizedData *digitizedData)
Plugin hook: does this digitized hit fail the per-channel efficiency draw?
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 bool efficiencies_are_intrinsic_impl() const
Declares that this plugin's efficiency policy is intrinsic to GEMC2 digitization.
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.
virtual bool thresholds_are_intrinsic_impl() const
Declares that this plugin's threshold is intrinsic to its GEMC2 digitization.
virtual bool shouldStopTrackAfterHitImpl(const G4Step *thisStep) const
Plugin hook controlling post-hit track termination.
void setHitRejectionPolicies(const std::string &systemName)
Resolves this routine's hit-rejection policies from the global options.
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 void stream_hit(GHit *hit, std::size_t hit_index, const GSROEventContext &event, const GSROEmit &emit) const
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
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.
std::function< void(GSROCrateId, GSROTime, std::unique_ptr< const GSROData >)> GSROEmit
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
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.
constexpr char RST[]
constexpr char KRED[]