CMS 3D CMS Logo

TauNNIdHW.h
Go to the documentation of this file.
1 #ifndef L1Trigger_Phase2L1ParticleFlow_TAUNNIDHW_H_
2 #define L1Trigger_Phase2L1ParticleFlow_TAUNNIDHW_H_
3 
5 
6 #include <cstdio>
7 #include <complex>
8 #include "ap_int.h"
9 #include "ap_fixed.h"
11 
13 
16 
17 //hls-fpga-machine-learning insert weights
26 
27 typedef ap_ufixed<16, 14> pt_t;
28 typedef ap_fixed<10, 4> etaphi_t;
29 
30 namespace L1TauEmu {
31  // Data types and constants used in the FPGA and FPGA-optimized functions
32  //etaphi_base maps physical eta phi units onto bits
33  //This way, the least significant bit of etaphi_t is exactly 0.01
34  //Even though 0.01 is not a power of 2
35  static float etaphi_base = 100. / 64;
36  typedef ap_ufixed<16, 14> pt_t; // 1 unit = 0.25 GeV;
37  typedef ap_fixed<10, 4> etaphi_t; // 1 unit = 0.01;
38  typedef ap_fixed<12, 6> detaphi_t; // type for the difference between etas or phis
39  typedef ap_fixed<18, 9> detaphi2_t; // type for detaphi_t squared
40  typedef ap_fixed<22, 16> pt_etaphi_t; // type for product of pt with deta or phi
41  typedef ap_int<8> dxy_t;
42  typedef ap_int<10> z0_t;
43  typedef ap_uint<5> count_t; // type for multiplicity
44  typedef ap_uint<5> id_t; // type for multiplicity
45 
46  // constants for the axis update
47  typedef ap_ufixed<18, -2> inv_pt_t;
48  static constexpr int N_table_inv_pt = 1024;
49  static const detaphi_t TWOPI = 3.14159 * 2. * etaphi_base;
50  static const detaphi_t PI = 3.14159 * etaphi_base;
51  static const detaphi_t HALFPI = 3.14159 / 2 * etaphi_base;
52  static const detaphi_t RCONE = 0.4 * 100 / 128;
53  static const detaphi_t R2CONE = RCONE * RCONE;
54  //
55  static const etaphi_t FIDUCIAL_ETA_PHI = 5.11 * etaphi_base;
56 
57  constexpr int ceillog2(int x) { return (x <= 2) ? 1 : 1 + ceillog2((x + 1) / 2); }
58  constexpr int floorlog2(int x) { return (x < 2) ? 0 : 1 + floorlog2(x / 2); }
59  constexpr int pow2(int x) { return x == 0 ? 1 : 2 * pow2(x - 1); }
60 
61  template <class data_T, int N>
62  inline float real_val_from_idx(unsigned i) {
63  // Treat the index as the top N bits
64  static constexpr int NB = ceillog2(N); // number of address bits for table
65  data_T x(0);
66  // The MSB of 1 is implicit in the table
67  x[x.width - 1] = 1;
68  // So we can use the next NB bits for real data
69  x(x.width - 2, x.width - NB - 1) = i;
70  return (float)x;
71  }
72 
73  template <class data_T, int N>
74  inline unsigned idx_from_real_val(data_T x) {
75  // Slice the top N bits to get an index into the table
76  static constexpr int NB = ceillog2(N); // number of address bits for table
77  // Slice the top-1 NB bits of the value
78  // the MSB of '1' is implicit, so only slice below that
79  ap_uint<NB> y = x(x.width - 2, x.width - NB - 1);
80  return (unsigned)y(NB - 1, 0);
81  }
82 
83  template <class data_T, class table_T, int N>
84  void init_invert_table(table_T table_out[N]) {
85  // The template data_T is the data type used to address the table
86  for (unsigned i = 0; i < N; i++) {
87  float x = real_val_from_idx<data_T, N>(i);
88  table_T inv_x = 1 / x;
89  table_out[i] = inv_x;
90  }
91  }
92 
93  template <class in_t, class table_t, int N>
94  table_t invert_with_shift(in_t in, bool debug = false) {
95  table_t inv_table[N];
96  init_invert_table<in_t, table_t, N>(inv_table);
97 
98  // find the first '1' in the denominator
99  int msb = 0;
100  for (int b = 0; b < in.width; b++) {
101  if (in[b])
102  msb = b;
103  }
104  // shift up the denominator such that the left-most bit (msb) is '1'
105  in_t in_shifted = in << (in.width - msb - 1);
106  // lookup the inverse of the shifted input
107  int idx = idx_from_real_val<in_t, N>(in_shifted);
108  table_t inv_in = inv_table[idx];
109  // shift the output back
110  table_t out = inv_in << (in.width - msb - 1);
111 
112  return out;
113  }
114 
116  // scale the particle eta, phi to hardware units
117  etaphi_t aphi = etaphi_t(a.phi() * etaphi_base);
118  etaphi_t bphi = etaphi_t(b.phi() * etaphi_base);
119  detaphi_t dphi = detaphi_t(aphi) - detaphi_t(bphi);
120  // phi wrap
121  detaphi_t dphi0 =
123  detaphi_t dphi1 =
125  //dphi > PI ? detaphi_t(TWOPI - dphi) : detaphi_t(dphi);
126  //dphi < -PI ? detaphi_t(TWOPI + dphi) : detaphi_t(dphi);
127  detaphi_t dphiw = dphi > detaphi_t(0) ? dphi0 : dphi1;
128  return dphiw;
129  }
130 
132  // scale the particle eta, phi to hardware units
133  etaphi_t seta = etaphi_t(seed.eta() * etaphi_base);
134  etaphi_t peta = etaphi_t(part.eta() * etaphi_base);
135  detaphi_t deta = detaphi_t(seta) - detaphi_t(peta);
136  detaphi_t dphi = deltaPhi(seed, part);
137  bool ret = (deta * deta + dphi * dphi) < cone2;
138  return ret;
139  }
140 
141 }; // namespace L1TauEmu
142 
143 class TauNNIdHW {
144 public:
145  TauNNIdHW();
146  ~TauNNIdHW();
147 
148  void initialize(const std::string &iName, int iNParticles);
149  void SetNNVectorVar();
151  result_t compute(const l1t::PFCandidate &iSeed, std::vector<l1t::PFCandidate> &iParts);
152  //void print();
153 
155  unsigned fNParticles_;
156  unique_ptr<pt_t[]> fPt_;
157  unique_ptr<etaphi_t[]> fEta_;
158  unique_ptr<etaphi_t[]> fPhi_;
159  unique_ptr<id_t[]> fId_;
160  //FILE *file_;
161 
162 private:
163  std::vector<input_t> NNvectorVar_;
164 };
165 
166 #endif
detaphi_t deltaPhi(l1t::PFCandidate a, l1t::PFCandidate b)
Definition: TauNNIdHW.h:115
constexpr int INTPHI_PI
Definition: datatypes.h:145
result_t compute(const l1t::PFCandidate &iSeed, std::vector< l1t::PFCandidate > &iParts)
Definition: TauNNIdHW.cc:81
static constexpr int N_table_inv_pt
Definition: TauNNIdHW.h:48
static const etaphi_t FIDUCIAL_ETA_PHI
Definition: TauNNIdHW.h:55
ret
prodAgent to be discontinued
ap_ufixed< 18, -2 > inv_pt_t
Definition: TauNNIdHW.h:47
std::string fInput_
Definition: TauNNIdHW.h:154
ap_fixed< 12, 6 > detaphi_t
Definition: TauNNIdHW.h:38
static float etaphi_base
Definition: TauNNIdHW.h:35
static const detaphi_t HALFPI
Definition: TauNNIdHW.h:51
constexpr int pow2(int x)
Definition: TauNNIdHW.h:59
float real_val_from_idx(unsigned i)
Definition: TauNNIdHW.h:62
static const detaphi_t RCONE
Definition: TauNNIdHW.h:52
ap_fixed< 22, 16 > pt_etaphi_t
Definition: TauNNIdHW.h:40
ap_uint< 5 > count_t
Definition: TauNNIdHW.h:43
void SetNNVectorVar()
Definition: TauNNIdHW.cc:15
unique_ptr< id_t[]> fId_
Definition: TauNNIdHW.h:159
unique_ptr< etaphi_t[]> fPhi_
Definition: TauNNIdHW.h:158
const float bphi
unique_ptr< etaphi_t[]> fEta_
Definition: TauNNIdHW.h:157
constexpr int ceillog2(int x)
Definition: TauNNIdHW.h:57
unsigned idx_from_real_val(data_T x)
Definition: TauNNIdHW.h:74
TauNNIdHW()
Definition: TauNNIdHW.cc:4
~TauNNIdHW()
Definition: TauNNIdHW.cc:5
ap_fixed< 18, 9 > detaphi2_t
Definition: TauNNIdHW.h:39
unsigned fNParticles_
Definition: TauNNIdHW.h:155
std::vector< input_t > NNvectorVar_
Definition: TauNNIdHW.h:163
#define debug
Definition: HDRShower.cc:19
#define N
Definition: blowfish.cc:9
void init_invert_table(table_T table_out[N])
Definition: TauNNIdHW.h:84
table_t invert_with_shift(in_t in, bool debug=false)
Definition: TauNNIdHW.h:94
part
Definition: HCALResponse.h:20
result_t EvaluateNN()
Definition: TauNNIdHW.cc:38
double b
Definition: hdecay.h:118
static const detaphi_t TWOPI
Definition: TauNNIdHW.h:49
ap_fixed< 10, 4 > etaphi_t
Definition: TauNNIdHW.h:37
ap_ufixed< 16, 14 > pt_t
Definition: TauNNIdHW.h:36
double a
Definition: hdecay.h:119
ap_uint< 5 > id_t
Definition: TauNNIdHW.h:44
constexpr int INTPHI_TWOPI
Definition: datatypes.h:146
ap_fixed< 16, 8 > result_t
unique_ptr< pt_t[]> fPt_
Definition: TauNNIdHW.h:156
float x
ap_int< 10 > z0_t
Definition: TauNNIdHW.h:42
ap_int< 8 > dxy_t
Definition: TauNNIdHW.h:41
bool inCone(l1t::PFCandidate seed, l1t::PFCandidate part, detaphi_t cone2)
Definition: TauNNIdHW.h:131
void initialize(const std::string &iName, int iNParticles)
Definition: TauNNIdHW.cc:7
ap_fixed< 10, 4 > etaphi_t
Definition: TauNNIdHW.h:28
ap_ufixed< 16, 14 > pt_t
Definition: TauNNIdHW.h:27
static const detaphi_t R2CONE
Definition: TauNNIdHW.h:53
constexpr int floorlog2(int x)
Definition: TauNNIdHW.h:58