ATLAS Offline Software
Loading...
Searching...
No Matches
PMGDecayProductsSelectionTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8
9//
10// includes
11//
12
14
16#include <xAODBase/IParticle.h>
17
18#include <sstream>
19
20//
21// method implementations
22//
23
24namespace PMGTools
25{
26 PMGDecayProductsSelectionTool ::
27 PMGDecayProductsSelectionTool (const std::string& name)
28 : AsgTool (name)
29 {
30 declareProperty ("requiredParentPDGIDs", m_requiredParentPDGIDs, "required parent particle PDG IDs (positive only)");
31 declareProperty ("allowedIntermediatePDGIDs", m_allowedIntermediatePDGIDs, "allowed intermediate particles PDG IDs (positive only)");
32 }
33
34
35
36 StatusCode PMGDecayProductsSelectionTool ::
37 initialize ()
38 {
39 if (m_requiredParentPDGIDs.empty())
40 {
41 ATH_MSG_ERROR ("required parent particles need to be set");
42 return StatusCode::FAILURE;
43 }
44
45 m_truthParticleIndex = m_accept.addCut ("truthParticle", "has truth particle cut");
46
47 std::ostringstream particles;
48 for (int pdgId : m_requiredParentPDGIDs)
49 {
50 particles << " " << pdgId;
51 }
52 ATH_MSG_DEBUG ("Performing required parent particle selection with parent PDG IDs:" << particles.str());
53 m_requiredParentIndex = m_accept.addCut ("requiredParents", "required parent particles cut");
54
55 if (!m_allowedIntermediatePDGIDs.empty())
56 {
57 std::ostringstream particles;
58 for (int pdgId : m_allowedIntermediatePDGIDs)
59 {
60 particles << " " << pdgId;
61 }
62 ATH_MSG_DEBUG ("Performing allowed intermediate particle selection with allowed PDG IDs:" << particles.str());
63 }
64
65 m_parentsAccessor = std::make_unique<const SG::AuxElement::Accessor<std::vector<ElementLink<xAOD::TruthParticleContainer>>>>("parentLinks");
66
67 return StatusCode::SUCCESS;
68 }
69
70
71
72 const asg::AcceptInfo& PMGDecayProductsSelectionTool ::
73 getAcceptInfo () const
74 {
75 return m_accept;
76 }
77
78
79
80 asg::AcceptData PMGDecayProductsSelectionTool ::
81 accept (const xAOD::IParticle *particle) const
82 {
83 asg::AcceptData acceptData (&m_accept);
84
85 // Check if xAOD::TruthParticle or if not if it has the TruthParticleLink
86 const xAOD::TruthParticle *truthParticle
87 = dynamic_cast<const xAOD::TruthParticle *> (particle);
88 if (truthParticle == nullptr)
89 {
90 // need to find the truth particle
91 truthParticle = xAOD::TruthHelpers::getTruthParticle(*particle);
92 if (truthParticle == nullptr)
93 {
94 acceptData.setCutResult (m_truthParticleIndex, false);
95 return acceptData;
96 }
97 }
98 acceptData.setCutResult (m_truthParticleIndex, true);
99
100 return hasRequiredInitialParent(truthParticle, acceptData);
101 }
102
103
104
105 asg::AcceptData PMGDecayProductsSelectionTool ::
106 hasRequiredInitialParent (const xAOD::TruthParticle *truthParticle, asg::AcceptData& acceptData) const{
107 const int i = 0;
108 const xAOD::TruthParticle *parent = getParent(truthParticle, i);
109 if (parent) {
110 if (std::find(m_requiredParentPDGIDs.begin(), m_requiredParentPDGIDs.end(), std::abs(parent->pdgId())) != m_requiredParentPDGIDs.end()){
111 acceptData.setCutResult (m_requiredParentIndex, true);
112 return acceptData;
113 } else if (m_allowedIntermediatePDGIDs.empty() || std::find(m_allowedIntermediatePDGIDs.begin(), m_allowedIntermediatePDGIDs.end(), std::abs(parent->pdgId())) != m_allowedIntermediatePDGIDs.end()){
114 return hasRequiredInitialParent(parent, acceptData);
115 } else {
116 ATH_MSG_VERBOSE("Removing particle as parent is not allowed: " << parent->pdgId());
117 return acceptData;
118 }
119 }
120 ATH_MSG_WARNING("Particle parent is not valid");
121 return acceptData;
122 }
123
124
125
126 size_t PMGDecayProductsSelectionTool ::
127 getNParents (const xAOD::TruthParticle *truthParticle) const
128 {
129 if (m_parentsAccessor->isAvailable (*truthParticle))
130 {
131 return (*m_parentsAccessor)(*truthParticle).size();
132 }
133 else
134 {
135 return truthParticle->nParents();
136 }
137 }
138
139
140
141 const xAOD::TruthParticle* PMGDecayProductsSelectionTool ::
142 getParent (const xAOD::TruthParticle *truthParticle,
143 size_t index) const
144 {
145 if (m_parentsAccessor->isAvailable (*truthParticle))
146 {
148 = (*m_parentsAccessor)(*truthParticle).at(index);
149 return parentElementLink.isValid() ? *parentElementLink : nullptr;
150 }
151 else
152 {
153 return truthParticle->parent(index);
154 }
155 }
156}
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
std::unique_ptr< const SG::AuxElement::Accessor< std::vector< ElementLink< xAOD::TruthParticleContainer > > > > m_parentsAccessor
common parents accessor
const xAOD::TruthParticle * getParent(const xAOD::TruthParticle *truthParticle, size_t index) const
Helper function get a parent by index.
std::vector< int > m_requiredParentPDGIDs
tool properties
int m_requiredParentIndex
Index for the required parent particles.
int m_truthParticleIndex
Index for the truth particle link.
asg::AcceptInfo m_accept
the AcceptInfo we are using
asg::AcceptData hasRequiredInitialParent(const xAOD::TruthParticle *truthParticle, asg::AcceptData &acceptData) const
Helper function to check for required parent particles.
void setCutResult(const std::string &cutName, bool cutResult)
Set the result of a cut, based on the cut name (safer).
Definition AcceptData.h:135
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
Class providing the definition of the 4-vector interface.
const TruthParticle_v1 * parent(size_t i) const
Retrieve the i-th mother (TruthParticle) of this TruthParticle.
size_t nParents() const
Number of parents of this particle.
Tool providing sample cross-sections and k-factors etc.
Definition index.py:1
const xAOD::TruthParticle * getTruthParticle(const xAOD::IParticle &p)
Return the truthParticle associated to the given IParticle (if any).
TruthParticle_v1 TruthParticle
Typedef to implementation.