Project
Loading...
Searching...
No Matches
MCTrack.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#ifndef ALICEO2_DATA_MCTRACK_H_
17#define ALICEO2_DATA_MCTRACK_H_
18
22#include "Rtypes.h"
24#include "TLorentzVector.h"
25#include "TMCProcess.h"
26#include "TMath.h"
27#include "TParticle.h"
28#include "TParticlePDG.h"
29#include "TVector3.h"
30#include <type_traits>
31
32namespace o2
33{
34
35namespace MCTrackHelper
36{
37void printMassError(int pdg);
38};
39
45template <class _T>
47{
48 public:
49 static constexpr int NHITBITS = 22; // do not modify this
50
53
55 MCTrackT(Int_t pdgCode, Int_t motherID, Int_t secondMotherID, Int_t firstDaughterID, Int_t lastDaughterID,
56 Double_t px, Double_t py, Double_t pz, Double_t x, Double_t y, Double_t z, Double_t t,
57 Int_t nPoints);
58
60 MCTrackT(const MCTrackT& track) = default;
61
63 MCTrackT(const TParticle& particle);
64
66 ~MCTrackT() = default;
67
69 void Print(Int_t iTrack = 0) const;
70
72 Int_t GetPdgCode() const { return mPdgCode; }
73 Int_t getMotherTrackId() const { return mMotherTrackId; }
74 Int_t getSecondMotherTrackId() const { return mSecondMotherTrackId; }
75 bool isPrimary() const { return (getProcess() == TMCProcess::kPPrimary) || (getMotherTrackId() < 0 && getSecondMotherTrackId() < 0); }
76 bool isSecondary() const { return !isPrimary(); }
77 Int_t getFirstDaughterTrackId() const { return mFirstDaughterTrackId; }
78 Int_t getLastDaughterTrackId() const { return mLastDaughterTrackId; }
79 Double_t GetStartVertexMomentumX() const { return mStartVertexMomentumX; }
80 Double_t GetStartVertexMomentumY() const { return mStartVertexMomentumY; }
81 Double_t GetStartVertexMomentumZ() const { return mStartVertexMomentumZ; }
82 Double_t GetStartVertexCoordinatesX() const { return mStartVertexCoordinatesX; }
83 Double_t GetStartVertexCoordinatesY() const { return mStartVertexCoordinatesY; }
84 Double_t GetStartVertexCoordinatesZ() const { return mStartVertexCoordinatesZ; }
85 Double_t GetStartVertexCoordinatesT() const { return mStartVertexCoordinatesT; }
86
88 Double_t R2() const { return Vx() * Vx() + Vy() * Vy(); }
90 Double_t R() const { return std::sqrt(R2()); }
91
93 Double_t GetMass() const;
94
96 _T getWeight() const { return mWeight; }
97
98 Double_t GetEnergy() const;
99
100 // Alternative accessors with TParticle like shorter names
101 Double_t Px() const { return mStartVertexMomentumX; }
102 Double_t Py() const { return mStartVertexMomentumY; }
103 Double_t Pz() const { return mStartVertexMomentumZ; }
104 Double_t Vx() const { return mStartVertexCoordinatesX; }
105 Double_t Vy() const { return mStartVertexCoordinatesY; }
106 Double_t Vz() const { return mStartVertexCoordinatesZ; }
107 Double_t T() const { return mStartVertexCoordinatesT; }
108
109 Double_t GetPt() const
110 {
111 double mx(mStartVertexMomentumX);
112 double my(mStartVertexMomentumY);
113 return std::sqrt(mx * mx + my * my);
114 }
115
116 Double_t GetP() const
117 {
118 double mx(mStartVertexMomentumX);
119 double my(mStartVertexMomentumY);
120 double mz(mStartVertexMomentumZ);
121 return std::sqrt(mx * mx + my * my + mz * mz);
122 }
123
124 Double_t GetPhi() const
125 {
126 double mx(mStartVertexMomentumX);
127 double my(mStartVertexMomentumY);
128 return (TMath::Pi() + TMath::ATan2(-my, -mx));
129 }
130
131 Double_t GetEta() const
132 {
133 double_t pmom = GetP();
134 double mz(mStartVertexMomentumZ);
135 if (pmom != TMath::Abs(mz)) {
136 return 0.5 * std::log((pmom + mz) / (pmom - mz));
137 } else {
138 return 1.e30;
139 }
140 }
141
142 Double_t GetTgl() const
143 {
144 auto pT = GetPt();
145 return pT > 1e-6 ? mStartVertexMomentumZ / pT : (GetStartVertexMomentumZ() > 0 ? 999. : -999.);
146 }
147
148 Double_t GetTheta() const
149 {
150 double mz(mStartVertexMomentumZ);
151 return (mz == 0) ? TMath::PiOver2() : TMath::ACos(mz / GetP());
152 }
153
154 Double_t GetRapidity() const;
155
156 void GetMomentum(TVector3& momentum) const;
157
158 void Get4Momentum(TLorentzVector& momentum) const;
159
160 void GetStartVertex(TVector3& vertex) const;
161
163 Int_t getHitMask() const { return ((PropEncoding)mProp).hitmask; }
164 void setHitMask(Int_t m) { ((PropEncoding)mProp).hitmask = m; }
166 void SetMotherTrackId(Int_t id) { mMotherTrackId = id; }
167 void SetSecondMotherTrackId(Int_t id) { mSecondMotherTrackId = id; }
168 void SetFirstDaughterTrackId(Int_t id) { mFirstDaughterTrackId = id; }
169 void SetLastDaughterTrackId(Int_t id) { mLastDaughterTrackId = id; }
170
171 // set bit indicating that this track
172 // left a hit in detector that corresponds to bit iDetBit. This bit is found in
173 // a lookup table.
174 void setHit(Int_t iDetBit)
175 {
176 assert(0 <= iDetBit && iDetBit < o2::detectors::DetID::nDetectors);
177 auto prop = ((PropEncoding)mProp);
178 prop.hitmask |= 1 << iDetBit;
179 mProp = prop.i;
180 }
181
182 bool leftTraceGivenBitField(int bit) const
183 {
184 return (((PropEncoding)mProp).hitmask & (1 << bit)) > 0;
185 }
186
189 bool leftTrace(Int_t iDet, std::vector<int> const& detIDtoBit) const
190 {
191 auto bit = detIDtoBit[iDet];
192 if (bit != -1) {
193 return leftTraceGivenBitField(bit);
194 }
195 return false;
196 }
197
198 // determine how many detectors "saw" this track
199 int getNumDet() const
200 {
201 int count = 0;
202 for (auto i = 0; i < NHITBITS; ++i) {
204 count++;
205 }
206 }
207 return count;
208 }
209
210 // keep track if this track will be persistet
211 // using last bit in mHitMask to do so
212 void setStore(bool f)
213 {
214 auto prop = ((PropEncoding)mProp);
215 prop.storage = f;
216 mProp = prop.i;
217 }
218 bool getStore() const { return ((PropEncoding)mProp).storage; }
220 bool hasHits() const { return ((PropEncoding)mProp).hitmask != 0; }
222 void setProcess(int proc)
223 {
224 auto prop = ((PropEncoding)mProp);
225 prop.process = proc;
226 mProp = prop.i;
227 }
228
230 int getProcess() const { return ((PropEncoding)mProp).process; }
231
234
235 void setToBeDone(bool f)
236 {
237 auto prop = ((PropEncoding)mProp);
238 prop.toBeDone = f;
239 mProp = prop.i;
240 }
241 bool getToBeDone() const { return ((PropEncoding)mProp).toBeDone; }
242
243 void setInhibited(bool f)
244 {
245 auto prop = ((PropEncoding)mProp);
246 prop.inhibited = f;
247 mProp = prop.i;
248 }
249 bool getInhibited() const { return ((PropEncoding)mProp).inhibited; }
250
251 bool isTransported() const { return getToBeDone() && !getInhibited(); };
252
254 const char* getProdProcessAsString() const;
255
256 private:
258 _T mStartVertexMomentumX, mStartVertexMomentumY, mStartVertexMomentumZ;
259
261 _T mStartVertexCoordinatesX, mStartVertexCoordinatesY, mStartVertexCoordinatesZ, mStartVertexCoordinatesT;
262
264 _T mWeight;
265
267 Int_t mPdgCode;
268
270 Int_t mMotherTrackId = -1;
271 Int_t mSecondMotherTrackId = -1;
272
273 Int_t mFirstDaughterTrackId = -1;
274 Int_t mLastDaughterTrackId = -1;
275 // hitmask stored as an int
276 // if bit i is set it means that this track left a trace in detector i
277 // we should have sizeof(int) < o2::base::DetId::nDetectors
278 Int_t mProp = 0;
279
280 // internal structure to allow convenient manipulation
281 // of properties as bits on an int
282 union PropEncoding {
283 PropEncoding(int a) : i(a) {}
284 int i;
285 struct {
286 int storage : 1; // encoding whether to store this track to the output
287 unsigned int process : 6; // encoding process that created this track (enough to store TMCProcess from ROOT)
288 int hitmask : NHITBITS; // encoding hits per detector
289 int reserved1 : 1; // bit reserved for possible future purposes
290 int inhibited : 1; // whether tracking of this was inhibited
291 int toBeDone : 1; // whether this (still) needs tracking --> we might more complete information to cover full ParticleStatus space
292 };
293 };
294
295 // Additional status codes for MC generator information.
296 // NOTE: This additional memory cost might be reduced by using bits elsewhere
297 // such as part of mProp (process) or mPDG
298 Int_t mStatusCode = 0;
299
300 ClassDefNV(MCTrackT, 8);
301};
302
303template <typename T>
304inline Double_t MCTrackT<T>::GetEnergy() const
305{
306 const auto mass = GetMass();
307 Double_t px = mStartVertexMomentumX;
308 Double_t py = mStartVertexMomentumY;
309 Double_t pz = mStartVertexMomentumZ;
310 return std::sqrt(mass * mass + px * px + py * py + pz * pz);
311}
312
313template <typename T>
314inline void MCTrackT<T>::GetMomentum(TVector3& momentum) const
315{
316 momentum.SetXYZ(mStartVertexMomentumX, mStartVertexMomentumY, mStartVertexMomentumZ);
317}
318
319template <typename T>
320inline void MCTrackT<T>::Get4Momentum(TLorentzVector& momentum) const
321{
322 momentum.SetXYZT(mStartVertexMomentumX, mStartVertexMomentumY, mStartVertexMomentumZ, GetEnergy());
323}
324
325template <typename T>
326inline void MCTrackT<T>::GetStartVertex(TVector3& vertex) const
327{
328 vertex.SetXYZ(mStartVertexCoordinatesX, mStartVertexCoordinatesY, mStartVertexCoordinatesZ);
329}
330
331template <typename T>
333 : mPdgCode(0),
334 mMotherTrackId(-1),
335 mSecondMotherTrackId(-1),
336 mFirstDaughterTrackId(-1),
337 mLastDaughterTrackId(-1),
338 mStartVertexMomentumX(0.),
339 mStartVertexMomentumY(0.),
340 mStartVertexMomentumZ(0.),
341 mStartVertexCoordinatesX(0.),
342 mStartVertexCoordinatesY(0.),
343 mStartVertexCoordinatesZ(0.),
344 mStartVertexCoordinatesT(0.),
345 mProp(0),
346 mWeight(0)
347{
348}
349
350template <typename T>
351inline MCTrackT<T>::MCTrackT(Int_t pdgCode, Int_t motherId, Int_t secondMotherId, Int_t firstDaughterId, Int_t lastDaughterId,
352 Double_t px, Double_t py, Double_t pz, Double_t x,
353 Double_t y, Double_t z, Double_t t, Int_t mask)
354 : mPdgCode(pdgCode),
355 mMotherTrackId(motherId),
356 mSecondMotherTrackId(secondMotherId),
357 mFirstDaughterTrackId(firstDaughterId),
358 mLastDaughterTrackId(lastDaughterId),
359 mStartVertexMomentumX(px),
360 mStartVertexMomentumY(py),
361 mStartVertexMomentumZ(pz),
362 mStartVertexCoordinatesX(x),
363 mStartVertexCoordinatesY(y),
364 mStartVertexCoordinatesZ(z),
365 mStartVertexCoordinatesT(t),
366 mProp(mask),
367 mWeight(0)
368{
369}
370
371template <typename T>
372inline MCTrackT<T>::MCTrackT(const TParticle& part)
373 : mPdgCode(part.GetPdgCode()),
374 mMotherTrackId(part.GetMother(0)),
375 mSecondMotherTrackId(part.GetMother(1)),
376 mFirstDaughterTrackId(part.GetFirstDaughter()),
377 mLastDaughterTrackId(part.GetLastDaughter()),
378 mStartVertexMomentumX(part.Px()),
379 mStartVertexMomentumY(part.Py()),
380 mStartVertexMomentumZ(part.Pz()),
381 mStartVertexCoordinatesX(part.Vx()),
382 mStartVertexCoordinatesY(part.Vy()),
383 mStartVertexCoordinatesZ(part.Vz()),
384 mStartVertexCoordinatesT(part.T() * 1e09),
385 mWeight(part.GetWeight()),
386 mProp(0),
387 mStatusCode(0)
388{
389 // our convention is to communicate the process as (part) of the unique ID
390 setProcess(part.GetUniqueID());
391 // extract storage flag
392 setStore(part.TestBit(ParticleStatus::kKeep));
393 // extract toBeDone flag
395 // extract inhibited flag
396 if (part.TestBit(ParticleStatus::kInhibited)) {
397 setToBeDone(true); // if inhibited, it had to be done: restore flag
398 setInhibited(true);
399 }
400 // set MC generator status code only for primaries
401 mStatusCode = part.TestBit(ParticleStatus::kPrimary) ? part.GetStatusCode() : -1;
402}
403
404template <typename T>
405inline void MCTrackT<T>::Print(Int_t trackId) const
406{
407 // LOG(debug) << "Track " << trackId << ", mother : " << mMotherTrackId << ", Type " << mPdgCode << ", momentum ("
408 // << mStartVertexMomentumX << ", " << mStartVertexMomentumY << ", " << mStartVertexMomentumZ << ") GeV"
409 // ;
410}
411
412template <typename T>
413inline Double_t MCTrackT<T>::GetMass() const
414{
415 bool success{};
416 auto mass = O2DatabasePDG::Mass(mPdgCode, success);
417 if (!success) {
418 // coming here is a mistake which should not happen
420 }
421 return mass;
422}
423
424template <typename T>
425inline Double_t MCTrackT<T>::GetRapidity() const
426{
427 const auto e = GetEnergy();
428 Double_t y =
429 0.5 * std::log((e + static_cast<double>(mStartVertexMomentumZ)) / (e - static_cast<double>(mStartVertexMomentumZ)));
430 return y;
431}
432
433template <typename T>
434inline const char* MCTrackT<T>::getProdProcessAsString() const
435{
436 auto procID = getProcess();
437 if (procID >= 0) {
438 return TMCProcessName[procID];
439 } else {
440 return TMCProcessName[TMCProcess::kPNoProcess];
441 }
442}
443
445} // end namespace o2
446
447#endif
uint64_t vertex
Definition RawEventData.h:9
int32_t i
@ kKeep
@ kInhibited
@ kPrimary
@ kToBeDone
bool leftTraceGivenBitField(int bit) const
Definition MCTrack.h:182
int getProcess() const
get the production process (id) of this track
Definition MCTrack.h:230
o2::mcgenstatus::MCGenStatusEncoding getStatusCode() const
get generator status code
Definition MCTrack.h:233
bool getToBeDone() const
Definition MCTrack.h:241
Int_t getFirstDaughterTrackId() const
Definition MCTrack.h:77
Double_t GetStartVertexMomentumZ() const
Definition MCTrack.h:81
void GetMomentum(TVector3 &momentum) const
Definition MCTrack.h:314
bool isSecondary() const
Definition MCTrack.h:76
Double_t GetP() const
Definition MCTrack.h:116
Double_t GetTheta() const
Definition MCTrack.h:148
Double_t Vy() const
Definition MCTrack.h:105
Double_t Vx() const
Definition MCTrack.h:104
Double_t R() const
production radius
Definition MCTrack.h:90
bool getInhibited() const
Definition MCTrack.h:249
void Get4Momentum(TLorentzVector &momentum) const
Definition MCTrack.h:320
static constexpr int NHITBITS
Definition MCTrack.h:49
Double_t GetStartVertexMomentumX() const
Definition MCTrack.h:79
bool leftTrace(Int_t iDet, std::vector< int > const &detIDtoBit) const
Definition MCTrack.h:189
MCTrackT(const MCTrackT &track)=default
Copy constructor.
bool isPrimary() const
Definition MCTrack.h:75
Int_t getLastDaughterTrackId() const
Definition MCTrack.h:78
_T getWeight() const
return particle weight
Definition MCTrack.h:96
void SetLastDaughterTrackId(Int_t id)
Definition MCTrack.h:169
Double_t Vz() const
Definition MCTrack.h:106
Double_t GetMass() const
return mass from PDG Database if known (print message in case cannot look up)
Definition MCTrack.h:413
void SetMotherTrackId(Int_t id)
Modifiers.
Definition MCTrack.h:166
void setToBeDone(bool f)
Definition MCTrack.h:235
const char * getProdProcessAsString() const
get the string representation of the production process
Definition MCTrack.h:434
Double_t GetRapidity() const
Definition MCTrack.h:425
void SetFirstDaughterTrackId(Int_t id)
Definition MCTrack.h:168
Double_t GetStartVertexCoordinatesY() const
Definition MCTrack.h:83
bool isTransported() const
Definition MCTrack.h:251
void setInhibited(bool f)
Definition MCTrack.h:243
void Print(Int_t iTrack=0) const
Output to screen.
Definition MCTrack.h:405
Double_t GetPt() const
Definition MCTrack.h:109
void setHitMask(Int_t m)
Definition MCTrack.h:164
MCTrackT()
Default constructor.
Definition MCTrack.h:332
Double_t GetStartVertexCoordinatesZ() const
Definition MCTrack.h:84
int getNumDet() const
Definition MCTrack.h:199
void SetSecondMotherTrackId(Int_t id)
Definition MCTrack.h:167
Double_t R2() const
production radius squared
Definition MCTrack.h:88
Double_t Py() const
Definition MCTrack.h:102
Double_t GetStartVertexMomentumY() const
Definition MCTrack.h:80
Double_t GetStartVertexCoordinatesT() const
Definition MCTrack.h:85
void GetStartVertex(TVector3 &vertex) const
Definition MCTrack.h:326
void setStore(bool f)
Definition MCTrack.h:212
Double_t GetEnergy() const
Definition MCTrack.h:304
MCTrackT(const TParticle &particle)
Constructor from TParticle.
Definition MCTrack.h:372
MCTrackT(Int_t pdgCode, Int_t motherID, Int_t secondMotherID, Int_t firstDaughterID, Int_t lastDaughterID, Double_t px, Double_t py, Double_t pz, Double_t x, Double_t y, Double_t z, Double_t t, Int_t nPoints)
Standard constructor.
Definition MCTrack.h:351
Double_t GetTgl() const
Definition MCTrack.h:142
Double_t T() const
Definition MCTrack.h:107
Int_t getSecondMotherTrackId() const
Definition MCTrack.h:74
Double_t Px() const
Definition MCTrack.h:101
~MCTrackT()=default
Destructor.
void setHit(Int_t iDetBit)
Definition MCTrack.h:174
Double_t GetEta() const
Definition MCTrack.h:131
Double_t Pz() const
Definition MCTrack.h:103
Int_t getHitMask() const
Accessors to the hit mask.
Definition MCTrack.h:163
Double_t GetStartVertexCoordinatesX() const
Definition MCTrack.h:82
Int_t GetPdgCode() const
Accessors.
Definition MCTrack.h:72
Double_t GetPhi() const
Definition MCTrack.h:124
bool getStore() const
Definition MCTrack.h:218
bool hasHits() const
determine if this track has hits
Definition MCTrack.h:220
void setProcess(int proc)
set process property
Definition MCTrack.h:222
Int_t getMotherTrackId() const
Definition MCTrack.h:73
static Double_t Mass(int pdg, bool &success, TDatabasePDG *db=O2DatabasePDG::Instance())
static constexpr int nDetectors
number of defined detectors
Definition DetID.h:96
GLint GLenum GLint x
Definition glcorearb.h:403
const GLfloat * m
Definition glcorearb.h:4066
GLint GLsizei count
Definition glcorearb.h:399
GLdouble f
Definition glcorearb.h:310
GLint y
Definition glcorearb.h:270
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLint GLuint mask
Definition glcorearb.h:291
GLuint id
Definition glcorearb.h:650
GLdouble GLdouble GLdouble z
Definition glcorearb.h:843
void printMassError(int pdg)
Definition MCTrack.cxx:21
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...