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