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