ATLAS Offline Software
Loading...
Searching...
No Matches
eFEXtauBDT.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5//*************************************************************************
6// eFEXtauBDT - description
7// --------------------
8// begin : 15 09 2022
9// email : david.reikher@gmail.com
10//*************************************************************************
11
13#include <string>
14
15#define ENERGY_WIDTH 16
16#define PARAM_WIDTH 8
17
18// default constructor for persistency
19LVL1::eFEXtauBDT::eFEXtauBDT(AthAlgTool *log, std::string config_path)
20 : m_bdt(config_path), m_log(log) {
21 m_log->msg(MSG::DEBUG) << "Configured BDT with this file: " << config_path
22 << endmsg;
23 m_bdtVars.resize(m_bdt.getVariables().size());
24 m_towers.resize(m_bdt.getNTowers());
25 int n_multipliers = sizeof(m_fracMultipliers) / sizeof(m_fracMultipliers[0]);
26 m_emEtXMultiplier.resize(n_multipliers);
27 m_emEtXMultiplierOverflow.resize(n_multipliers);
28}
29
32
34 unsigned int *sCellPtr) {
35 switch (layer) {
36 case 0:
37 m_em0cells[eta][phi] = sCellPtr;
38 break;
39 case 1:
40 m_em1cells[eta][phi] = sCellPtr;
41 break;
42 case 2:
43 m_em2cells[eta][phi] = sCellPtr;
44 break;
45 case 3:
46 m_em3cells[eta][phi] = sCellPtr;
47 break;
48 case 4:
49 m_hadcells[eta][phi] = sCellPtr;
50 break;
51 }
52}
53
55 int index, unsigned int *fracMultiplierPtr) {
56 m_fracMultipliers[index] = fracMultiplierPtr;
57}
58
60 int index, unsigned int *bdtThresholdPtr) {
61 m_bdtThresholds[index] = bdtThresholdPtr;
62}
63
64void LVL1::eFEXtauBDT::setPointerToETThresholdParam(unsigned int *etThreshold) {
65 m_etThreshold = etThreshold;
66}
67
69 unsigned int *maxEtThreshold) {
70 m_maxEtThreshold = maxEtThreshold;
71}
72
74 unsigned int *maxEtThreshold) {
75 m_maxEtThresholdFrac = maxEtThreshold;
76}
77
79 unsigned int *bdtMinEtThreshold) {
80 m_bdtMinEtThreshold = bdtMinEtThreshold;
81}
82
83unsigned int *LVL1::eFEXtauBDT::superCellToPtr(int eta, int phi, int layer) {
84 unsigned int *ptr = 0;
85 switch (layer) {
86 case 0:
87 ptr = m_em0cells[eta][phi];
88 break;
89 case 1:
90 ptr = m_em1cells[eta][phi];
91 break;
92 case 2:
93 ptr = m_em2cells[eta][phi];
94 break;
95 case 3:
96 ptr = m_em3cells[eta][phi];
97 break;
98 case 4:
99 ptr = m_hadcells[eta][phi];
100 break;
101 }
102 return ptr;
103}
104
108
112
116
117void LVL1::eFEXtauBDT::initPointers(const std::vector<std::vector<int>> &scells,
118 std::vector<unsigned int *> &ptr_list) {
119 m_log->msg(MSG::DEBUG) << "Will use sum of supercells: " << endmsg;
120 for (const auto& scell : scells) {
121 int eta = scell[0];
122 int phi = scell[1];
123 int layer = scell[2];
124 m_log->msg(MSG::DEBUG) << "\teta=" << eta << "\tphi=" << phi
125 << "\tlayer=" << layer << endmsg;
126 unsigned int *ptr = superCellToPtr(eta, phi, layer);
127 if (ptr == 0) {
128 m_log->msg(MSG::DEBUG)
129 << "Could not convert eta=" << eta << " phi=" << phi
130 << " layer=" << layer
131 << " to a pointer to supercell. Are they within range?" << endmsg;
132 throw std::domain_error(
133 std::string("Could not convert eta=") + std::to_string(eta) +
134 " phi=" + std::to_string(phi) + " layer=" + std::to_string(layer) +
135 " to a pointer to supercell. Are they within range?");
136 }
137 ptr_list.push_back(ptr);
138 }
139}
140
142 m_towersComputeSCellPointers.resize(m_bdt.getNTowers());
143 for (size_t i = 0; i < m_towers.size(); i++) {
144 m_log->msg(MSG::DEBUG) << "Tower " << i << endmsg;
145 initPointers(m_bdt.getTowerSCells(i), m_towersComputeSCellPointers[i]);
146 }
147}
148
150 for (size_t i = 0; i < m_bdt.getVariables().size(); i++) {
151 BDTVariable var = m_bdt.getVariables()[i];
152
153 m_log->msg(MSG::DEBUG) << i << " is " << var.m_name << ", sum of supercells"
154 << endmsg;
155 std::vector<unsigned int *> pointersToSCells;
156 for (size_t j = 0; j < var.m_scells.size(); j++) {
157 int eta = var.m_scells[j][0];
158 int phi = var.m_scells[j][1];
159 int layer = var.m_scells[j][2];
160 m_log->msg(MSG::DEBUG) << "\teta=" << eta << "\tphi=" << phi
161 << "\tlayer=" << layer << endmsg;
162 unsigned int *ptr = superCellToPtr(eta, phi, layer);
163 if (ptr == 0) {
164 m_log->msg(MSG::DEBUG)
165 << "Could not convert eta=" << eta << " phi=" << phi
166 << " layer=" << layer
167 << " to a pointer to supercell. Are they within range?" << endmsg;
168 throw std::domain_error(
169 std::string("Could not convert eta=") + std::to_string(eta) +
170 " phi=" + std::to_string(phi) + " layer=" + std::to_string(layer) +
171 " to a pointer to supercell. Are they within range?");
172 }
173 pointersToSCells.push_back(ptr);
174 }
175
176 m_bdtVarComputeSCellPointers.push_back(std::move(pointersToSCells));
177 }
178}
179
190
192 std::string bdtVariables = "";
193 for (size_t i = 0; i < m_bdtVars.size(); i++) {
194 bdtVariables += std::to_string(m_bdtVars[i]) + " ";
195 }
196
197 m_log->msg(MSG::DEBUG) << "BDT Variables: " << bdtVariables << endmsg;
198}
199
200// Build BDT Variables
202 for (size_t i = 0; i < m_bdtVarComputeSCellPointers.size(); i++) {
203 bool overflow;
206 if (overflow) {
207 m_bdtVars[i] = (1 << ENERGY_WIDTH) - 1;
208 }
209 }
211 m_bdtVarsComputed = true;
212}
213
215 // Need BDT variables to be computed
216 if (m_bdtVarsComputed == false) {
217 m_log->msg(MSG::DEBUG)
218 << "BDT Variables not computed. BDT score will be garbage." << endmsg;
219 }
220
221 m_bdtScore = m_bdt.getBDT().decision_function(m_bdtVars)[0];
222 m_log->msg(MSG::DEBUG) << "BDT Score: " << m_bdtScore << endmsg;
223}
224
230
236
242
244 m_log->msg(MSG::DEBUG) << "Towers Estimate: " << endmsg;
245 for (int eta = 0; eta < 3; eta++) {
246 for (int phi = 0; phi < 3; phi++) {
247 int flatIndex = flatTowerIndex(eta, phi);
248 m_log->msg(MSG::DEBUG)
249 << "Tower " << flatIndex << " ET (eta=" << eta << ", phi=" << phi
250 << "): " << m_towers[flatIndex] << endmsg;
251 }
252 }
253}
254
255int LVL1::eFEXtauBDT::flatTowerIndex(int eta, int phi) { return 3 * phi + eta; }
256
258 for (size_t i = 0; i < m_towers.size(); i++) {
259 bool overflow;
262 if (overflow) {
263 m_towers[i] = (1 << ENERGY_WIDTH) - 1;
264 }
265 }
266
268}
269
270bool LVL1::eFEXtauBDT::isOverflow(unsigned int number, int nBits) {
271 if ((number >> nBits) != 0) {
272 return true;
273 }
274 return false;
275}
276
277unsigned int
278LVL1::eFEXtauBDT::computeEstimate(std::vector<unsigned int *> &ptr_list,
279 bool &overflow, int resultNBits) {
280 unsigned int estimate = 0;
281 overflow = false;
282 for (unsigned int *it : ptr_list) {
283 estimate += *it;
284 if (isOverflow(estimate, resultNBits)) {
285 overflow = true;
286 }
287 }
288 return estimate;
289}
290
291unsigned int LVL1::eFEXtauBDT::multWithOverflow(unsigned int a, unsigned int b,
292 bool &overflow,
293 int resultNBits) {
294 overflow = false;
295 if (b == 0) {
296 return 0;
297 }
298 unsigned int result = a * b;
299 if (a != result / b) {
300 // This shouldn't happen (a and b are in reality 16 bit numbers), but just
301 // in case.
302 overflow = true;
303 }
304
305 overflow = isOverflow(result, resultNBits);
306
307 return result;
308}
309
310unsigned int LVL1::eFEXtauBDT::BitLeftShift(unsigned int number, int by,
311 int totalNBits) {
312 if ((number >> (totalNBits - by)) != 0) {
313 return (1 << totalNBits) - 1;
314 }
315 return number << by;
316}
317
319 int n_multipliers = sizeof(m_fracMultipliers) / sizeof(m_fracMultipliers[0]);
320
323
324 m_fracCondition = (1 << (n_multipliers - 1)) - 1;
325 return;
326 }
327
329 m_fracCondition = 0;
330 return;
331 }
332
334 int i = 0;
335 for (; i < n_multipliers; i++) {
336
337 bool overflow;
340 m_emEtXMultiplierOverflow[i] = (int)overflow;
341
344 break;
345 }
346 }
347 m_fracCondition = i;
348}
349
352 int n_thresholds = sizeof(m_bdtThresholds) / sizeof(m_bdtThresholds[0]);
353
354 int toShiftRight = m_bdt.getScorePrecision() - PARAM_WIDTH;
355 // Only compare the MSB bits of the BDT score to the thresholds provided in
356 // the parameters
357 m_bdtScoreShifted = (m_bdtScore >> toShiftRight);
358
361
362 m_bdtCondition = (1 << (n_thresholds - 1)) - 1;
363 return;
364 }
365
366 int i = 0;
367 for (; i < n_thresholds; i++) {
369 break;
370 }
371 }
372 m_bdtCondition = i;
373}
374
375// Check if central tower qualifies as a seed tower for the tau algorithm
376// Bitwise computation (as opposed to implementation in eFEXtauBDTAlgo.cxx)
378 m_isSeeded = true;
379
380 // Get central tower ET
381 unsigned int centralET = m_towers[4];
382
383 // Loop over all cells and check that the central tower is a local maximum
384 for (unsigned int beta = 0; beta < 3; beta++) {
385 for (unsigned int bphi = 0; bphi < 3; bphi++) {
386 int flatIndex = flatTowerIndex(beta, bphi);
387 // Don't need to compare central cell with itself
388 if ((beta == 1) && (bphi == 1)) {
389 continue;
390 }
391
392 // Cells to the up and right must have strictly lesser ET
393 if (beta == 2 || (beta == 1 && bphi == 2)) {
394 if (centralET <= m_towers[flatIndex]) {
395 m_isSeeded = false;
396 }
397 }
398 // Cells down and to the left must have lesser or equal ET. If strictly
399 // lesser would create zero TOB if two adjacent cells had equal energy
400 else if (beta == 0 || (beta == 1 && bphi == 0)) {
401 if (centralET < m_towers[flatIndex]) {
402 m_isSeeded = false;
403 }
404 }
405 }
406 }
407
409 m_isSeeded = false;
410 }
411
412 m_log->msg(MSG::DEBUG) << "Seeded: " << (int)m_isSeeded << endmsg;
413}
414
415unsigned int LVL1::eFEXtauBDT::getET() const {
417 return (1 << ENERGY_WIDTH) - 1;
418 }
420 return 0;
421 }
422 return m_eTEstimate;
423}
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define endmsg
static Double_t a
unsigned int * m_em3cells[3][3]
Definition eFEXtauBDT.h:109
int flatTowerIndex(int eta, int phi)
unsigned int * m_maxEtThreshold
Definition eFEXtauBDT.h:114
std::vector< unsigned int * > m_HAD_eTComputeSCellPointers
Definition eFEXtauBDT.h:145
void initPointers(const std::vector< std::vector< int > > &scells, std::vector< unsigned int * > &ptr_list)
unsigned int BitLeftShift(unsigned int number, int by, int totalNBits)
void setPointerToSCell(int eta, int phi, int layer, unsigned int *sCellPtr)
void computeHADETEstimate()
unsigned int m_hadEstimateShifted
Definition eFEXtauBDT.h:128
unsigned int m_eTEstimate
Definition eFEXtauBDT.h:119
void setPointerToBDTMinETParam(unsigned int *bdtMinEtThreshold)
unsigned int m_bdtCondition
Definition eFEXtauBDT.h:126
unsigned int m_bdtScore
Definition eFEXtauBDT.h:117
std::vector< unsigned int > m_emEtXMultiplierOverflow
Definition eFEXtauBDT.h:130
std::vector< unsigned int * > m_eTComputeSCellPointers
Definition eFEXtauBDT.h:143
bool m_eTEstimateOverflow
Definition eFEXtauBDT.h:120
unsigned int * m_etThreshold
Definition eFEXtauBDT.h:113
unsigned int computeEstimate(std::vector< unsigned int * > &ptr_list, bool &overflow, int resultNBits)
unsigned int * m_em1cells[12][3]
Definition eFEXtauBDT.h:107
unsigned int multWithOverflow(unsigned int a, unsigned int b, bool &overflow, int resultNBits)
unsigned int m_fracCondition
Definition eFEXtauBDT.h:125
unsigned int * m_fracMultipliers[3]
Definition eFEXtauBDT.h:111
unsigned int * superCellToPtr(int eta, int phi, int layer)
unsigned int * m_maxEtThresholdFrac
Definition eFEXtauBDT.h:115
void setPointerToBDTThresholdsParam(int index, unsigned int *bdtThresholds)
void setPointerToMaxETParam(unsigned int *maxEtThreshold)
std::vector< unsigned int > m_towers
Definition eFEXtauBDT.h:131
void computeBDTCondition()
std::vector< std::vector< unsigned int * > > m_towersComputeSCellPointers
Definition eFEXtauBDT.h:146
virtual ~eFEXtauBDT()
Destructor.
unsigned int getET() const
std::vector< unsigned int > m_emEtXMultiplier
Definition eFEXtauBDT.h:129
void computeIsCentralTowerSeed()
void initTowersPointers()
unsigned int * m_hadcells[3][3]
Definition eFEXtauBDT.h:110
unsigned int * m_em2cells[12][3]
Definition eFEXtauBDT.h:108
AthAlgTool * m_log
Definition eFEXtauBDT.h:150
bool m_EM_eTEstimateOverflow
Definition eFEXtauBDT.h:122
unsigned int * m_bdtMinEtThreshold
Definition eFEXtauBDT.h:116
unsigned int * m_em0cells[3][3]
Definition eFEXtauBDT.h:106
void setPointerToETThresholdParam(unsigned int *etThreshold)
void computeFracCondition()
std::vector< unsigned int * > m_EM_eTComputeSCellPointers
Definition eFEXtauBDT.h:144
bool isOverflow(unsigned int number, int nBits)
void setPointerToFracMultipliersParam(int index, unsigned int *fracMultipliers)
void debugPrintBDTVariables()
eFEXtauBDT(AthAlgTool *log, std::string config_path)
Constructors.
void setPointerToMaxETParamFrac(unsigned int *maxEtThreshold)
unsigned int m_HAD_eTEstimate
Definition eFEXtauBDT.h:123
std::vector< unsigned int > m_bdtVars
Definition eFEXtauBDT.h:134
void computeEMETEstimate()
unsigned int * m_bdtThresholds[3]
Definition eFEXtauBDT.h:112
unsigned int m_EM_eTEstimate
Definition eFEXtauBDT.h:121
std::vector< std::vector< unsigned int * > > m_bdtVarComputeSCellPointers
Definition eFEXtauBDT.h:139
bool m_HAD_eTEstimateOverflow
Definition eFEXtauBDT.h:124
unsigned int m_bdtScoreShifted
Definition eFEXtauBDT.h:118
#define ENERGY_WIDTH
#define PARAM_WIDTH
Definition index.py:1
std::string number(const double &d, const std::string &s)
Definition utils.cxx:186