gsd
Loading...
Searching...
No Matches
gsd.h
Go to the documentation of this file.
1#pragma once
2
3// geant4
4#include "G4VSensitiveDetector.hh"
5
6// gemc
7#include <gemc/goptions/goptions.h>
8#include <gemc/gdynamicDigitization/gdynamicdigitization.h>
9#include <gemc/gbase/gbase.h>
10
30constexpr int ERR_DYNAMICPLUGINNOTFOUND = 2001;
31
35constexpr int ERR_HITNOTFOUNDINCOLLECTION = 2002;
36
40constexpr int ERR_NOCOLLECTION = 2003;
41
47constexpr const char* GSENSITIVE_LOGGER = "gsd";
48
52
63
64} // namespace gsensitivedetector
65
73using GHitsCollection = G4THitsCollection<GHit>;
74
99class GSensitiveDetector : public GBase<GSensitiveDetector>, public G4VSensitiveDetector {
100
101public:
111 GSensitiveDetector(const std::string& sdName,
112 const std::shared_ptr<GOptions>& goptions);
113
122 void Initialize(G4HCofThisEvent* g4hc) override;
123
136 G4bool ProcessHits(G4Step* thisStep, G4TouchableHistory* g4th) override;
137
146 void EndOfEvent(G4HCofThisEvent* g4HitCollection) override;
147
155 void assign_digi_routine(std::shared_ptr<GDynamicDigitization> digi_routine) { digitization_routine = digi_routine; }
156
164 void resetTouchableMap() { gTouchableMap.clear(); }
165
166private:
175 std::shared_ptr<GDynamicDigitization> digitization_routine;
176
183 std::map<std::string, std::shared_ptr<GTouchable>> gTouchableMap;
184
194 inline std::shared_ptr<GTouchable> getGTouchable(const G4Step* thisStep) {
195 std::string vname = thisStep->GetPreStepPoint()->GetTouchable()->GetVolume()->GetName();
196
197 auto it = gTouchableMap.find(vname);
198 // Return a per-step copy: ProcessHits and processTouchable mutate trackId, pid and the
199 // time-cell index, so the registry entry must stay pristine across steps and events.
200 if (it != gTouchableMap.end()) { return std::make_shared<GTouchable>(*it->second); }
201 // If not found, log an error. The calling code assumes a valid pointer.
202 log->error(ERR_DYNAMICPLUGINNOTFOUND, "GTouchable for volume " + vname + " not found in gTouchableMap");
203 }
204
211 std::vector<GTouchable> touchableVector;
212
222 bool isThisANewTouchable(const std::shared_ptr<GTouchable>& thisTouchable);
223
230 GHitsCollection* gHitsCollection;
231
240 GHit* getHitInHitCollectionUsingTouchable(const std::shared_ptr<GTouchable>& gtouchable);
241
242public:
252 inline void registerGVolumeTouchable(const std::string& name, std::shared_ptr<GTouchable> gt) {
253 log->info(2, "Registering touchable gvolume <" + name + "> with value: " + gt->getIdentityString());
254
255 // Store the GTouchable in the map; this module retains a shared ownership reference.
256 gTouchableMap[name] = gt;
257 }
258};
std::shared_ptr< GLogger > log
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
Thread-local sensitive detector bridging Geant4 steps to GEMC hits via digitization plugins.
Definition gsd.h:99
GSensitiveDetector(const std::string &sdName, const std::shared_ptr< GOptions > &goptions)
Constructs a sensitive detector instance for a given detector name.
Definition gsd.cc:9
void EndOfEvent(G4HCofThisEvent *g4HitCollection) override
End-of-event hook called by Geant4.
Definition gsd.cc:131
void resetTouchableMap()
Clears the volume-to-touchable map so a reused SD reflects the current geometry.
Definition gsd.h:164
void Initialize(G4HCofThisEvent *g4hc) override
Per-event initialization hook called by Geant4.
Definition gsd.cc:27
void assign_digi_routine(std::shared_ptr< GDynamicDigitization > digi_routine)
Assigns the digitization routine used to interpret steps and define hit content.
Definition gsd.h:155
G4bool ProcessHits(G4Step *thisStep, G4TouchableHistory *g4th) override
Processes a Geant4 step and creates or updates hits in the current hits collection.
Definition gsd.cc:49
void registerGVolumeTouchable(const std::string &name, std::shared_ptr< GTouchable > gt)
Registers a GTouchable for a given gvolume name into the internal lookup map.
Definition gsd.h:252
G4THitsCollection< GHit > GHitsCollection
GOptions defineOptions()
Defines the module options for GSensitiveDetector.
Definition gsd.h:62
constexpr int ERR_NOCOLLECTION
Error code used when the Geant4 hits collection is unexpectedly missing.
Definition gsd.h:40
constexpr int ERR_DYNAMICPLUGINNOTFOUND
Error code used when a required dynamic plugin-dependent resource is missing.
Definition gsd.h:30
constexpr int ERR_HITNOTFOUNDINCOLLECTION
Error code used when a hit is expected to exist but cannot be found in the current hit collection.
Definition gsd.h:35
constexpr const char * GSENSITIVE_LOGGER
Logger name used by this module.
Definition gsd.h:47