ATLAS Offline Software
FlowElementPrepAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 #include "FlowElementPrepAlg.h"
6 #include "StoreGate/ReadHandle.h"
12 
14 namespace
15 {
16  const static SG::AuxElement::ConstAccessor<char> accPVMatched("matchedToPV");
17 }
18 
19 namespace HLT
20 {
21  namespace MET
22  {
23  FlowElementPrepAlg::FlowElementPrepAlg(const std::string &name, ISvcLocator *pSvcLocator)
24  : AthReentrantAlgorithm(name, pSvcLocator)
25  {
26  }
27 
29  {
30  m_manualTVA = !m_tvaTool.empty();
31  CHECK(m_inputNeutralKey.initialize());
32  CHECK(m_inputChargedKey.initialize());
33  CHECK(m_outputKey.initialize());
34  if (m_outputCategoryKey.key().find(".") == std::string::npos)
36  else if (SG::contKeyFromKey(m_outputCategoryKey.key()) != m_outputKey.key())
37  {
38  ATH_MSG_ERROR("Category key does not match output container key!");
39  return StatusCode::FAILURE;
40  }
41  CHECK(m_outputCategoryKey.initialize());
43 
44  if (m_manualTVA)
45  {
46  CHECK(m_tvaTool.retrieve());
47  if (!m_trackSelTool.empty())
48  CHECK(m_trackSelTool.retrieve());
49  }
50  CHECK(m_inputVertexKey.initialize(m_manualTVA));
51  return StatusCode::SUCCESS;
52  }
53 
54  StatusCode FlowElementPrepAlg::execute(const EventContext &ctx) const
55  {
57  if (!charged.isValid())
58  {
59  ATH_MSG_ERROR("Failed to retrieve " << m_inputChargedKey);
60  return StatusCode::FAILURE;
61  }
63  if (!neutral.isValid())
64  {
65  ATH_MSG_ERROR("Failed to retrieve " << m_inputNeutralKey);
66  return StatusCode::FAILURE;
67  }
68  const xAOD::Vertex *priVtx = nullptr;
69  std::vector<const xAOD::Vertex *> vertices;
70  if (m_manualTVA)
71  {
72  auto vertexHandle = SG::makeHandle(m_inputVertexKey, ctx);
73  if (!vertexHandle.isValid())
74  {
75  ATH_MSG_ERROR("Failed to retrieve " << m_inputVertexKey);
76  return StatusCode::FAILURE;
77  }
78  // Find the primary vertex and build up the vector for the TVA tool
79  vertices.reserve(vertexHandle->size());
80  for (const xAOD::Vertex *ivtx : *vertexHandle)
81  {
82  if (!priVtx && ivtx->vertexType() == xAOD::VxType::PriVtx)
83  priVtx = ivtx;
84  vertices.push_back(ivtx);
85  }
86  }
87  auto outputHandle = SG::makeHandle(m_outputKey, ctx);
88  auto &decCategory = *m_decCategory;
89 
90  auto combined = std::make_unique<ConstDataVector<xAOD::FlowElementContainer>>(SG::VIEW_ELEMENTS);
91  combined->reserve(charged->size() + neutral->size());
92 
93  for (const xAOD::FlowElement *ife : *charged)
94  {
95  combined->push_back(ife);
96  if (m_manualTVA)
97  {
98  if (!priVtx)
99  // If we could not identify a primary vertex, classify as neutral
100  // Note that this means in order to distinguish cPFOs from nPFOs the 'isCharged'
101  // function should still be used
102  decCategory(*ife) = PUClassification::NeutralForward;
103  else
104  {
105  const xAOD::IParticle *icharged = ife->chargedObject(0);
106  const xAOD::TrackParticle *itrk = dynamic_cast<const xAOD::TrackParticle *>(icharged);
107  if (!itrk)
108  {
109  ATH_MSG_ERROR("Could not retrieve primary track particle from FlowElement");
110  return StatusCode::FAILURE;
111  }
112  if (itrk && (m_trackSelTool.empty() || m_trackSelTool->accept(itrk)))
113  {
114  bool isPVMatched;
115  if (m_useCompatible)
116  isPVMatched = m_tvaTool->isCompatible(*itrk, *priVtx);
117  else
118  isPVMatched = m_tvaTool->getUniqueMatchVertex(*itrk, vertices) == priVtx;
119  if (isPVMatched)
120  decCategory(*ife) = PUClassification::ChargedHS;
121  else
122  decCategory(*ife) = PUClassification::ChargedPU;
123  }
124  }
125  }
126  else
127  {
128  if (accPVMatched(*ife))
129  decCategory(*ife) = PUClassification::ChargedHS;
130  else
131  decCategory(*ife) = PUClassification::ChargedPU;
132  }
133  }
134  for (const xAOD::FlowElement *ife : *neutral)
135  {
136  combined->push_back(ife);
137  decCategory(*ife) = PUClassification::NeutralForward;
138  }
139 
140  CHECK(outputHandle.put(ctx, std::move(combined)) != nullptr);
141  return StatusCode::SUCCESS;
142  }
143  } // namespace MET
144 } // namespace HLT
HLT::MET::FlowElementPrepAlg::FlowElementPrepAlg
FlowElementPrepAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition: FlowElementPrepAlg.cxx:23
SG::contKeyFromKey
std::string contKeyFromKey(const std::string &key)
Extract the container part of key.
Definition: DecorKeyHelpers.cxx:26
HLT::MET::FlowElementPrepAlg::m_decCategory
deferred_t< SG::AuxElement::Decorator< int > > m_decCategory
Definition: FlowElementPrepAlg.h:129
HLT::MET::FlowElementPrepAlg::execute
virtual StatusCode execute(const EventContext &context) const override
Run the algorithm.
Definition: FlowElementPrepAlg.cxx:54
HLT::MET::FlowElementPrepAlg::m_outputKey
SG::WriteHandleKey< xAOD::FlowElementContainer > m_outputKey
The output combined container.
Definition: FlowElementPrepAlg.h:102
HLT::MET::FlowElementPrepAlg::m_inputVertexKey
SG::ReadHandleKey< xAOD::VertexContainer > m_inputVertexKey
The input vertex container.
Definition: FlowElementPrepAlg.h:98
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
ConstDataVector.h
DataVector adapter that acts like it holds const pointers.
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
DecorKeyHelpers.h
Some common helper functions used by decoration handles.
xAOD::Vertex_v1::vertexType
VxType::VertexType vertexType() const
The type of the vertex.
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
HLT::MET::FlowElementPrepAlg::m_outputCategoryKey
SG::WriteDecorHandleKey< xAOD::FlowElementContainer > m_outputCategoryKey
The output classification.
Definition: FlowElementPrepAlg.h:105
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
WriteHandle.h
Handle class for recording to StoreGate.
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
HLT
It used to be useful piece of code for replacing actual SG with other store of similar functionality ...
Definition: HLTResultReader.h:26
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
HLT::MET::PUClassification::ChargedHS
@ ChargedHS
Definition: PUClassification.h:20
HLT::MET::FlowElementPrepAlg::m_manualTVA
bool m_manualTVA
Whether to perform track -> vertex matching manually.
Definition: FlowElementPrepAlg.h:126
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:572
HLT::MET::FlowElementPrepAlg::initialize
virtual StatusCode initialize() override
Definition: FlowElementPrepAlg.cxx:28
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
SG::decorKeyFromKey
std::string decorKeyFromKey(const std::string &key, const std::string &deflt)
Extract the decoration part of key.
Definition: DecorKeyHelpers.cxx:42
CP::neutral
@ neutral
Definition: Reconstruction/PFlow/PFlowUtils/PFlowUtils/PFODefs.h:11
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
MET
Definition: MET.py:1
HLT::MET::PUClassification::ChargedPU
@ ChargedPU
Definition: PUClassification.h:19
TrackParticle.h
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
HLT::MET::FlowElementPrepAlg::m_trackSelTool
ToolHandle< InDet::IInDetTrackSelectionTool > m_trackSelTool
Track selection tool.
Definition: FlowElementPrepAlg.h:113
PUClassification.h
HLT::MET::FlowElementPrepAlg::m_inputChargedKey
SG::ReadHandleKey< xAOD::FlowElementContainer > m_inputChargedKey
The input charged container.
Definition: FlowElementPrepAlg.h:95
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:44
ReadHandle.h
Handle class for reading from StoreGate.
FlowElementPrepAlg.h
HLT::MET::FlowElementPrepAlg::m_inputNeutralKey
SG::ReadHandleKey< xAOD::FlowElementContainer > m_inputNeutralKey
The input neutral container.
Definition: FlowElementPrepAlg.h:92
HLT::MET::FlowElementPrepAlg::m_tvaTool
ToolHandle< CP::ITrackVertexAssociationTool > m_tvaTool
Track -> vertex association.
Definition: FlowElementPrepAlg.h:108
HLT::MET::FlowElementPrepAlg::m_useCompatible
Gaudi::Property< bool > m_useCompatible
Choose between the unique (many-to-one) track->vertex association and just checking compatibility wit...
Definition: FlowElementPrepAlg.h:120
CP::charged
@ charged
Definition: Reconstruction/PFlow/PFlowUtils/PFlowUtils/PFODefs.h:11
HLT::MET::PUClassification::NeutralForward
@ NeutralForward
Definition: PUClassification.h:18
xAOD::FlowElement_v1
A detector object made of other lower level object(s)
Definition: FlowElement_v1.h:25