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};
19
20// MT definitions, as from:
21// https://twiki.cern.ch/twiki/bin/view/Geant4/QuickMigrationGuideForGeant4V10
22G4ThreadLocal G4Allocator<GHit>* GHitAllocator = nullptr;
23
24GHit::GHit(std::shared_ptr<GTouchable> gt,
25 const HitBitSet hbs,
26 const G4Step* thisStep,
27 const string& cScheme) :
28 G4VHit(),
29 colorSchema(cScheme),
30 gtouchable(gt) {
31
32 // initialize quantities based on HitBitSet, like globalPositions
33 if (thisStep) { addHitInfosForBitset(hbs, thisStep); }
34
35 // unitialized quantities, to be calculated at the end of the steps by collectTrueInformation
36 // bit 0: always there
37 averageTime = UNINITIALIZEDNUMBERQUANTITY;
38 avgGlobalPosition = G4ThreeVector(UNINITIALIZEDNUMBERQUANTITY, UNINITIALIZEDNUMBERQUANTITY, UNINITIALIZEDNUMBERQUANTITY);
39 avgLocalPosition = G4ThreeVector(UNINITIALIZEDNUMBERQUANTITY, UNINITIALIZEDNUMBERQUANTITY, UNINITIALIZEDNUMBERQUANTITY);
40 processName = UNINITIALIZEDSTRINGQUANTITY;
41}
42
43bool GHit::is_same_hit(const GHit* hit) const {
44 if (!hit) // guard against nullptr
45 return false;
46
47 return *gtouchable == *(hit->getGTouchable());
48}
49
50vector<int> GHit::getTTID() const {
51 vector<int> ttid;
52 // Retrieve the identity vector from the associated GTouchable.
53 vector<GIdentifier> gids = getGID();
54 ttid.reserve(gids.size());
55 for (auto& gid : gids) {
56 // Push back the integer value of each identifier.
57 ttid.push_back(gid.getValue());
58 }
59 return ttid;
60}
61
62
70void GHit::Draw() {
71 auto visManager = G4VVisManager::GetConcreteInstance();
72 if (!visManager) return;
73
74 // only care about schema if we are interactive
75 setColorSchema();
76
77 // Check that globalPositions is not empty before accessing the first element.
78 if (globalPositions.empty()) return;
79
80 G4Circle circle(globalPositions[0]);
81 circle.SetFillStyle(G4Circle::filled);
82
83
84 double etot = getTotalEnergyDeposited();
85
86 if (etot > 0) {
87 circle.SetScreenDiameter(10);
88 circle.SetVisAttributes(G4VisAttributes(colour_hit));
89 circle.SetFillStyle(G4Circle::filled);
90 }
91 else if (etot == 0) {
92 circle.SetScreenSize(8);
93 circle.SetVisAttributes(G4VisAttributes(colour_passby));
94 circle.SetFillStyle(G4Circle::hashed);
95 }
96
97 visManager->Draw(circle);
98}
99
100
107bool GHit::setColorSchema() {
108 // For now, hard-code the color schema.
109 colour_hit = G4Colour(1.0, 0.0, 0.0); // Red for hits with energy.
110 colour_passby = G4Colour(0.0, 1.0, 0.0); // Green for pass-by.
111 return false;
112}
113
114
116 // This function is for testing purposes only.
117 // It randomizes the hit's global position and energy deposition.
118 // It should not be used in production code.
119 for (int i = 0; i < nsteps + 1; ++i) {
120 globalPositions.emplace_back(G4UniformRand() * 100, G4UniformRand() * 100, G4UniformRand() * 100);
121 localPositions.emplace_back(G4UniformRand() * 10, G4UniformRand() * 10, G4UniformRand() * 10);
122 times.emplace_back(G4UniformRand() * 100);
123 edeps.emplace_back(G4UniformRand() * 10);
124 Es.emplace_back(G4UniformRand() * 10);
125
126 pids.emplace_back(static_cast<int>(G4UniformRand() * 1000)); // Random particle ID
127 }
128}
Represents a hit in the detector.
Definition ghit.h:30
GHit(std::shared_ptr< GTouchable > gt, HitBitSet hbs, const G4Step *thisStep=nullptr, const std::string &cScheme="default")
Constructor for GHit.
Definition ghit.cc:24
void addHitInfosForBitset(HitBitSet hbs, const G4Step *thisStep)
Adds hit information based on a HitBitSet.
size_t nsteps() const
Definition ghit.h:159
std::vector< GIdentifier > getGID() const
Returns the detector element identity.
Definition ghit.h:178
std::vector< int > getTTID() const
Returns the touchable identity values as integers.
Definition ghit.cc:50
bool is_same_hit(const GHit *hit) const
Compare two ghits.
Definition ghit.cc:43
void randomizeHitForTesting(int nsteps)
Definition ghit.cc:115
void Draw() override
Draws the hit using Geant4 visualization.
Definition ghit.cc:70
double getTotalEnergyDeposited()
Computes the total energy deposited.
std::shared_ptr< GTouchable > getGTouchable() const
Returns a const reference to the shared_ptr managing the GTouchable.
Definition ghit.h:172
std::bitset< NHITBITS > HitBitSet
G4ThreadLocal G4Allocator< GHit > * GHitAllocator
Definition ghit.cc:22