FairShip
Loading...
Searching...
No Matches
ShipStack.cxx
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP
3// Collaboration
4
5// -------------------------------------------------------------------------
6// ----- ShipStack source file -----
7// -------------------------------------------------------------------------
8#include "ShipStack.h"
9
10#include <iosfwd> // for ostream
11#include <iostream> // for operator<<, etc
12
13#include "FairDetector.h" // for FairDetector
14#include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN
15#include "FairMCPoint.h" // for FairMCPoint
16#include "FairRootManager.h" // for FairRootManager
17#include "ISTLPointContainer.h" // for STL-based detector interface
18#include "ShipMCTrack.h" // for ShipMCTrack
19#include "TClonesArray.h" // for TClonesArray
20#include "TIterator.h" // for TIterator
21#include "TLorentzVector.h" // for TLorentzVector
22#include "TParticle.h" // for TParticle
23#include "TRefArray.h" // for TRefArray
24#include "TVirtualMC.h"
25#include "TVirtualMCStack.h"
26
27using std::cout;
28using std::endl;
29
30// ----- Default constructor -------------------------------------------
32 : FairGenericStack(),
33 fStack(),
34 fParticles(new TClonesArray("TParticle", size)),
35 fTracks(nullptr),
37 fIndexMap(),
39 fCurrentTrack(-1),
40 fNPrimaries(0),
41 fNParticles(0),
42 fNTracks(0),
43 fIndex(0),
44 fStoreSecondaries(kTRUE),
45 fMinPoints(1),
46 fEnergyCut(0.),
47 fStoreMothers(kTRUE),
48 fSplitting(kFALSE) {
49 fTracks = new std::vector<ShipMCTrack>();
50 fTracks->reserve(size);
51}
52
53// -------------------------------------------------------------------------
54
55// ----- Destructor ----------------------------------------------------
57 if (fParticles) {
58 fParticles->Delete();
59 delete fParticles;
60 }
61 if (fTracks) {
62 delete fTracks;
63 }
64}
65// -------------------------------------------------------------------------
66
67void ShipStack::PushTrack(Int_t toBeDone, Int_t parentId, Int_t pdgCode,
68 Double_t px, Double_t py, Double_t pz, Double_t e,
69 Double_t vx, Double_t vy, Double_t vz, Double_t time,
70 Double_t polx, Double_t poly, Double_t polz,
71 TMCProcess proc, Int_t& ntr, Double_t weight,
72 Int_t is) {
73 PushTrack(toBeDone, parentId, pdgCode, px, py, pz, e, vx, vy, vz, time, polx,
74 poly, polz, proc, ntr, weight, is, -1);
75}
76
77// ----- Virtual public method PushTrack -------------------------------
78void ShipStack::PushTrack(Int_t toBeDone, Int_t parentId, Int_t pdgCode,
79 Double_t px, Double_t py, Double_t pz, Double_t e,
80 Double_t vx, Double_t vy, Double_t vz, Double_t time,
81 Double_t polx, Double_t poly, Double_t polz,
82 TMCProcess proc, Int_t& ntr, Double_t weight,
83 Int_t is, Int_t secondparentID) {
84 // cout << "ShipStack: " << fNParticles << " " << pdgCode << " " << parentId
85 // << " " << secondparentID<<" "<<proc<< endl;
86
87 // update weight to correspond to parentWeight * weight for splitting cases
88 if (fSplitting) {
89 if (parentId >= 0) {
90 if (parentId < fParticles->GetEntriesFast()) {
91 TParticle* parentPart =
92 dynamic_cast<TParticle*>(fParticles->At(parentId));
93 if (parentPart) {
94 Double_t parentWeight = parentPart->GetWeight();
95 weight = weight * parentWeight;
96 }
97 }
98 }
99 }
100
101 Int_t trackId = fNParticles;
102
103 // --> Get TParticle array
104 TClonesArray& partArray = *fParticles;
105
106 // --> Set argument variable
107 ntr = trackId;
108
109 Int_t nPoints = 0;
110 Int_t daughter1Id = -1;
111 Int_t daughter2Id = -1;
112
113 // --> Create new TParticle and add it to the TParticle array
114 TParticle* particle = new (partArray[fNParticles++])
115 TParticle(pdgCode, trackId, parentId, nPoints, daughter1Id, daughter2Id,
116 px, py, pz, e, vx, vy, vz, time);
117 // from root, how does this fit ? misuse of status and mother2 ??? status is
118 // used for trackID definitely TParticle(Int_t pdg, Int_t status, Int_t
119 // mother1, Int_t mother2,
120 // Int_t daughter1, Int_t daughter2, Double_t px, Double_t py, Double_t pz,
121 // Double_t etot, Double_t vx, Double_t vy, Double_t vz, Double_t time)
122 particle->SetPolarisation(polx, poly, polz);
123 particle->SetWeight(weight);
124 particle->SetUniqueID(proc);
125 // TR August 2014, still trying to understand the logic of FairRoot, due to
126 // misuse of secondparentID, all is a big mess
127 if (parentId < 0) {
128 particle->SetFirstMother(secondparentID);
129 particle->SetLastMother(secondparentID);
130 } else {
131 particle->SetFirstMother(parentId);
132 particle->SetLastMother(parentId);
133 }
134 // --> Increment counter
135 if (parentId < 0) {
136 fNPrimaries++;
137 }
138 if (toBeDone == 1) {
139 // --> Push particle on the stack if toBeDone is set
140 particle->SetBit(kDoneBit);
141 fStack.push(particle);
142 }
143}
144// -------------------------------------------------------------------------
145
146// ----- Virtual method PopNextTrack -----------------------------------
147TParticle* ShipStack::PopNextTrack(Int_t& iTrack) {
148 // If end of stack: Return empty pointer
149 if (fStack.empty()) {
150 iTrack = -1;
151 return nullptr;
152 }
153
154 // If not, get next particle from stack
155 TParticle* thisParticle = fStack.top();
156 fStack.pop();
157
158 if (!thisParticle) {
159 iTrack = 0;
160 return nullptr;
161 }
162
163 fCurrentTrack = thisParticle->GetStatusCode();
164 iTrack = fCurrentTrack;
165
166 return thisParticle;
167}
168// -------------------------------------------------------------------------
169
170// ----- Virtual method PopPrimaryForTracking --------------------------
171TParticle* ShipStack::PopPrimaryForTracking(Int_t iPrim) {
172 // Get the iPrimth particle from the fStack TClonesArray. This
173 // should be a primary (if the index is correct).
174
175 // Test for index
176 if (iPrim < 0 || iPrim >= fNPrimaries) {
177 LOGF(fatal, "ShipStack: Primary index out of range! %i ", iPrim);
178 }
179
180 // Return the iPrim-th TParticle from the fParticle array. This should be
181 // a primary.
182 TParticle* part = dynamic_cast<TParticle*>(fParticles->At(iPrim));
183 /* do not understand the logic behind this !!! TR July 2014
184 if ( ! (part->GetMother(0) < 0) ) {
185 fLogger->Fatal(MESSAGE_ORIGIN, "ShipStack:: Not a primary track! %i
186 ",iPrim); Fatal("ShipStack::PopPrimaryForTracking", "Not a primary track");
187 }*/
188 if (!part->TestBit(kDoneBit))
189 return nullptr;
190 else
191 return part;
192}
193// -------------------------------------------------------------------------
194
195// ----- Virtual public method GetCurrentTrack -------------------------
196TParticle* ShipStack::GetCurrentTrack() const {
197 TParticle* currentPart = GetParticle(fCurrentTrack);
198 if (!currentPart) {
199 LOG(warning) << "ShipStack: Current track not found in stack!";
200 }
201 return currentPart;
202}
203// -------------------------------------------------------------------------
204
205// ----- Public method AddParticle -------------------------------------
206void ShipStack::AddParticle(TParticle* oldPart) {
207 TClonesArray& array = *fParticles;
208 TParticle* newPart = new (array[fIndex]) TParticle(*oldPart);
209 newPart->SetWeight(oldPart->GetWeight());
210 newPart->SetUniqueID(oldPart->GetUniqueID());
211 fIndex++;
212}
213// -------------------------------------------------------------------------
214
215// ----- Public method FillTrackArray ----------------------------------
217 LOG(debug) << "ShipStack: Filling MCTrack array...";
218
219 // Some safeties to make sure everything exists
220 if (!gMC) return;
221 if (gMC->GetStack() == nullptr) return;
222
223 Int_t evtNo = -1;
224
225 // --> Reset index map (-2 = not stored) and number of output tracks
226 fIndexMap.assign(fNParticles, -2);
227 fNTracks = 0;
228
229 // --> Check tracks for selection criteria
230 SelectTracks();
231
232 if (fNParticles > 0) evtNo = gMC->CurrentEvent();
233
234 // --> Loop over fParticles array and copy selected tracks
235 for (Int_t iPart = 0; iPart < fNParticles; iPart++) {
236 if (fStoreFlags[iPart]) {
237 fTracks->emplace_back(dynamic_cast<TParticle*>(GetParticle(iPart)));
238 ShipMCTrack& track = fTracks->back();
239 if (iPart < static_cast<Int_t>(fSplitSets.size())) {
240 const SplitInfo& info = fSplitSets[iPart];
241 track.SetSplitSet(info.splitSet, info.role, info.weight);
242 }
243 fIndexMap[iPart] = fNTracks;
244 track.SetTrackID(fNTracks);
245 track.SetEventID(evtNo);
246 fNTracks++;
247 }
248 }
249
250 // --> Screen output
251 // Print(1);
252}
253// -------------------------------------------------------------------------
254
255// ----- Public method UpdateTrackIndex --------------------------------
256void ShipStack::UpdateTrackIndex(TRefArray* detList) {
257 LOG(debug) << "ShipStack: Updating track indizes...";
258 Int_t nColl = 0;
259
260 // First update mother ID in MCTracks
261 for (Int_t i = 0; i < fNTracks; i++) {
262 ShipMCTrack& track = (*fTracks)[i];
263 Int_t iMotherOld = track.GetMotherId();
264 if (iMotherOld >= static_cast<Int_t>(fIndexMap.size())) {
265 LOGF(fatal, "ShipStack: Particle index %i not found in dex map! ",
266 iMotherOld);
267 }
268 track.SetMotherId(iMotherOld < 0 ? -1 : fIndexMap[iMotherOld]);
269 }
270
271 if (fDetList == nullptr) {
272 // Now iterate through all active detectors
273 fDetList = detList;
274 fDetIter = detList->MakeIterator();
275 fDetIter->Reset();
276 } else {
277 fDetIter->Reset();
278 }
279
280 FairDetector* det = nullptr;
281 while ((det = dynamic_cast<FairDetector*>(fDetIter->Next()))) {
282 // --> Check if detector uses STL containers (std::vector)
283 if (auto* stlDet = dynamic_cast<ISTLPointContainer*>(det)) {
284 // Use new interface for STL-based detectors
285 stlDet->UpdatePointTrackIndices(fIndexMap);
286 nColl++;
287 } else {
288 // --> Get hit collections from detector (legacy TClonesArray approach)
289 Int_t iColl = 0;
290 TClonesArray* hitArray;
291 while ((hitArray = det->GetCollection(iColl++))) {
292 nColl++;
293 Int_t nPoints = hitArray->GetEntriesFast();
294
295 // --> Update track index for all MCPoints in the collection
296 for (Int_t iPoint = 0; iPoint < nPoints; iPoint++) {
297 FairMCPoint* point = dynamic_cast<FairMCPoint*>(hitArray->At(iPoint));
298 Int_t iTrack = point->GetTrackID();
299
300 if (iTrack < -1 || iTrack >= static_cast<Int_t>(fIndexMap.size())) {
301 LOGF(fatal, "ShipStack: Particle index %i not found in index map! ",
302 iTrack);
303 }
304 point->SetTrackID(iTrack < 0 ? -1 : fIndexMap[iTrack]);
305 }
306
307 } // Collections of this detector
308 }
309 } // List of active detectors
310 LOGF(debug, "...stack and %i collections updated.", nColl);
311}
312// -------------------------------------------------------------------------
313
314// ----- Public method Reset -------------------------------------------
316 fIndex = 0;
317 fCurrentTrack = -1;
319 while (!fStack.empty()) {
320 fStack.pop();
321 }
322 fParticles->Clear();
323 fTracks->clear();
324 fPointsPerTrack.clear();
325 fSplitSets.clear();
326}
327
328void ShipStack::SetSplitSet(Int_t iTrack, Int_t splitSet, Int_t role,
329 Double_t weight) {
330 if (iTrack < 0 || iTrack >= fNParticles) {
331 LOG(error) << "ShipStack::SetSplitSet: no particle with index " << iTrack;
332 return;
333 }
334 if (iTrack >= static_cast<Int_t>(fSplitSets.size())) {
335 fSplitSets.resize(fNParticles);
336 }
337 fSplitSets[iTrack] = {splitSet, role, weight};
338}
339// -------------------------------------------------------------------------
340
341// ----- Public method Register ----------------------------------------
343 FairRootManager::Instance()->RegisterAny("MCTrack", fTracks, kTRUE);
344}
345// -------------------------------------------------------------------------
346
347// ----- Public method Print --------------------------------------------
348void ShipStack::Print(Int_t iVerbose) const {
349 cout << "-I- ShipStack: Number of primaries = " << fNPrimaries << endl;
350 cout << " Total number of particles = " << fNParticles << endl;
351 cout << " Number of tracks in output = " << fNTracks << endl;
352 if (iVerbose) {
353 for (Int_t iTrack = 0; iTrack < fNTracks; iTrack++) {
354 (*fTracks)[iTrack].Print(iTrack);
355 }
356 }
357}
358// -------------------------------------------------------------------------
359
360// ----- Public method AddPoint (for current track) --------------------
362// -------------------------------------------------------------------------
363
364// ----- Public method AddPoint (for arbitrary track) -------------------
365void ShipStack::AddPoint(DetectorId /*detId*/, Int_t iTrack) {
366 if (iTrack < 0) {
367 return;
368 }
369 if (iTrack >= static_cast<Int_t>(fPointsPerTrack.size())) {
370 fPointsPerTrack.resize(iTrack + 1, 0);
371 }
372 ++fPointsPerTrack[iTrack];
373}
374// -------------------------------------------------------------------------
375
376// ----- Virtual method GetCurrentParentTrackNumber --------------------
378 TParticle* currentPart = GetCurrentTrack();
379 if (currentPart) {
380 return currentPart->GetFirstMother();
381 } else {
382 return -1;
383 }
384}
385// -------------------------------------------------------------------------
386
387// ----- Public method GetParticle -------------------------------------
388TParticle* ShipStack::GetParticle(Int_t trackID) const {
389 if (trackID < 0 || trackID >= fNParticles) {
390 LOGF(fatal, "ShipStack: Particle index %i out of range. Max=%i", trackID,
392 }
393 return dynamic_cast<TParticle*>(fParticles->At(trackID));
394}
395// -------------------------------------------------------------------------
396
397// ----- Private method SelectTracks -----------------------------------
399 // --> Reset storage flags
400 fStoreFlags.assign(fNParticles, kFALSE);
401
402 // --> Check particles in the fParticle array
403 for (Int_t i = 0; i < fNParticles; i++) {
404 TParticle* thisPart = GetParticle(i);
405 Bool_t store = kTRUE;
406
407 // --> Get track parameters
408 Int_t iMother = thisPart->GetMother(0);
409 TLorentzVector p;
410 thisPart->Momentum(p);
411 Double_t energy = p.E();
412 Double_t mass = p.M();
413 // Double_t mass = thisPart->GetMass();
414 Double_t eKin = energy - mass;
415
416 // --> Get number of points
417 Int_t nPoints =
418 i < static_cast<Int_t>(fPointsPerTrack.size()) ? fPointsPerTrack[i] : 0;
419
420 // --> Check for cuts (store primaries in any case)
421 if (iMother < 0) {
422 store = kTRUE;
423 } else {
424 if (!fStoreSecondaries) {
425 store = kFALSE;
426 }
427 if (nPoints < fMinPoints) {
428 store = kFALSE;
429 }
430 if (eKin < fEnergyCut) {
431 store = kFALSE;
432 }
433 if ((nPoints >= fMinPoints && fMinPoints > 0) ||
434 (eKin >= fEnergyCut && fEnergyCut > 0)) {
435 store = kTRUE;
436 }
437 }
438 // --> Set storage flag
439 fStoreFlags[i] = store;
440 }
441 // --> If flag is set, flag recursively mothers of selected tracks.
442 // An already-flagged mother implies its whole ancestor chain is flagged,
443 // so the walk can stop there.
444 if (fStoreMothers) {
445 for (Int_t i = 0; i < fNParticles; i++) {
446 if (fStoreFlags[i]) {
447 Int_t iMother = GetParticle(i)->GetMother(0);
448 while (iMother >= 0 && !fStoreFlags[iMother]) {
449 fStoreFlags[iMother] = kTRUE;
450 iMother = GetParticle(iMother)->GetMother(0);
451 }
452 }
453 }
454 }
455}
456// -------------------------------------------------------------------------
const Int_t debug
DetectorId
@ kDoneBit
Definition ShipStack.h:44
Interface for detectors using STL containers (std::vector) for MC points.
void SetSplitSet(Int_t splitSet, Int_t role, Double_t splitWeight)
void SetTrackID(const Int_t &trackID)
void SetEventID(const Int_t &eventID)
Int_t GetMotherId() const
Definition ShipMCTrack.h:61
void SetMotherId(Int_t id)
Int_t fMinPoints
Definition ShipStack.h:222
Bool_t fStoreMothers
Definition ShipStack.h:224
void AddPoint(DetectorId iDet)
Int_t GetCurrentParentTrackNumber() const override
void Register() override
~ShipStack() override
Definition ShipStack.cxx:56
void PushTrack(Int_t toBeDone, Int_t parentID, Int_t pdgCode, Double_t px, Double_t py, Double_t pz, Double_t e, Double_t vx, Double_t vy, Double_t vz, Double_t time, Double_t polx, Double_t poly, Double_t polz, TMCProcess proc, Int_t &ntr, Double_t weight, Int_t is) override
Definition ShipStack.cxx:67
Int_t fNTracks
Number of entries in fParticles.
Definition ShipStack.h:217
void Reset() override
TParticle * GetParticle(Int_t trackId) const
Int_t fIndex
Number of entries in fTracks.
Definition ShipStack.h:218
TParticle * GetCurrentTrack() const override
virtual void Print(Int_t iVerbose=0) const
std::vector< Int_t > fPointsPerTrack
Definition ShipStack.h:203
Double32_t fEnergyCut
Definition ShipStack.h:223
void FillTrackArray() override
std::vector< Bool_t > fStoreFlags
Definition ShipStack.h:197
Bool_t fStoreSecondaries
Used for merging.
Definition ShipStack.h:221
TParticle * PopPrimaryForTracking(Int_t iPrim) override
Bool_t fSplitting
Definition ShipStack.h:225
TParticle * PopNextTrack(Int_t &iTrack) override
Int_t fNParticles
Number of primary particles.
Definition ShipStack.h:216
std::vector< Int_t > fIndexMap
Definition ShipStack.h:200
std::vector< SplitInfo > fSplitSets
Definition ShipStack.h:211
Int_t fNPrimaries
Index of current track.
Definition ShipStack.h:215
std::stack< TParticle * > fStack
Definition ShipStack.h:186
void SetSplitSet(Int_t iTrack, Int_t splitSet, Int_t role, Double_t weight)
TClonesArray * fParticles
Definition ShipStack.h:191
void SelectTracks()
Int_t fCurrentTrack
Definition ShipStack.h:214
void UpdateTrackIndex(TRefArray *detArray=nullptr) override
std::vector< ShipMCTrack > * fTracks
Definition ShipStack.h:194
ShipStack(Int_t size=100)
Definition ShipStack.cxx:31
void AddParticle(TParticle *part)