ghit
Loading...
Searching...
No Matches
ghit.h
Go to the documentation of this file.
1#pragma once
2
3// geant4
4#include "G4VHit.hh"
5#include "G4THitsCollection.hh"
6#include "G4Allocator.hh"
7
8#include "G4ThreeVector.hh"
9#include "G4Step.hh"
10#include "G4Colour.hh"
11
12// gemc
13#include <gemc/gtouchable/gtouchable.h>
14
15// c++
16#include <optional>
17#include <atomic>
18#include <map>
19
37class GHit : public G4VHit
38{
39public:
51 GHit(std::shared_ptr<GTouchable> gt, const G4Step* thisStep = nullptr,
52 const std::string& cScheme = "default");
53
57 ~GHit() override = default;
58
65 inline void* operator new(size_t);
66
70 inline void operator delete(void*);
71
81 void Draw() override;
82
92 [[nodiscard]] bool is_same_hit(const GHit* hit) const;
93
94private:
104 G4Colour colour_touch, colour_hit, colour_passby;
105
111 bool setColorSchema();
112
114 std::string colorSchema;
115
125 std::shared_ptr<GTouchable> gtouchable;
126
127 // -------------------------------------------------------------------------
128 // Per-step data (vectors)
129 // -------------------------------------------------------------------------
130
137 std::vector<double> edeps;
138
144 std::vector<double> times;
145
151 std::vector<G4ThreeVector> globalPositions;
152
158 std::vector<G4ThreeVector> localPositions;
159
165 std::vector<G4ThreeVector> trackVertexPositions;
166
174 std::vector<G4ThreeVector> motherTrackVertexPositions;
175
179 std::vector<int> pids;
180
184 std::vector<int> tids;
185
191 std::vector<int> motherTids;
192
199 std::vector<std::string> processNames;
200
207 std::vector<G4ThreeVector> momenta;
208
214 std::vector<double> trackEs;
215
222 std::vector<int> motherPids;
223
230 static thread_local std::map<int, int> pdgById;
231
232 // -------------------------------------------------------------------------
233 // Aggregated / calculated quantities (lazy)
234 // -------------------------------------------------------------------------
235
241 std::optional<double> totalEnergyDeposited;
242
243
252 double averageTime;
253
259 G4ThreeVector avgGlobalPosition;
260
266 G4ThreeVector avgLocalPosition;
267
273 std::string processName;
274
275
279 static std::atomic<int> globalHitCounter;
280
288 static thread_local std::map<int, G4ThreeVector> trackVertexById;
289
290public:
291 // -------------------------------------------------------------------------
292 // Inline accessors (returning copies by design)
293 // -------------------------------------------------------------------------
294
299 [[nodiscard]] inline std::vector<double> getEdeps() const { return edeps; }
300
305 [[nodiscard]] inline std::vector<double> getTimes() const { return times; }
306
311 [[nodiscard]] inline std::vector<G4ThreeVector> getGlobalPositions() const { return globalPositions; }
312
317 [[nodiscard]] inline std::vector<G4ThreeVector> getLocalPositions() const { return localPositions; }
318
323 [[nodiscard]] inline std::vector<G4ThreeVector> getTrackVertexPositions() const { return trackVertexPositions; }
324
331 [[nodiscard]] inline G4ThreeVector getTrackVertexPosition() const { return trackVertexPositions.front(); }
332
337 [[nodiscard]] inline std::vector<G4ThreeVector> getMotherTrackVertexPositions() const {
338 return motherTrackVertexPositions;
339 }
340
347 [[nodiscard]] inline G4ThreeVector getMotherTrackVertexPosition() const {
348 return motherTrackVertexPositions.front();
349 }
350
355 [[nodiscard]] inline std::vector<int> getPids() const { return pids; }
356
363 [[nodiscard]] inline int getPid() const { return pids.front(); }
364
369 [[nodiscard]] inline std::vector<int> getTids() const { return tids; }
370
377 [[nodiscard]] inline int getTid() const { return tids.front(); }
378
383 [[nodiscard]] inline std::vector<int> getMotherTids() const { return motherTids; }
384
391 [[nodiscard]] inline int getMotherTid() const { return motherTids.front(); }
392
401 [[nodiscard]] inline double getE() const { return trackEs.front(); }
402
407 [[nodiscard]] inline size_t nsteps() const { return edeps.size(); }
408
413 [[nodiscard]] inline size_t getStepCount() const { return edeps.size(); }
414
419 [[nodiscard]] inline std::vector<G4ThreeVector> getMomenta() const { return momenta; }
420
427 [[nodiscard]] inline G4ThreeVector getMomentum() const { return momenta.front(); }
428
433 [[nodiscard]] inline std::vector<double> getTrackEs() const { return trackEs; }
434
441 [[nodiscard]] inline double getTrackE() const { return trackEs.front(); }
442
447 [[nodiscard]] inline std::vector<int> getMotherPids() const { return motherPids; }
448
455 [[nodiscard]] inline int getMpid() const { return motherPids.front(); }
456
463 [[nodiscard]] inline std::string getProcID() const { return processName; }
464
469 [[nodiscard]] inline std::string getProcessName() const { return processName; }
470
475 [[nodiscard]] inline std::shared_ptr<GTouchable> getGTouchable() const { return gtouchable; }
476
483 [[nodiscard]] inline std::vector<GIdentifier> getGID() const { return gtouchable->getIdentity(); }
484
491 [[nodiscard]] inline std::vector<double> getDetectorDimensions() const {
492 return gtouchable->getDetectorDimensions();
493 }
494
499 [[nodiscard]] inline double getMass() const { return gtouchable->getMass(); }
500
501 // -------------------------------------------------------------------------
502 // Aggregation / calculation API
503 // -------------------------------------------------------------------------
504
511 void calculateInfos();
512
520
528 double getAverageTime();
529
534 G4ThreeVector getAvgLocalPosition();
535
542 G4ThreeVector getAvgGlobaPosition();
543
544 // -------------------------------------------------------------------------
545 // Hit filling / testing helpers
546 // -------------------------------------------------------------------------
547
556 void addHitInfos(const G4Step* thisStep);
557
563 static void clearTrackVertexCache();
564
575
583 [[nodiscard]] std::vector<int> getTTID() const;
584
596 static GHit* create(const std::shared_ptr<GOptions>& gopts) {
597 auto gt = GTouchable::create(gopts);
598 auto hit = new GHit(gt);
599 // Randomize between 1 and 10 steps in a deterministic, thread-safe manner.
600 hit->randomizeHitForTesting(1 + globalHitCounter.fetch_add(1, std::memory_order_relaxed) % 10);
601 return hit;
602 }
603};
604
605// MT definitions, as from:
606// https://twiki.cern.ch/twiki/bin/view/Geant4/QuickMigrationGuideForGeant4V10
607extern G4ThreadLocal G4Allocator<GHit>* GHitAllocator;
608using GHitsCollection = G4THitsCollection<GHit>;
609
610inline void* GHit::operator new(size_t) {
611 if (!GHitAllocator) GHitAllocator = new G4Allocator<GHit>;
612 return (void*)GHitAllocator->MallocSingle();
613}
614
615inline void GHit::operator delete(void* hit) {
616 if (!GHitAllocator) { GHitAllocator = new G4Allocator<GHit>; }
617
618 GHitAllocator->FreeSingle((GHit*)hit);
619}
620
621[[nodiscard]] inline std::string getIdentityString(std::vector<GIdentifier> gidentity) {
622 // Build a compact label from the stored identifier vector.
623 std::string identifierString;
624 for (size_t i = 0; i < gidentity.size() - 1; i++) {
625 identifierString += gidentity[i].getName() + "->" + std::to_string(gidentity[i].getValue()) + ", ";
626 }
627 identifierString += gidentity.back().getName() + "->" + std::to_string(gidentity.back().getValue());
628 return identifierString;
629}
630
631[[nodiscard]] inline std::map<std::string, int> getIdentityMap(std::vector<GIdentifier> gidentity) {
632 std::map<std::string, int> identityMap;
633 for (auto& id : gidentity) {
634 identityMap[id.getName()] = id.getValue();
635 }
636 return identityMap;
637}
Stores step-by-step and aggregated information for a detector hit.
Definition ghit.h:38
size_t getStepCount() const
Number of recorded steps (same as nsteps()).
Definition ghit.h:413
std::vector< int > getPids() const
Get per-step particle PDG encodings (when enabled).
Definition ghit.h:355
int getPid() const
Convenience accessor for the first particle ID.
Definition ghit.h:363
static void clearTrackVertexCache()
Clear the per-thread track vertex cache.
Definition ghit.cc:98
G4ThreeVector getMomentum() const
Convenience accessor for the first step 3-momentum.
Definition ghit.h:427
std::vector< int > getMotherTids() const
Get per-step mother track IDs.
Definition ghit.h:383
std::vector< G4ThreeVector > getMotherTrackVertexPositions() const
Get per-step mother-track vertex positions.
Definition ghit.h:337
size_t nsteps() const
Number of recorded steps.
Definition ghit.h:407
G4ThreeVector getMotherTrackVertexPosition() const
Convenience accessor for the first mother-track vertex position.
Definition ghit.h:347
int getMotherTid() const
Convenience accessor for the first mother track ID.
Definition ghit.h:391
std::string getProcID() const
Get the representative creator process name for this hit.
Definition ghit.h:463
std::vector< double > getTimes() const
Get per-step global times.
Definition ghit.h:305
std::vector< double > getTrackEs() const
Get per-step track total energies (always present).
Definition ghit.h:433
std::vector< GIdentifier > getGID() const
Get the detector element identity.
Definition ghit.h:483
~GHit() override=default
Destructor.
static GHit * create(const std::shared_ptr< GOptions > &gopts)
Create a fake hit for testing, using the current options.
Definition ghit.h:596
std::vector< G4ThreeVector > getLocalPositions() const
Get per-step local positions.
Definition ghit.h:317
std::vector< int > getTTID() const
Get the touchable identity values as integers.
Definition ghit.cc:51
bool is_same_hit(const GHit *hit) const
Compare this hit against another hit by sensitive-element identity.
Definition ghit.cc:44
void randomizeHitForTesting(int nsteps)
Randomize internal vectors for test-only usage.
Definition ghit.cc:103
std::vector< G4ThreeVector > getTrackVertexPositions() const
Get per-step current-track vertex positions.
Definition ghit.h:323
GHit(std::shared_ptr< GTouchable > gt, const G4Step *thisStep=nullptr, const std::string &cScheme="default")
Construct a hit container and optionally seed it from a step.
Definition ghit.cc:26
void calculateInfos()
Compute and cache derived hit quantities.
std::vector< double > getDetectorDimensions() const
Get the sensitive-element dimensions.
Definition ghit.h:491
std::vector< int > getMotherPids() const
Get per-step mother particle PDG encodings (always present).
Definition ghit.h:447
void Draw() override
Visualize the hit using Geant4 visualization primitives.
Definition ghit.cc:63
int getMpid() const
Convenience accessor for the first step mother particle PDG encoding.
Definition ghit.h:455
std::vector< G4ThreeVector > getMomenta() const
Get per-step track 3-momenta (always present).
Definition ghit.h:419
G4ThreeVector getAvgGlobaPosition()
Get the average global position of the hit.
std::vector< double > getEdeps() const
Get per-step energy depositions.
Definition ghit.h:299
double getTotalEnergyDeposited()
Get the total deposited energy across all recorded steps.
std::shared_ptr< GTouchable > getGTouchable() const
Get the associated sensitive-element descriptor.
Definition ghit.h:475
double getAverageTime()
Get the average time associated with the hit.
double getTrackE() const
Convenience accessor for the first step track total energy.
Definition ghit.h:441
double getMass() const
Get the sensitive element mass.
Definition ghit.h:499
G4ThreeVector getTrackVertexPosition() const
Convenience accessor for the first current-track vertex position.
Definition ghit.h:331
G4ThreeVector getAvgLocalPosition()
Get the average local position of the hit.
int getTid() const
Convenience accessor for the first track ID.
Definition ghit.h:377
double getE() const
Convenience accessor for the first step track total energy.
Definition ghit.h:401
std::vector< G4ThreeVector > getGlobalPositions() const
Get per-step global positions.
Definition ghit.h:311
std::string getProcessName() const
Get the representative creator process name for the hit.
Definition ghit.h:469
void addHitInfos(const G4Step *thisStep)
Append per-step information from a G4Step.
std::vector< int > getTids() const
Get per-step particle track id (when enabled).
Definition ghit.h:369
static std::shared_ptr< GTouchable > create(const std::shared_ptr< GOptions > &gopt)
std::string getIdentityString(std::vector< GIdentifier > gidentity)
Definition ghit.h:621
G4ThreadLocal G4Allocator< GHit > * GHitAllocator
Definition ghit.cc:22
std::map< std::string, int > getIdentityMap(std::vector< GIdentifier > gidentity)
Definition ghit.h:631
G4THitsCollection< GHit > GHitsCollection