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
10
11// RoIDescriptor for wedge selections
13
14//Include some helpful ROOT objects here.
15
16#include "CxxUtils/phihelper.h"
17#include <cmath>
18#include <memory> //unique_ptr
19#include <algorithm> //sort, min, max
20
21
22namespace FlavorTagDiscriminants {
23
24 JetHitAssociationAlg::JetHitAssociationAlg(const std::string& name, ISvcLocator* loc)
25 : AthReentrantAlgorithm(name, loc) {}
26
27
29 ATH_MSG_INFO("Initializing " << name());
30
31 // Initialize jet keys
32 ATH_CHECK(m_jetCollectionKey.initialize());
33 CHECK(m_hitAssociationKey.initialize());
34
35 // Initialize hits position decoration keys
37 ATH_CHECK(m_hitsXRelToVertexKey.initialize());
38 ATH_CHECK(m_hitsYRelToVertexKey.initialize());
39 ATH_CHECK(m_hitsZRelToVertexKey.initialize());
40
42 ATH_MSG_FATAL("Invalid RoiDescriptor size!");
43 return StatusCode::FAILURE;
44 }
45
46 return StatusCode::SUCCESS;
47 }
48
49
50 StatusCode JetHitAssociationAlg::execute(const EventContext& ctx) const {
51 ATH_MSG_DEBUG("Executing " << name());
52
53 // Read out jets
55 if(!jetReadHandle.isValid()) {
56 ATH_MSG_ERROR("Failed to retrieve jet container with key " << m_jetCollectionKey.key());
57 return StatusCode::FAILURE;
58 }
59 // Avoid reading out hits if there are no jets to attach them to
60 if(jetReadHandle->empty()) return StatusCode::SUCCESS;
61
62 // Set up jet decorators
64
65
66 // Read out hits (we need to keep the handles open to later build the ElementLinks)
68 if(!hitsHandle.isValid()) {
69 ATH_MSG_ERROR("Failed to retrieve hits container with key " << hitsHandle.key());
70 return StatusCode::FAILURE;
71 }
72
76
77 // The bec aux-variable is not declaared through keys to the SG, so it creates dependency errors in the
78 // schedules if we try to use a ReadDecorHandle(Key) for it.
79 static const SG::AuxElement::ConstAccessor<int> bec("bec");
80
81 // Filter input hits
82 std::vector<std::pair<TLorentzVector, const xAOD::TrackMeasurementValidation*>> hits;
83 for(const xAOD::TrackMeasurementValidation* hit : *hitsHandle) {
84 const int hit_bec = bec(*hit);
85 if(m_removeBadIDPixelHits && !isGoodIDPixelHit(hit)) continue;
86 if(hit_bec == 0 && !m_includeBarrel) continue;
87 if(hit_bec != 0 && !m_includeEndcap) continue;
88
89 hits.emplace_back(TLorentzVector(x(*hit), y(*hit), z(*hit), 0), hit);
90 }
91
92 // Loop over jets
93 for(const xAOD::IParticle* jet : *jetReadHandle) {
94 // Create links and decorate the jet
95 std::vector<ElementLink<xAOD::TrackMeasurementValidationContainer>> links;
96 for(const auto& [dist, hit] : getJetHits(jet, hits)) {
99 hit->index(),
100 ctx
101 ));
102 }
103
104 hitAssociation(*jet) = std::move(links);
105 }
106
107 return StatusCode::SUCCESS;
108 }
109
110
111 const std::vector<std::pair<float, const xAOD::TrackMeasurementValidation*>>
113 const std::vector<std::pair<TLorentzVector, const xAOD::TrackMeasurementValidation*>>& hits) const {
114 const TLorentzVector p4 = jet->p4();
115
116 std::unique_ptr<RoiDescriptor> roi;
118 roi = std::make_unique<RoiDescriptor>(
119 jet->eta(), jet->eta() - m_dEtaHitToVertex, jet->eta() + m_dEtaHitToVertex,
120 jet->phi(), jet->phi() - m_dPhiHitToJet, jet->phi() + m_dPhiHitToJet,
121 0, m_dZHitToVertex, m_dZHitToVertex // We're working with coordinates w.r.t. vertex
122 );
123 }
124
125 // Loop over all hits, and calculate/check their dPhi distances w.r.t. the jet
126 std::vector<std::pair<float, const xAOD::TrackMeasurementValidation*>> ret;
127 for(const auto& [hitP4, hit] : hits) {
128
129 if(m_useWedgeSelection && !RoiUtil::contains(*roi, p4.Z(), p4.Perp(), p4.Phi())) continue;
130
131 const float dPhi = std::abs(CxxUtils::wrapToPi(p4.Phi() - hitP4.Phi()));
132 if(!m_useWedgeSelection && m_dPhiHitToJet >= 0 && dPhi > m_dPhiHitToJet) continue;
133
134 ret.emplace_back(dPhi, hit);
135 }
136
137 // If a maximum number of hits has been provided, sort them by distance and truncate the vector
138 if(m_maxHits > 0) {
139 std::sort(ret.begin(), ret.end(), [](const auto& h1, const auto& h2) -> bool { return h1.first < h2.first; });
140 ret.resize(std::min(static_cast<unsigned int>(m_maxHits), static_cast<unsigned int>(ret.size())));
141 }
142
143 return ret;
144 }
145
146
148 if(!hit) return false;
149
150 static const SG::AuxElement::ConstAccessor<char> isFake("isFake");
151 if(isFake(*hit)) return false;
152
153 static const SG::AuxElement::ConstAccessor<int> hasBSError("hasBSError");
154 if(hasBSError(*hit)) return false;
155
156 static const SG::AuxElement::ConstAccessor<char> DCSState("DCSState");
157 if(DCSState(*hit)) return false;
158
159 return true;
160 }
161
162}
#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_DEBUG(x)
#define CHECK(...)
Evaluate an expression and check for errors.
Handle class for reading a decoration on an object.
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
bool isGoodIDPixelHit(const xAOD::TrackMeasurementValidation *hit) const
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< std::pair< TLorentzVector, const xAOD::TrackMeasurementValidation * > > &hits) const
SG::WriteDecorHandleKey< xAOD::IParticleContainer > m_hitAssociationKey
SG::ReadDecorHandleKey< xAOD::TrackMeasurementValidationContainer > m_hitsZRelToVertexKey
SG::ConstAccessor< T, ALLOC > ConstAccessor
Definition AuxElement.h:570
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.
T wrapToPi(T phi)
Wrap angle in radians to [-pi, pi].
Definition phihelper.h:24
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.