CMS 3D CMS Logo

L1SeedConePFJetEmulator.h
Go to the documentation of this file.
1 #ifndef L1Trigger_Phase2L1ParticleFlow_L1SeedConePFJetEmulator_h
2 #define L1Trigger_Phase2L1ParticleFlow_L1SeedConePFJetEmulator_h
3 
6 
7 #include <iostream>
8 #include <vector>
9 #include <numeric>
10 #include <algorithm>
11 
13 
14 class L1SCJetEmu {
15 public:
16  // Data types and constants used in the FPGA and FPGA-optimized functions
17  // This header file is also for use in the standalone FPGA-tools simulation
18  // and thus contains no CMSSW/EDM specific content
19  typedef l1ct::pt_t pt_t;
20  typedef l1ct::glbeta_t etaphi_t; // Type for eta & phi
21  typedef ap_int<13> detaphi_t; // Type for deta & dphi
22  typedef ap_fixed<18, 23> detaphi2_t; // Type for deta^2 & dphi^2
23  typedef ap_fixed<22, 22> pt_etaphi_t; // Type for product of pt with deta & dphi
25 
26  class Jet : public l1ct::Jet {
27  public:
28  std::vector<l1ct::PuppiObjEmu> constituents;
29  };
30 
31  L1SCJetEmu(bool debug, float coneSize, unsigned nJets);
32 
33  std::vector<Jet> emulateEvent(std::vector<Particle>& parts) const;
34 
35 private:
36  // Configuration settings
37  bool debug_;
38  float coneSize_;
39  unsigned nJets_;
41 
42  // constants for the axis update
43  typedef ap_ufixed<18, -2> inv_pt_t;
44  static constexpr int N_table_inv_pt = 1024;
46 
47  static constexpr int ceillog2(int x) { return (x <= 2) ? 1 : 1 + ceillog2((x + 1) / 2); }
48 
49  static constexpr int floorlog2(int x) { return (x < 2) ? 0 : 1 + floorlog2(x / 2); }
50 
51  template <int B>
52  static constexpr int pow(int x) {
53  return x == 0 ? 1 : B * pow<B>(x - 1);
54  }
55 
56  static constexpr int pow2(int x) { return pow<2>(x); }
57 
58  /* ---
59  * Balanced tree reduce implementation.
60  * Reduces an array of inputs to a single value using the template binary operator 'Op',
61  * for example summing all elements with Op_add, or finding the maximum with Op_max
62  * Use only when the input array is fully unrolled. Or, slice out a fully unrolled section
63  * before applying and accumulate the result over the rolled dimension.
64  * Required for emulation to guarantee equality of ordering.
65  * --- */
66  template <class T, class Op>
67  static T reduce(std::vector<T> x, Op op) {
68  int N = x.size();
69  int leftN = pow2(floorlog2(N - 1)) > 0 ? pow2(floorlog2(N - 1)) : 0;
70  //static constexpr int rightN = N - leftN > 0 ? N - leftN : 0;
71  if (N == 1) {
72  return x.at(0);
73  } else if (N == 2) {
74  return op(x.at(0), x.at(1));
75  } else {
76  std::vector<T> left(x.begin(), x.begin() + leftN);
77  std::vector<T> right(x.begin() + leftN, x.end());
78  return op(reduce<T, Op>(left, op), reduce<T, Op>(right, op));
79  }
80  }
81 
82  class OpPuppiObjMax {
83  public:
84  Particle const& operator()(Particle const& a, Particle const& b) const { return a.hwPt >= b.hwPt ? a : b; }
85  };
86 
88 
89  template <class data_T, int N>
90  static inline float real_val_from_idx(unsigned i) {
91  // Treat the index as the top N bits
92  static constexpr int NB = ceillog2(N); // number of address bits for table
93  data_T x(0);
94  // The MSB of 1 is implicit in the table
95  x[x.width - 1] = 1;
96  // So we can use the next NB bits for real data
97  x(x.width - 2, x.width - NB - 1) = i;
98  return (float)x;
99  }
100 
101  template <class data_T, int N>
102  static inline unsigned idx_from_real_val(data_T x) {
103  // Slice the top N bits to get an index into the table
104  static constexpr int NB = ceillog2(N); // number of address bits for table
105  // Slice the top-1 NB bits of the value
106  // the MSB of '1' is implicit, so only slice below that
107  ap_uint<NB> y = x(x.width - 2, x.width - NB - 1);
108  return (unsigned)y(NB - 1, 0);
109  }
110 
111  template <class data_T, class table_T, int N>
112  static void init_invert_table(table_T table_out[N]) {
113  // The template data_T is the data type used to address the table
114  for (unsigned i = 0; i < N; i++) {
115  float x = real_val_from_idx<data_T, N>(i);
116  table_T inv_x = 1 / x;
117  table_out[i] = inv_x;
118  }
119  }
120 
121  template <class in_t, class table_t, int N>
122  static table_t invert_with_shift(const in_t in, const table_t inv_table[N], bool debug = false) {
123  // find the first '1' in the denominator
124  int msb = 0;
125  for (int b = 0; b < in.width; b++) {
126  if (in[b])
127  msb = b;
128  }
129  // shift up the denominator such that the left-most bit (msb) is '1'
130  in_t in_shifted = in << (in.width - msb - 1);
131  // lookup the inverse of the shifted input
132  int idx = idx_from_real_val<in_t, N>(in_shifted);
133  table_t inv_in = inv_table[idx];
134  // shift the output back
135  table_t out = inv_in << (in.width - msb - 1);
136  if (debug) {
137  dbgCout() << " x " << in << ", msb = " << msb << ", shift = " << (in.width - msb) << ", idx = " << idx
138  << std::endl;
139  dbgCout() << " pre 1 / " << in_shifted << " = " << inv_in << "(" << 1 / (float)in_shifted << ")" << std::endl;
140  dbgCout() << " post 1 / " << in << " = " << out << "(" << 1 / (float)in << ")" << std::endl;
141  }
142  return out;
143  }
144 
146  bool inCone(Particle seed, Particle part) const;
147  Jet makeJet_HW(const std::vector<Particle>& parts) const;
148 
149 }; // class L1SCJetEmu
150 
151 #endif
static T reduce(std::vector< T > x, Op op)
Definition: jets.h:12
Definition: APVGainStruct.h:7
static void init_invert_table(table_T table_out[N])
static constexpr int pow(int x)
static constexpr int pow2(int x)
static float real_val_from_idx(unsigned i)
static constexpr int nJets
Particle const & operator()(Particle const &a, Particle const &b) const
L1SCJetEmu(bool debug, float coneSize, unsigned nJets)
static unsigned idx_from_real_val(data_T x)
Definition: Jet.py:1
ap_fixed< 18, 23 > detaphi2_t
ap_ufixed< 18, -2 > inv_pt_t
l1ct::PuppiObjEmu Particle
std::ostream & dbgCout()
Definition: dbgPrintf.h:25
l1ct::glbeta_t etaphi_t
std::vector< Jet > emulateEvent(std::vector< Particle > &parts) const
static constexpr int floorlog2(int x)
ap_ufixed< 14, 12, AP_TRN, AP_SAT > pt_t
Definition: datatypes.h:10
#define debug
Definition: HDRShower.cc:19
#define N
Definition: blowfish.cc:9
static constexpr int ceillog2(int x)
static table_t invert_with_shift(const in_t in, const table_t inv_table[N], bool debug=false)
static detaphi_t deltaPhi(Particle a, Particle b)
ap_int< 13 > detaphi_t
part
Definition: HCALResponse.h:20
double b
Definition: hdecay.h:120
static constexpr OpPuppiObjMax op_max
Jet makeJet_HW(const std::vector< Particle > &parts) const
double a
Definition: hdecay.h:121
inv_pt_t inv_pt_table_[N_table_inv_pt]
bool inCone(Particle seed, Particle part) const
static constexpr int N_table_inv_pt
ap_fixed< 22, 22 > pt_etaphi_t
long double T
std::vector< l1ct::PuppiObjEmu > constituents
ap_int< 12 > glbeta_t
Definition: datatypes.h:18