ATLAS Offline Software
Loading...
Searching...
No Matches
PMGDecayProductsSelectionTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2019 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 {
108 size_t nParents = getNParents (truthParticle);
109 for (size_t i = 0; i < nParents; i++)
110 {
111 const xAOD::TruthParticle *parent = getParent(truthParticle, i);
112 if (parent)
113 {
114 if (std::find(m_requiredParentPDGIDs.begin(), m_requiredParentPDGIDs.end(), std::abs(parent->pdgId())) != m_requiredParentPDGIDs.end())
115 {
116 acceptData.setCutResult (m_requiredParentIndex, true);
117 return acceptData;
118 }
119 else if (m_allowedIntermediatePDGIDs.empty() || std::find(m_allowedIntermediatePDGIDs.begin(), m_allowedIntermediatePDGIDs.end(), std::abs(parent->pdgId())) != m_allowedIntermediatePDGIDs.end())
120 {
121 return hasRequiredInitialParent(parent, acceptData);
122 }
123 else
124 {
125 ATH_MSG_VERBOSE("Removing particle as parent is not allowed: " << parent->pdgId());
126 return acceptData;
127 }
128 }
129 else
130 {
131 ATH_MSG_WARNING("Particle parent is not valid");
132 return acceptData;
133 }
134 }
135
136 if (std::find(m_requiredParentPDGIDs.begin(), m_requiredParentPDGIDs.end(), std::abs(truthParticle->pdgId())) != m_requiredParentPDGIDs.end())
137 {
138 acceptData.setCutResult (m_requiredParentIndex, true);
139 return acceptData;
140 }
141
142 return acceptData;
143 }
144
145
146
147 size_t PMGDecayProductsSelectionTool ::
148 getNParents (const xAOD::TruthParticle *truthParticle) const
149 {
150 if (m_parentsAccessor->isAvailable (*truthParticle))
151 {
152 return (*m_parentsAccessor)(*truthParticle).size();
153 }
154 else
155 {
156 return truthParticle->nParents();
157 }
158 }
159
160
161
162 const xAOD::TruthParticle* PMGDecayProductsSelectionTool ::
163 getParent (const xAOD::TruthParticle *truthParticle,
164 size_t index) const
165 {
166 if (m_parentsAccessor->isAvailable (*truthParticle))
167 {
169 = (*m_parentsAccessor)(*truthParticle).at(index);
170 return parentElementLink.isValid() ? *parentElementLink : nullptr;
171 }
172 else
173 {
174 return truthParticle->parent(index);
175 }
176 }
177}
#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.
size_t getNParents(const xAOD::TruthParticle *truthParticle) const
Helper function to get the number of 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:134
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.
int pdgId() const
PDG ID code.
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.