ATLAS Offline Software
Loading...
Searching...
No Matches
PFTauFlowElementAssoc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "xAODTau/TauTrack.h"
8#include <map>
9#include <utility>
10
13
15 ISvcLocator* pSvcLocator): AthReentrantAlgorithm(name, pSvcLocator)
16{}
17
19
21
22 ATH_MSG_DEBUG("Initializing" << name() << "...");
23
28
29 ATH_CHECK(m_tauJetReadHandleKey.initialize());
32
33 if (!m_jetReadHandleKey.empty()) ATH_CHECK(m_jetReadHandleKey.initialize());
34
35 ATH_MSG_DEBUG("Initialization completed successfully");
36
37 return StatusCode::SUCCESS;
38}
39
49
50StatusCode PFTauFlowElementAssoc::execute(const EventContext &ctx) const {
51
52 // Write decoration handles for linking the TauJet container with the FlowElement container and vice versa
57 // Read handles for the TauJet container and the FlowElement container
61
62 // Initialize flow element link containers
63 std::vector<std::vector<FELink_t>> tauNeutralFEVec(tauJetReadHandle->size());
64 std::vector<std::vector<FELink_t>> tauChargedFEVec(tauJetReadHandle->size());
65
66 // Prepare <tau,clusters> and <tau,tracks> maps to avoid nested loops
67 std::multimap<size_t, size_t> map_tau_clusters, map_tau_tracks;
68 // Skip taus that won't be written to AOD - first check the variable exists
69 // because older ESD and any AOD used as input do not have this variable.
70 static const SG::ConstAccessor<char> acc_passThinning("passThinning");
71 for (const xAOD::TauJet* tau : *tauJetReadHandle) {
72 if(acc_passThinning.isAvailable(*tau) && !acc_passThinning(*tau)) continue;
73
74 // Vertexed clusters are transiently available in RAWtoALL, retrieve them if available
75 static const SG::Accessor< std::vector< xAOD::CaloVertexedTopoCluster > > vertexedClustersAcc( "VertexedClusters" );
76 if (vertexedClustersAcc.isAvailable(*tau)) {
77 std::vector<xAOD::CaloVertexedTopoCluster> vertexedClusters = tau->vertexedClusters();
78 for (const auto& cluster : vertexedClusters) {
79 // Check if the cluster is within R = 0.2 of tau axis
80 if (cluster.p4().DeltaR(tau->p4(xAOD::TauJetParameters::IntermediateAxis)) > 0.2) continue;
81 // Get the index of the cluster associated to the tau
82 map_tau_clusters.insert(std::make_pair<size_t,size_t>(tau->index(),cluster.clust().index()));
83 }
84 }
85 else {
86 // Recompute vertexed clusters
87 // Get tau vertex
88 const xAOD::Vertex* tauVertex = tau->vertex();
89 // Get the clusters associated to the tau
90 std::vector<const xAOD::IParticle*> tauClusters = tau->clusters();
91 for (const auto *cluster : tauClusters) {
92 const xAOD::CaloCluster* clus = static_cast<const xAOD::CaloCluster*>(cluster);
93 TLorentzVector clusterp4 = clus->p4();
94 // Correct cluster to tau vertex if it exists
95 if (tauVertex != nullptr) {
96 xAOD::CaloVertexedTopoCluster vertexedClus(*clus, tauVertex->position());
97 clusterp4 = vertexedClus.p4();
98 }
99 // Check if the cluster is within R = 0.2 of tau axis
100 if (clusterp4.DeltaR(tau->p4(xAOD::TauJetParameters::IntermediateAxis)) > 0.2) continue;
101 // Get the index of the cluster associated to the tau
102 map_tau_clusters.insert(std::make_pair<size_t,size_t>(tau->index(),clus->index()));
103 }
104 }
105
106 // Get tau tracks associated to the tau
107 std::vector<const xAOD::TauTrack*> tauTracks = tau->tracks();
108 for (const auto *tauTrack : tauTracks) {
109 // Get track associated to the tau track to use for matching
110 const xAOD::TrackParticle* tauIDTrack = tauTrack->track();
111 // Get the index of the track associated to the tau
112 map_tau_tracks.insert(std::make_pair<size_t,size_t>(tau->index(),tauIDTrack->index()));
113 }
114 }
115
117 // Loop over the neutral flow elements
119 for (const xAOD::FlowElement* FE : *neutralFETauWriteDecorHandle) {
120 // Check that the flow element cluster exists and is not null
121 if (FE->otherObjects().empty()) continue;
122 if (FE->otherObjects().at(0) == nullptr) continue;
123 // Get the index of the flow element cluster
124 size_t FEClusterIndex = FE->otherObjects().at(0)->index();
125
126 std::vector<TauJetLink_t> FETauJetLinks;
127
128 // Loop over the tau/clusters map
129 for (const auto [tauIndex, tauClusterIndex] : map_tau_clusters) {
130 // Link the tau and the neutral FE if the cluster indices match
131 if (tauClusterIndex == FEClusterIndex) {
132 FETauJetLinks.emplace_back(*tauJetReadHandle, tauIndex);
133 tauNeutralFEVec.at(tauIndex).emplace_back(*neutralFEReadHandle, FE->index() );
134 }
135 }
136
137 // Add vector of elements links to the tau jets as a decoration to the FE container
138 neutralFETauWriteDecorHandle (*FE) = FETauJetLinks;
139
140 } // end neutral FE loop
141
143 // Loop over the charged flow elements
145 for (const xAOD::FlowElement* FE : *chargedFETauWriteDecorHandle) {
146 // Check that the flow element track exists and is not null
147 if (FE->chargedObjects().empty()) continue;
148 if (FE->chargedObjects().at(0) == nullptr) continue;
149
150 // Get the index of the flow element track
151 size_t FETrackIndex = FE->chargedObjects().at(0)->index();
152
153 std::vector<TauJetLink_t> FETauJetLinks;
154
155 // Loop over the tau/tracks map
156 for (const auto [tauIndex, tauIDTrackIndex] : map_tau_tracks) {
157 // Link the tau and the charged FE if the track indices match
158 if (tauIDTrackIndex == FETrackIndex) {
159 FETauJetLinks.emplace_back(*tauJetReadHandle, tauIndex);
160 tauChargedFEVec.at(tauIndex).emplace_back(*chargedFEReadHandle, FE->index() );
161 }
162 }
163
164 // Add vector of elements links to the tau jets as a decoration to the FE container
165 chargedFETauWriteDecorHandle (*FE) = FETauJetLinks;
166
167 } // end charged FE loop
168
170 // Write decorations to TauJet container
172 // Add vectors of Flow Element (FE) Links as decorations to the TauJet container
173 for (const xAOD::TauJet* tau : *tauNeutralFEWriteDecorHandle) {
174 tauNeutralFEWriteDecorHandle (*tau) = tauNeutralFEVec.at(tau->index());
175 tauChargedFEWriteDecorHandle (*tau) = tauChargedFEVec.at(tau->index());
176 }
177
178 ATH_MSG_DEBUG("Execute completed successfully");
179
180 return StatusCode::SUCCESS;
181}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x)
ElementLink< xAOD::TauJetContainer > TauJetLink_t
ElementLink< xAOD::FlowElementContainer > FELink_t
An algorithm that can be simultaneously executed in multiple threads.
SG::ReadHandleKey< xAOD::TauJetContainer > m_tauJetReadHandleKey
SG::ReadHandleKey< xAOD::JetContainer > m_jetReadHandleKey
virtual StatusCode initialize()
SG::ReadHandleKey< xAOD::FlowElementContainer > m_neutralFEReadHandleKey
SG::ReadHandleKey< xAOD::FlowElementContainer > m_chargedFEReadHandleKey
PFTauFlowElementAssoc(const std::string &name, ISvcLocator *pSvcLocator)
virtual StatusCode execute(const EventContext &ctx) const
This algorithm: 1) Accesses the relevant FlowElement and TauJet containers 2) Loops over the neutral ...
SG::WriteDecorHandleKey< xAOD::TauJetContainer > m_tauChargedFEWriteDecorKey
The write key for adding Charged Flow Element links to the taus.
SG::WriteDecorHandleKey< xAOD::TauJetContainer > m_tauNeutralFEWriteDecorKey
The write key for adding Neutral Flow Element links to the taus.
SG::WriteDecorHandleKey< xAOD::FlowElementContainer > m_neutralFETauWriteDecorKey
The write key for adding tau element links to the Neutral Flow Elements.
virtual ~PFTauFlowElementAssoc()
SG::WriteDecorHandleKey< xAOD::FlowElementContainer > m_chargedFETauWriteDecorKey
The write key for adding tau element links to the Charged Flow Elements.
Helper class to provide type-safe access to aux data.
size_t index() const
Return the index of this element within its container.
Helper class to provide constant type-safe access to aux data.
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
Handle class for adding a decoration to an object.
virtual FourMom_t p4() const
The full 4-momentum of the particle.
virtual FourMom_t p4() const final
The full 4-momentum of the particle.
Evaluate cluster kinematics with a different vertex / signal state.
const Amg::Vector3D & position() const
Returns the 3-pos.
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
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.
TauJet_v3 TauJet
Definition of the current "tau version".