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 void setContributesToAHit(bool f = true)
255 {
256 auto prop = ((PropEncoding)mProp);
257 prop.contributesToAHit = f;
258 mProp = prop.i;
259 }
260 bool getContributesToAHit() const { return ((PropEncoding)mProp).contributesToAHit; }
261
263 const char* getProdProcessAsString() const;
264
265 private:
267 _T mStartVertexMomentumX, mStartVertexMomentumY, mStartVertexMomentumZ;
268
270 _T mStartVertexCoordinatesX, mStartVertexCoordinatesY, mStartVertexCoordinatesZ, mStartVertexCoordinatesT;
271
273 _T mWeight;
274
276 Int_t mPdgCode;
277
279 Int_t mMotherTrackId = -1;
280 Int_t mSecondMotherTrackId = -1;
281
282 Int_t mFirstDaughterTrackId = -1;
283 Int_t mLastDaughterTrackId = -1;
284 // hitmask stored as an int
285 // if bit i is set it means that this track left a trace in detector i
286 // we should have sizeof(int) < o2::base::DetId::nDetectors
287 Int_t mProp = 0;
288
289 // internal structure to allow convenient manipulation
290 // of properties as bits on an int
291 union PropEncoding {
292 PropEncoding(int a) : i(a) {}
293 int i;
294 struct {
295 int storage : 1; // encoding whether to store this track to the output
296 unsigned int process : 6; // encoding process that created this track (enough to store TMCProcess from ROOT)
297 int hitmask : NHITBITS; // encoding hits per detector
298 int contributesToAHit : 1; // whether this track deposited into a hit created by another track
299 int inhibited : 1; // whether tracking of this was inhibited
300 int toBeDone : 1; // whether this (still) needs tracking --> we might more complete information to cover full ParticleStatus space
301 };
302 };
303
304 // Additional status codes for MC generator information.
305 // NOTE: This additional memory cost might be reduced by using bits elsewhere
306 // such as part of mProp (process) or mPDG
307 Int_t mStatusCode = 0;
308
309 ClassDefNV(MCTrackT, 8);
310};
311
312template <typename T>
313inline Double_t MCTrackT<T>::GetEnergy() const
314{
315 const auto mass = GetMass();
316 Double_t px = mStartVertexMomentumX;
317 Double_t py = mStartVertexMomentumY;
318 Double_t pz = mStartVertexMomentumZ;
319 return std::sqrt(mass * mass + px * px + py * py + pz * pz);
320}
321
322template <typename T>
323inline void MCTrackT<T>::GetMomentum(TVector3& momentum) const
324{
325 momentum.SetXYZ(mStartVertexMomentumX, mStartVertexMomentumY, mStartVertexMomentumZ);
326}
327
328template <typename T>
329inline void MCTrackT<T>::Get4Momentum(TLorentzVector& momentum) const
330{
331 momentum.SetXYZT(mStartVertexMomentumX, mStartVertexMomentumY, mStartVertexMomentumZ, GetEnergy());
332}
333
334template <typename T>
335inline void MCTrackT<T>::GetStartVertex(TVector3& vertex) const
336{
337 vertex.SetXYZ(mStartVertexCoordinatesX, mStartVertexCoordinatesY, mStartVertexCoordinatesZ);
338}
339
340template <typename T>
342 : mPdgCode(0),
343 mMotherTrackId(-1),
344 mSecondMotherTrackId(-1),
345 mFirstDaughterTrackId(-1),
346 mLastDaughterTrackId(-1),
347 mStartVertexMomentumX(0.),
348 mStartVertexMomentumY(0.),
349 mStartVertexMomentumZ(0.),
350 mStartVertexCoordinatesX(0.),
351 mStartVertexCoordinatesY(0.),
352 mStartVertexCoordinatesZ(0.),
353 mStartVertexCoordinatesT(0.),
354 mProp(0),
355 mWeight(0)
356{
357}
358
359template <typename T>
360inline MCTrackT<T>::MCTrackT(Int_t pdgCode, Int_t motherId, Int_t secondMotherId, Int_t firstDaughterId, Int_t lastDaughterId,
361 Double_t px, Double_t py, Double_t pz, Double_t x,
362 Double_t y, Double_t z, Double_t t, Int_t mask)
363 : mPdgCode(pdgCode),
364 mMotherTrackId(motherId),
365 mSecondMotherTrackId(secondMotherId),
366 mFirstDaughterTrackId(firstDaughterId),
367 mLastDaughterTrackId(lastDaughterId),
368 mStartVertexMomentumX(px),
369 mStartVertexMomentumY(py),
370 mStartVertexMomentumZ(pz),
371 mStartVertexCoordinatesX(x),
372 mStartVertexCoordinatesY(y),
373 mStartVertexCoordinatesZ(z),
374 mStartVertexCoordinatesT(t),
375 mProp(mask),
376 mWeight(0)
377{
378}
379
380template <typename T>
381inline MCTrackT<T>::MCTrackT(const TParticle& part)
382 : mPdgCode(part.GetPdgCode()),
383 mMotherTrackId(part.GetMother(0)),
384 mSecondMotherTrackId(part.GetMother(1)),
385 mFirstDaughterTrackId(part.GetFirstDaughter()),
386 mLastDaughterTrackId(part.GetLastDaughter()),
387 mStartVertexMomentumX(part.Px()),
388 mStartVertexMomentumY(part.Py()),
389 mStartVertexMomentumZ(part.Pz()),
390 mStartVertexCoordinatesX(part.Vx()),
391 mStartVertexCoordinatesY(part.Vy()),
392 mStartVertexCoordinatesZ(part.Vz()),
393 mStartVertexCoordinatesT(part.T() * 1e09),
394 mWeight(part.GetWeight()),
395 mProp(0),
396 mStatusCode(0)
397{
398 // our convention is to communicate the process as (part) of the unique ID
399 setProcess(part.GetUniqueID());
400 // extract storage flag
401 setStore(part.TestBit(ParticleStatus::kKeep));
402 // extract toBeDone flag
404 // extract inhibited flag
405 if (part.TestBit(ParticleStatus::kInhibited)) {
406 setToBeDone(true); // if inhibited, it had to be done: restore flag
407 setInhibited(true);
408 }
409 // set MC generator status code only for primaries
410 mStatusCode = part.TestBit(ParticleStatus::kPrimary) ? part.GetStatusCode() : -1;
411}
412
413template <typename T>
414inline void MCTrackT<T>::Print(Int_t trackId) const
415{
416 // LOG(debug) << "Track " << trackId << ", mother : " << mMotherTrackId << ", Type " << mPdgCode << ", momentum ("
417 // << mStartVertexMomentumX << ", " << mStartVertexMomentumY << ", " << mStartVertexMomentumZ << ") GeV"
418 // ;
419}
420
421template <typename T>
422inline Double_t MCTrackT<T>::GetMass() const
423{
424 bool success{};
425 auto mass = O2DatabasePDG::Mass(mPdgCode, success);
426 if (!success) {
427 // coming here is a mistake which should not happen
429 }
430 return mass;
431}
432
433template <typename T>
434inline Double_t MCTrackT<T>::GetRapidity() const
435{
436 const auto e = GetEnergy();
437 Double_t y =
438 0.5 * std::log((e + static_cast<double>(mStartVertexMomentumZ)) / (e - static_cast<double>(mStartVertexMomentumZ)));
439 return y;
440}
441
442template <typename T>
443inline const char* MCTrackT<T>::getProdProcessAsString() const
444{
445 auto procID = getProcess();
446 if (procID >= 0) {
447 return TMCProcessName[procID];
448 } else {
449 return TMCProcessName[TMCProcess::kPNoProcess];
450 }
451}
452
454} // end namespace o2
455
456#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:323
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:329
static constexpr int NHITBITS
Definition MCTrack.h:49
Double_t GetStartVertexMomentumX() const
Definition MCTrack.h:79
bool getContributesToAHit() const
Definition MCTrack.h:260
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:422
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:443
Double_t GetRapidity() const
Definition MCTrack.h:434
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:414
Double_t GetPt() const
Definition MCTrack.h:109
void setHitMask(Int_t m)
Definition MCTrack.h:164
MCTrackT()
Default constructor.
Definition MCTrack.h:341
Double_t GetStartVertexCoordinatesZ() const
Definition MCTrack.h:84
int getNumDet() const
Definition MCTrack.h:199
void setContributesToAHit(bool f=true)
mark that this track deposited into a hit created by another track
Definition MCTrack.h:254
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:335
void setStore(bool f)
Definition MCTrack.h:212
Double_t GetEnergy() const
Definition MCTrack.h:313
MCTrackT(const TParticle &particle)
Constructor from TParticle.
Definition MCTrack.h:381
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:360
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())
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 ...