ATLAS Offline Software
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 
24 namespace PMGTools
25 {
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 
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 
73  getAcceptInfo () const
74  {
75  return m_accept;
76  }
77 
78 
79 
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
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 
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  }
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 
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 
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 }
xAOD::TruthParticle_v1::parent
const TruthParticle_v1 * parent(size_t i=0) const
Retrieve the i-th mother (TruthParticle) of this TruthParticle.
Definition: TruthParticle_v1.cxx:131
PMGTools::PMGDecayProductsSelectionTool::m_requiredParentPDGIDs
std::vector< int > m_requiredParentPDGIDs
tool properties
Definition: PMGDecayProductsSelectionTool.h:70
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
IParticle.h
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
index
Definition: index.py:1
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
PMGTools::PMGDecayProductsSelectionTool::hasRequiredInitialParent
asg::AcceptData hasRequiredInitialParent(const xAOD::TruthParticle *truthParticle, asg::AcceptData &acceptData) const
Helper function to check for required parent particles.
Definition: PMGDecayProductsSelectionTool.cxx:106
PMGTools::PMGDecayProductsSelectionTool::getNParents
size_t getNParents(const xAOD::TruthParticle *truthParticle) const
Helper function to get the number of parent particles.
Definition: PMGDecayProductsSelectionTool.cxx:148
PMGTools::PMGDecayProductsSelectionTool::getParent
const xAOD::TruthParticle * getParent(const xAOD::TruthParticle *truthParticle, size_t index) const
Helper function get a parent by index.
Definition: PMGDecayProductsSelectionTool.cxx:163
PMGTools::PMGDecayProductsSelectionTool::m_accept
asg::AcceptInfo m_accept
the AcceptInfo we are using
Definition: PMGDecayProductsSelectionTool.h:83
PMGTools::PMGDecayProductsSelectionTool::m_parentsAccessor
std::unique_ptr< const SG::AuxElement::Accessor< std::vector< ElementLink< xAOD::TruthParticleContainer > > > > m_parentsAccessor
common parents accessor
Definition: PMGDecayProductsSelectionTool.h:87
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
xAODTruthHelpers.h
PMGTools::PMGDecayProductsSelectionTool::m_requiredParentIndex
int m_requiredParentIndex
Index for the required parent particles.
Definition: PMGDecayProductsSelectionTool.h:79
xAOD::TruthParticle_v1::nParents
size_t nParents() const
Number of parents of this particle.
Definition: TruthParticle_v1.cxx:122
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
asg::AcceptInfo
Definition: AcceptInfo.h:28
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
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
PMGTools::PMGDecayProductsSelectionTool::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: PMGDecayProductsSelectionTool.cxx:37
PMGTools
Tool providing sample cross-sections and k-factors etc.
Definition: AnalysisCommon/PMGTools/PMGTools/IPMGCrossSectionTool.h:15
test_pyathena.parent
parent
Definition: test_pyathena.py:15
PMGTools::PMGDecayProductsSelectionTool::m_truthParticleIndex
int m_truthParticleIndex
Index for the truth particle link.
Definition: PMGDecayProductsSelectionTool.h:77
PMGDecayProductsSelectionTool.h
PMGTools::PMGDecayProductsSelectionTool::getAcceptInfo
virtual const asg::AcceptInfo & getAcceptInfo() const override
Declare the interface ID for this pure-virtual interface class to the Athena framework.
Definition: PMGDecayProductsSelectionTool.cxx:73
PMGTools::PMGDecayProductsSelectionTool::accept
virtual asg::AcceptData accept(const xAOD::IParticle *) const override
The main accept method: the actual cuts are applied here.
Definition: PMGDecayProductsSelectionTool.cxx:81
PMGTools::PMGDecayProductsSelectionTool::PMGDecayProductsSelectionTool
PMGDecayProductsSelectionTool(const std::string &name)
standard constructor
Definition: PMGDecayProductsSelectionTool.cxx:27
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
xAOD::TruthHelpers::getTruthParticle
const xAOD::TruthParticle * getTruthParticle(const xAOD::IParticle &p)
Return the truthParticle associated to the given IParticle (if any)
Definition: xAODTruthHelpers.cxx:25
asg::AcceptData::setCutResult
void setCutResult(const std::string &cutName, bool cutResult)
Set the result of a cut, based on the cut name (safer)
Definition: AcceptData.h:134
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
PMGTools::PMGDecayProductsSelectionTool::m_allowedIntermediatePDGIDs
std::vector< int > m_allowedIntermediatePDGIDs
Definition: PMGDecayProductsSelectionTool.h:71
xAOD::TruthParticle_v1::pdgId
int pdgId() const
PDG ID code.
asg::AcceptData
Definition: AcceptData.h:30
asg::AcceptInfo::addCut
int addCut(const std::string &cutName, const std::string &cutDescription)
Add a cut; returning the cut position.
Definition: AcceptInfo.h:53