gdynamicDigitization
Loading...
Searching...
No Matches
gdynamicdigitization.h
Go to the documentation of this file.
1#pragma once
2
3#include "greadoutSpecs.h"
4#include "gfactory_options.h"
5
6// gemc
7#include "gfactory.h"
8#include "gtouchable.h"
9#include "ghit.h"
10#include "gDigitizedData.h"
11#include "gTrueInfoData.h"
12#include "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
28public:
36 explicit GTouchableModifiers(const std::vector<std::string>& touchableNames);
37
38private:
39 // Only one of these maps can be filled with values:
40 // the size of the map is used by processGTouchableModifiers.
41
43 std::map<std::string, std::vector<double>> modifierWeightsMap;
44
46 std::map<std::string, std::vector<double>> modifierWeightsAndTimesMap;
47
48public:
55 void insertIdAndWeight(const std::string& touchableName, int idValue, double weight);
56
64 void insertIdWeightAndTime(const std::string& touchableName, int idValue, double weight, double time);
65
75 void assignOverallWeight(const std::string& touchableName, double totalWeight);
76
82 [[nodiscard]] inline bool isWeightsOnly() const { return !modifierWeightsMap.empty(); }
83
91 inline std::vector<double> getModifierWeightsVector(const std::string& touchableName) { return modifierWeightsMap[touchableName]; }
92
100 inline std::vector<double> getModifierWeightsAndTimeVector(const std::string& touchableName) { return modifierWeightsAndTimesMap[touchableName]; }
101};
102
104class GDynamicDigitization : public GBase<GDynamicDigitization> {
105public:
106 explicit GDynamicDigitization(const std::shared_ptr<GOptions>& g) : GBase(g, GDIGITIZATION_LOGGER) {}
107
111 virtual ~GDynamicDigitization() = default;
112
122 [[nodiscard]] double processStepTime(const std::shared_ptr<GTouchable>& gTouchID, [[maybe_unused]] G4Step* thisStep) {
124 log->debug(NORMAL, FUNCTION_NAME);
125 return processStepTimeImpl(gTouchID, thisStep);
126 }
127
128 [[nodiscard]] virtual double processStepTimeImpl(const std::shared_ptr<GTouchable>& gTouchID, [[maybe_unused]] G4Step* thisStep);
129
130
141 [[nodiscard]] std::vector<std::shared_ptr<GTouchable>> processTouchable(std::shared_ptr<GTouchable> gtouchable, G4Step* thisStep) {
143 log->debug(NORMAL, FUNCTION_NAME);
144 return processTouchableImpl(std::move(gtouchable), thisStep);
145 }
146
147 [[nodiscard]] virtual std::vector<std::shared_ptr<GTouchable>> processTouchableImpl(std::shared_ptr<GTouchable> gtouchable, G4Step* thisStep);
148
158 [[nodiscard]] std::vector<std::shared_ptr<GTouchable>> processGTouchableModifiers(const std::shared_ptr<GTouchable>& gTouchID, const GTouchableModifiers& gmods) {
160 log->debug(NORMAL, FUNCTION_NAME);
161 return processGTouchableModifiersImpl(gTouchID, gmods);
162 }
163
164 virtual std::vector<std::shared_ptr<GTouchable>> processGTouchableModifiersImpl([[maybe_unused]] const std::shared_ptr<GTouchable>& gTouchID,
165 [[maybe_unused]] const GTouchableModifiers& gmods);
166
176 [[nodiscard]] std::unique_ptr<GTrueInfoData> collectTrueInformation(GHit* ghit, size_t hitn) {
178 log->info(2, "GDynamicDigitization::collect true information for hit number ", hitn, " with size ", ghit->nsteps(), " steps");
179 return collectTrueInformationImpl(ghit, hitn);
180 }
181
182 [[nodiscard]] virtual std::unique_ptr<GTrueInfoData> collectTrueInformationImpl(GHit* ghit, size_t hitn);
183
191 [[nodiscard]] std::unique_ptr<GDigitizedData> digitizeHit(GHit* ghit, [[maybe_unused]] size_t hitn) {
193 log->info(2, "GDynamicDigitization::digitize hit number ", hitn, " with size ", ghit->nsteps(), " steps");
194 return digitizeHitImpl(ghit, hitn);
195 }
196
197 [[nodiscard]] virtual std::unique_ptr<GDigitizedData> digitizeHitImpl([[maybe_unused]] GHit* ghit, [[maybe_unused]] size_t hitn) { return nullptr; }
198
206 [[nodiscard]] bool loadConstants([[maybe_unused]] int runno, [[maybe_unused]] std::string const& variation) {
208 log->debug(NORMAL, "GDynamicDigitization::load constants");
209 return loadConstantsImpl(runno, variation);
210 }
211
212 virtual bool loadConstantsImpl([[maybe_unused]] int runno, [[maybe_unused]] std::string const& variation) { return true; }
213
221 [[nodiscard]] bool loadTT([[maybe_unused]] int runno, [[maybe_unused]] std::string const& variation) {
223 log->debug(NORMAL, "GDynamicDigitization::load Translation Table for run ", runno, " with variation ", variation);
224 return loadTTImpl(runno, variation);
225 }
226
227 virtual bool loadTTImpl([[maybe_unused]] int runno, [[maybe_unused]] std::string const& variation) { return true; }
228
241 void chargeAndTimeAtHardware(int time, int q, const GHit* ghit, GDigitizedData& gdata);
242
250 [[nodiscard]] bool defineReadoutSpecs() {
252 log->debug(NORMAL, "GDynamicDigitization::define readout specs");
253 return defineReadoutSpecsImpl();
254 };
255 virtual bool defineReadoutSpecsImpl() = 0;
256
258 std::shared_ptr<const GReadoutSpecs> readoutSpecs;
259 std::shared_ptr<const GTranslationTable> translationTable;
260
267 // static GDynamicDigitization* instantiate(const dlhandle handle) {
268 // if (handle == nullptr) return nullptr;
269 // // Must match the extern "C" declaration in the derived factories.
270 // void* maker = dlsym(handle, "GDynamicDigitizationFactory");
271 // if (maker == nullptr) return nullptr;
272 // typedef GDynamicDigitization*(*fptr)();
273 // // Use reinterpret_cast as required.
274 // fptr func = reinterpret_cast<fptr>(reinterpret_cast<void*>(maker));
275 // return func();
276 // }
277
278 static GDynamicDigitization* instantiate(const dlhandle h, std::shared_ptr<GOptions> g) {
279 if (!h) return nullptr;
280 using fptr = GDynamicDigitization* (*)(std::shared_ptr<GOptions>);
281
282 // Must match the extern "C" declaration in the derived factories.
283 auto sym = dlsym(h, "GDynamicDigitizationFactory");
284 if (!sym) return nullptr;
285
286 auto func = reinterpret_cast<fptr>(sym);
287 return func(g);
288 }
289
290
291 // decides if the hit should be processed or not
292 virtual bool decisionToSkipHit(double energy) {
293 if (energy == 0 && !recordZeroEdep) { return true; }
294
295 return false;
296 }
297
298
305 // TODO: REMOVE THIS EVERYWHERE also remove check_if_log_defined
306 void set_loggers(const std::shared_ptr<GOptions>& g) { gopts = g; }
307
308private:
309 bool recordZeroEdep = false;
310
311protected:
313 std::shared_ptr<GOptions> gopts;
314
320 void check_if_log_defined() const {
321 if (gopts == nullptr) {
322 std::cerr << KRED <<
323 "Fatal Error: GDynamicDigitization: goption is not set for this plugin or one of the loggers is null."
324 << std::endl;
325 std::cerr << "The set_loggers function needs to be called." << std::endl;
326 std::cerr << "For example: dynamicRoutines[\"ctof\"]->set_loggers(gopts);" << std::endl;
327 std::cerr << RST << std::endl;
328 exit(1);
329 }
330 }
331};
332
333
335
336using dRoutinesMap = std::unordered_map<std::string, std::shared_ptr<GDynamicDigitization>>;
337
338inline std::shared_ptr<GDynamicDigitization> load_dynamicRoutine(const std::string& plugin_name, const std::shared_ptr<GOptions>& gopts) {
339 GManager manager(gopts);
340 return manager.LoadAndRegisterObjectFromLibrary<GDynamicDigitization>(plugin_name, gopts);
341}
342
343// the returned map is shared and immutable
344inline std::shared_ptr<const dRoutinesMap> dynamicRoutinesMap(const std::vector<std::string>& plugin_names, const std::shared_ptr<GOptions>& gopts) {
345 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, GDIGITIZATION_LOGGER);
346 GManager manager(gopts);
347
348 auto routines = std::make_shared<dRoutinesMap>();
349
350 for (const auto& plugin : plugin_names) {
351 routines->emplace(plugin, manager.LoadAndRegisterObjectFromLibrary<GDynamicDigitization>(plugin, gopts));
352 log->info(0, "dynamicRoutinesMap[", plugin, "]: ", (*routines)[plugin]);
353 }
354
355 return routines; // shared_ptr<const ...> prevents mutation
356}
357
358
359} // namespace gdynamicdigitization
Abstract base class for dynamic digitization functionality.
std::shared_ptr< GOptions > gopts
Optional pointer to GOptions.
virtual ~GDynamicDigitization()=default
Virtual destructor.
virtual bool defineReadoutSpecsImpl()=0
bool loadConstants(int runno, std::string const &variation)
Loads digitization constants.
virtual std::unique_ptr< GTrueInfoData > collectTrueInformationImpl(GHit *ghit, size_t hitn)
Collects true hit information from a GHit.
GDynamicDigitization(const std::shared_ptr< GOptions > &g)
bool loadTT(int runno, std::string const &variation)
Loads the translation table.
double processStepTime(const std::shared_ptr< GTouchable > &gTouchID, G4Step *thisStep)
Processes the step time.
virtual std::vector< std::shared_ptr< GTouchable > > processGTouchableModifiersImpl(const std::shared_ptr< GTouchable > &gTouchID, const GTouchableModifiers &gmods)
Default implementation for processing touchable modifiers.
void check_if_log_defined() const
Checks that all required loggers and options are defined.
virtual double processStepTimeImpl(const std::shared_ptr< GTouchable > &gTouchID, G4Step *thisStep)
Default implementation of processStepTime.
std::unique_ptr< GTrueInfoData > collectTrueInformation(GHit *ghit, size_t hitn)
Collects true hit information into a GTrueInfoData object.
std::shared_ptr< const GTranslationTable > translationTable
std::vector< std::shared_ptr< GTouchable > > processTouchable(std::shared_ptr< GTouchable > gtouchable, G4Step *thisStep)
Processes a GTouchable based on a G4Step.
static GDynamicDigitization * instantiate(const dlhandle h, std::shared_ptr< GOptions > g)
Dynamically instantiates a GDynamicDigitization object from a dynamic library.
virtual std::vector< std::shared_ptr< GTouchable > > processTouchableImpl(std::shared_ptr< GTouchable > gtouchable, G4Step *thisStep)
Processes a GTouchable based on the current G4Step.
std::shared_ptr< const GReadoutSpecs > readoutSpecs
After init, we never mutate these:
virtual bool loadTTImpl(int runno, std::string const &variation)
void set_loggers(const std::shared_ptr< GOptions > &g)
Sets the loggers for the digitization process.
virtual std::unique_ptr< GDigitizedData > digitizeHitImpl(GHit *ghit, size_t hitn)
virtual bool decisionToSkipHit(double energy)
std::unique_ptr< GDigitizedData > digitizeHit(GHit *ghit, size_t hitn)
Digitizes hit information into a GDigitizedData object.
void chargeAndTimeAtHardware(int time, int q, const GHit *ghit, GDigitizedData &gdata)
Sets hardware-level charge and time information in the digitized data.
virtual bool loadConstantsImpl(int runno, std::string const &variation)
std::vector< std::shared_ptr< GTouchable > > processGTouchableModifiers(const std::shared_ptr< GTouchable > &gTouchID, const GTouchableModifiers &gmods)
Processes touchable modifiers.
bool defineReadoutSpecs()
Pure virtual function to initialize readout specifications.
Class to manage modifications to a GTouchable using defined modifier weights.
void insertIdAndWeight(const std::string &touchableName, int idValue, double weight)
Inserts a new (id, weight) pair for given touchable.
std::vector< double > getModifierWeightsVector(const std::string &touchableName)
Gets the modifier weights vector for a given touchable.
std::vector< double > getModifierWeightsAndTimeVector(const std::string &touchableName)
Gets the modifier weights and time vector for a given touchable.
bool isWeightsOnly() const
Checks whether only weight modifiers (without time) are defined.
void insertIdWeightAndTime(const std::string &touchableName, int idValue, double weight, double time)
Inserts a new (id, weight, time) triplet for given touchable.
void assignOverallWeight(const std::string &touchableName, double totalWeight)
Normalizes the modifier weights using a total weight.
GTouchableModifiers(const std::vector< std::string > &touchableNames)
Constructs GTouchableModifiers with a list of touchable names.
Defines the options for the GDynamicDigitization module.
constexpr const char * GDIGITIZATION_LOGGER
std::shared_ptr< GDynamicDigitization > load_dynamicRoutine(const std::string &plugin_name, const std::shared_ptr< GOptions > &gopts)
std::shared_ptr< const dRoutinesMap > dynamicRoutinesMap(const std::vector< std::string > &plugin_names, const std::shared_ptr< GOptions > &gopts)
std::unordered_map< std::string, std::shared_ptr< GDynamicDigitization > > dRoutinesMap
const std::string plugin_name