ATLAS Offline Software
Loading...
Searching...
No Matches
KalmanMETCorrection.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4/*********************************
5 * KalmanMETCorrection.cpp
6 * Created by Joerg Stelzer on 11/16/12.
7 * Re-written by Ralf Gugel on 04/19/24.
8 *
9 * @brief algorithm calculates the KF correction per jet , get new XE and apply cut
10 *
11 * @param NumberLeading
12**********************************/
13
14
15
21// Bitwise implementation utils
25//
26#include <cmath>
27#include <string>
28
29
30REGISTER_ALG_TCS(KalmanMETCorrection)
31
33{
34 defineParameter("InputWidth", 9);
35 defineParameter("NumResultBits", 6);
36 for (size_t weightIndex = 0; weightIndex < TCS::KFMET::nWeightWords; weightIndex++) {
37 defineParameter("weights"+std::to_string(weightIndex), 0);
38 }
39 defineParameter("MinET", 0);
40 defineParameter("KFXE",0,0);
41 defineParameter("KFXE",0,1);
42 defineParameter("KFXE",0,2);
43 defineParameter("KFXE",0,3);
44 defineParameter("KFXE",0,4);
45 defineParameter("KFXE",0,5);
47}
48
50
51
54
55 p_NumberLeading2 = parameter("InputWidth").value();
56
57 p_MinEt = parameter("MinET").value();
58
59 for(unsigned int i=0; i<numberOutputBits(); ++i) {
60 p_XE[i] = parameter("KFXE",i).value();
61
62 }
63 TRG_MSG_INFO("NumberLeading2 : " << p_NumberLeading2);
64 for(unsigned int i=0; i<numberOutputBits(); ++i) {
65 TRG_MSG_INFO("KFXE : " << p_XE[i]);
66 }
67 TRG_MSG_INFO("MinET : " << p_MinEt);
68
69 TRG_MSG_INFO("number output : " << numberOutputBits());
70
71 //retrieve all weight words (in compacted representation)
72 std::vector<unsigned> weightWords(TCS::KFMET::nWeightWords);
73 for (size_t weightIndex = 0; weightIndex < TCS::KFMET::nWeightWords; weightIndex++) {
74 weightWords[weightIndex] = parameter("weights"+std::to_string(weightIndex)).value();
75 }
76 //unpack and convert individual weights
77 for (size_t iET=0; iET < TCS::KFMET::nLogEtBins; ++iET) {
78 for (size_t jEta=0; jEta < TCS::KFMET::nEtaBins; ++jEta) {
79 if (jEta == TCS::KFMET::nEtaBins-1) {
80 p_correctionLut[jEta][iET] = 0; //eta fallback bin
81 continue;
82 }
83 //assuming here that weights are max. 32 bits each to simplify unpacking logic
84 size_t startBit = (iET + jEta * TCS::KFMET::nLogEtBins) * TCS::KFMET::correctionBitWidth;
85 constexpr unsigned weightMask = (1 << TCS::KFMET::correctionBitWidth)-1;
86 unsigned rawValue = ( weightWords[startBit/32] >> (startBit%32) ) & weightMask;
87 int nOverflowBits = (startBit%32) + TCS::KFMET::correctionBitWidth - 32;
88 if (nOverflowBits > 0) {
89 //overflow into next word
90 // ( next word & bit mask for parts of 2nd word ) << bits already taken from previus word
91 rawValue |= (weightWords[startBit/32 + 1] & ( (1 << nOverflowBits) - 1 ) ) << (TCS::KFMET::correctionBitWidth - nOverflowBits);
92 }
93 //convert raw value to suitable signed integer (assuming 2's complement)
95 /*
96 if (rawValue >> (TCS::KFMET::correctionBitWidth-1) == 0) { //sign bit is not set
97 p_correctionLut[jEta][iET] = rawValue;
98 } else {
99 constexpr int twosComplementOffset = 1 << TCS::KFMET::correctionBitWidth; // 2^bitwidth
100 p_correctionLut[jEta][iET] = rawValue - twosComplementOffset;
101 }
102 */
103 }
104 }
105
106 return StatusCode::SUCCESS;
107}
108
109
110
112TCS::KalmanMETCorrection::processBitCorrect( const std::vector<TCS::TOBArray const *> & input,
113 const std::vector<TCS::TOBArray *> & /*output*/,
114 Decision & decision )
115
116{
117
118 if (isLegacyTopo()){
119 //KFMET was never fully commissioned on legacy L1Topo, hence, simply ignore
121 }
122
123 if(input.size()!=2) {
124 TCS_EXCEPTION("KalmanMETCorrection alg must have exactly two input list (jets and MET list), but got " << input.size());
125 }
126
127 const TCS::GenericTOB & met = (*input[0])[0];
128 int64_t metXY[2] {met.Ex(), met.Ey()};
129 int64_t jetSumXY[2] {0, 0};
130 for( TOBArray::const_iterator tob = input[1]->begin();
131 tob != input[1]->end() && distance( input[1]->begin(), tob) < p_NumberLeading2;
132 ++tob) {
133 if( (*tob)->Et() <= p_MinEt ) continue; // E_T cut
134 unsigned tobEta = abs((*tob)->eta());
136
137 //ignore given number of least significant bits right away
138 unsigned tobET = (*tob)->Et() >> TCS::KFMET::jetEtBinOffset;
139 unsigned etBin = 0;
140 //KFMET LUT is binned in log2(ET) with ET in Topo's internal granularity
141 //-> determining the bin index reduces to determining the position of the higest non-zero bit
142 // highest log2(ET) bin also acts as overflow bin
143 while (tobET > 1 && etBin < TCS::KFMET::nLogEtBins-1) {
144 etBin++;
145 tobET >>= 1;
146 }
147 int scaledEt = (*tob)->Et() * p_correctionLut[etaBin][etBin];
148 unsigned tobPhi = (*tob)->phi();
149 jetSumXY[0] += static_cast<int64_t>(scaledEt) * TSU::Trigo::CosInt.at(tobPhi);
150 jetSumXY[1] += static_cast<int64_t>(scaledEt) * TSU::Trigo::SinInt.at(tobPhi);
151
152 }
153
154 //compute "corrected" MET values
155 int64_t kfmetXY[2] {
156 metXY[0] + ( jetSumXY[0] >> (TCS::KFMET::correctionDecimalBitWidth + 10 /*cos/sin decimal bits*/) ),
157 metXY[1] + ( jetSumXY[1] >> (TCS::KFMET::correctionDecimalBitWidth + 10 /*cos/sin decimal bits*/) )
158 };
159
160 uint64_t kfmetSq = kfmetXY[0] * kfmetXY[0] + kfmetXY[1] * kfmetXY[1];
161
162 for(unsigned int i=0; i<numberOutputBits(); ++i) {
163 const auto XE64 = static_cast<uint64_t >(p_XE[i]);
164 decision.setBit( i, kfmetSq > XE64 * XE64 );
165 }
166
168
169}
170
172TCS::KalmanMETCorrection::process( const std::vector<TCS::TOBArray const *> & input,
173 const std::vector<TCS::TOBArray *> & output,
174 Decision & decision )
175
176{
177 //we have a bitwise correct implementation, so use it
178 return this->processBitCorrect(input, output, decision);
179}
#define REGISTER_ALG_TCS(CLASS)
Definition AlgFactory.h:62
const Parameter & parameter(const std::string &parameterName) const
bool isLegacyTopo() const
const std::string & name() const
void defineParameter(const std::string &name, TCS::parType_t value)
data_t::const_iterator const_iterator
void setNumberOutputBits(unsigned int numberOutputBits)
Definition DecisionAlg.h:40
DecisionAlg(const std::string &name)
Definition DecisionAlg.h:25
unsigned int numberOutputBits() const
Definition DecisionAlg.h:39
void setBit(unsigned int index, bool value)
Definition Decision.cxx:12
KalmanMETCorrection(const std::string &name)
virtual StatusCode process(const std::vector< TCS::TOBArray const * > &input, const std::vector< TCS::TOBArray * > &output, Decision &decison)
int p_correctionLut[KFMET::nEtaBins][KFMET::nLogEtBins]
virtual StatusCode processBitCorrect(const std::vector< TCS::TOBArray const * > &input, const std::vector< TCS::TOBArray * > &output, Decision &decison)
virtual StatusCode initialize()
constexpr size_t lookupEtaBinFallback
const std::map< unsigned, size_t > lookupEtaBin
constexpr unsigned nEtaBins
constexpr unsigned correctionBitWidth
constexpr unsigned nLogEtBins
constexpr unsigned correctionDecimalBitWidth
constexpr size_t nWeightWords
int toSigned(unsigned bits, unsigned length)
STL namespace.
static const std::vector< int > CosInt
Definition Trigo.h:28
static const std::vector< int > SinInt
Definition Trigo.h:29