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 
30  const unsigned int kMHTSize{15}; // For output Magnitude default 15
31  const unsigned int kMHTPhiSize{14}; // For output Phi default 14
32  const float kMaxMHT{4096}; // 4 TeV
33  const float kMaxMHTPhi{2 * M_PI};
34 
35  typedef ap_uint<5> ntracks_t;
36  typedef ap_uint<kInternalPtWidth> pt_t;
37  typedef ap_int<kInternalEtaWidth> eta_t;
38  typedef ap_int<kInternalPhiWidth> phi_t;
39 
40  typedef ap_int<kInternalPtWidth + kEtExtra> Et_t;
41  typedef ap_uint<kMHTSize> MHT_t;
42  typedef ap_uint<kMHTPhiSize> MHTphi_t;
43 
44  const unsigned int kMHTBins = 1 << kMHTSize;
45  const unsigned int kMHTPhiBins = 1 << kMHTPhiSize;
46 
47  const double kStepPt{0.25};
48  const double kStepEta{M_PI / (720)};
49  const double kStepPhi{M_PI / (720)};
50 
53 
54  const unsigned int kPhiBins = 1 << kInternalPhiWidth;
55 
56  const float kMaxCosLUTPhi{M_PI};
57 
58  template <typename T>
59  T digitizeSignedValue(double value, unsigned int nBits, double lsb) {
60  T digitized_value = std::floor(std::abs(value) / lsb);
61  T digitized_maximum = (1 << (nBits - 1)) - 1; // The remove 1 bit from nBits to account for the sign
62  if (digitized_value > digitized_maximum)
63  digitized_value = digitized_maximum;
64  if (value < 0)
65  digitized_value = (1 << nBits) - digitized_value; // two's complement encoding
66  return digitized_value;
67  }
68 
69  inline std::vector<phi_t> generateCosLUT(unsigned int size) { // Fill cosine LUT with integer values
70  float phi = 0;
71  std::vector<phi_t> cosLUT;
72  for (unsigned int LUT_idx = 0; LUT_idx < size; LUT_idx++) {
73  cosLUT.push_back(digitizeSignedValue<phi_t>(cos(phi), l1tmhtemu::kInternalPhiWidth, l1tmhtemu::kStepPhi));
74  phi += l1tmhtemu::kStepPhi;
75  }
76  cosLUT.push_back((phi_t)(0)); //Prevent overflow in last bin
77  return cosLUT;
78  }
79 
80  inline std::vector<MHTphi_t> generateaTanLUT(int cordicSteps) { // Fill atan LUT with integer values
81  std::vector<MHTphi_t> atanLUT;
82  atanLUT.reserve(cordicSteps);
83  for (int cordicStep = 0; cordicStep < cordicSteps; cordicStep++) {
84  atanLUT.push_back(MHTphi_t(round((kMHTPhiBins * atan(pow(2, -1 * cordicStep))) / (2 * M_PI))));
85  }
86  return atanLUT;
87  }
88 
89  inline std::vector<Et_t> generatemagNormalisationLUT(int cordicSteps) {
90  float val = 1.0;
91  std::vector<Et_t> magNormalisationLUT;
92  for (int cordicStep = 0; cordicStep < cordicSteps; cordicStep++) {
93  val = val / (pow(1 + pow(4, -1 * cordicStep), 0.5));
94  magNormalisationLUT.push_back(Et_t(round(kMHTBins * val)));
95  }
96  return magNormalisationLUT;
97  }
98 
99  struct EtMiss {
102  };
103 
105  Et_t y,
106  int cordicSteps,
107  std::vector<l1tmhtemu::MHTphi_t> atanLUT,
108  std::vector<Et_t> magNormalisationLUT) {
109  Et_t new_x = 0;
110  Et_t new_y = 0;
111 
112  MHTphi_t phi = 0;
113  MHTphi_t new_phi = 0;
114  bool sign = false;
115 
116  EtMiss ret_etmiss;
117 
118  if (x >= 0 && y >= 0) {
119  phi = 0;
120  sign = true;
121  //x = x;
122  //y = y;
123  } else if (x < 0 && y >= 0) {
124  phi = kMHTPhiBins >> 1;
125  sign = false;
126  x = -x;
127  //y = y;
128  } else if (x < 0 && y < 0) {
129  phi = kMHTPhiBins >> 1;
130  sign = true;
131  x = -x;
132  y = -y;
133  } else {
134  phi = kMHTPhiBins;
135  sign = false;
136  //x = x;
137  y = -y;
138  }
139 
140  for (int step = 0; step < cordicSteps; step++) {
141  if (y < 0) {
142  new_x = x - (y >> step);
143  new_y = y + (x >> step);
144  } else {
145  new_x = x + (y >> step);
146  new_y = y - (x >> step);
147  }
148 
149  if ((y < 0) == sign) {
150  new_phi = phi - atanLUT[step];
151  } else {
152  new_phi = phi + atanLUT[step];
153  }
154 
155  x = new_x;
156  y = new_y;
157  phi = new_phi;
158  }
159 
160  float sqrtval = (float(x * magNormalisationLUT[cordicSteps - 1]) / float(kMHTBins)) * float(kStepPt * kStepPhi);
161 
162  ret_etmiss.Et = std::floor(sqrtval / l1tmhtemu::kStepMHT);
163  ret_etmiss.Phi = phi;
164 
165  return ret_etmiss;
166  }
167 
168 } // namespace l1tmhtemu
169 #endif
size
Write out results.
ap_uint< kMHTSize > MHT_t
const unsigned int kMHTBins
ap_uint< 5 > ntracks_t
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
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 kInternalEtaWidth
ap_int< kInternalPtWidth+kEtExtra > Et_t
const unsigned int kMHTPhiBins
std::vector< phi_t > generateCosLUT(unsigned int size)
float x
step
Definition: StallMonitor.cc:98
std::vector< Et_t > generatemagNormalisationLUT(int cordicSteps)
T digitizeSignedValue(double value, unsigned int nBits, double lsb)
const unsigned int kEtExtra
long double T
ap_int< kInternalPhiWidth > phi_t
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29