ATLAS Offline Software
Loading...
Searching...
No Matches
TauGNNEvaluator.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
7
9
10#include <algorithm>
11
12
13TauGNNEvaluator::TauGNNEvaluator(const std::string &name):
14 TauRecToolBase(name) {}
15
17
20 ATH_MSG_FATAL("Invalid TauGNNEvaluator discriminant setting: " << m_output_discriminant);
21 }
22
23 if(!m_tauContainerName.empty()) {
24 // We should move to using WriteDecorHandles in the future, but for now
25 // we create keys to enforce data-dependencies in the scheduler
26
28 ATH_CHECK(m_scoreHandleKey.initialize());
29
31 ATH_CHECK(m_pTauHandleKey.initialize());
32
34 ATH_CHECK(m_pJetHandleKey.initialize());
35 }
36
37 if(!m_tauContainerName.empty() && !m_hitsHandleKey.empty()) {
40 ATH_CHECK(m_hitsHandleKey.initialize());
41 } else if (m_max_hits > 0) {
42 ATH_MSG_ERROR("TauContainerName and HitsHandleKey must be provided to read hits for the GNN evaluation");
43 return StatusCode::FAILURE;
44 }
45
46
47 ATH_MSG_INFO("Initializing TauGNNEvaluator with "<<m_max_tracks.value()<<" tracks, "<<m_max_clusters<<" clusters, and "<<m_max_hits<<" hits...");
48 // We can either use an inclussive GNN (e.g. Offline GNTauv0), or a prong-dependent GNN (e.g. HLT GNTau), not both!
49
50 if(!m_weightfile_inclusive.empty()) { // Prong-inclusive network
51 if(!m_weightfile_0p.empty() || !m_weightfile_1p.empty() || !m_weightfile_2p.empty() || !m_weightfile_3p.empty()) {
52 ATH_MSG_ERROR("Cannot load both prong-inclusive and prong-dependent networks!");
53 return StatusCode::FAILURE;
54 }
55
56 ATH_MSG_INFO("Loading prong-inclusive TauID GNN");
58 if(!m_net_inclusive) return StatusCode::FAILURE;
59
60 } else { // Prong-dependent networks
61
62 // 0-prong is optional
63 if(!m_weightfile_0p.empty()) {
64 ATH_MSG_INFO("Loading 0-prong TauID GNN");
66 if(!m_net_0p) return StatusCode::FAILURE;
67 }
68
69 ATH_MSG_INFO("Loading 1-prong TauID GNN");
71 if(!m_net_1p) return StatusCode::FAILURE;
72
73 // 2-prong is optional
74 if(!m_weightfile_2p.empty()) {
75 ATH_MSG_INFO("Loading 2-prong TauID GNN");
77 if(!m_net_2p) return StatusCode::FAILURE;
78 }
79
80 ATH_MSG_INFO("Loading 3-prong TauID GNN");
82 if(!m_net_3p) return StatusCode::FAILURE;
83 }
84
85 return StatusCode::SUCCESS;
86}
87
88std::unique_ptr<TauGNN> TauGNNEvaluator::load_network(const std::string& network_file) const {
89 // Use PathResolver to search for the weight files
90 if(network_file.empty()) return nullptr;
91
92 const std::string pr_network_file = find_file(network_file);
93 if(pr_network_file.empty()) {
94 ATH_MSG_ERROR("Could not find network weights: " << network_file);
95 return nullptr;
96 }
97
98 ATH_MSG_INFO("Using network config: " << pr_network_file);
99
100 // Load the weights and create the network
102 config.nnFile = pr_network_file;
103 config.input_layer_scalar = m_input_layer_scalar.value();
104 config.input_layer_tracks = m_input_layer_tracks.value();
105 config.input_layer_clusters = m_input_layer_clusters.value();
106 config.input_layer_hits = m_input_layer_hits.value();
107 config.output_node_tau = m_outnode_tau.value();
108 config.output_node_jet = m_outnode_jet.value();
109 config.n_max_tracks = m_max_tracks.value();
110 config.n_max_clusters = m_max_clusters.value();
111 config.max_dr_cluster = m_max_cluster_dr.value();
112 config.n_max_hits = m_max_hits.value();
113 config.doVertexCorrection = m_doVertexCorrection.value();
114 config.trackClassification = m_doTrackClassification.value();
115 config.useTRT = m_useTRT.value();
116 config.hits_decor_name = m_hits_decor_name;
117
118 std::unique_ptr<TauGNN> net = std::make_unique<TauGNN>(config);
119 if(!net) ATH_MSG_ERROR("No network configured.");
120
121 return net;
122}
123
125 // Output variable Decorators
127 const SG::Accessor<float> out_ptau(m_output_ptau);
128 const SG::Accessor<float> out_pjet(m_output_pjet);
129 // Set default score and overwrite later
130 output(tau) = -1111.0f;
131 out_ptau(tau) = -1111.0f;
132 out_pjet(tau) = -1111.0f;
133
134 //Skip execution for low-pT taus to save resources
135 if (tau.pt() < m_minTauPt) {
136 return StatusCode::SUCCESS;
137 }
138
139 // save CPU when running PHYS derivations
141 if (tau.nTracks()>5) return StatusCode::SUCCESS;
142 }
143
144 // save CPU when running in RAWtoALL for tau trigger monitoring purpose
146 if (tau.nTracks()!=1 && tau.nTracks()!=3) return StatusCode::SUCCESS;
147 }
148
149 // Network outputs
150 std::map<std::string, float> out_f;
151 std::map<std::string, std::vector<char>> out_vc;
152 std::map<std::string, std::vector<float>> out_vf;
153
154 // Evaluate networks
155 ATH_MSG_DEBUG("Evaluating GNN for tau with nTracks = " << tau.nTracksCharged());
156 if(m_net_inclusive) {
157 std::tie(out_f, out_vc, out_vf) = m_net_inclusive->compute(tau);
158 } else {
159 // First we calculate the tau prongness
160 int n_tracks = tau.nTracksCharged();
161 // in trigger, we need to apply a min pT cut on the tracks to count the prongs,
162 // as no track classification is available
164 auto trks = tau.allTracks();
165 const float threshold = m_min_prong_track_pt;
166 n_tracks = std::count_if(trks.begin(), trks.end(),
167 [&threshold](const xAOD::TauTrack* trk) { return trk->pt() > threshold; }
168 );
169 }
170
171 if(n_tracks == 0 && m_net_0p) std::tie(out_f, out_vc, out_vf) = m_net_0p->compute(tau);
172 else if(n_tracks == 1) std::tie(out_f, out_vc, out_vf) = m_net_1p->compute(tau);
173 else if(n_tracks == 2) {
174 if(m_net_2p) std::tie(out_f, out_vc, out_vf) = m_net_2p->compute(tau);
175 else std::tie(out_f, out_vc, out_vf) = m_net_3p->compute(tau);
176 } else if(n_tracks == 3) std::tie(out_f, out_vc, out_vf) = m_net_3p->compute(tau);
177 }
178
179 // Store scores only if the inferences actually ran
180 if(out_f.contains(m_outnode_tau)) {
182 output(tau) = std::log10(1/(1-out_f.at(m_outnode_tau)));
184 output(tau) = out_f.at(m_outnode_tau);
185 }
186
187 out_ptau(tau) = out_f.at(m_outnode_tau);
188 out_pjet(tau) = out_f.at(m_outnode_jet);
189 }
190
191 return StatusCode::SUCCESS;
192}
193
#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)
Helper class to provide type-safe access to aux data.
Gaudi::Property< std::string > m_weightfile_inclusive
TauGNNEvaluator(const std::string &name="TauGNNEvaluator")
Gaudi::Property< int > m_max_tracks
Gaudi::Property< std::string > m_input_layer_hits
std::unique_ptr< TauGNN > m_net_1p
Gaudi::Property< std::string > m_input_layer_scalar
std::string m_hits_decor_name
SG::WriteDecorHandleKey< xAOD::TauJetContainer > m_scoreHandleKey
virtual ~TauGNNEvaluator()
Gaudi::Property< float > m_max_cluster_dr
std::unique_ptr< TauGNN > m_net_3p
Gaudi::Property< float > m_minTauPt
std::unique_ptr< TauGNN > load_network(const std::string &network_file) const
Gaudi::Property< int > m_output_discriminant
Gaudi::Property< std::string > m_input_layer_clusters
Gaudi::Property< int > m_max_clusters
Gaudi::Property< float > m_min_prong_track_pt
Gaudi::Property< std::string > m_outnode_tau
std::unique_ptr< TauGNN > m_net_0p
Gaudi::Property< std::string > m_tauContainerName
Gaudi::Property< bool > m_doVertexCorrection
Gaudi::Property< int > m_max_hits
Gaudi::Property< bool > m_applyTightTrackSel
Gaudi::Property< std::string > m_output_varname
Gaudi::Property< std::string > m_output_pjet
Gaudi::Property< bool > m_useTRT
std::unique_ptr< TauGNN > m_net_inclusive
std::unique_ptr< TauGNN > m_net_2p
SG::WriteDecorHandleKey< xAOD::TauJetContainer > m_pJetHandleKey
Gaudi::Property< bool > m_doTrackClassification
SG::WriteDecorHandleKey< xAOD::TauJetContainer > m_pTauHandleKey
Gaudi::Property< std::string > m_outnode_jet
Gaudi::Property< std::string > m_output_ptau
Gaudi::Property< std::string > m_weightfile_1p
Gaudi::Property< std::string > m_weightfile_3p
SG::ReadDecorHandleKey< xAOD::TauJetContainer > m_hitsHandleKey
virtual StatusCode execute(xAOD::TauJet &tau) const override
Execute - called for each tau candidate.
Gaudi::Property< bool > m_applyLooseTrackSel
virtual StatusCode initialize() override
Tool initializer.
Gaudi::Property< std::string > m_weightfile_2p
Gaudi::Property< std::string > m_input_layer_tracks
Gaudi::Property< std::string > m_weightfile_0p
TauRecToolBase(const std::string &name)
std::string find_file(const std::string &fname) const
virtual double pt() const
The transverse momentum ( ) of the particle.
size_t nTracksCharged() const
std::vector< const TauTrack * > allTracks() const
Get the v<const pointer> to all tracks associated with this tau, regardless of classification.
size_t nTracks(TauJetParameters::TauTrackFlag flag=TauJetParameters::TauTrackFlag::classifiedCharged) const
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