ATLAS Offline Software
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());
30  ATH_CHECK(m_neutralFEReadHandleKey.initialize());
31  ATH_CHECK(m_chargedFEReadHandleKey.initialize());
32 
34 
35  ATH_MSG_DEBUG("Initialization completed successfully");
36 
37  return StatusCode::SUCCESS;
38 }
39 
50 StatusCode 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 }
PFTauFlowElementAssoc::~PFTauFlowElementAssoc
virtual ~PFTauFlowElementAssoc()
xAOD::CaloVertexedClusterBase::p4
virtual FourMom_t p4() const final
The full 4-momentum of the particle.
Definition: Event/xAOD/xAODCaloEvent/xAODCaloEvent/CaloVertexedClusterBase.h:88
xAOD::TauJetParameters::IntermediateAxis
@ IntermediateAxis
Definition: TauDefs.h:338
PFTauFlowElementAssoc.h
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
PFTauFlowElementAssoc::PFTauFlowElementAssoc
PFTauFlowElementAssoc(const std::string &name, ISvcLocator *pSvcLocator)
Definition: PFTauFlowElementAssoc.cxx:14
xAOD::Vertex_v1::position
const Amg::Vector3D & position() const
Returns the 3-pos.
SG::ConstAccessor< char >
PFTauFlowElementAssoc::initialize
virtual StatusCode initialize()
Definition: PFTauFlowElementAssoc.cxx:20
PFTauFlowElementAssoc::m_jetReadHandleKey
SG::ReadHandleKey< xAOD::JetContainer > m_jetReadHandleKey
Definition: PFTauFlowElementAssoc.h:65
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
PFTauFlowElementAssoc::m_neutralFEReadHandleKey
SG::ReadHandleKey< xAOD::FlowElementContainer > m_neutralFEReadHandleKey
Definition: PFTauFlowElementAssoc.h:53
PFTauFlowElementAssoc::m_tauNeutralFEWriteDecorKey
SG::WriteDecorHandleKey< xAOD::TauJetContainer > m_tauNeutralFEWriteDecorKey
The write key for adding Neutral Flow Element links to the taus.
Definition: PFTauFlowElementAssoc.h:57
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:62
FlowElement.h
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
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
xAOD::TauJet_v3
Class describing a tau jet.
Definition: TauJet_v3.h:41
PFTauFlowElementAssoc::m_chargedFEReadHandleKey
SG::ReadHandleKey< xAOD::FlowElementContainer > m_chargedFEReadHandleKey
Definition: PFTauFlowElementAssoc.h:54
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
PFTauFlowElementAssoc::m_tauChargedFEWriteDecorKey
SG::WriteDecorHandleKey< xAOD::TauJetContainer > m_tauChargedFEWriteDecorKey
The write key for adding Charged Flow Element links to the taus.
Definition: PFTauFlowElementAssoc.h:59
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
xAOD::CaloCluster_v1::p4
virtual FourMom_t p4() const
The full 4-momentum of the particle.
Definition: CaloCluster_v1.cxx:465
PFTauFlowElementAssoc::m_neutralFETauWriteDecorKey
SG::WriteDecorHandleKey< xAOD::FlowElementContainer > m_neutralFETauWriteDecorKey
The write key for adding tau element links to the Neutral Flow Elements.
Definition: PFTauFlowElementAssoc.h:61
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
PFTauFlowElementAssoc::m_chargedFETauWriteDecorKey
SG::WriteDecorHandleKey< xAOD::FlowElementContainer > m_chargedFETauWriteDecorKey
The write key for adding tau element links to the Charged Flow Elements.
Definition: PFTauFlowElementAssoc.h:63
TauTrack.h
PFTauFlowElementAssoc::m_tauJetReadHandleKey
SG::ReadHandleKey< xAOD::TauJetContainer > m_tauJetReadHandleKey
Definition: PFTauFlowElementAssoc.h:51
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
PFTauFlowElementAssoc::execute
virtual StatusCode execute(const EventContext &ctx) const
This algorithm: 1) Accesses the relevant FlowElement and TauJet containers 2) Loops over the neutral ...
Definition: PFTauFlowElementAssoc.cxx:50
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
xAOD::TrackParticle_v1::track
const Trk::Track * track() const
Returns a pointer (which can be NULL) to the Trk::Track which was used to make this TrackParticle.
Definition: TrackParticle_v1.cxx:806
xAOD::CaloVertexedTopoCluster
Evaluate cluster kinematics with a different vertex / signal state.
Definition: Event/xAOD/xAODCaloEvent/xAODCaloEvent/CaloVertexedTopoCluster.h:38
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
xAOD::TauHelpers::vertexedClusters
std::vector< xAOD::CaloVertexedTopoCluster > vertexedClusters(const xAOD::TauJet &tau, double dRMax)
Definition: TauxAODHelpers.cxx:66
xAOD::FlowElement_v1
A detector object made of other lower level object(s)
Definition: FlowElement_v1.h:25