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