gsd
Loading...
Searching...
No Matches
gsd.cc
Go to the documentation of this file.
1// gemc
2#include "gsd.h"
3
4// geant4
5#include "G4SDManager.hh"
6
7// this is thread-local
8GSensitiveDetector::GSensitiveDetector(const std::string& sdName,
9 const std::shared_ptr<GOptions>& goptions) :
11 G4VSensitiveDetector(sdName) {
12 log->info(2, FUNCTION_NAME, " for " + sdName);
13
14 // collectionName is a G4VSensitiveDetector G4CollectionNameVector
15 // not really used in gemc, using it here to mimic the examples
16 std::string hitCollectionName = sdName + "__HitCollection";
17 collectionName.insert(hitCollectionName);
18
19 log->debug(CONSTRUCTOR, FUNCTION_NAME, " for " + sdName);
20}
21
22
23// thread local
24// run at the beginning of the event
25void GSensitiveDetector::Initialize(G4HCofThisEvent* g4hc) {
26 std::string sdName = GetName();
27 log->info(1, FUNCTION_NAME, sdName);
28
29 gHitBitSet = digitization_routine->readoutSpecs->getHitBitSet();
30
31 // clearing touchableVector at the start of the event
32 touchableVector.clear();
33
34 // initializing gHitsCollection collection using the geant4 G4THitsCollection constructor
35 // this uses two arguments (not sure why)
36 gHitsCollection = new GHitsCollection(sdName, collectionName[0]);
37
38 // adding gHitsCollection to the G4HCofThisEvent
39 // hcID is incrememnted by 1 every time we instantiate a new G4THitsCollection
40 // it can then be retrieved at the end of the event
41 auto hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
42 g4hc->AddHitsCollection(hcID, gHitsCollection);
43
44 log->info(2, "Added collection id ", hcID, " to G4HCofThisEvent");
45}
46
47
48// thread local
49G4bool GSensitiveDetector::ProcessHits(G4Step* thisStep, [[maybe_unused]] G4TouchableHistory* g4th) {
50 // if there is a decision to skip this event based on depe, return
51 double depe = thisStep->GetTotalEnergyDeposit();
52 if (digitization_routine->decisionToSkipHit(depe)) { return true; }
53 if (gHitsCollection == nullptr) { log->error(ERR_NOCOLLECTION, "No hit collection found"); }
54
55 // get the vector of GTouchables returned by gDynamicDigitization
56 // if not defined by the plugin, base class will return a vector with one element (the input)
57 std::vector<std::shared_ptr<GTouchable>> thisStepProcessedTouchables = digitization_routine->processTouchable(getGTouchable(thisStep), thisStep);
58
59 auto hcsize = gHitsCollection->GetSize();
60
61 log->info(2, FUNCTION_NAME, " for ", GetName(),
62 " with ", std::to_string(thisStepProcessedTouchables.size()), " touchable(s), edep: ",
63 std::to_string(depe), ", Hit collection size: ", hcsize);
64
65 // if a new touchable is created, create a new GHit
66 // otherwise, set the hit hitbitset
67 for (auto thisGTouchable : thisStepProcessedTouchables) {
68 // assign track id
69 thisGTouchable->assignTrackId(thisStep->GetTrack()->GetTrackID());
70
71 if (isThisANewTouchable(thisGTouchable)) {
72 // new ghit, insert in gHitsCollection
73 // the constructor takes care of the filling of the hit step according to gHitBitSet
74 gHitsCollection->insert(new GHit(thisGTouchable, gHitBitSet, thisStep));
75 }
76 else {
77 // not a new touchable, must be in the hit collection
78 // retrieve it and add information according to gHitBitSet
79 GHit* existingHit = getHitInHitCollectionUsingTouchable(thisGTouchable);
80 existingHit->addHitInfosForBitset(gHitBitSet, thisStep);
81 }
82 }
83
84 return true;
85}
86
87// checking if it is present in the map. If not, add it.
88bool GSensitiveDetector::isThisANewTouchable(const std::shared_ptr<GTouchable>& thisTouchable) {
89 log->info(2, "GSensitiveDetector::isThisANewTouchable for " + GetName(),
90 " with touchable: " + thisTouchable->getIdentityString());
91
92 if (thisTouchable->exists_in_vector(touchableVector)) {
93 log->info(2, " ❌ not a new GTouchable, it is found, retrieving hit...");
94 return false; // not a new touchable
95 }
96 else {
97 log->info(2, " ✅ yes, new GTouchable. Adding it to touchableVector.");
98 touchableVector.push_back(*thisTouchable);
99 return true; // it's a new touchable
100 }
101}
102
103
104GHit* GSensitiveDetector::getHitInHitCollectionUsingTouchable(const std::shared_ptr<GTouchable>& gtouchable) {
105 for (unsigned int i = 0; i < gHitsCollection->GetSize(); i++) {
106 GHit* thisHit = (*gHitsCollection)[i];
107 const std::shared_ptr<GTouchable>& thisHitGTouchable = thisHit->getGTouchable();
108
109 if (*gtouchable == *thisHitGTouchable) {
110 log->info(2, "GSensitiveDetector::getHitInHitCollectionUsingTouchable for " + GetName() +
111 " found existing hit in collection for touchable: " + gtouchable->getIdentityString() +
112 " at index: " + std::to_string(i));
113 return thisHit;
114 }
115 }
116
117 log->error(ERR_HITNOTFOUNDINCOLLECTION, "GHit not found in hit collection for touchable: " + gtouchable->getIdentityString(), "in ", GetName());
118}
119
120
121// thread local
122void GSensitiveDetector::EndOfEvent([[maybe_unused]] G4HCofThisEvent* g4hc) { log->info(1, "GSensitiveDetector::EndOfEvent for " + GetName()); }
std::shared_ptr< GLogger > log
std::vector< std::shared_ptr< GTouchable > > processTouchable(std::shared_ptr< GTouchable > gtouchable, G4Step *thisStep)
std::shared_ptr< const GReadoutSpecs > readoutSpecs
virtual bool decisionToSkipHit(double energy)
void addHitInfosForBitset(HitBitSet hbs, const G4Step *thisStep)
std::shared_ptr< GTouchable > getGTouchable() const
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
HitBitSet getHitBitSet() const
GSensitiveDetector(const std::string &sdName, const std::shared_ptr< GOptions > &goptions)
Definition gsd.cc:8
void EndOfEvent(G4HCofThisEvent *g4HitCollection) override
Definition gsd.cc:122
void Initialize(G4HCofThisEvent *g4hc) override
Definition gsd.cc:25
G4bool ProcessHits(G4Step *thisStep, G4TouchableHistory *g4th) override
Definition gsd.cc:49
#define FUNCTION_NAME
CONSTRUCTOR
constexpr int ERR_NOCOLLECTION
Definition gsd.h:13
G4THitsCollection< GHit > GHitsCollection
Definition gsd.h:21
constexpr int ERR_HITNOTFOUNDINCOLLECTION
Definition gsd.h:12
constexpr const char * GSENSITIVE_LOGGER
Definition gsd.h:14