ATLAS Offline Software
AthTruthSelectionTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
12 #include "AthTruthSelectionTool.h"
15 #include "xAODTruth/TruthVertex.h"
16 
17 #include <vector>
18 #include <cmath>
19 
21 
22 namespace {
23  constexpr int electronId(11);
24  constexpr int gammaId(22);
25  bool hasAncestor(const xAOD::TruthParticle* particle, const std::vector<int>& allowedAncestors) {
26  bool pass = false;
27  uint nPar = particle->nParents();
28  for (uint i = 0; i < nPar; i++) {
29  for (const int & ancestorID : allowedAncestors) {
30  if (std::abs(particle->parent(i)->pdgId()) == ancestorID) {
31  return true;
32  }
33  }
34  }
35  for (uint i = 0; i < nPar; i++) {
36  const xAOD::TruthParticle* parent = particle->parent(i);
37  if (hasAncestor(parent, allowedAncestors)) return true;
38  }
39  return pass;
40  }
41 }
42 
43 
44 AthTruthSelectionTool::AthTruthSelectionTool(const std::string& type, const std::string& name,
45  const IInterface* parent) :
47 {
48  // declare interface from base class
49  declareInterface<IAthSelectionTool>(this);
50 }
51 
54  // can set cut properties now
55  using P_t = xAOD::TruthParticle;
56  using Accept_t = Accept<P_t>;
57  //
58  const std::vector<Accept_t> filters = {
59  // if p.pt=0, TVector3 generates an error when querying p.eta(); a limit of 1e-7 was not found to be enough to
60  // prevent this
61  // the following also vetoes the case where the p.pt()=NaN, as any inequality with NaN evaluates to false
62  Accept_t([this](const P_t& p) -> bool {
63  return((p.pt() > 0.1) ? (std::abs(p.eta()) < m_maxEta) : false);
64  }, std::string("eta")),
65  Accept_t([this](const P_t& p) -> bool {
66  return(p.pt() > m_minPt);
67  }, std::string("min_pt"))
68  };
69  //
70  m_cutList = CutList<P_t>(filters);
71  if (m_maxProdVertRadius>0) {
72  m_cutList.add(Accept_t([&m_maxProdVertRadius = std::as_const(m_maxProdVertRadius)](const P_t& p) -> bool {
73  return((not (p.hasProdVtx()))or(p.prodVtx()->perp() < m_maxProdVertRadius));
74  },
75  "decay_before_" + std::to_string(m_maxProdVertRadius)));
76  }
77  if (m_maxPt > 0) {
78  m_cutList.add(Accept_t([&m_maxPt = std::as_const(m_maxPt)](const P_t& p) {
79  return(p.pt() < m_maxPt);
80  }, "max_pt"));
81  }
82  if (m_requireSiHit > 0) {
83  m_cutList.add(Accept_t([&m_requireSiHit = std::as_const(m_requireSiHit)](const P_t& p) {
84  static const SG::AuxElement::ConstAccessor< float > nSilHitsAcc("nSilHits");
85  if (nSilHitsAcc.isAvailable(p)) return (nSilHitsAcc(p) >= m_requireSiHit);
86  else return false;
87  }, "siHit"));
88  }
90  m_cutList.add(Accept_t([](const P_t& p) {
92  }, "OnlyPrimary"));
93  }
94  if (m_requireCharged) {
95  m_cutList.add(Accept_t([&m_selectedCharge = std::as_const(m_selectedCharge)](const P_t& p) {
96  if(m_selectedCharge ==0) return(not (p.isNeutral()));
97  else return(not(p.isNeutral()) and p.charge()==m_selectedCharge);
98  }, "charged"));
99  }
100  if (m_requireStable) {
101  m_cutList.add(Accept_t([](const P_t& p) {
102  return(MC::isStable(&p));
103  }, "stable"));
104  }
105  if (m_pdgId > 0) {
106  m_cutList.add(Accept_t([&m_pdgId = std::as_const(m_pdgId)](const P_t& p) {
107  return(std::abs(p.pdgId()) == m_pdgId);
108  }, "pdgId"));
109  }
110  if (m_vetoPdgId > 0) {
111  m_cutList.add(Accept_t([&m_vetoPdgId = std::as_const(m_vetoPdgId)](const P_t& p) {
112  return(std::abs(p.pdgId()) != m_vetoPdgId);
113  }, "vetoPdgId"));
114  }
115  if (m_grandparent) {
116  m_cutList.add(Accept_t([](const P_t& p) {
117  return((p.nParents() == 0) || ((p.nParents() == 1)and((p.parent(0))->nParents() == 0)));
118  }, "hasNoGrandparent"));
119  }
120  //require the truth particles to come from certain ancesters
121  if (!m_ancestors.empty()) {
122  m_cutList.add(Accept_t([&m_ancestors = std::as_const(m_ancestors)](const P_t& p) -> bool {
123  const xAOD::TruthParticle* pTruth = dynamic_cast<const xAOD::TruthParticle*>(&p);
124  if (not pTruth) return false;
125  else return hasAncestor(pTruth, m_ancestors);
126  }, "ancestors"));
127  }
129  m_cutList.add(Accept_t([](const P_t& p) {
130  return((p.absPdgId() == electronId)and(p.nParents() >= 1) and(p.parent(0)) and(p.parent(0)->pdgId() == gammaId));
131  }, "poselectronfromgamma"));
132  }
133  if (m_radiusCylinder > 0) {
134  // m_cutList.add(Accept_t(acceptExtrapolatedTPToSurface, "SelectCylinder"));
135  if (not m_cylinder) {
136  ATH_MSG_VERBOSE("Creating and caching cylinder surface");
137  Amg::Transform3D trnsf;
138  trnsf.setIdentity();
139  m_cylinder = std::make_unique<Trk::CylinderSurface>( trnsf, m_radiusCylinder, 20000.);
140  }
141  m_cutList.add(Accept_t([this](const P_t& p) -> bool {
142  ATH_MSG_VERBOSE("Checking particle for intersection with cylinder of radius " << m_radiusCylinder);
143  //create surface we extrapolate to and cache it
144  const xAOD::TruthVertex* ptruthVertex = p.prodVtx();
145  if (ptruthVertex == nullptr) {
146  //cannot derive production vertex, reject track
147  ATH_MSG_VERBOSE("Rejecting particle without production vertex.");
148  return false;
149  }
150  const auto xPos = ptruthVertex->x();
151  const auto yPos = ptruthVertex->y();
152  const auto z_truth = ptruthVertex->z();
153  const Amg::Vector3D position(xPos, yPos, z_truth);
154  const Amg::Vector3D momentum(p.px(), p.py(), p.pz());
155  const Trk::CurvilinearParameters cParameters(position, momentum, p.charge());
156  const Trk::TrackParameters *exParameters = m_extrapolator->extrapolate(Gaudi::Hive::currentContext(),
157  cParameters,
158  *m_cylinder,
159  Trk::anyDirection, false, Trk::pion).release();
160  if (!exParameters) {
161  ATH_MSG_VERBOSE("Failed extrapolation. Rejecting track.");
162  return false;
163  }
164  ATH_MSG_VERBOSE("Extrapolated parameters to cylinder: " << *exParameters);
165  const float ex_abs_z = fabs(exParameters->position().z());
166  if ( (ex_abs_z > m_minZCylinder) and (ex_abs_z < m_maxZCylinder) ) {
167  ATH_MSG_VERBOSE("Particle accepted.");
168  return true;
169  }
170  //else..
171  ATH_MSG_VERBOSE("Particle rejected");
172  return false;
173  }, "SelectCylinder"));
174  } else if (m_zDisc > 0) {
175  //m_cutList.add(Accept_t(acceptExtrapolatedTPToSurface, "SelectDisc"));
176  if (not m_disc1 || not m_disc2) { //m_disc2 == 0 implied
177  ATH_MSG_VERBOSE("Creating and caching disc surface");
179  m_disc1 = std::make_unique<Trk::DiscSurface>( trnsf_shiftZ, m_minRadiusDisc, m_maxRadiusDisc);
180  trnsf_shiftZ = Amg::Translation3D(0.,0.,-m_zDisc);
181  m_disc2 = std::make_unique<Trk::DiscSurface>( trnsf_shiftZ, m_minRadiusDisc, m_maxRadiusDisc);
182  }
183  m_cutList.add(Accept_t([this](const P_t& p) -> bool {
184  ATH_MSG_VERBOSE("Checking particle for intersection with discs of |z| " << m_zDisc);
185  //create surface we extrapolate to and cache it
186  const xAOD::TruthVertex* ptruthVertex = p.prodVtx();
187  if (ptruthVertex == nullptr) {
188  //cannot derive production vertex, reject track
189  ATH_MSG_VERBOSE("Rejecting particle without production vertex.");
190  return false;
191  }
192  const auto xPos = ptruthVertex->x();
193  const auto yPos = ptruthVertex->y();
194  const auto z_truth = ptruthVertex->z();
195  const Amg::Vector3D position(xPos, yPos, z_truth);
196  const Amg::Vector3D momentum(p.px(), p.py(), p.pz());
197  const Trk::CurvilinearParameters cParameters(position, momentum, p.charge());
198  const Trk::TrackParameters *exParameters = m_extrapolator->extrapolate(Gaudi::Hive::currentContext(),
199  cParameters,
200  *m_disc1, Trk::anyDirection, true, Trk::pion).release();
201  if (exParameters) {
202  //since boundary check is true, should be enough to say we've hit the disk..
203  ATH_MSG_VERBOSE("Successfully extrapolated track to disk at +" << m_zDisc << ": " << *exParameters);
204  float ex_radius = sqrt(pow(exParameters->position().x(),2)+pow(exParameters->position().y(),2));
205  ATH_MSG_VERBOSE("radial position at surface: " << ex_radius);
206  if ((ex_radius > m_minRadiusDisc) and (ex_radius < m_maxRadiusDisc)) {
207  ATH_MSG_VERBOSE("Confirmed within the disk. Accepting particle");
208  return true;
209  }
210  //else...
211  ATH_MSG_VERBOSE("Strange, extrapolation succeeded but extrapolated position not within disc radius! Test next disc");
212  }
213  exParameters = m_extrapolator->extrapolate(Gaudi::Hive::currentContext(),cParameters, *m_disc2, Trk::anyDirection, true, Trk::pion).release();
214  if (exParameters) {
215  //since boundary check is true, should be enough to say we've hit the disk..
216  ATH_MSG_VERBOSE("Successfully extrapolated track to disk at -" << m_zDisc << ": " << *exParameters);
217  float ex_radius = sqrt(pow(exParameters->position().x(),2)+pow(exParameters->position().y(),2));
218  ATH_MSG_VERBOSE("radial position at surface: " << ex_radius);
219  if ((ex_radius > m_minRadiusDisc) and (ex_radius < m_maxRadiusDisc)) {
220  ATH_MSG_VERBOSE("Confirmed within the disk. Accepting particle");
221  return true;
222  }
223  //else...
224  ATH_MSG_VERBOSE("Strange, extrapolation succeeded but extrapolated position not within disc radius! Rejecting");
225  }
226  //else..
227  ATH_MSG_VERBOSE("Particle rejected");
228  return false;
229  }, "SelectDisc"));
230  } //m_zDisc > 0
231 
232  std::string msg = std::to_string(m_cutList.size()) + " truth acceptance cuts are used:\n";
233  for (const auto& i:m_cutList.names()) {
234  msg += i + "\n";
235  }
236  ATH_MSG_INFO(msg);
237 
238  ATH_CHECK(m_extrapolator.retrieve(EnableTool{ m_radiusCylinder > 0 || m_zDisc >0 }));
239 
240  return StatusCode::SUCCESS;
241 }
242 
245  // nop
246  return StatusCode::SUCCESS;
247 }
248 
249 
250 std::vector<std::string>
252  return m_cutList.names();
253 }
254 
257  const xAOD::TruthParticle* pTruth = dynamic_cast<const xAOD::TruthParticle*>(particle);
258 
259  //@TODO
260  if (not pTruth) {
261  return m_cutList.size()+1; // marker for invalid particles
262  }
263  return m_cutList.accept(*pTruth);
264 }
265 
267 AthTruthSelectionTool::testAllCuts(const xAOD::IParticle * particle, std::vector<unsigned int> &counter) const {
268  const xAOD::TruthParticle* pTruth = dynamic_cast<const xAOD::TruthParticle*>(particle);
269 
270  //@TODO
271  if (not pTruth) {
272  return m_cutList.size()+1; // marker for invalid particles
273  }
274  return m_cutList.testAllCuts(*pTruth,counter);
275 }
276 
AthTruthSelectionTool::m_vetoPdgId
IntegerProperty m_vetoPdgId
Definition: AthTruthSelectionTool.h:64
Trk::anyDirection
@ anyDirection
Definition: PropDirection.h:22
AthTruthSelectionTool::m_maxRadiusDisc
FloatProperty m_maxRadiusDisc
Definition: AthTruthSelectionTool.h:83
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:79
AthTruthSelectionTool::m_cylinder
std::unique_ptr< Trk::CylinderSurface > m_cylinder
Definition: AthTruthSelectionTool.h:87
TrackParameters.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AthTruthSelectionTool::m_pdgId
IntegerProperty m_pdgId
Definition: AthTruthSelectionTool.h:63
AthTruthSelectionTool::m_radiusCylinder
FloatProperty m_radiusCylinder
Definition: AthTruthSelectionTool.h:73
Trk::ParametersBase::position
const Amg::Vector3D & position() const
Access method for the position.
IAthSelectionTool::CutResult
Definition: IAthSelectionTool.h:30
AthTruthSelectionTool::finalize
StatusCode finalize() final
Definition: AthTruthSelectionTool.cxx:244
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
CutList
Templated CutList class to contain a group of cuts.
Definition: CutFlow.h:93
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
xAOD::TruthVertex_v1::y
float y() const
Vertex y displacement.
AthTruthSelectionTool::m_minPt
FloatProperty m_minPt
Definition: AthTruthSelectionTool.h:54
AthTruthSelectionTool::m_maxEta
FloatProperty m_maxEta
Definition: AthTruthSelectionTool.h:52
AthTruthSelectionTool::m_requireOnlyPrimary
BooleanProperty m_requireOnlyPrimary
Definition: AthTruthSelectionTool.h:55
uint
unsigned int uint
Definition: LArOFPhaseFill.cxx:20
AthTruthSelectionTool::m_disc1
std::unique_ptr< Trk::DiscSurface > m_disc1
Definition: AthTruthSelectionTool.h:88
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
ParticleGun_EoverP_Config.momentum
momentum
Definition: ParticleGun_EoverP_Config.py:63
HepMC::is_simulation_particle
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
Definition: MagicNumbers.h:354
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
AthTruthSelectionTool::names
std::vector< std::string > names() const final
return the names of the cuts as a vector<string>
Definition: AthTruthSelectionTool.cxx:251
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
Trk::pion
@ pion
Definition: ParticleHypothesis.h:32
Amg::Transform3D
Eigen::Affine3d Transform3D
Definition: GeoPrimitives.h:46
xAOD::TruthParticle
TruthParticle_v1 TruthParticle
Typedef to implementation.
Definition: Event/xAOD/xAODTruth/xAODTruth/TruthParticle.h:15
AthTruthSelectionTool::initialize
StatusCode initialize() final
Definition: AthTruthSelectionTool.cxx:53
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
AthTruthSelectionTool::m_requireSiHit
IntegerProperty m_requireSiHit
Definition: AthTruthSelectionTool.h:59
AthTruthSelectionTool::m_minZCylinder
FloatProperty m_minZCylinder
Definition: AthTruthSelectionTool.h:75
AthTruthSelectionTool.h
Trk::ParametersBase
Definition: ParametersBase.h:55
Trk::CurvilinearParametersT
Definition: CurvilinearParametersT.h:48
AthTruthSelectionTool::m_ancestors
IntegerArrayProperty m_ancestors
Definition: AthTruthSelectionTool.h:68
TruthVertex.h
xAOD::TruthVertex_v1
Class describing a truth vertex in the MC record.
Definition: TruthVertex_v1.h:37
AthTruthSelectionTool::m_selectedCharge
IntegerProperty m_selectedCharge
Definition: AthTruthSelectionTool.h:57
AthTruthSelectionTool::m_extrapolator
PublicToolHandle< Trk::IExtrapolator > m_extrapolator
Too handle for truth-track extrapolation.
Definition: AthTruthSelectionTool.h:96
AthTruthSelectionTool::testAllCuts
virtual IAthSelectionTool::CutResult testAllCuts(const xAOD::IParticle *p, std::vector< unsigned int > &counter) const final
The most important method to determine whether the particle is accepted.
Definition: AthTruthSelectionTool.cxx:267
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
MagicNumbers.h
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
AthTruthSelectionTool::m_poselectronfromgamma
BooleanProperty m_poselectronfromgamma
Definition: AthTruthSelectionTool.h:66
xAOD::TruthVertex_v1::x
float x() const
Vertex x displacement.
MC::isStable
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
Definition: HepMCHelpers.h:45
xAOD::TruthVertex_v1::z
float z() const
Vertex longitudinal distance along the beam line form the origin.
Amg::Translation3D
Eigen::Translation< double, 3 > Translation3D
Definition: GeoPrimitives.h:44
AthTruthSelectionTool::AthTruthSelectionTool
AthTruthSelectionTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: AthTruthSelectionTool.cxx:44
AthCommonMsg< AlgTool >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
AthTruthSelectionTool::m_requireCharged
BooleanProperty m_requireCharged
Definition: AthTruthSelectionTool.h:56
AthTruthSelectionTool::m_disc2
std::unique_ptr< Trk::DiscSurface > m_disc2
Definition: AthTruthSelectionTool.h:89
AthTruthSelectionTool::m_zDisc
FloatProperty m_zDisc
Definition: AthTruthSelectionTool.h:79
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
AthTruthSelectionTool::m_minRadiusDisc
FloatProperty m_minRadiusDisc
Definition: AthTruthSelectionTool.h:81
AthTruthSelectionTool::m_cutList
CutList< xAOD::TruthParticle > m_cutList
Definition: AthTruthSelectionTool.h:50
AthAlgTool
Definition: AthAlgTool.h:26
Accept
Templated class containing a cut, name of cut and description of cut(optional) Typically,...
Definition: CutFlow.h:28
test_pyathena.counter
counter
Definition: test_pyathena.py:15
AthTruthSelectionTool::m_grandparent
BooleanProperty m_grandparent
Definition: AthTruthSelectionTool.h:65
pow
constexpr int pow(int base, int exp) noexcept
Definition: ap_fixedTest.cxx:15
AthTruthSelectionTool::accept
virtual IAthSelectionTool::CutResult accept(const xAOD::IParticle *particle) const final
The most important method to determine whether the particle is accepted.
Definition: AthTruthSelectionTool.cxx:256
AthTruthSelectionTool::m_maxProdVertRadius
FloatProperty m_maxProdVertRadius
Definition: AthTruthSelectionTool.h:62
HepMCHelpers.h
AthTruthSelectionTool::m_maxPt
FloatProperty m_maxPt
Definition: AthTruthSelectionTool.h:53
AthTruthSelectionTool::m_maxZCylinder
FloatProperty m_maxZCylinder
Definition: AthTruthSelectionTool.h:77
AthTruthSelectionTool::m_requireStable
BooleanProperty m_requireStable
Definition: AthTruthSelectionTool.h:58