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