ghit
Loading...
Searching...
No Matches
ghit.cc
Go to the documentation of this file.
1// ghit
2#include "ghit.h"
3
4#include <utility>
5
6// glibrary
7#include "gutsConventions.h"
8
9// geant4
10#include "G4VVisManager.hh"
11#include "G4Circle.hh"
12#include "G4VisAttributes.hh"
13#include "Randomize.hh"
14
15using std::string;
16using std::vector;
17
18std::atomic<int> GHit::globalHitCounter{0};
19thread_local std::map<int, G4ThreeVector> GHit::trackVertexById;
20
21// MT definitions, as from:
22// https://twiki.cern.ch/twiki/bin/view/Geant4/QuickMigrationGuideForGeant4V10
23G4ThreadLocal G4Allocator<GHit>* GHitAllocator = nullptr;
24
25// See header for API docs.
26
27GHit::GHit(std::shared_ptr<GTouchable> gt,
28 const HitBitSet hbs,
29 const G4Step* thisStep,
30 const string& cScheme) :
31 G4VHit(),
32 colorSchema(cScheme),
33 gtouchable(gt) {
34 // Initialize per-step vectors if a step is provided.
35 if (thisStep) { addHitInfosForBitset(hbs, thisStep); }
36
37 // Uninitialized quantities, to be calculated at the end of the steps by collectTrueInformation
38 // bit 0: always there
39 averageTime = UNINITIALIZEDNUMBERQUANTITY;
40 avgGlobalPosition = G4ThreeVector(UNINITIALIZEDNUMBERQUANTITY, UNINITIALIZEDNUMBERQUANTITY,
42 avgLocalPosition = G4ThreeVector(UNINITIALIZEDNUMBERQUANTITY, UNINITIALIZEDNUMBERQUANTITY,
44 processName = UNINITIALIZEDSTRINGQUANTITY;
45}
46
47bool GHit::is_same_hit(const GHit* hit) const {
48 if (!hit) // guard against nullptr
49 return false;
50
51 return *gtouchable == *(hit->getGTouchable());
52}
53
54vector<int> GHit::getTTID() const {
55 vector<int> ttid;
56 // Retrieve the identity vector from the associated GTouchable.
57 vector<GIdentifier> gids = getGID();
58 ttid.reserve(gids.size());
59 for (auto& gid : gids) {
60 // Push back the integer value of each identifier.
61 ttid.push_back(gid.getValue());
62 }
63 return ttid;
64}
65
66void GHit::Draw() {
67 auto visManager = G4VVisManager::GetConcreteInstance();
68 if (!visManager) return;
69
70 // Only care about schema if we are interactive.
71 setColorSchema();
72
73 // Check that globalPositions is not empty before accessing the first element.
74 if (globalPositions.empty()) return;
75
76 G4Circle circle(globalPositions[0]);
77 circle.SetFillStyle(G4Circle::filled);
78
79 double etot = getTotalEnergyDeposited();
80
81 if (etot > 0) {
82 circle.SetScreenSize(50);
83 circle.SetVisAttributes(G4VisAttributes(colour_hit));
84 }
85 else if (etot == 0) {
86 circle.SetScreenSize(15);
87 circle.SetVisAttributes(G4VisAttributes(colour_passby));
88 circle.SetFillStyle(G4Circle::hashed);
89 }
90
91 visManager->Draw(circle);
92}
93
94bool GHit::setColorSchema() {
95 // For now, hard-code the color schema.
96 colour_hit = G4Colour(1.0, 0.0, 0.0); // Red for hits with energy.
97 colour_passby = G4Colour(0.0, 1.0, 0.0); // Green for pass-by.
98 return false;
99}
100
102 trackVertexById.clear();
103}
104
106 // This function is for testing purposes only.
107 // It randomizes the hit's global position and energy deposition.
108 // It should not be used in production code.
109
110 // Generate nsteps+1 entries to preserve the existing behavior exactly.
111 for (int i = 0; i < nsteps + 1; ++i) {
112 globalPositions.emplace_back(G4UniformRand() * 100, G4UniformRand() * 100, G4UniformRand() * 100);
113 localPositions.emplace_back(G4UniformRand() * 10, G4UniformRand() * 10, G4UniformRand() * 10);
114 trackVertexPositions.emplace_back(G4UniformRand() * 100, G4UniformRand() * 100, G4UniformRand() * 100);
115 motherTrackVertexPositions.emplace_back(UNINITIALIZEDNUMBERQUANTITY, UNINITIALIZEDNUMBERQUANTITY,
117 times.emplace_back(G4UniformRand() * 100);
118 edeps.emplace_back(G4UniformRand() * 10);
119 pids.emplace_back(11);
120 tids.emplace_back(i);
121 motherTids.emplace_back(0);
122 Es.emplace_back(G4UniformRand() * 10);
123
124 pids.emplace_back(static_cast<int>(G4UniformRand() * 1000)); // Random particle ID
125 }
126}
Stores step-by-step and aggregated information for a detector hit.
Definition ghit.h:44
GHit(std::shared_ptr< GTouchable > gt, HitBitSet hbs, const G4Step *thisStep=nullptr, const std::string &cScheme="default")
Construct a hit container and optionally seed it from a step.
Definition ghit.cc:27
void addHitInfosForBitset(HitBitSet hbs, const G4Step *thisStep)
Append per-step information from a G4Step according to a bitset.
static void clearTrackVertexCache()
Clear the per-thread track vertex cache.
Definition ghit.cc:101
size_t nsteps() const
Number of recorded steps for the optional-energy vector.
Definition ghit.h:423
std::vector< GIdentifier > getGID() const
Get the detector element identity.
Definition ghit.h:446
std::vector< int > getTTID() const
Get the touchable identity values as integers.
Definition ghit.cc:54
bool is_same_hit(const GHit *hit) const
Compare this hit against another hit by sensitive-element identity.
Definition ghit.cc:47
void randomizeHitForTesting(int nsteps)
Randomize internal vectors for test-only usage.
Definition ghit.cc:105
void Draw() override
Visualize the hit using Geant4 visualization primitives.
Definition ghit.cc:66
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:438
std::bitset< NHITBITS > HitBitSet
Bitset selecting which optional hit information is recorded.
G4ThreadLocal G4Allocator< GHit > * GHitAllocator
Definition ghit.cc:23
#define UNINITIALIZEDNUMBERQUANTITY
#define UNINITIALIZEDSTRINGQUANTITY