ATLAS Offline Software
FlowElementPrepAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 #include "FlowElementPrepAlg.h"
6 #include "StoreGate/ReadHandle.h"
11 
13 namespace
14 {
15  const static SG::AuxElement::ConstAccessor<char> accPVMatched("matchedToPV");
16 }
17 
18 namespace HLT
19 {
20  namespace MET
21  {
22  FlowElementPrepAlg::FlowElementPrepAlg(const std::string &name, ISvcLocator *pSvcLocator)
23  : AthReentrantAlgorithm(name, pSvcLocator)
24  {
25  }
26 
28  {
29  m_manualTVA = !m_tvaTool.empty();
30  CHECK(m_inputNeutralKey.initialize());
31  CHECK(m_inputChargedKey.initialize());
32  CHECK(m_outputKey.initialize());
33  if (m_outputCategoryKey.key().find(".") == std::string::npos)
35  else if (SG::contKeyFromKey(m_outputCategoryKey.key()) != m_outputKey.key())
36  {
37  ATH_MSG_ERROR("Category key does not match output container key!");
38  return StatusCode::FAILURE;
39  }
40  CHECK(m_outputCategoryKey.initialize());
42 
43  if (m_manualTVA)
44  {
45  CHECK(m_tvaTool.retrieve());
46  if (!m_trackSelTool.empty())
47  CHECK(m_trackSelTool.retrieve());
48  }
49  CHECK(m_inputVertexKey.initialize(m_manualTVA));
50  return StatusCode::SUCCESS;
51  }
52 
53  StatusCode FlowElementPrepAlg::execute(const EventContext &ctx) const
54  {
56  if (!charged.isValid())
57  {
58  ATH_MSG_ERROR("Failed to retrieve " << m_inputChargedKey);
59  return StatusCode::FAILURE;
60  }
62  if (!neutral.isValid())
63  {
64  ATH_MSG_ERROR("Failed to retrieve " << m_inputNeutralKey);
65  return StatusCode::FAILURE;
66  }
67  const xAOD::Vertex *priVtx = nullptr;
68  std::vector<const xAOD::Vertex *> vertices;
69  if (m_manualTVA)
70  {
71  auto vertexHandle = SG::makeHandle(m_inputVertexKey, ctx);
72  if (!vertexHandle.isValid())
73  {
74  ATH_MSG_ERROR("Failed to retrieve " << m_inputVertexKey);
75  return StatusCode::FAILURE;
76  }
77  // Find the primary vertex and build up the vector for the TVA tool
78  vertices.reserve(vertexHandle->size());
79  for (const xAOD::Vertex *ivtx : *vertexHandle)
80  {
81  if (!priVtx && ivtx->vertexType() == xAOD::VxType::PriVtx)
82  priVtx = ivtx;
83  vertices.push_back(ivtx);
84  }
85  }
86  auto outputHandle = SG::makeHandle(m_outputKey, ctx);
87  auto &decCategory = *m_decCategory;
88 
89  auto combined = std::make_unique<ConstDataVector<xAOD::FlowElementContainer>>(SG::VIEW_ELEMENTS);
90  combined->reserve(charged->size() + neutral->size());
91 
92  for (const xAOD::FlowElement *ife : *charged)
93  {
94  combined->push_back(ife);
95  if (m_manualTVA)
96  {
97  if (!priVtx)
98  // If we could not identify a primary vertex, classify as neutral
99  // Note that this means in order to distinguish cPFOs from nPFOs the 'isCharged'
100  // function should still be used
101  decCategory(*ife) = PUClassification::NeutralForward;
102  else
103  {
104  const xAOD::IParticle *icharged = ife->chargedObject(0);
105  const xAOD::TrackParticle *itrk = dynamic_cast<const xAOD::TrackParticle *>(icharged);
106  if (!itrk)
107  {
108  ATH_MSG_ERROR("Could not retrieve primary track particle from FlowElement");
109  return StatusCode::FAILURE;
110  }
111  if (itrk && (m_trackSelTool.empty() || m_trackSelTool->accept(itrk)))
112  {
113  bool isPVMatched;
114  if (m_useCompatible)
115  isPVMatched = m_tvaTool->isCompatible(*itrk, *priVtx);
116  else
117  isPVMatched = m_tvaTool->getUniqueMatchVertex(*itrk, vertices) == priVtx;
118  if (isPVMatched)
119  decCategory(*ife) = PUClassification::ChargedHS;
120  else
121  decCategory(*ife) = PUClassification::ChargedPU;
122  }
123  }
124  }
125  else
126  {
127  if (accPVMatched(*ife))
128  decCategory(*ife) = PUClassification::ChargedHS;
129  else
130  decCategory(*ife) = PUClassification::ChargedPU;
131  }
132  }
133  for (const xAOD::FlowElement *ife : *neutral)
134  {
135  combined->push_back(ife);
136  decCategory(*ife) = PUClassification::NeutralForward;
137  }
138 
139  CHECK(outputHandle.put(ctx, std::move(combined)) != nullptr);
140  return StatusCode::SUCCESS;
141  }
142  } // namespace MET
143 } // namespace HLT
HLT::MET::FlowElementPrepAlg::FlowElementPrepAlg
FlowElementPrepAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition: FlowElementPrepAlg.cxx:22
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:53
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::decorKeyFromKey
std::string decorKeyFromKey(const std::string &key)
Extract the decoration part of key.
Definition: DecorKeyHelpers.cxx:41
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:40
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:83
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:571
TCS::MET
@ MET
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/L1TopoCommon/Types.h:16
HLT::MET::FlowElementPrepAlg::initialize
virtual StatusCode initialize() override
Definition: FlowElementPrepAlg.cxx:27
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
CP::neutral
@ neutral
Definition: Reconstruction/PFlow/PFlowUtils/PFlowUtils/PFODefs.h:11
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
HLT::MET::PUClassification::ChargedPU
@ ChargedPU
Definition: PUClassification.h:19
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:43
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