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"
14 #include "xAODTruth/TruthVertex.h"
15 
16 #include <vector>
17 #include <cmath>
18 
20 
21 namespace {
22  constexpr int electronId(11);
23  constexpr int gammaId(22);
24  bool hasAncestor(const xAOD::TruthParticle* particle, const std::vector<int>& allowedAncestors) {
25  bool pass = false;
26  uint nPar = particle->nParents();
27  for (uint i = 0; i < nPar; i++) {
28  for (const int & ancestorID : allowedAncestors) {
29  if (std::abs(particle->parent(i)->pdgId()) == ancestorID) {
30  return true;
31  }
32  }
33  }
34  for (uint i = 0; i < nPar; i++) {
35  const xAOD::TruthParticle* parent = particle->parent(i);
36  if (hasAncestor(parent, allowedAncestors)) return true;
37  }
38  return pass;
39  }
40 }
41 
42 
43 AthTruthSelectionTool::AthTruthSelectionTool(const std::string& type, const std::string& name,
44  const IInterface* parent) :
46  m_counters{}
47 {
48  // declare interface from base class
49  declareInterface<IAthSelectionTool>(this);
50  // declareProperty( "Property", m_nProperty ); set defaults
51  declareProperty("maxEta", m_maxEta = 2.5);
52  declareProperty("minPt", m_minPt = 400);
53  declareProperty("maxPt", m_maxPt = -1);
54  declareProperty("requireOnlyPrimary", m_requireOnlyPrimary = true);
55  declareProperty("requireCharged", m_requireCharged = true);
56  declareProperty("selectedCharge", m_selectedCharge = 0);
57  declareProperty("requireStatus1", m_requireStatus1 = true);
58  declareProperty("requireSiHit", m_requireSiHit = 0);
59  declareProperty("maxProdVertRadius", m_maxProdVertRadius = 110.);
60  declareProperty("pdgId", m_pdgId = -1);
61  declareProperty("hasNoGrandparent", m_grandparent = false);
62  declareProperty("ancestorList", m_ancestors = {});
63  declareProperty("poselectronfromgamma", m_poselectronfromgamma = false);
64  declareProperty("radiusCylinder", m_radiusCylinder=-1, "Select truth particle based on extrapolated position on cylinder placed at this radius. Enabled if greater than 0.");
65  declareProperty("minZCylinder", m_minZCylinder=0.0, "Minimum |Z| on cylinder for accepting extrapolated truth particle to surface.");
66  declareProperty("maxZCylinder", m_maxZCylinder=0.0, "Maximum |Z| on cylinder for accepting extrapolated truth particle to surface.");
67  declareProperty("zDisc", m_zDisc=-1.0, "Select truth particle based on extrapolated position on disks placed at +/- z positions. Enabled if greater than 0.");
68  declareProperty("minRadiusDisc", m_minRadiusDisc=0.0, "Minimum radius on disk for accepting extrapolated truth particle to surface.");
69  declareProperty("maxRadiusDisc", m_maxRadiusDisc=0.0, "Maximum radius on disk for accepting extrapolated truth particle to surface.");
70 }
71 
74  // can set cut properties now
75  using P_t = xAOD::TruthParticle;
76  using Accept_t = Accept<P_t>;
77  //
78  const std::vector<Accept_t> filters = {
79  // if p.pt=0, TVector3 generates an error when querying p.eta(); a limit of 1e-7 was not found to be enough to
80  // prevent this
81  // the following also vetoes the case where the p.pt()=NaN, as any inequality with NaN evaluates to false
82  Accept_t([this](const P_t& p) -> bool {
83  return((p.pt() > 0.1) ? (std::abs(p.eta()) < m_maxEta) : false);
84  }, std::string("eta")),
85  Accept_t([this](const P_t& p) -> bool {
86  return(p.pt() > m_minPt);
87  }, std::string("min_pt"))
88  };
89  //
90  m_cutList = CutList<P_t>(filters);
91  if (m_maxProdVertRadius>0) {
92  m_cutList.add(Accept_t([&m_maxProdVertRadius = std::as_const(m_maxProdVertRadius)](const P_t& p) -> bool {
93  return((not (p.hasProdVtx()))or(p.prodVtx()->perp() < m_maxProdVertRadius));
94  },
95  "decay_before_" + std::to_string(m_maxProdVertRadius)));
96  }
97  if (m_maxPt > 0) {
98  m_cutList.add(Accept_t([&m_maxPt = std::as_const(m_maxPt)](const P_t& p) {
99  return(p.pt() < m_maxPt);
100  }, "max_pt"));
101  }
102  if (m_requireSiHit > 0) {
103  m_cutList.add(Accept_t([&m_requireSiHit = std::as_const(m_requireSiHit)](const P_t& p) {
104  static const SG::AuxElement::ConstAccessor< float > nSilHitsAcc("nSilHits");
105  if (nSilHitsAcc.isAvailable(p)) return (nSilHitsAcc(p) >= m_requireSiHit);
106  else return false;
107  }, "siHit"));
108  }
109  if (m_requireOnlyPrimary) {
110  m_cutList.add(Accept_t([](const P_t& p) {
111  return(!HepMC::is_simulation_particle(&p));
112  }, "OnlyPrimary"));
113  }
114  if (m_requireCharged) {
115  m_cutList.add(Accept_t([&m_selectedCharge = std::as_const(m_selectedCharge)](const P_t& p) {
116  if(m_selectedCharge ==0) return(not (p.isNeutral()));
117  else return(not(p.isNeutral()) and p.charge()==m_selectedCharge);
118  }, "charged"));
119  }
120  if (m_requireStatus1) {
121  m_cutList.add(Accept_t([](const P_t& p) {
122  return(p.status() == 1);
123  }, "status1"));
124  }
125  if (m_pdgId > 0) {
126  m_cutList.add(Accept_t([&m_pdgId = std::as_const(m_pdgId)](const P_t& p) {
127  return(std::abs(p.pdgId()) == m_pdgId);
128  }, "pdgId"));
129  }
130  if (m_grandparent) {
131  m_cutList.add(Accept_t([](const P_t& p) {
132  return((p.nParents() == 0) || ((p.nParents() == 1)and((p.parent(0))->nParents() == 0)));
133  }, "hasNoGrandparent"));
134  }
135  //require the truth particles to come from certain ancesters
136  if (!m_ancestors.empty()) {
137  m_cutList.add(Accept_t([&m_ancestors = std::as_const(m_ancestors)](const P_t& p) -> bool {
138  const xAOD::TruthParticle* pTruth = dynamic_cast<const xAOD::TruthParticle*>(&p);
139  if (not pTruth) return false;
140  else return hasAncestor(pTruth, m_ancestors);
141  }, "ancestors"));
142  }
144  m_cutList.add(Accept_t([](const P_t& p) {
145  return((p.absPdgId() == electronId)and(p.nParents() >= 1) and(p.parent(0)) and(p.parent(0)->pdgId() == gammaId));
146  }, "poselectronfromgamma"));
147  }
148  if (m_radiusCylinder > 0) {
149  // m_cutList.add(Accept_t(acceptExtrapolatedTPToSurface, "SelectCylinder"));
150  if (not m_cylinder) {
151  ATH_MSG_VERBOSE("Creating and caching cylinder surface");
152  Amg::Transform3D trnsf;
153  trnsf.setIdentity();
154  m_cylinder = std::make_unique<Trk::CylinderSurface>( trnsf, m_radiusCylinder, 20000.);
155  }
156  m_cutList.add(Accept_t([this](const P_t& p) -> bool {
157  ATH_MSG_VERBOSE("Checking particle for intersection with cylinder of radius " << m_radiusCylinder);
158  //create surface we extrapolate to and cache it
159  const xAOD::TruthVertex* ptruthVertex = p.prodVtx();
160  if (ptruthVertex == nullptr) {
161  //cannot derive production vertex, reject track
162  ATH_MSG_VERBOSE("Rejecting particle without production vertex.");
163  return false;
164  }
165  const auto xPos = ptruthVertex->x();
166  const auto yPos = ptruthVertex->y();
167  const auto z_truth = ptruthVertex->z();
168  const Amg::Vector3D position(xPos, yPos, z_truth);
169  const Amg::Vector3D momentum(p.px(), p.py(), p.pz());
170  const Trk::CurvilinearParameters cParameters(position, momentum, p.charge());
171  const Trk::TrackParameters *exParameters = m_extrapolator->extrapolate(Gaudi::Hive::currentContext(),
172  cParameters,
173  *m_cylinder,
174  Trk::anyDirection, false, Trk::pion).release();
175  if (!exParameters) {
176  ATH_MSG_VERBOSE("Failed extrapolation. Rejecting track.");
177  return false;
178  }
179  ATH_MSG_VERBOSE("Extrapolated parameters to cylinder: " << *exParameters);
180  const float ex_abs_z = fabs(exParameters->position().z());
181  if ( (ex_abs_z > m_minZCylinder) and (ex_abs_z < m_maxZCylinder) ) {
182  ATH_MSG_VERBOSE("Particle accepted.");
183  return true;
184  }
185  //else..
186  ATH_MSG_VERBOSE("Particle rejected");
187  return false;
188  }, "SelectCylinder"));
189  } else if (m_zDisc > 0) {
190  //m_cutList.add(Accept_t(acceptExtrapolatedTPToSurface, "SelectDisc"));
191  if (not m_disc1 || not m_disc2) { //m_disc2 == 0 implied
192  ATH_MSG_VERBOSE("Creating and caching disc surface");
194  m_disc1 = std::make_unique<Trk::DiscSurface>( trnsf_shiftZ, m_minRadiusDisc, m_maxRadiusDisc);
195  trnsf_shiftZ = Amg::Translation3D(0.,0.,-m_zDisc);
196  m_disc2 = std::make_unique<Trk::DiscSurface>( trnsf_shiftZ, m_minRadiusDisc, m_maxRadiusDisc);
197  }
198  m_cutList.add(Accept_t([this](const P_t& p) -> bool {
199  ATH_MSG_VERBOSE("Checking particle for intersection with discs of |z| " << m_zDisc);
200  //create surface we extrapolate to and cache it
201  const xAOD::TruthVertex* ptruthVertex = p.prodVtx();
202  if (ptruthVertex == nullptr) {
203  //cannot derive production vertex, reject track
204  ATH_MSG_VERBOSE("Rejecting particle without production vertex.");
205  return false;
206  }
207  const auto xPos = ptruthVertex->x();
208  const auto yPos = ptruthVertex->y();
209  const auto z_truth = ptruthVertex->z();
210  const Amg::Vector3D position(xPos, yPos, z_truth);
211  const Amg::Vector3D momentum(p.px(), p.py(), p.pz());
212  const Trk::CurvilinearParameters cParameters(position, momentum, p.charge());
213  const Trk::TrackParameters *exParameters = m_extrapolator->extrapolate(Gaudi::Hive::currentContext(),
214  cParameters,
215  *m_disc1, Trk::anyDirection, true, Trk::pion).release();
216  if (exParameters) {
217  //since boundary check is true, should be enough to say we've hit the disk..
218  ATH_MSG_VERBOSE("Successfully extrapolated track to disk at +" << m_zDisc << ": " << *exParameters);
219  float ex_radius = sqrt(pow(exParameters->position().x(),2)+pow(exParameters->position().y(),2));
220  ATH_MSG_VERBOSE("radial position at surface: " << ex_radius);
221  if ((ex_radius > m_minRadiusDisc) and (ex_radius < m_maxRadiusDisc)) {
222  ATH_MSG_VERBOSE("Confirmed within the disk. Accepting particle");
223  return true;
224  }
225  //else...
226  ATH_MSG_VERBOSE("Strange, extrapolation succeeded but extrapolated position not within disc radius! Test next disc");
227  }
228  exParameters = m_extrapolator->extrapolate(Gaudi::Hive::currentContext(),cParameters, *m_disc2, Trk::anyDirection, true, Trk::pion).release();
229  if (exParameters) {
230  //since boundary check is true, should be enough to say we've hit the disk..
231  ATH_MSG_VERBOSE("Successfully extrapolated track to disk at -" << m_zDisc << ": " << *exParameters);
232  float ex_radius = sqrt(pow(exParameters->position().x(),2)+pow(exParameters->position().y(),2));
233  ATH_MSG_VERBOSE("radial position at surface: " << ex_radius);
234  if ((ex_radius > m_minRadiusDisc) and (ex_radius < m_maxRadiusDisc)) {
235  ATH_MSG_VERBOSE("Confirmed within the disk. Accepting particle");
236  return true;
237  }
238  //else...
239  ATH_MSG_VERBOSE("Strange, extrapolation succeeded but extrapolated position not within disc radius! Rejecting");
240  }
241  //else..
242  ATH_MSG_VERBOSE("Particle rejected");
243  return false;
244  }, "SelectDisc"));
245  } //m_zDisc > 0
246 
247  std::string msg = std::to_string(m_cutList.size()) + " truth acceptance cuts are used:\n";
248  for (const auto& i:m_cutList.names()) {
249  msg += i + "\n";
250  }
251  ATH_MSG_INFO(msg);
252 
253  ATH_CHECK(m_extrapolator.retrieve(EnableTool{ m_radiusCylinder > 0 || m_zDisc >0 }));
254 
255  return StatusCode::SUCCESS;
256 }
257 
260  // nop
261  return StatusCode::SUCCESS;
262 }
263 
264 
265 std::vector<std::string>
267  return m_cutList.names();
268 }
269 
272  const xAOD::TruthParticle* pTruth = dynamic_cast<const xAOD::TruthParticle*>(particle);
273 
274  //@TODO
275  if (not pTruth) {
276  return m_cutList.size()+1; // marker for invalid particles
277  }
278  return m_cutList.accept(*pTruth);
279 }
280 
282 AthTruthSelectionTool::testAllCuts(const xAOD::IParticle * particle, std::vector<unsigned int> &counter) const {
283  const xAOD::TruthParticle* pTruth = dynamic_cast<const xAOD::TruthParticle*>(particle);
284 
285  //@TODO
286  if (not pTruth) {
287  return m_cutList.size()+1; // marker for invalid particles
288  }
289  return m_cutList.testAllCuts(*pTruth,counter);
290 }
291 
Trk::anyDirection
@ anyDirection
Definition: PropDirection.h:22
AthTruthSelectionTool::m_grandparent
bool m_grandparent
Definition: AthTruthSelectionTool.h:64
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
AthTruthSelectionTool::m_cylinder
std::unique_ptr< Trk::CylinderSurface > m_cylinder
Definition: AthTruthSelectionTool.h:80
TrackParameters.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
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:259
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
AthTruthSelectionTool::m_pdgId
int m_pdgId
Definition: AthTruthSelectionTool.h:63
AthTruthSelectionTool::m_radiusCylinder
float m_radiusCylinder
for cylinder topology: radius of cylinder
Definition: AthTruthSelectionTool.h:72
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
AthTruthSelectionTool::m_requireCharged
bool m_requireCharged
Definition: AthTruthSelectionTool.h:56
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:40
CutList
Templated CutList class to contain a group of cuts.
Definition: CutFlow.h:93
AthTruthSelectionTool::m_minRadiusDisc
float m_minRadiusDisc
for disk topology: minimum radius
Definition: AthTruthSelectionTool.h:76
AthTruthSelectionTool::m_ancestors
std::vector< int > m_ancestors
Definition: AthTruthSelectionTool.h:67
xAOD::TruthVertex_v1::y
float y() const
Vertex y displacement.
AthTruthSelectionTool::m_requireOnlyPrimary
bool m_requireOnlyPrimary
Definition: AthTruthSelectionTool.h:55
uint
unsigned int uint
Definition: LArOFPhaseFill.cxx:20
AthTruthSelectionTool::m_maxEta
float m_maxEta
Definition: AthTruthSelectionTool.h:52
AthTruthSelectionTool::m_disc1
std::unique_ptr< Trk::DiscSurface > m_disc1
Definition: AthTruthSelectionTool.h:81
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:299
lumiFormat.i
int i
Definition: lumiFormat.py:92
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:266
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
Trk::pion
@ pion
Definition: ParticleHypothesis.h:29
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:73
test_pyathena.parent
parent
Definition: test_pyathena.py:15
AthTruthSelectionTool::m_poselectronfromgamma
bool m_poselectronfromgamma
Definition: AthTruthSelectionTool.h:65
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
AthTruthSelectionTool::m_maxProdVertRadius
double m_maxProdVertRadius
Definition: AthTruthSelectionTool.h:62
AthTruthSelectionTool.h
Trk::ParametersBase
Definition: ParametersBase.h:55
Trk::CurvilinearParametersT
Definition: CurvilinearParametersT.h:48
AthTruthSelectionTool::m_minPt
float m_minPt
Definition: AthTruthSelectionTool.h:54
TruthVertex.h
xAOD::TruthVertex_v1
Class describing a truth vertex in the MC record.
Definition: TruthVertex_v1.h:41
AthTruthSelectionTool::m_selectedCharge
int m_selectedCharge
Definition: AthTruthSelectionTool.h:57
AthTruthSelectionTool::m_extrapolator
PublicToolHandle< Trk::IExtrapolator > m_extrapolator
Too handle for truth-track extrapolation.
Definition: AthTruthSelectionTool.h:89
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:282
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
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_requireSiHit
int m_requireSiHit
Definition: AthTruthSelectionTool.h:59
xAOD::TruthVertex_v1::x
float x() const
Vertex x displacement.
AthTruthSelectionTool::m_zDisc
float m_zDisc
for disk topology: Z position of disk
Definition: AthTruthSelectionTool.h:75
AthTruthSelectionTool::m_requireStatus1
bool m_requireStatus1
Definition: AthTruthSelectionTool.h:58
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
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:43
AthCommonMsg< AlgTool >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
AthTruthSelectionTool::m_disc2
std::unique_ptr< Trk::DiscSurface > m_disc2
Definition: AthTruthSelectionTool.h:82
AthTruthSelectionTool::m_maxPt
float m_maxPt
Definition: AthTruthSelectionTool.h:53
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
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
AthTruthSelectionTool::m_minZCylinder
float m_minZCylinder
for cylinder topology: minimum |z|
Definition: AthTruthSelectionTool.h:73
test_pyathena.counter
counter
Definition: test_pyathena.py: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:271
AthTruthSelectionTool::m_maxRadiusDisc
float m_maxRadiusDisc
for disk topology: maximum radius
Definition: AthTruthSelectionTool.h:77
AthTruthSelectionTool::m_maxZCylinder
float m_maxZCylinder
for cylinder topology: maximum |z|
Definition: AthTruthSelectionTool.h:74