REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
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 << "' in volume '" << GetInitialVolume()
122 << "' with initial KE of " << ToEnergyString(fInitialKineticEnergy) << " - Initial position "
123 << VectorToString(fInitialPosition) << " mm at time " << ToTimeString(fGlobalTimestamp)
124 << " - Time length of " << ToTimeString(fTimeLength) << " and spatial length of "
125 << ToLengthString(fLength) << endl;
126
127 cout << " Initial position " << VectorToString(fInitialPosition) << " mm at time "
128 << ToTimeString(fGlobalTimestamp) << " - Time length of " << ToTimeString(fTimeLength)
129 << " and spatial length of " << ToLengthString(fLength) << endl;
130
131 size_t nHits = GetNumberOfHits();
132 if (maxHits > 0 && maxHits < nHits) {
133 nHits = min(maxHits, nHits);
134 cout << "Printing only the first " << nHits << " hits of the track" << endl;
135 }
136
137 const TRestGeant4Metadata* metadata = GetGeant4Metadata();
138 for (unsigned int i = 0; i < nHits; i++) {
139 TString processName = GetProcessName(fHits.GetHitProcess(i));
140 if (processName.IsNull()) {
141 processName =
142 TString(std::to_string(fHits.GetHitProcess(i))); // in case process name is not found, use ID
143 }
144
145 TString volumeName = "";
146 if (metadata != nullptr) {
147 volumeName = metadata->GetGeant4GeometryInfo().GetVolumeFromID(fHits.GetHitVolume(i));
148 }
149 if (volumeName.IsNull()) {
150 // in case process name is not found, use ID
151 volumeName = TString(std::to_string(fHits.GetHitVolume(i)));
152 }
153 cout << " - Hit " << i << " - Energy: " << ToEnergyString(fHits.GetEnergy(i))
154 << " - Process: " << processName << " - Volume: " << volumeName
155 << " - Position: " << VectorToString(TVector3(fHits.GetX(i), fHits.GetY(i), fHits.GetZ(i)))
156 << " mm - Time: " << ToTimeString(fHits.GetTime(i))
157 << " - KE: " << ToEnergyString(fHits.GetKineticEnergy(i)) << endl;
158 }
159}
160
161void TRestGeant4Track::PrintTrackFilterVolumes(const std::set<std::string>& volumeNames) const {
162 const TRestGeant4Metadata* metadata = GetGeant4Metadata();
163 if (metadata == nullptr) {
164 return;
165 }
166 bool skip = true;
167 for (unsigned int i = 0; i < GetNumberOfHits(); i++) {
168 // check volumeName is in set
169 TString volumeName = metadata->GetGeant4GeometryInfo().GetVolumeFromID(fHits.GetHitVolume(i));
170 if (volumeName.IsNull()) {
171 // in case process name is not found, use ID
172 volumeName = TString(std::to_string(fHits.GetHitVolume(i)));
173 }
174 if (volumeNames.find(volumeName.Data()) != volumeNames.end()) {
175 skip = false;
176 break;
177 }
178 }
179
180 if (skip) {
181 return;
182 }
183
184 cout
185 << " * TrackID: " << fTrackID << " - Particle: " << fParticleName << " - ParentID: " << fParentID
186 << ""
187 << (GetParentTrack() != nullptr
188 ? TString::Format(" - Parent particle: %s", GetParentTrack()->GetParticleName().Data()).Data()
189 : "")
190 << " - Created by '" << fCreatorProcess << "' in volume '" << GetInitialVolume()
191 << "' with initial KE of " << ToEnergyString(fInitialKineticEnergy) << " - Initial position "
192 << VectorToString(fInitialPosition) << " mm at time " << ToTimeString(fGlobalTimestamp)
193 << " - Time length of " << ToTimeString(fTimeLength) << " and spatial length of "
194 << ToLengthString(fLength) << endl;
195
196 size_t nHits = GetNumberOfHits();
197 for (unsigned int i = 0; i < nHits; i++) {
198 TString processName = GetProcessName(fHits.GetHitProcess(i));
199 if (processName.IsNull()) {
200 processName =
201 TString(std::to_string(fHits.GetHitProcess(i))); // in case process name is not found, use ID
202 }
203
204 TString volumeName = metadata->GetGeant4GeometryInfo().GetVolumeFromID(fHits.GetHitVolume(i));
205 if (volumeName.IsNull()) {
206 // in case process name is not found, use ID
207 volumeName = TString(std::to_string(fHits.GetHitVolume(i)));
208 }
209 if (volumeNames.find(volumeName.Data()) == volumeNames.end()) {
210 continue;
211 }
212 cout << " - Hit " << i << " - Energy: " << ToEnergyString(fHits.GetEnergy(i))
213 << " - Process: " << processName << " - Volume: " << volumeName
214 << " - Position: " << VectorToString(TVector3(fHits.GetX(i), fHits.GetY(i), fHits.GetZ(i)))
215 << " mm - Time: " << ToTimeString(fHits.GetTime(i))
216 << " - KE: " << ToEnergyString(fHits.GetKineticEnergy(i)) << endl;
217 }
218}
219
220Bool_t TRestGeant4Track::ContainsProcessInVolume(Int_t processID, Int_t volumeID) const {
221 for (unsigned int i = 0; i < GetNumberOfHits(); i++) {
222 if (fHits.GetHitProcess(i) != processID) continue;
223 if (volumeID == -1 || fHits.GetVolumeId(i) == volumeID) return true;
224 }
225 return false;
226}
227
228Bool_t TRestGeant4Track::ContainsProcessInVolume(const TString& processName, Int_t volumeID) const {
229 const TRestGeant4Metadata* metadata = GetGeant4Metadata();
230 if (metadata == nullptr) {
231 return false;
232 }
233 const auto& processID = metadata->GetGeant4PhysicsInfo().GetProcessID(processName);
234 for (unsigned int i = 0; i < GetNumberOfHits(); i++) {
235 if (fHits.GetHitProcess(i) != processID) continue;
236 if (volumeID == -1 || fHits.GetVolumeId(i) == volumeID) return true;
237 }
238 return false;
239}
240
241const TRestGeant4Metadata* TRestGeant4Track::GetGeant4Metadata() const {
242 if (GetEvent() == nullptr) {
243 return nullptr;
244 }
245 return GetEvent()->GetGeant4Metadata();
246}
247
248TRestGeant4Track* TRestGeant4Track::GetParentTrack() const {
249 if (fEvent == nullptr) {
250 return nullptr;
251 }
252 return fEvent->GetTrackByID(GetParentID());
253}
254
255vector<const TRestGeant4Track*> TRestGeant4Track::GetSecondaryTracks() const {
256 vector<const TRestGeant4Track*> secondaryTracks = {};
257 for (const auto trackID : fSecondaryTrackIDs) {
258 const TRestGeant4Track* track = fEvent->GetTrackByID(trackID);
259 if (track != nullptr) {
260 secondaryTracks.push_back(track);
261 }
262 }
263 return secondaryTracks;
264}
265
266TString TRestGeant4Track::GetInitialVolume() const {
267 const auto metadata = GetGeant4Metadata();
268 if (metadata == nullptr) {
269 return "";
270 }
271 const auto& hits = GetHits();
272 return GetGeant4Metadata()->GetGeant4GeometryInfo().GetVolumeFromID(hits.GetVolumeId(0));
273}
274
275TString TRestGeant4Track::GetFinalVolume() const {
276 const auto metadata = GetGeant4Metadata();
277 if (metadata == nullptr) {
278 return "";
279 }
280 const auto& hits = GetHits();
281 return GetGeant4Metadata()->GetGeant4GeometryInfo().GetVolumeFromID(
282 hits.GetVolumeId(hits.GetNumberOfHits() - 1));
283}
284
285Double_t TRestGeant4Track::GetEnergyInVolume(const TString& volumeName, bool children) const {
286 const auto metadata = GetGeant4Metadata();
287 if (metadata == nullptr) {
288 return 0;
289 }
290
291 const auto volumeId = metadata->GetGeant4GeometryInfo().GetIDFromVolume(volumeName);
292
293 if (!children) {
294 return GetEnergyInVolume(volumeId);
295 }
296
297 Double_t energy = 0;
298 vector<const TRestGeant4Track*> tracks = {this};
299 while (!tracks.empty()) {
300 const TRestGeant4Track* track = tracks.back();
301 tracks.pop_back();
302 if (track == nullptr) {
303 continue;
304 }
305 energy += track->GetEnergyInVolume(volumeId);
306 for (const TRestGeant4Track* secondaryTrack : track->GetSecondaryTracks()) {
307 tracks.push_back(secondaryTrack);
308 }
309 }
310 return energy;
311}
312
313TString TRestGeant4Track::GetLastProcessName() const {
314 const auto metadata = GetGeant4Metadata();
315 if (metadata == nullptr) {
316 return "";
317 }
318
319 const auto& hits = GetHits();
320 return GetGeant4Metadata()->GetGeant4PhysicsInfo().GetProcessName(
321 hits.GetProcess(hits.GetNumberOfHits() - 1));
322}
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...