CMS 3D CMS Logo

L1TkHTMissEmulatorProducer.h
Go to the documentation of this file.
1 #ifndef L1Trigger_L1TTrackMatch_L1TkHTMissEmulatorProducer_HH
2 #define L1Trigger_L1TTrackMatch_L1TkHTMissEmulatorProducer_HH
3 
4 // Original Author: Hardik Routray
5 // Created: Mon, 11 Oct 2021
6 
7 #include <ap_int.h>
8 
9 #include <cmath>
10 #include <cstdint>
11 #include <filesystem>
12 #include <fstream>
13 #include <iostream>
14 #include <numeric>
15 
18 
19 // Namespace that defines constants and types used by the HTMiss Emulation
20 
21 namespace l1tmhtemu {
22 
23  const unsigned int kInternalPtWidth{l1t::TkJetWord::TkJetBitWidths::kPtSize};
24  const unsigned int kInternalEtaWidth{l1t::TkJetWord::TkJetBitWidths::kGlbEtaSize};
25  const unsigned int kInternalPhiWidth{l1t::TkJetWord::TkJetBitWidths::kGlbPhiSize};
26 
27  // extra room for sumPx, sumPy
28  const unsigned int kEtExtra{10};
29  const unsigned int kValidSize{1};
30  const unsigned int kMHTSize{16}; // For output Magnitude default 16
31  const unsigned int kMHTIntSize{11};
32  const unsigned int kMHTPhiSize{13}; // For output Phi default 13
33  const unsigned int kHTSize{kInternalPtWidth + kEtExtra};
34  const unsigned int kUnassignedSize{64 - (kHTSize + kMHTSize + kMHTPhiSize + kValidSize)};
35 
36  enum BitLocations {
37  // The location of the least significant bit (LSB) and most significant bit (MSB) in the sum word for different fields
38  kValidLSB = 0,
48  };
49 
50  const float kMaxMHT{2048}; // 2 TeV
51  const float kMaxMHTPhi{2 * M_PI};
52 
53  typedef ap_uint<5> ntracks_t;
54  typedef ap_uint<kInternalPtWidth> pt_t;
55  typedef ap_int<kInternalEtaWidth> eta_t;
56  typedef ap_int<kInternalPhiWidth> phi_t;
57 
58  typedef ap_int<kHTSize> Et_t;
59  typedef ap_ufixed<kMHTSize, kMHTIntSize> MHT_t;
60  typedef ap_uint<kMHTPhiSize> MHTphi_t;
61 
62  const unsigned int kMHTBins = 1 << kMHTSize;
63  const unsigned int kMHTPhiBins = 1 << kMHTPhiSize;
64 
65  const double kStepPt{0.25};
66  const double kStepEta{M_PI / (720)};
67  const double kStepPhi{M_PI / (720)};
68 
71 
72  const unsigned int kPhiBins = 1 << kInternalPhiWidth;
73 
74  const float kMaxCosLUTPhi{M_PI};
75 
76  template <typename T>
77  T digitizeSignedValue(double value, unsigned int nBits, double lsb) {
78  T digitized_value = std::floor(std::abs(value) / lsb);
79  T digitized_maximum = (1 << (nBits - 1)) - 1; // The remove 1 bit from nBits to account for the sign
80  if (digitized_value > digitized_maximum)
81  digitized_value = digitized_maximum;
82  if (value < 0)
83  digitized_value = (1 << nBits) - digitized_value; // two's complement encoding
84  return digitized_value;
85  }
86 
87  inline std::vector<phi_t> generateCosLUT(unsigned int size) { // Fill cosine LUT with integer values
88  float phi = 0;
89  std::vector<phi_t> cosLUT;
90  for (unsigned int LUT_idx = 0; LUT_idx < size; LUT_idx++) {
91  cosLUT.push_back(digitizeSignedValue<phi_t>(cos(phi), l1tmhtemu::kInternalPhiWidth, l1tmhtemu::kStepPhi));
92  phi += l1tmhtemu::kStepPhi;
93  }
94  cosLUT.push_back((phi_t)(0)); //Prevent overflow in last bin
95  return cosLUT;
96  }
97 
98  inline std::vector<MHTphi_t> generateaTanLUT(int cordicSteps) { // Fill atan LUT with integer values
99  std::vector<MHTphi_t> atanLUT;
100  atanLUT.reserve(cordicSteps);
101  for (int cordicStep = 0; cordicStep < cordicSteps; cordicStep++) {
102  atanLUT.push_back(MHTphi_t(round((kMHTPhiBins * atan(pow(2, -1 * cordicStep))) / (2 * M_PI))));
103  }
104  return atanLUT;
105  }
106 
107  inline std::vector<Et_t> generatemagNormalisationLUT(int cordicSteps) {
108  float val = 1.0;
109  std::vector<Et_t> magNormalisationLUT;
110  for (int cordicStep = 0; cordicStep < cordicSteps; cordicStep++) {
111  val = val / (pow(1 + pow(4, -1 * cordicStep), 0.5));
112  magNormalisationLUT.push_back(Et_t(round(kMHTBins * val)));
113  }
114  return magNormalisationLUT;
115  }
116 
117  struct EtMiss {
120  };
121 
123  Et_t y,
124  int cordicSteps,
125  std::vector<l1tmhtemu::MHTphi_t> atanLUT,
126  std::vector<Et_t> magNormalisationLUT) {
127  Et_t new_x = 0;
128  Et_t new_y = 0;
129 
130  MHTphi_t phi = 0;
131  MHTphi_t new_phi = 0;
132  bool sign = false;
133 
134  EtMiss ret_etmiss;
135 
136  if (x >= 0 && y >= 0) {
137  phi = 0;
138  sign = true;
139  //x = x;
140  //y = y;
141  } else if (x < 0 && y >= 0) {
142  phi = kMHTPhiBins >> 1;
143  sign = false;
144  x = -x;
145  //y = y;
146  } else if (x < 0 && y < 0) {
147  phi = kMHTPhiBins >> 1;
148  sign = true;
149  x = -x;
150  y = -y;
151  } else {
152  phi = kMHTPhiBins;
153  sign = false;
154  //x = x;
155  y = -y;
156  }
157 
158  for (int step = 0; step < cordicSteps; step++) {
159  if (y < 0) {
160  new_x = x - (y >> step);
161  new_y = y + (x >> step);
162  } else {
163  new_x = x + (y >> step);
164  new_y = y - (x >> step);
165  }
166 
167  if ((y < 0) == sign) {
168  new_phi = phi - atanLUT[step];
169  } else {
170  new_phi = phi + atanLUT[step];
171  }
172 
173  x = new_x;
174  y = new_y;
175  phi = new_phi;
176  }
177 
178  float sqrtval = (float(x * magNormalisationLUT[cordicSteps - 1]) / (float)kMHTBins) * float(kStepPt * kStepPhi);
179  ret_etmiss.Et = sqrtval;
180  ret_etmiss.Phi = phi;
181 
182  return ret_etmiss;
183  }
184 
185 } // namespace l1tmhtemu
186 #endif
const unsigned int kMHTBins
ap_uint< 5 > ntracks_t
constexpr int pow(int x)
Definition: conifer.h:24
ap_uint< kMHTPhiSize > MHTphi_t
EtMiss cordicSqrt(Et_t x, Et_t y, int cordicSteps, std::vector< l1tmhtemu::MHTphi_t > atanLUT, std::vector< Et_t > magNormalisationLUT)
std::vector< MHTphi_t > generateaTanLUT(int cordicSteps)
const unsigned int kInternalPhiWidth
const unsigned int kMHTPhiSize
const float kMaxCosLUTPhi
ap_uint< kInternalPtWidth > pt_t
const double kStepMHTPhi
ap_ufixed< kMHTSize, kMHTIntSize > MHT_t
const unsigned int kValidSize
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Definition: value.py:1
const unsigned int kPhiBins
const unsigned int kMHTSize
#define M_PI
const unsigned int kInternalPtWidth
ap_int< kInternalEtaWidth > eta_t
const unsigned int kMHTIntSize
const unsigned int kInternalEtaWidth
const unsigned int kMHTPhiBins
std::vector< phi_t > generateCosLUT(unsigned int size)
const unsigned int kUnassignedSize
float x
step
Definition: StallMonitor.cc:83
std::vector< Et_t > generatemagNormalisationLUT(int cordicSteps)
T digitizeSignedValue(double value, unsigned int nBits, double lsb)
const unsigned int kEtExtra
long double T
const unsigned int kHTSize
ap_int< kInternalPhiWidth > phi_t
ap_int< kHTSize > Et_t