ATLAS Offline Software
Loading...
Searching...
No Matches
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
12
14namespace
15{
16 const static SG::AuxElement::ConstAccessor<char> accPVMatched("matchedToPV");
17}
18
19namespace 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 }
51 return StatusCode::SUCCESS;
52 }
53
54 StatusCode FlowElementPrepAlg::execute(const EventContext &ctx) const
55 {
56 auto charged = SG::makeHandle(m_inputChargedKey, ctx);
57 if (!charged.isValid())
58 {
59 ATH_MSG_ERROR("Failed to retrieve " << m_inputChargedKey);
60 return StatusCode::FAILURE;
61 }
62 auto neutral = SG::makeHandle(m_inputNeutralKey, ctx);
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
#define ATH_MSG_ERROR(x)
DataVector adapter that acts like it holds const pointers.
#define CHECK(...)
Evaluate an expression and check for errors.
Some common helper functions used by decoration handles.
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
An algorithm that can be simultaneously executed in multiple threads.
Gaudi::Property< bool > m_useCompatible
Choose between the unique (many-to-one) track->vertex association and just checking compatibility wit...
virtual StatusCode initialize() override
FlowElementPrepAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
deferred_t< SG::AuxElement::Decorator< int > > m_decCategory
SG::ReadHandleKey< xAOD::VertexContainer > m_inputVertexKey
The input vertex container.
SG::ReadHandleKey< xAOD::FlowElementContainer > m_inputNeutralKey
The input neutral container.
bool m_manualTVA
Whether to perform track -> vertex matching manually.
ToolHandle< CP::ITrackVertexAssociationTool > m_tvaTool
Track -> vertex association.
ToolHandle< InDet::IInDetTrackSelectionTool > m_trackSelTool
Track selection tool.
SG::WriteHandleKey< xAOD::FlowElementContainer > m_outputKey
The output combined container.
SG::ReadHandleKey< xAOD::FlowElementContainer > m_inputChargedKey
The input charged container.
SG::WriteDecorHandleKey< xAOD::FlowElementContainer > m_outputCategoryKey
The output classification.
virtual StatusCode execute(const EventContext &context) const override
Run the algorithm.
SG::ConstAccessor< T, ALLOC > ConstAccessor
Definition AuxElement.h:569
Class providing the definition of the 4-vector interface.
VxType::VertexType vertexType() const
The type of the vertex.
It used to be useful piece of code for replacing actual SG with other store of similar functionality ...
std::string contKeyFromKey(const std::string &key)
Extract the container part of key.
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
std::string decorKeyFromKey(const std::string &key, const std::string &deflt)
Extract the decoration part of key.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
@ PriVtx
Primary vertex.
FlowElement_v1 FlowElement
Definition of the current "pfo version".
Definition FlowElement.h:16
TrackParticle_v1 TrackParticle
Reference the current persistent version:
Vertex_v1 Vertex
Define the latest version of the vertex class.