ATLAS Offline Software
Loading...
Searching...
No Matches
PFEnergyPredictorTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
9
10#include "CaloGeoHelpers/CaloSampling.h"
11
12PFEnergyPredictorTool::PFEnergyPredictorTool(const std::string& type, const std::string& name, const IInterface* parent) : AthAlgTool(type, name, parent)
13{
14
15}
16
17
19{
20 ATH_MSG_DEBUG("Initializing " << name());
21 if(m_model_filepath == "////"){
22 ATH_MSG_WARNING("model not provided tool will not work");
23 return StatusCode::SUCCESS;
24 }
25 ATH_CHECK(m_svc.retrieve());
26 std::string path = m_model_filepath;//Add path resolving code
27
28 Ort::SessionOptions session_options;
29 Ort::AllocatorWithDefaultOptions allocator;
30 session_options.SetIntraOpNumThreads(1);
31 session_options.SetGraphOptimizationLevel(ORT_ENABLE_BASIC);
32 m_session = std::make_unique<Ort::Session>(m_svc->env(), path.c_str(), session_options);
33
34 ATH_MSG_INFO("Created ONNX runtime session with model " << path);
35
36 size_t num_input_nodes = m_session->GetInputCount();
37 m_input_node_names.resize(num_input_nodes);
38
39 for (std::size_t i = 0; i < num_input_nodes; i++) {
40 // print input node names
41 char* input_name = m_session->GetInputNameAllocated(i, allocator).release();
42 ATH_MSG_INFO("Input " << i << " : "
43 << " name= " << input_name);
44 m_input_node_names[i] = input_name;
45 // print input node types
46 Ort::TypeInfo type_info = m_session->GetInputTypeInfo(i);
47 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
48 ONNXTensorElementDataType type = tensor_info.GetElementType();
49 ATH_MSG_INFO("Input " << i << " : "
50 << " type= " << type);
51
52 // print input shapes/dims
53 m_input_node_dims = tensor_info.GetShape();
54 m_input_node_dims[1] = 5430/5;
55 ATH_MSG_INFO("Input " << i << " : num_dims= " << m_input_node_dims.size());
56 for (std::size_t j = 0; j < m_input_node_dims.size(); j++) {
57 if (m_input_node_dims[j] < 0) m_input_node_dims[j] = 1;
58 ATH_MSG_INFO("Input " << i << " : dim " << j << "= " << m_input_node_dims[j]);
59 }
60 }
61
62 // output nodes
63 std::vector<int64_t> output_node_dims;
64 size_t num_output_nodes = m_session->GetOutputCount();
65 ATH_MSG_INFO("Have output nodes " << num_output_nodes);
66 m_output_node_names.resize(num_output_nodes);
67
68 for (std::size_t i = 0; i < num_output_nodes; i++) {
69 // print output node names
70 char* output_name = m_session->GetOutputNameAllocated(i, allocator).release();
71 ATH_MSG_INFO("Output " << i << " : "
72 << " name= " << output_name);
73 m_output_node_names[i] = output_name;
74
75 Ort::TypeInfo type_info = m_session->GetOutputTypeInfo(i);
76 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
77 ONNXTensorElementDataType type = tensor_info.GetElementType();
78 ATH_MSG_INFO("Output " << i << " : "
79 << " type= " << type);
80
81 // print output shapes/dims
82 output_node_dims = tensor_info.GetShape();
83 ATH_MSG_INFO("Output " << i << " : num_dims= " << output_node_dims.size());
84 for (std::size_t j = 0; j < output_node_dims.size(); j++) {
85 if (output_node_dims[j] < 0) output_node_dims[j] = 1;
86 ATH_MSG_INFO("Output" << i << " : dim " << j << "= " << output_node_dims[j]);
87 }
88 }
89
90 return StatusCode::SUCCESS;
91}
92
93float PFEnergyPredictorTool::runOnnxInference(std::vector<float> &tensor) const {
94 using std::endl;
95 using std::cout;
96 auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
97 auto input_tensor_size = tensor.size();
98
99 Ort::Value input_tensor =
100 Ort::Value::CreateTensor<float>(memory_info, tensor.data(), input_tensor_size,
101 m_input_node_dims.data(), m_input_node_dims.size());
102
103 auto output_tensors = m_session->Run(Ort::RunOptions{nullptr}, m_input_node_names.data(), &input_tensor, m_input_node_names.size(),
105
106 const float *output_score_array = output_tensors.front().GetTensorData<float>();
107
108 // Binary classification - the score is just the first element of the output tensor
109 float output_score = output_score_array[0];
110
111 return output_score;
112}
113
114std::array<double,19> getEtaTrackCalo(const eflowTrackCaloPoints& trackCaloPoints) {
115 return std::array<double,19> { trackCaloPoints.getEta(eflowCalo::EMB1), trackCaloPoints.getEta(eflowCalo::EMB2), trackCaloPoints.getEta(eflowCalo::EMB3),
116 trackCaloPoints.getEta(eflowCalo::EME1), trackCaloPoints.getEta(eflowCalo::EME2), trackCaloPoints.getEta(eflowCalo::EME3),
117 trackCaloPoints.getEta(eflowCalo::HEC1), trackCaloPoints.getEta(eflowCalo::HEC2), trackCaloPoints.getEta(eflowCalo::HEC3),trackCaloPoints.getEta(eflowCalo::HEC4),
118 trackCaloPoints.getTileEta(CaloSampling::TileBar0),trackCaloPoints.getTileEta(CaloSampling::TileBar1),trackCaloPoints.getTileEta(CaloSampling::TileBar2),
119 trackCaloPoints.getTileEta(CaloSampling::TileGap1),trackCaloPoints.getTileEta(CaloSampling::TileGap2),trackCaloPoints.getTileEta(CaloSampling::TileGap3),
120 trackCaloPoints.getTileEta(CaloSampling::TileExt0),trackCaloPoints.getTileEta(CaloSampling::TileExt1),trackCaloPoints.getTileEta(CaloSampling::TileExt2)};
121}
122
123
124std::array<double,19> getPhiTrackCalo(const eflowTrackCaloPoints& trackCaloPoints) {
125 return std::array<double,19> { trackCaloPoints.getPhi(eflowCalo::EMB1), trackCaloPoints.getPhi(eflowCalo::EMB2), trackCaloPoints.getPhi(eflowCalo::EMB3),
126 trackCaloPoints.getPhi(eflowCalo::EME1), trackCaloPoints.getPhi(eflowCalo::EME2), trackCaloPoints.getPhi(eflowCalo::EME3),
127 trackCaloPoints.getPhi(eflowCalo::HEC1), trackCaloPoints.getPhi(eflowCalo::HEC2), trackCaloPoints.getPhi(eflowCalo::HEC3),trackCaloPoints.getPhi(eflowCalo::HEC4),
128 trackCaloPoints.getTilePhi(CaloSampling::TileBar0),trackCaloPoints.getTilePhi(CaloSampling::TileBar1),trackCaloPoints.getTilePhi(CaloSampling::TileBar2),
129 trackCaloPoints.getTilePhi(CaloSampling::TileGap1),trackCaloPoints.getTilePhi(CaloSampling::TileGap2),trackCaloPoints.getTilePhi(CaloSampling::TileGap3),
130 trackCaloPoints.getTilePhi(CaloSampling::TileExt0),trackCaloPoints.getTilePhi(CaloSampling::TileExt1),trackCaloPoints.getTilePhi(CaloSampling::TileExt2)};
131}
132
133
135
136 constexpr std::array<int,19> calo_numbers{1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
137 constexpr std::array<int,12> fixed_r_numbers = {1,2,3,12,13,14,15,16,17,18,19,20};
138 constexpr std::array<double,12> fixed_r_vals = {1532.18, 1723.89, 1923.02, 2450.00, 2995.00, 3630.00, 3215.00,
139 3630.00, 2246.50, 2450.00, 2870.00, 3480.00
140 };
141 constexpr std::array<int, 7> fixed_z_numbers = {5,6,7,8,9,10,11};
142 constexpr std::array<double, 7> fixed_z_vals = {3790.03, 3983.68, 4195.84, 4461.25, 4869.50, 5424.50, 5905.00};
143 std::unordered_map<int, double> r_calo_dict;//change to flatmap in c++23
144 std::unordered_map<int, double> z_calo_dict;
145 for(size_t i=0; i<fixed_r_vals.size(); i++) r_calo_dict[fixed_r_numbers[i]] = fixed_r_vals[i];
146 for(size_t i=0; i<fixed_z_numbers.size(); i++) z_calo_dict[fixed_z_numbers[i]] = fixed_z_vals[i];
147
148 std::vector<float> inputnn;
149 inputnn.assign(5430, 0.0);
150 std::vector<eflowRecCluster*> matchedClusters;
151 std::vector<eflowTrackClusterLink*> links = ptr->getClusterMatches();
152
153 std::array<double, 19> etatotal = getEtaTrackCalo(ptr->getTrackCaloPoints());
154 std::array<double, 19> phitotal = getPhiTrackCalo(ptr->getTrackCaloPoints());
155
156 const std::array<double, 2> track{ptr->getTrack()->eta(), ptr->getTrack()->phi()};
157
158 for(auto *clink : links){
159 auto *cell = clink->getCluster()->getCluster();
160 float clusterE = cell->e()*1e-3;
161 float clusterEta = cell->eta();
162
163 if (clusterE < 0.0 || clusterE > 1e4f || std::abs(clusterEta) > 2.5) continue;
164
165 constexpr bool cutOnR = false;
166 if(cutOnR){
167 std::array<double, 2> p{clink->getCluster()->getCluster()->eta(), clink->getCluster()->getCluster()->phi()};
168 double part1 = p[0] - track[0];
169 double part2 = p[1] - track[1];
170 while(part1 > M_PI) part1 -= 2*M_PI;
171 while(part1 < -M_PI) part1 += 2*M_PI;
172 while(part2 > M_PI) part2 -= 2*M_PI;
173 while(part2 < -M_PI) part2 += 2*M_PI;
174 double R = std::sqrt(part1 * part1 + part2*part2);
175 if(R >= 1.2) continue;
176 }
177
178 matchedClusters.push_back(clink->getCluster());
179 }
180
181 std::vector<std::array<double, 5>> cells;
182
183 const eflowTrackCaloPoints& trackCaloPoints = ptr->getTrackCaloPoints();
184 bool trk_bool_em[2] = {false,false};
185 std::array<double,2> trk_em_eta = {trackCaloPoints.getEta(eflowCalo::EMB2), trackCaloPoints.getEta(eflowCalo::EME2)};
186 std::array<double,2> trk_em_phi = {trackCaloPoints.getPhi(eflowCalo::EMB2), trackCaloPoints.getPhi(eflowCalo::EME2)};
187 double eta_ctr;
188 double phi_ctr;
189 for(int i =0; i<2; i++) {
190 trk_bool_em[i] = std::abs(trk_em_eta[i]) < 2.5 && std::abs(trk_em_phi[i]) <= M_PI;
191 }
192 int nProj_em = (int)trk_bool_em[0] + (int)trk_bool_em[1];
193
194 if(nProj_em ==1) {
195 eta_ctr = trk_bool_em[0] ? trk_em_eta[0] : trk_em_eta[1];
196 phi_ctr = trk_bool_em[0] ? trk_em_phi[0] : trk_em_phi[1];
197 } else if(nProj_em==2) {
198 eta_ctr = (trk_em_eta[0] + trk_em_eta[1]) / 2.0;
199 phi_ctr = (trk_em_phi[0] + trk_em_phi[1]) / 2.0;
200 } else {
201 eta_ctr = ptr->getTrack()->eta();
202 phi_ctr = ptr->getTrack()->phi();
203 }
204
205
206
207 for(auto *cptr : matchedClusters){
208 auto *clustlink = cptr->getCluster();
209
210 for(auto it_cell = clustlink->cell_begin(); it_cell != clustlink->cell_end(); it_cell++){
211 const CaloCell* cell = (*it_cell);
212 float cellE = cell->e()*(it_cell.weight())*1e-3f;
213 if(cellE < 0.005) continue;//Cut from ntuple maker
214 const auto *theDDE=it_cell->caloDDE();
215 double cx=theDDE->x();
216 double cy=theDDE->y();
217
218 cells.emplace_back( std::array<double, 5> { cellE,
219 theDDE->eta() - eta_ctr,
220 theDDE->phi() - phi_ctr,
221 std::hypot(cx,cy), //rperp
222 0.0 } );
223 }
224 }
225
226
227 std::vector<bool> trk_bool(calo_numbers.size(), false);
228 std::vector<std::array<double,4>> trk_full(calo_numbers.size());
229 for(size_t j=0; j<phitotal.size(); j++) {
230 int cnum = calo_numbers[j];
231 double eta = etatotal[j];
232 double phi = phitotal[j];
233 if(std::abs(eta) < 2.5 && std::abs(phi) <= M_PI) {
234 trk_bool[j] = true;
235 trk_full[j][0] = eta;
236 trk_full[j][1] = phi;
237 trk_full[j][3] = cnum;
238 double rPerp =-99999;
239 if(auto itr = r_calo_dict.find(cnum); itr != r_calo_dict.end()) rPerp = itr->second;
240 else if(auto itr = z_calo_dict.find(cnum); itr != z_calo_dict.end())
241 {
242 double z = itr->second;
243 if(eta != 0.0){
244 double aeta = std::abs(eta);
245 rPerp = z*2.*std::exp(aeta)/(std::exp(2.0*aeta)-1.0);
246 }else rPerp =0.0; //Check if this makes sense
247 } else {
248 throw std::runtime_error("Calo sample num not found in dicts..");
249 }
250 trk_full[j][2] = rPerp;
251 } else {
252 trk_full[j].fill(0.0);
253 }
254 }
255 double trackP = std::abs(1. / ptr->getTrack()->qOverP()) * 1e-3;
256 int trk_proj_num = std::accumulate(trk_bool.begin(), trk_bool.end(), 0);
257 if(trk_proj_num ==0) {
258 trk_proj_num =1;
259 std::array<double,5> trk_arr{};
260
261 trk_arr[0] = trackP;
262 trk_arr[1] = ptr->getTrack()->eta() - eta_ctr;
263 trk_arr[2] = ptr->getTrack()->phi() - phi_ctr;
264 trk_arr[3] = 1532.18; // just place it in EMB1
265 trk_arr[4] = 1.;
266
267 cells.emplace_back(trk_arr);
268 } else {
269 for(size_t i =0; i<calo_numbers.size(); i++) {
270 if(!trk_bool[i]) continue;
271 std::array<double,5> trk_arr{};
272 trk_arr[0]= trackP/double(trk_proj_num);
273 trk_arr[1]= trk_full[i][0] - eta_ctr;
274 trk_arr[2]= trk_full[i][1] - phi_ctr;
275 trk_arr[3]= trk_full[i][2];
276 trk_arr[4]= 1.;
277
278 cells.emplace_back(trk_arr);
279 }
280 }
281
282 int index = 0;
283 for(auto &in : cells){
284 std::copy(in.begin(), in.end(), inputnn.begin() + index);
285 index+=5;
286 if(index >= static_cast<int>(inputnn.size()-4)) {
287 ATH_MSG_WARNING("Data exceeded tensor size");
288 break;
289 }
290 }
291
292 //Normalization prior to training
293 NormalizeTensor(inputnn, cells.size() * 5 );
294
295 float predictedEnergy = exp(runOnnxInference(inputnn)) * 1000.0;//Correct to MeV units
296 ATH_MSG_DEBUG("NN Predicted energy " << predictedEnergy);
297 return predictedEnergy;
298
299}
300
301void PFEnergyPredictorTool::NormalizeTensor(std::vector<float> &inputnn, size_t limit) const{
302 size_t i=0;
303 for(i =0;i<limit;i+=5){
304 auto &f = inputnn[i+3];
305 if(f!= 0.0f) f/= 3630.f;
306 auto &e = inputnn[i+0];
307 if(e!= 0.0f){
308 e = std::log(e);
309 e = (e - m_cellE_mean)/m_cellE_std;
310 }
311 auto &eta = inputnn[i+1];
312 if(eta!= 0.0) eta /= 0.7f;
313 auto &phi = inputnn[i+2];
314 if(phi!= 0.0) phi /= m_cellPhi_std;
315 }
316 if(i> inputnn.size()){
317 ATH_MSG_ERROR("Index exceeded tensor MEMORY CORRUPTION");
318 }
319}
320
321
322
324{
325 return StatusCode::SUCCESS;
326}
327
#define M_PI
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_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::array< double, 19 > getPhiTrackCalo(const eflowTrackCaloPoints &trackCaloPoints)
std::array< double, 19 > getEtaTrackCalo(const eflowTrackCaloPoints &trackCaloPoints)
#define z
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Data object for each calorimeter readout cell.
Definition CaloCell.h:57
void NormalizeTensor(std::vector< float > &tensor, size_t limit) const
float runOnnxInference(std::vector< float > &tensor) const
std::vector< const char * > m_input_node_names
Gaudi::Property< float > m_cellE_std
Gaudi::Property< std::string > m_model_filepath
virtual StatusCode finalize() override
std::vector< const char * > m_output_node_names
PFEnergyPredictorTool(const std::string &type, const std::string &name, const IInterface *parent)
float nnEnergyPrediction(const eflowRecTrack *ptr) const
virtual StatusCode initialize() override
Gaudi::Property< float > m_cellPhi_std
Gaudi::Property< float > m_cellE_mean
Normalization constants for the inputs to the onnx model.
std::vector< int64_t > m_input_node_dims
ServiceHandle< AthOnnx::IOnnxRuntimeSvc > m_svc
This class extends the information about a xAOD::Track.
This class stores a map of calorimeter layers and track parameters (the result of the track extrapola...
double getTileEta(CaloCell_ID::CaloSample layer) const
double getPhi(eflowCalo::LAYER layer) const
double getEta(eflowCalo::LAYER layer) const
double getTilePhi(CaloCell_ID::CaloSample layer) const
Definition index.py:1
Definition part1.py:1
Definition part2.py:1