CMS 3D CMS Logo

L1PFHtEmulator.h
Go to the documentation of this file.
1 #ifndef L1Trigger_Phase2L1ParticleFlow_HTMHT_h
2 #define L1Trigger_Phase2L1ParticleFlow_HTMHT_h
3 
8 
9 #ifndef CMSSW_GIT_HASH
10 #include "hls_math.h"
11 #endif
12 
13 #include <vector>
14 #include <numeric>
15 #include <algorithm>
16 #include "ap_int.h"
17 #include "ap_fixed.h"
18 
19 namespace P2L1HTMHTEmu {
20  typedef l1ct::pt_t pt_t; // Type for pt/ht 1 unit = 0.25 GeV; max = 16 TeV
21  typedef l1ct::glbeta_t etaphi_t; // Type for eta & phi
22 
23  typedef ap_fixed<12, 3> radians_t;
24  typedef ap_fixed<9, 2> cossin_t;
25  typedef ap_fixed<16, 13> pxy_t;
26 
27  static constexpr int N_TABLE = 2048;
28 
29  // Class for intermediate variables
30  class PtPxPy {
31  public:
32  pt_t pt = 0.;
33  pxy_t px = 0.;
34  pxy_t py = 0.;
35 
36  PtPxPy operator+(const PtPxPy& b) const {
37  PtPxPy c;
38  c.pt = this->pt + b.pt;
39  c.px = this->px + b.px;
40  c.py = this->py + b.py;
41  return c;
42  }
43  };
44 
45  namespace Scales {
46  const ap_fixed<12, -4> scale_degToRad = M_PI / 180.;
47  }; // namespace Scales
48 
49  template <class data_T, class table_T, int N>
50  void init_sinphi_table(table_T table_out[N]) {
51  for (int i = 0; i < N; i++) {
52  float x = i * (M_PI / 180.) / 2.;
53  table_T sin_x = std::sin(x);
54  table_out[i] = sin_x;
55  }
56  }
57  template <class in_t, class table_t, int N>
59  table_t sin_table[N];
60  init_sinphi_table<in_t, table_t, N>(sin_table);
61  table_t out = sin_table[hwPhi];
62  return out;
63  }
64 
65  inline etaphi_t phi_cordic(pxy_t y, pxy_t x) {
66 #ifdef CMSSW_GIT_HASH
67  ap_fixed<12, 3> phi = atan2(y.to_double(), x.to_double()); // hls_math.h not available yet in CMSSW
68 #else
69  ap_fixed<12, 3> phi = hls::atan2(y, x);
70 #endif
71  ap_fixed<16, 9> etaphiscale = (float)l1ct::Scales::INTPHI_PI / M_PI; // radians to hwPhi
72  return phi * etaphiscale;
73  }
74 
76  // Add an extra bit to px/py for the sign, and one additional bit to improve precision (pt_t is ap_ufixed<14, 12>)
77  PtPxPy v_pxpy;
78 
79  //Initialize table once
80  cossin_t sin_table[N_TABLE];
81  init_sinphi_table<etaphi_t, cossin_t, N_TABLE>(sin_table);
82 
83  cossin_t sinphi;
84  cossin_t cosphi;
85  bool sign = jet.hwPhi.sign();
86 
87  etaphi_t hwphi = jet.hwPhi;
88 
89  // Reduce precision of hwPhi
90  ap_int<10> phi;
91  phi.V = hwphi(11, 1);
92  phi = (phi > 0) ? phi : (ap_int<10>)-phi; //Only store values for positive phi, pick up sign later
93 
94  sinphi = sin_table[phi];
95 
96  sinphi = (sign > 0) ? (cossin_t)(-sign * sinphi) : sinphi; // Change sign bit if hwPt is negative, sin(-x)=-sin(x)
97  cosphi = sin_table[phi + 90 * 2]; //cos(x)=sin(x+90). Do nothing with sign, cos(-θ) = cos θ,
98 
99  v_pxpy.pt = jet.hwPt;
100  v_pxpy.py = jet.hwPt * sinphi;
101  v_pxpy.px = jet.hwPt * cosphi;
102 
103  return v_pxpy;
104  }
105 } // namespace P2L1HTMHTEmu
106 
107 //TODO replace with l1ct::Jet
108 inline l1ct::Sum htmht(std::vector<l1ct::Jet> jets) {
109  // compute jet px, py
110  std::vector<P2L1HTMHTEmu::PtPxPy> ptpxpy;
111  ptpxpy.resize(jets.size());
113  jets.begin(), jets.end(), ptpxpy.begin(), [](const l1ct::Jet& jet) { return P2L1HTMHTEmu::mht_compute(jet); });
114 
115  // Sum pt, px, py over jets
116  P2L1HTMHTEmu::PtPxPy hthxhy = std::accumulate(ptpxpy.begin(), ptpxpy.end(), P2L1HTMHTEmu::PtPxPy());
117 
118  // Compute the MHT magnitude and direction
119  l1ct::Sum ht;
120  ht.hwSumPt = hthxhy.pt;
121 #ifdef CMSSW_GIT_HASH
122  ht.hwPt =
123  sqrt(((hthxhy.px * hthxhy.px) + (hthxhy.py * hthxhy.py)).to_double()); // hls_math.h not available yet in CMSSW
124 #else
125  ht.hwPt = hls::sqrt(((hthxhy.px * hthxhy.px) + (hthxhy.py * hthxhy.py)));
126 #endif
127  ht.hwPhi = P2L1HTMHTEmu::phi_cordic(hthxhy.py, hthxhy.px);
128  return ht;
129 }
130 
131 #endif
Definition: jets.h:12
PtPxPy operator+(const PtPxPy &b) const
constexpr int INTPHI_PI
Definition: datatypes.h:145
l1ct::Sum htmht(std::vector< l1ct::Jet > jets)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
etaphi_t phi_cordic(pxy_t y, pxy_t x)
const ap_fixed< 12, -4 > scale_degToRad
T sqrt(T t)
Definition: SSEVec.h:19
ap_fixed< 16, 13 > pxy_t
ap_fixed< 9, 2 > cossin_t
#define M_PI
ap_ufixed< 14, 12, AP_TRN, AP_SAT > pt_t
Definition: datatypes.h:21
#define N
Definition: blowfish.cc:9
double b
Definition: hdecay.h:118
void init_sinphi_table(table_T table_out[N])
static constexpr int N_TABLE
PtPxPy mht_compute(l1ct::Jet jet)
l1ct::glbeta_t etaphi_t
ap_fixed< 12, 3 > radians_t
Definition: sums.h:10
float x
table_t sine_with_conversion(etaphi_t hwPhi)
l1ct::pt_t pt_t
ap_int< 12 > glbeta_t
Definition: datatypes.h:29
unsigned transform(const HcalDetId &id, unsigned transformCode)