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
11// c++
12#include <unordered_map>
13
19
27
33constexpr int ERR_DYNAMICPLUGINNOTFOUND = 2001;
34
38constexpr int ERR_HITNOTFOUNDINCOLLECTION = 2002;
39
43constexpr int ERR_NOCOLLECTION = 2003;
44
50constexpr const char* GSENSITIVE_LOGGER = "gsd";
51
53
55
66
67} // namespace gsensitivedetector
68
76using GHitsCollection = G4THitsCollection<GHit>;
77
102class GSensitiveDetector : public GBase<GSensitiveDetector>, public G4VSensitiveDetector {
103
104public:
114 GSensitiveDetector(const std::string& sdName,
115 const std::shared_ptr<GOptions>& goptions);
116
125 void Initialize(G4HCofThisEvent* g4hc) override;
126
139 G4bool ProcessHits(G4Step* thisStep, G4TouchableHistory* g4th) override;
140
149 void EndOfEvent(G4HCofThisEvent* g4HitCollection) override;
150
158 void assign_digi_routine(std::shared_ptr<GDynamicDigitization> digi_routine) { digitization_routine = digi_routine; }
159
167 void resetTouchableMap() { gTouchableMap.clear(); }
168
169private:
178 std::shared_ptr<GDynamicDigitization> digitization_routine;
179
186 std::map<std::string, std::shared_ptr<GTouchable>> gTouchableMap;
187
197 inline std::shared_ptr<GTouchable> getGTouchable(const G4Step* thisStep) {
198 std::string vname = thisStep->GetPreStepPoint()->GetTouchable()->GetVolume()->GetName();
199
200 auto it = gTouchableMap.find(vname);
201 // Return a per-step copy: ProcessHits and processTouchable mutate trackId, pid and the
202 // time-cell index, so the registry entry must stay pristine across steps and events.
203 if (it != gTouchableMap.end()) { return std::make_shared<GTouchable>(*it->second); }
204 // If not found, log an error. The calling code assumes a valid pointer.
205 log->error(ERR_DYNAMICPLUGINNOTFOUND, "GTouchable for volume " + vname + " not found in gTouchableMap");
206 }
207
215 std::unordered_map<std::string, GHit*> hitsByCellKey;
216
223 GHitsCollection* gHitsCollection;
224
225public:
235 inline void registerGVolumeTouchable(const std::string& name, std::shared_ptr<GTouchable> gt) {
236 log->info(2, "Registering touchable gvolume <" + name + "> with value: " + gt->getIdentityString());
237
238 // Store the GTouchable in the map; this module retains a shared ownership reference.
239 gTouchableMap[name] = gt;
240 }
241};
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
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:95
void resetTouchableMap()
Clears the volume-to-touchable map so a reused SD reflects the current geometry.
Definition gsd.h:167
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:158
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:235
G4THitsCollection< GHit > GHitsCollection
GOptions defineOptions()
Defines the module options for GSensitiveDetector.
Definition gsd.h:65
G4THitsCollection< GHit > GHitsCollection
Convenience alias for the Geant4 hits collection used by this module.
Definition gsd.h:76
constexpr int ERR_NOCOLLECTION
Error code used when the Geant4 hits collection is unexpectedly missing.
Definition gsd.h:43
constexpr int ERR_DYNAMICPLUGINNOTFOUND
Error code used when a required dynamic plugin-dependent resource is missing.
Definition gsd.h:33
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:38
constexpr const char * GSENSITIVE_LOGGER
Logger name used by this module.
Definition gsd.h:50