ATLAS Offline Software
Loading...
Searching...
No Matches
JetHitAssociationAlg.cxx
Go to the documentation of this file.
1/*
2Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5//Include header file
7
11
12// RoIDescriptor for wedge selections
14
15//Include some helpful ROOT objects here.
16
17#include "CxxUtils/phihelper.h"
18#include <cmath>
19#include <optional>
20#include <algorithm> //sort, min, max
21
22
23namespace FlavorTagDiscriminants {
24
25 JetHitAssociationAlg::JetHitAssociationAlg(const std::string& name, ISvcLocator* loc)
26 : AthReentrantAlgorithm(name, loc) {}
27
28
30 ATH_MSG_INFO("Initializing " << name());
31
32 // Initialize jet keys
33 ATH_CHECK(m_jetCollectionKey.initialize());
34 CHECK(m_hitAssociationKey.initialize());
35
36 ATH_CHECK(m_wedgeZKey.initialize(!m_wedgeZKey.empty()));
37
38 // Initialize hits position decoration keys
40 ATH_CHECK(m_hitsXRelToVertexKey.initialize());
41 ATH_CHECK(m_hitsYRelToVertexKey.initialize());
42 ATH_CHECK(m_hitsZRelToVertexKey.initialize());
43
44 if(m_useDRCone) {
45 if(m_dRHitToJet <= 0) {
46 ATH_MSG_FATAL("Invalid dR cone size!");
47 return StatusCode::FAILURE;
48 }
49 }
50 else if(m_dPhiHitToJet <= 0 || m_dEtaHitToVertex <= 0 || m_dZHitToVertex <= 0) {
51 ATH_MSG_FATAL("Invalid RoiDescriptor size!");
52 return StatusCode::FAILURE;
53 }
54
55 return StatusCode::SUCCESS;
56 }
57
58
59 StatusCode JetHitAssociationAlg::execute(const EventContext& ctx) const {
60 ATH_MSG_DEBUG("Executing " << name());
61
62 // Read out jets
64 if(!jetReadHandle.isValid()) {
65 ATH_MSG_ERROR("Failed to retrieve jet container with key " << m_jetCollectionKey.key());
66 return StatusCode::FAILURE;
67 }
68 // Avoid reading out hits if there are no jets to attach them to
69 if(jetReadHandle->empty()) return StatusCode::SUCCESS;
70
71 // Set up jet decorators
73
74 std::optional<SG::ReadDecorHandle<xAOD::IParticleContainer, float>> wedgeZ;
75 if(!m_wedgeZKey.empty()) wedgeZ.emplace(m_wedgeZKey, ctx);
76
77
78 // Read out hits (we need to keep the handles open to later build the ElementLinks)
80 if(!hitsHandle.isValid()) {
81 ATH_MSG_ERROR("Failed to retrieve hits container with key " << hitsHandle.key());
82 return StatusCode::FAILURE;
83 }
84
88
89 // The bec aux-variable is not declaared through keys to the SG, so it creates dependency errors in the
90 // schedules if we try to use a ReadDecorHandle(Key) for it.
91 static const SG::AuxElement::ConstAccessor<int> bec("bec");
92
93 // Filter input hits
94 std::vector<Hit> hits;
95 hits.reserve(hitsHandle->size());
96 for(const xAOD::TrackMeasurementValidation* hit : *hitsHandle) {
97 const int hit_bec = bec(*hit);
98 if(hit_bec == 0 && !m_includeBarrel) continue;
99 if(hit_bec != 0 && !m_includeEndcap) continue;
100
101 const float hx = x(*hit);
102 const float hy = y(*hit);
103 hits.push_back({hit, std::atan2(hy, hx), z(*hit), std::sqrt(hx*hx + hy*hy)});
104 }
105
106 // Loop over jets
107 for(const xAOD::IParticle* jet : *jetReadHandle) {
108
109 double zed = 0.0;
110 if(wedgeZ) {
111 zed = (*wedgeZ)(*jet);
112 if(!std::isfinite(zed)) {
113 ATH_MSG_WARNING("Wedge z-centre is not finite, dropping hits for this jet.");
114 hitAssociation(*jet) = {};
115 continue;
116 }
117 }
118
119 // Create links and decorate the jet
120 std::vector<ElementLink<xAOD::TrackMeasurementValidationContainer>> links;
121 for(const auto& [dist, hit] : getJetHits(jet, hits, zed)) {
124 hit->index(),
125 ctx
126 ));
127 }
128
129 hitAssociation(*jet) = std::move(links);
130 }
131
132 return StatusCode::SUCCESS;
133 }
134
135
136 const std::vector<std::pair<float, const xAOD::TrackMeasurementValidation*>>
138 const std::vector<Hit>& hits,
139 double zed) const {
140 const double eta = jet->eta();
141 const double phi = jet->phi();
142
143 std::vector<std::pair<float, const xAOD::TrackMeasurementValidation*>> ret;
144
145 if(m_useDRCone) {
146 for(const Hit& hit : hits) {
147 const float dEta = eta - std::asinh((hit.z - zed) / hit.r);
148 const float dPhi = CxxUtils::wrapToPi(phi - hit.phi);
149
150 const float dR = std::sqrt(dEta * dEta + dPhi * dPhi);
151 if(dR > m_dRHitToJet) continue;
152
153 ret.emplace_back(dR, hit.original_hit);
154 }
155 }
156 else {
157 const RoiDescriptor roi(
160 zed, zed - m_dZHitToVertex, zed + m_dZHitToVertex
161 );
162
163 for(const Hit& hit : hits) {
164 if(!RoiUtil::contains(roi, hit.z, hit.r, hit.phi)) continue;
165
166 ret.emplace_back(std::abs(CxxUtils::wrapToPi(phi - hit.phi)), hit.original_hit);
167 }
168 }
169
170 // If a maximum number of hits has been provided, sort them by distance and truncate the vector
171 if(m_maxHits > 0) {
172 std::sort(ret.begin(), ret.end(), [](const auto& h1, const auto& h2) -> bool { return h1.first < h2.first; });
173 ret.resize(std::min(static_cast<unsigned int>(m_maxHits), static_cast<unsigned int>(ret.size())));
174 }
175
176 return ret;
177 }
178
179}
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
#define CHECK(...)
Evaluate an expression and check for errors.
bool hit(const Container &ids, int pdgId)
Handle class for reading a decoration on an object.
Handle class for reading from StoreGate.
Handle class for adding a decoration to an object.
#define y
#define x
#define z
An algorithm that can be simultaneously executed in multiple threads.
virtual StatusCode execute(const EventContext &) const override
SG::ReadDecorHandleKey< xAOD::IParticleContainer > m_wedgeZKey
SG::ReadDecorHandleKey< xAOD::TrackMeasurementValidationContainer > m_hitsYRelToVertexKey
SG::ReadHandleKey< xAOD::TrackMeasurementValidationContainer > m_inputHitCollectionKey
SG::ReadHandleKey< xAOD::IParticleContainer > m_jetCollectionKey
JetHitAssociationAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadDecorHandleKey< xAOD::TrackMeasurementValidationContainer > m_hitsXRelToVertexKey
const std::vector< std::pair< float, const xAOD::TrackMeasurementValidation * > > getJetHits(const xAOD::IParticle *jet, const std::vector< Hit > &hits, double zed) const
SG::WriteDecorHandleKey< xAOD::IParticleContainer > m_hitAssociationKey
SG::ReadDecorHandleKey< xAOD::TrackMeasurementValidationContainer > m_hitsZRelToVertexKey
Describes the Region of Ineterest geometry It has basically 9 parameters.
Handle class for reading a decoration on an object.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
Handle class for adding a decoration to an object.
Class providing the definition of the 4-vector interface.
constexpr T wrapToPi(T phi)
Wrap angle in radians to [-pi, pi].
Definition phihelper.h:31
bool contains(const IRoiDescriptor &roi, double z0, double dzdr)
see whether a segment is contained within the roi in r-z
Definition RoiUtil.cxx:42
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
TrackMeasurementValidation_v1 TrackMeasurementValidation
Reference the current persistent version:
Helper for azimuthal angle calculations.