REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
Loading...
Searching...
No Matches
TRestGeant4Track.cxx
1
17
18#include "TRestGeant4Track.h"
19
20#include "TRestGeant4Event.h"
21#include "TRestGeant4Metadata.h"
22
23using namespace std;
24
25ClassImp(TRestGeant4Track);
26
27TRestGeant4Track::TRestGeant4Track() = default;
28
29TRestGeant4Track::~TRestGeant4Track() = default;
30
31Int_t TRestGeant4Track::GetProcessID(const TString& processName) const {
32 const TRestGeant4Metadata* metadata = GetGeant4Metadata();
33 if (metadata != nullptr) {
34 const auto processID = metadata->GetGeant4PhysicsInfo().GetProcessID(processName);
35 if (processID != Int_t{}) {
36 return processID;
37 }
38 }
39
40 cout << "WARNING : The process " << processName << " was not found" << endl;
41 return -1;
42}
43
44TString TRestGeant4Track::GetProcessName(Int_t processID) const {
45 const TRestGeant4Metadata* metadata = GetGeant4Metadata();
46 if (metadata != nullptr) {
47 const auto& processName = metadata->GetGeant4PhysicsInfo().GetProcessName(processID);
48 if (processName != TString{}) {
49 return processName;
50 }
51 }
52
53 cout << "WARNING : The process " << processID << " was not found" << endl;
54
55 return "";
56}
57
58EColor TRestGeant4Track::GetParticleColor() const {
59 EColor color = kGray;
60
61 if (GetParticleName() == "e-")
62 color = kRed;
63 else if (GetParticleName() == "e+")
64 color = kBlue;
65 else if (GetParticleName() == "alpha")
66 color = kOrange;
67 else if (GetParticleName() == "mu-")
68 color = kViolet;
69 else if (GetParticleName() == "gamma")
70 color = kGreen;
71 else
72 cout << "TRestGeant4Track::GetParticleColor. Particle NOT found! Returning "
73 "gray color."
74 << endl;
75
76 return color;
77}
78
84size_t TRestGeant4Track::GetNumberOfHits(Int_t volID) const {
85 size_t numberOfHits = 0;
86 for (unsigned int n = 0; n < fHits.GetNumberOfHits(); n++) {
87 if (volID != -1 && fHits.GetVolumeId(n) != volID) {
88 continue;
89 }
90 numberOfHits++;
91 }
92 return numberOfHits;
93}
94
101 size_t numberOfHits = 0;
102 for (unsigned int n = 0; n < fHits.GetNumberOfHits(); n++) {
103 if (volID != -1 && fHits.GetVolumeId(n) != volID) {
104 continue;
105 }
106 if (fHits.GetEnergy(n) <= 0) {
107 continue;
108 }
109 numberOfHits++;
110 }
111 return numberOfHits;
112}
113
114void TRestGeant4Track::PrintTrack(size_t maxHits) const {
115 cout
116 << " * TrackID: " << fTrackID << " - Particle: " << fParticleName << " - ParentID: " << fParentID
117 << ""
118 << (GetParentTrack() != nullptr
119 ? TString::Format(" - Parent particle: %s", GetParentTrack()->GetParticleName().Data()).Data()
120 : "")
121 << " - Created by '" << fCreatorProcess
122 << "' with initial "
123 "KE of "
124 << ToEnergyString(fInitialKineticEnergy) << "" << endl;
125
126 cout << " Initial position " << VectorToString(fInitialPosition) << " mm at time "
127 << ToTimeString(fGlobalTimestamp) << " - Time length of " << ToTimeString(fTimeLength)
128 << " and spatial length of " << ToLengthString(fLength) << endl;
129
130 size_t nHits = GetNumberOfHits();
131 if (maxHits > 0 && maxHits < nHits) {
132 nHits = min(maxHits, nHits);
133 cout << "Printing only the first " << nHits << " hits of the track" << endl;
134 }
135
136 const TRestGeant4Metadata* metadata = GetGeant4Metadata();
137 for (unsigned int i = 0; i < nHits; i++) {
138 TString processName = GetProcessName(fHits.GetHitProcess(i));
139 if (processName.IsNull()) {
140 processName =
141 TString(std::to_string(fHits.GetHitProcess(i))); // in case process name is not found, use ID
142 }
143
144 TString volumeName = "";
145 if (metadata != nullptr) {
146 volumeName = metadata->GetGeant4GeometryInfo().GetVolumeFromID(fHits.GetHitVolume(i));
147 }
148 if (volumeName.IsNull()) {
149 // in case process name is not found, use ID
150 volumeName = TString(std::to_string(fHits.GetHitVolume(i)));
151 }
152 cout << " - Hit " << i << " - Energy: " << ToEnergyString(fHits.GetEnergy(i))
153 << " - Process: " << processName << " - Volume: " << volumeName
154 << " - Position: " << VectorToString(TVector3(fHits.GetX(i), fHits.GetY(i), fHits.GetZ(i)))
155 << " mm - Time: " << ToTimeString(fHits.GetTime(i))
156 << " - KE: " << ToEnergyString(fHits.GetKineticEnergy(i)) << endl;
157 }
158}
159
160Bool_t TRestGeant4Track::ContainsProcessInVolume(Int_t processID, Int_t volumeID) const {
161 for (unsigned int i = 0; i < GetNumberOfHits(); i++) {
162 if (fHits.GetHitProcess(i) != processID) continue;
163 if (volumeID == -1 || fHits.GetVolumeId(i) == volumeID) return true;
164 }
165 return false;
166}
167
168Bool_t TRestGeant4Track::ContainsProcessInVolume(const TString& processName, Int_t volumeID) const {
169 const TRestGeant4Metadata* metadata = GetGeant4Metadata();
170 if (metadata == nullptr) {
171 return false;
172 }
173 const auto& processID = metadata->GetGeant4PhysicsInfo().GetProcessID(processName);
174 for (unsigned int i = 0; i < GetNumberOfHits(); i++) {
175 if (fHits.GetHitProcess(i) != processID) continue;
176 if (volumeID == -1 || fHits.GetVolumeId(i) == volumeID) return true;
177 }
178 return false;
179}
180
181const TRestGeant4Metadata* TRestGeant4Track::GetGeant4Metadata() const {
182 if (GetEvent() == nullptr) {
183 return nullptr;
184 }
185 return GetEvent()->GetGeant4Metadata();
186}
187
188TRestGeant4Track* TRestGeant4Track::GetParentTrack() const {
189 if (fEvent == nullptr) {
190 return nullptr;
191 }
192 return fEvent->GetTrackByID(GetParentID());
193}
194
195vector<const TRestGeant4Track*> TRestGeant4Track::GetSecondaryTracks() const {
196 vector<const TRestGeant4Track*> secondaryTracks = {};
197 for (const auto trackID : fSecondaryTrackIDs) {
198 const TRestGeant4Track* track = fEvent->GetTrackByID(trackID);
199 if (track != nullptr) {
200 secondaryTracks.push_back(track);
201 }
202 }
203 return secondaryTracks;
204}
205
206TString TRestGeant4Track::GetInitialVolume() const {
207 const auto metadata = GetGeant4Metadata();
208 if (metadata == nullptr) {
209 return "";
210 }
211 const auto& hits = GetHits();
212 return GetGeant4Metadata()->GetGeant4GeometryInfo().GetVolumeFromID(hits.GetVolumeId(0));
213}
214
215TString TRestGeant4Track::GetFinalVolume() const {
216 const auto metadata = GetGeant4Metadata();
217 if (metadata == nullptr) {
218 return "";
219 }
220 const auto& hits = GetHits();
221 return GetGeant4Metadata()->GetGeant4GeometryInfo().GetVolumeFromID(
222 hits.GetVolumeId(hits.GetNumberOfHits() - 1));
223}
224
225Double_t TRestGeant4Track::GetEnergyInVolume(const TString& volumeName, bool children) const {
226 const auto metadata = GetGeant4Metadata();
227 if (metadata == nullptr) {
228 return 0;
229 }
230
231 const auto volumeId = metadata->GetGeant4GeometryInfo().GetIDFromVolume(volumeName);
232
233 if (!children) {
234 return GetEnergyInVolume(volumeId);
235 }
236
237 Double_t energy = 0;
238 vector<const TRestGeant4Track*> tracks = {this};
239 while (!tracks.empty()) {
240 const TRestGeant4Track* track = tracks.back();
241 tracks.pop_back();
242 if (track == nullptr) {
243 continue;
244 }
245 energy += track->GetEnergyInVolume(volumeId);
246 for (const TRestGeant4Track* secondaryTrack : track->GetSecondaryTracks()) {
247 tracks.push_back(secondaryTrack);
248 }
249 }
250 return energy;
251}
252
253TString TRestGeant4Track::GetLastProcessName() const {
254 const auto metadata = GetGeant4Metadata();
255 if (metadata == nullptr) {
256 return "";
257 }
258
259 const auto& hits = GetHits();
260 return GetGeant4Metadata()->GetGeant4PhysicsInfo().GetProcessName(
261 hits.GetProcess(hits.GetNumberOfHits() - 1));
262}
The main class to store the Geant4 simulation conditions that will be used by restG4.
const TRestGeant4PhysicsInfo & GetGeant4PhysicsInfo() const
Returns an immutable reference to the physics info.
const TRestGeant4GeometryInfo & GetGeant4GeometryInfo() const
Returns an immutable reference to the geometry info.
void PrintTrack(size_t maxHits=0) const
Prints the track information. N number of hits to print, 0 = all.
size_t GetNumberOfHits(Int_t volID=-1) const
Function that returns the number of hit depositions found inside the TRestGeant4Track....
size_t GetNumberOfPhysicalHits(Int_t volID=-1) const
Function that returns the number of hit depositions found inside the TRestGeant4Track with energy > 0...