ATLAS Offline Software
Loading...
Searching...
No Matches
TauJetRNNEvaluator.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
8
10
11#include <algorithm>
12
13
15 TauRecToolBase(name),
16 m_net_0p(nullptr),
17 m_net_1p(nullptr),
18 m_net_2p(nullptr),
19 m_net_3p(nullptr) {
20}
21
22
24
26 ATH_MSG_INFO("Initializing TauJetRNNEvaluator");
27
28 std::string weightfile_0p("");
29 std::string weightfile_1p("");
30 std::string weightfile_2p("");
31 std::string weightfile_3p("");
32
33 // Use PathResolver to search for the weight files
34 if (!m_weightfile_0p.empty()) {
35 weightfile_0p = find_file(m_weightfile_0p);
36 if (weightfile_0p.empty()) {
37 ATH_MSG_ERROR("Could not find network weights: " << m_weightfile_0p);
38 return StatusCode::FAILURE;
39 } else {
40 ATH_MSG_INFO("Using network config [0-prong]: " << weightfile_0p);
41 }
42 }
43
44 if (!m_weightfile_1p.empty()) {
45 weightfile_1p = find_file(m_weightfile_1p);
46 if (weightfile_1p.empty()) {
47 ATH_MSG_ERROR("Could not find network weights: " << m_weightfile_1p);
48 return StatusCode::FAILURE;
49 } else {
50 ATH_MSG_INFO("Using network config [1-prong]: " << weightfile_1p);
51 }
52 }
53
54 if (!m_weightfile_2p.empty()) {
55 weightfile_2p = find_file(m_weightfile_2p);
56 if (weightfile_2p.empty()) {
57 ATH_MSG_ERROR("Could not find network weights: " << m_weightfile_2p);
58 return StatusCode::FAILURE;
59 } else {
60 ATH_MSG_INFO("Using network config [2-prong]: " << weightfile_2p);
61 }
62 }
63
64 if (!m_weightfile_3p.empty()) {
65 weightfile_3p = find_file(m_weightfile_3p);
66 if (weightfile_3p.empty()) {
67 ATH_MSG_ERROR("Could not find network weights: " << m_weightfile_3p);
68 return StatusCode::FAILURE;
69 } else {
70 ATH_MSG_INFO("Using network config [3-prong]: " << weightfile_3p);
71 }
72 }
73
74 // Set the layer and node names in the weight file
75 TauJetRNN::Config config;
76 config.input_layer_scalar = m_input_layer_scalar;
77 config.input_layer_tracks = m_input_layer_tracks;
78 config.input_layer_clusters = m_input_layer_clusters;
79 config.output_layer = m_output_layer;
80 config.output_node = m_output_node;
81
82 // Load the weights and create the network
83 // 0p is for trigger only
84 if (!weightfile_0p.empty()) {
85 m_net_0p = std::make_unique<TauJetRNN>(weightfile_0p, config, m_useTRT);
86 if (!m_net_0p) {
87 ATH_MSG_ERROR("No network configured for 0-prong taus.");
88 return StatusCode::FAILURE;
89 }
90 }
91
92 m_net_1p = std::make_unique<TauJetRNN>(weightfile_1p, config, m_useTRT);
93 if (!m_net_1p) {
94 ATH_MSG_ERROR("No network configured for 1-prong taus.");
95 return StatusCode::FAILURE;
96 }
97
98 // 2p is optional
99 if (!weightfile_2p.empty()) {
100 m_net_2p = std::make_unique<TauJetRNN>(weightfile_2p, config, m_useTRT);
101 if (!m_net_2p) {
102 ATH_MSG_ERROR("No network configured for 2-prong taus.");
103 return StatusCode::FAILURE;
104 }
105 }
106
107 m_net_3p = std::make_unique<TauJetRNN>(weightfile_3p, config, m_useTRT);
108 if (!m_net_3p) {
109 ATH_MSG_ERROR("No network configured for 3-prong taus.");
110 return StatusCode::FAILURE;
111 }
112
113 return StatusCode::SUCCESS;
114}
115
117 const EventContext& /*ctx*/) const {
118 // Output variable accessor
120
121 // Set default score and overwrite later
122 output(tau) = -1111.0f;
123
124 // save CPU when running PHYS derivations
126 if (tau.nTracks()>5) return StatusCode::SUCCESS;
127 }
128
129 const auto nTracksCharged = tau.nTracksCharged();
130
131 // Get input objects
132 std::vector<const xAOD::TauTrack *> tracks;
133 ATH_CHECK(get_tracks(tau, tracks));
134 std::vector<xAOD::CaloVertexedTopoCluster> clusters;
135 ATH_CHECK(get_clusters(tau, clusters));
136
137 // Evaluate networks
138 if (nTracksCharged==0 && m_net_0p) {
139 output(tau) = m_net_0p->compute(tau, tracks, clusters);
140 }
141 else if (nTracksCharged == 1) {
142 output(tau) = m_net_1p->compute(tau, tracks, clusters);
143 }
144 else if (nTracksCharged == 2) {
145 if(m_net_2p) {
146 output(tau) = m_net_2p->compute(tau, tracks, clusters);
147 } else {
148 output(tau) = m_net_3p->compute(tau, tracks, clusters);
149 }
150 }
151 else if (nTracksCharged > 2) {
152 output(tau) = m_net_3p->compute(tau, tracks, clusters);
153 }
154
155 return StatusCode::SUCCESS;
156}
157
158StatusCode TauJetRNNEvaluator::get_tracks(const xAOD::TauJet &tau, std::vector<const xAOD::TauTrack *> &out) const {
159 std::vector<const xAOD::TauTrack*> tracks = tau.allTracks();
160
161 // Skip unclassified tracks:
162 // - the track is a LRT and classifyLRT = false
163 // - the track is not among the MaxNtracks highest-pt tracks in the track classifier
164 // - track classification is not run (trigger)
166 std::vector<const xAOD::TauTrack*>::iterator it = tracks.begin();
167 while(it != tracks.end()) {
168 if((*it)->flag(xAOD::TauJetParameters::unclassified)) {
169 it = tracks.erase(it);
170 }
171 else {
172 ++it;
173 }
174 }
175 }
176
177 // Sort by descending pt
178 auto cmp_pt = [](const xAOD::TauTrack *lhs, const xAOD::TauTrack *rhs) {
179 return lhs->pt() > rhs->pt();
180 };
181 std::sort(tracks.begin(), tracks.end(), cmp_pt);
182
183 // Truncate tracks
184 if (tracks.size() > m_max_tracks) {
185 tracks.resize(m_max_tracks);
186 }
187 out = std::move(tracks);
188
189 return StatusCode::SUCCESS;
190}
191
192StatusCode TauJetRNNEvaluator::get_clusters(const xAOD::TauJet &tau, std::vector<xAOD::CaloVertexedTopoCluster> &clusters) const {
193
194 TLorentzVector tauAxis = tauRecTools::getTauAxis(tau, m_doVertexCorrection);
195
196 std::vector<xAOD::CaloVertexedTopoCluster> vertexedClusterList = tau.vertexedClusters();
197 for (const xAOD::CaloVertexedTopoCluster& vertexedCluster : vertexedClusterList) {
198 TLorentzVector clusterP4 = vertexedCluster.p4();
199 if (clusterP4.DeltaR(tauAxis) > m_max_cluster_dr) continue;
200
201 clusters.push_back(vertexedCluster);
202 }
203
204 // Sort by descending et
205 auto et_cmp = [](const xAOD::CaloVertexedTopoCluster& lhs,
207 return lhs.p4().Et() > rhs.p4().Et();
208 };
209 std::sort(clusters.begin(), clusters.end(), et_cmp);
210
211 // Truncate clusters
212 if (clusters.size() > m_max_clusters) {
213 clusters.resize(m_max_clusters, clusters[0]);
214 }
215
216 return StatusCode::SUCCESS;
217}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_INFO(x,...)
Helper class to provide type-safe access to aux data.
Gaudi::Property< std::string > m_weightfile_2p
virtual StatusCode initialize() override
Tool initializer.
Gaudi::Property< std::string > m_input_layer_clusters
virtual StatusCode executeTool(xAOD::TauJet &tau, const EventContext &ctx) const override
Execute - called for each tau candidate.
Gaudi::Property< std::string > m_input_layer_tracks
Gaudi::Property< float > m_max_cluster_dr
std::unique_ptr< TauJetRNN > m_net_0p
Gaudi::Property< bool > m_doTrackClassification
Gaudi::Property< std::string > m_weightfile_1p
Gaudi::Property< std::size_t > m_max_clusters
Gaudi::Property< bool > m_useTRT
TauJetRNNEvaluator(const std::string &name="TauJetRNNEvaluator")
StatusCode get_tracks(const xAOD::TauJet &tau, std::vector< const xAOD::TauTrack * > &out) const
std::unique_ptr< TauJetRNN > m_net_2p
Gaudi::Property< std::string > m_output_node
std::unique_ptr< TauJetRNN > m_net_3p
Gaudi::Property< std::string > m_weightfile_0p
StatusCode get_clusters(const xAOD::TauJet &tau, std::vector< xAOD::CaloVertexedTopoCluster > &out) const
Gaudi::Property< std::string > m_weightfile_3p
Gaudi::Property< std::string > m_output_layer
Gaudi::Property< std::string > m_input_layer_scalar
std::unique_ptr< TauJetRNN > m_net_1p
Gaudi::Property< bool > m_doVertexCorrection
Gaudi::Property< std::string > m_output_varname
Gaudi::Property< bool > m_applyLooseTrackSel
Gaudi::Property< std::size_t > m_max_tracks
TauRecToolBase(const std::string &name)
std::string find_file(const std::string &fname) const
virtual FourMom_t p4() const final
The full 4-momentum of the particle.
Evaluate cluster kinematics with a different vertex / signal state.
virtual double pt() const
The transverse momentum ( ) of the particle.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
TLorentzVector getTauAxis(const xAOD::TauJet &tau, bool doVertexCorrection=true)
Return the four momentum of the tau axis The tau axis is widely used to select clusters and cells in ...
TauTrack_v1 TauTrack
Definition of the current version.
Definition TauTrack.h:16
TauJet_v3 TauJet
Definition of the current "tau version".
Definition TauJet.h:17