Project
Loading...
Searching...
No Matches
MCUtils.cxx
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
12//
13// Created by Sandro Wenzel on 03.11.21.
14//
15
18
19namespace o2::mcutils
20{
21
22o2::MCTrack const* MCTrackNavigator::getMother(const o2::MCTrack& p, const std::vector<o2::MCTrack>& pcontainer)
23{
24 const auto mid = p.getMotherTrackId();
25 if (mid < 0 or mid > pcontainer.size()) {
26 return nullptr;
27 }
28 return &(pcontainer[mid]);
29}
30
31o2::MCTrack const* MCTrackNavigator::getDaughter(const o2::MCTrack& p, const std::vector<o2::MCTrack>& pcontainer)
32{
33 const auto did = p.getFirstDaughterTrackId();
34 if (did < 0 or did > pcontainer.size()) {
35 return nullptr;
36 }
37 return &(pcontainer[did]);
38}
39
40o2::MCTrack const* MCTrackNavigator::getDaughter0(const o2::MCTrack& p, const std::vector<o2::MCTrack>& pcontainer)
41{
42 const auto did = p.getFirstDaughterTrackId();
43 if (did < 0 or did > pcontainer.size()) {
44 return nullptr;
45 }
46 return &(pcontainer[did]);
47}
48
49o2::MCTrack const* MCTrackNavigator::getDaughter1(const o2::MCTrack& p, const std::vector<o2::MCTrack>& pcontainer)
50{
51 const auto did = p.getLastDaughterTrackId();
52 if (did < 0 or did > pcontainer.size()) {
53 return nullptr;
54 }
55 return &(pcontainer[did]);
56}
57
58o2::MCTrack const& MCTrackNavigator::getFirstPrimary(const o2::MCTrack& p, const std::vector<o2::MCTrack>& pcontainer)
59{
60 if (p.isPrimary()) {
61 return p;
62 }
63 o2::MCTrack const* ptr = &p;
64 while (true) {
65 ptr = getMother(*ptr, pcontainer);
66 if (ptr->isPrimary()) {
67 return *ptr;
68 }
69 }
70}
71
72// taken from AliRoot and adapted to use of o2::MCTrack class
73bool MCTrackNavigator::isPhysicalPrimary(o2::MCTrack const& p, std::vector<o2::MCTrack> const& pcontainer)
74{
75 // Test if a particle is a physical primary according to the following definition:
76 // Particles produced in the collision including products of strong and
77 // electromagnetic decay and excluding feed-down from weak decays of strange
78 // particles.
79 //
80
81 const int hepmcStatusCode = o2::mcgenstatus::getHepMCStatusCode(p.getStatusCode()); // the HepMC status code
82 const int pdg = std::abs(p.GetPdgCode());
83 //
84 // Initial state particle
85 // Solution for K0L decayed by Pythia6
86 // ->
87 // ist > 1 --> essentially means unstable
88 if ((hepmcStatusCode > 1) && (pdg != 130) && p.isPrimary()) {
89 return false;
90 }
91 if ((hepmcStatusCode > 1) && p.isSecondary()) {
92 return false;
93 }
94 // <-
95
96 if (!isStable(pdg)) {
97 return false;
98 }
99
100 if (p.isPrimary()) {
101 //
102 // Particle produced by generator
103 // Solution for K0L decayed by Pythia6
104 // ->
105 const auto ipm = getMother(p, pcontainer);
106 if (ipm != nullptr) {
107 if (std::abs(ipm->GetPdgCode()) == 130) {
108 return false;
109 }
110 }
111 // <-
112 // check for direct photon in parton shower
113 // ->
114 if (pdg == 22) {
115 const auto ipd = getDaughter(p, pcontainer);
116 if (ipd && ipd->GetPdgCode() == 22) {
117 return false;
118 }
119 }
120 // <-
121 return true;
122 } else {
123 //
124 // Particle produced during transport
125 //
126
127 auto pm = getMother(p, pcontainer);
128 int mpdg = std::abs(pm->GetPdgCode());
129 // Check for Sigma0
130 if ((mpdg == 3212) && pm->isPrimary()) {
131 return true;
132 }
133
134 //
135 // Check if it comes from a pi0 decay
136 //
137 if ((mpdg == kPi0) && pm->isPrimary()) {
138 return true;
139 }
140
141 // Check if this is a heavy flavor decay product
142 int mfl = int(mpdg / std::pow(10, int(std::log10(mpdg))));
143 //
144 // Light hadron
145 if (mfl < 4) {
146 return false;
147 }
148
149 //
150 // Heavy flavor hadron produced by generator
151 if (pm->isPrimary()) {
152 return true;
153 }
154
155 // To be sure that heavy flavor has not been produced in a secondary interaction
156 // Loop back to the generated mother
157 while (!pm->isPrimary()) {
158 pm = getMother(*pm, pcontainer);
159 }
160 mpdg = std::abs(pm->GetPdgCode());
161 mfl = int(mpdg / std::pow(10, int(std::log10(mpdg))));
162
163 if (mfl < 4) {
164 return false;
165 } else {
166 return true;
167 }
168 } // end else branch produced by generator
169}
170
171bool MCTrackNavigator::isFromPrimaryDecayChain(o2::MCTrack const& p, std::vector<o2::MCTrack> const& pcontainer)
172{
177 if (p.getProcess() != kPDecay) {
178 return false;
179 }
181 auto mother = getMother(p, pcontainer);
182 if (!mother || mother->isPrimary()) {
183 return true;
184 }
186 return isFromPrimaryDecayChain(*mother, pcontainer);
187}
188
189bool MCTrackNavigator::isKeepPhysics(o2::MCTrack const& p, std::vector<o2::MCTrack> const& pcontainer)
190{
191 auto isFromPrimaryPairProduction = [&pcontainer](const MCTrack& part) {
197 if (part.getProcess() != kPPair) {
198 return false;
199 }
200 auto mother = getMother(part, pcontainer);
201 if (!mother || mother->isPrimary()) {
202 return true;
203 }
205 return isFromPrimaryDecayChain(*mother, pcontainer);
206 };
207 //
208 return p.isPrimary() || isFromPrimaryPairProduction(p) || isFromPrimaryDecayChain(p, pcontainer);
209}
210
211void MCGenHelper::encodeParticleStatusAndTracking(TParticle& particle, bool wanttracking)
212{
213 auto status = particle.GetStatusCode();
214 if (!mcgenstatus::isEncoded(status)) {
215 particle.SetStatusCode(mcgenstatus::MCGenStatusEncoding(status, 0).fullEncoding);
216 }
217 particle.SetBit(ParticleStatus::kToBeDone, wanttracking);
218}
219
220void MCGenHelper::encodeParticleStatusAndTracking(TParticle& particle, int hepmcStatus, int genStatus, bool wanttracking)
221{
222 particle.SetStatusCode(mcgenstatus::MCGenStatusEncoding(hepmcStatus, genStatus).fullEncoding);
223 particle.SetBit(ParticleStatus::kToBeDone, wanttracking);
224}
225
226} // namespace o2::mcutils
Utility functions for MC particles.
@ kToBeDone
TBranch * ptr
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
Int_t getFirstDaughterTrackId() const
Definition MCTrack.h:77
bool isSecondary() const
Definition MCTrack.h:76
bool isPrimary() const
Definition MCTrack.h:75
Int_t getLastDaughterTrackId() const
Definition MCTrack.h:78
Int_t GetPdgCode() const
Accessors.
Definition MCTrack.h:72
Int_t getMotherTrackId() const
Definition MCTrack.h:73
static void encodeParticleStatusAndTracking(TParticle &particle, bool wanttracking=true)
Definition MCUtils.cxx:211
static o2::MCTrack const * getMother(o2::MCTrack const &p, std::vector< o2::MCTrack > const &pcontainer)
Given an MCTrack p; Return it's direct mother or nullptr. (we follow only first mother)
Definition MCUtils.cxx:22
static bool isPhysicalPrimary(o2::MCTrack const &p, std::vector< o2::MCTrack > const &pcontainer)
Definition MCUtils.cxx:73
static o2::MCTrack const & getFirstPrimary(o2::MCTrack const &p, std::vector< o2::MCTrack > const &pcontainer)
Definition MCUtils.cxx:58
static o2::MCTrack const * getDaughter1(o2::MCTrack const &p, std::vector< o2::MCTrack > const &pcontainer)
Given an MCTrack p; Return it's second direct daughter or nullptr.
Definition MCUtils.cxx:49
static bool isFromPrimaryDecayChain(o2::MCTrack const &p, std::vector< o2::MCTrack > const &pcontainer)
Definition MCUtils.cxx:171
static o2::MCTrack const * getDaughter(o2::MCTrack const &p, std::vector< o2::MCTrack > const &pcontainer)
Given an MCTrack p; Return it's direct daughter or nullptr. (we follow only first daughter)
Definition MCUtils.cxx:31
static o2::MCTrack const * getDaughter0(o2::MCTrack const &p, std::vector< o2::MCTrack > const &pcontainer)
Given an MCTrack p; Return it's first direct daughter or nullptr.
Definition MCUtils.cxx:40
static bool isKeepPhysics(o2::MCTrack const &p, std::vector< o2::MCTrack > const &pcontainer)
Definition MCUtils.cxx:189
int getHepMCStatusCode(MCGenStatusEncoding enc)
bool isEncoded(MCGenStatusEncoding statusCode)
bool isStable(int pdg)
Determine if a particle (identified by pdg) is stable.
Definition MCUtils.h:87