ATLAS Offline Software
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 
18 #include "L1TopoCommon/Exception.h"
22 // Bitwise implementation utils
26 //
27 
28 REGISTER_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);
44  setNumberOutputBits(6);
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)
92  p_correctionLut[jEta][iET] = TSU::toSigned(rawValue, TCS::KFMET::correctionBitWidth );
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 
110 TCS::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 
169 TCS::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 }
Kinematics.h
TCS::StatusCode::SUCCESS
@ SUCCESS
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/L1TopoCommon/StatusCode.h:17
TCS::DataArrayImpl< GenericTOB >::const_iterator
data_t::const_iterator const_iterator
Definition: DataArrayImpl.h:18
TCS::KFMET::nEtaBins
constexpr unsigned nEtaBins
Definition: KalmanMETCorrectionConstants.h:18
TSU::toSigned
int toSigned(unsigned bits, unsigned length)
Definition: Conversions.cxx:32
TCS::KalmanMETCorrection::processBitCorrect
virtual StatusCode processBitCorrect(const std::vector< TCS::TOBArray const * > &input, const std::vector< TCS::TOBArray * > &output, Decision &decison)
Definition: KalmanMETCorrection.cxx:110
KFLUT.h
TCS::KFMET::correctionBitWidth
constexpr unsigned correctionBitWidth
Definition: KalmanMETCorrectionConstants.h:23
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
Decision.h
const
bool const RAWDATA *ch2 const
Definition: LArRodBlockPhysicsV0.cxx:562
TSU::Trigo::CosInt
static const std::vector< int > CosInt
Definition: Trigo.h:42
Conversions.h
TCS::KFMET::nLogEtBins
constexpr unsigned nLogEtBins
Definition: KalmanMETCorrectionConstants.h:20
TCS::KFMET::correctionDecimalBitWidth
constexpr unsigned correctionDecimalBitWidth
Definition: KalmanMETCorrectionConstants.h:22
TCS::DecisionAlg
Definition: Trigger/TrigT1/L1Topo/L1TopoInterfaces/L1TopoInterfaces/DecisionAlg.h:22
TCS::KFMET::lookupEtaBinFallback
constexpr size_t lookupEtaBinFallback
Definition: KalmanMETCorrectionConstants.h:28
TCS::Decision::setBit
void setBit(unsigned int index, bool value)
Definition: L1Topo/L1TopoInterfaces/Root/Decision.cxx:12
xAOD::etaBin
setSAddress setEtaMS setDirPhiMS setDirZMS setBarrelRadius setEndcapAlpha setEndcapRadius setInterceptInner setEtaMap etaBin
Definition: L2StandAloneMuon_v1.cxx:148
met
Definition: IMETSignificance.h:24
lumiFormat.i
int i
Definition: lumiFormat.py:92
TCS::KalmanMETCorrection
Definition: KalmanMETCorrection.h:18
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
TCS::KFMET::lookupEtaBin
const std::map< unsigned, size_t > lookupEtaBin
Definition: KalmanMETCorrectionConstants.h:29
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
TCS_EXCEPTION
#define TCS_EXCEPTION(MSG)
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/L1TopoCommon/Exception.h:14
TCS::Decision
Definition: L1Topo/L1TopoInterfaces/L1TopoInterfaces/Decision.h:19
KalmanMETCorrection.h
TRG_MSG_INFO
#define TRG_MSG_INFO(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:27
Trigo.h
merge.output
output
Definition: merge.py:17
TCS::GenericTOB
Definition: GenericTOB.h:35
REGISTER_ALG_TCS
#define REGISTER_ALG_TCS(CLASS)
Definition: AlgFactory.h:62
TCS::KFMET::nWeightWords
constexpr size_t nWeightWords
Definition: KalmanMETCorrectionConstants.h:16
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
TSU::Trigo::SinInt
static const std::vector< int > SinInt
Definition: Trigo.h:43
TCS::KFMET::jetEtBinOffset
constexpr int jetEtBinOffset
Definition: KalmanMETCorrectionConstants.h:25
L1TopoDataTypes.h
TCS
Definition: Global/GlobalSimulation/src/IO/Decision.h:18
TCS::KalmanMETCorrection::~KalmanMETCorrection
virtual ~KalmanMETCorrection()
Definition: KalmanMETCorrection.cxx:47
TCS::KalmanMETCorrection::process
virtual StatusCode process(const std::vector< TCS::TOBArray const * > &input, const std::vector< TCS::TOBArray * > &output, Decision &decison)
Definition: KalmanMETCorrection.cxx:169
TCS::KalmanMETCorrection::initialize
virtual StatusCode initialize()
Definition: KalmanMETCorrection.cxx:51
Exception.h
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
TCS::StatusCode
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/L1TopoCommon/StatusCode.h:15