CMS 3D CMS Logo

List of all members | Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes
L1SCJetEmu Class Reference

#include <L1SeedConePFJetEmulator.h>

Classes

class  Jet
 
class  OpPuppiObjMax
 

Public Types

typedef ap_fixed< 18, 23 > detaphi2_t
 
typedef ap_int< 13 > detaphi_t
 
typedef l1ct::glbeta_t etaphi_t
 
typedef l1ct::PuppiObjEmu Particle
 
typedef ap_fixed< 22, 22 > pt_etaphi_t
 
typedef l1ct::pt_t pt_t
 

Public Member Functions

std::vector< JetemulateEvent (std::vector< Particle > &parts) const
 
 L1SCJetEmu (bool debug, float coneSize, unsigned nJets)
 

Private Types

typedef ap_ufixed< 18, -2 > inv_pt_t
 

Private Member Functions

bool inCone (Particle seed, Particle part) const
 
Jet makeJet_HW (const std::vector< Particle > &parts) const
 

Static Private Member Functions

static constexpr int ceillog2 (int x)
 
static detaphi_t deltaPhi (Particle a, Particle b)
 
static constexpr int floorlog2 (int x)
 
template<class data_T , int N>
static unsigned idx_from_real_val (data_T x)
 
template<class data_T , class table_T , int N>
static void init_invert_table (table_T table_out[N])
 
template<class in_t , class table_t , int N>
static table_t invert_with_shift (const in_t in, const table_t inv_table[N], bool debug=false)
 
template<int B>
static constexpr int pow (int x)
 
static constexpr int pow2 (int x)
 
template<class data_T , int N>
static float real_val_from_idx (unsigned i)
 
template<class T , class Op >
static T reduce (std::vector< T > x, Op op)
 

Private Attributes

float coneSize_
 
bool debug_
 
inv_pt_t inv_pt_table_ [N_table_inv_pt]
 
unsigned nJets_
 
detaphi2_t rCone2_
 

Static Private Attributes

static constexpr int N_table_inv_pt = 1024
 
static constexpr OpPuppiObjMax op_max {}
 

Detailed Description

Definition at line 14 of file L1SeedConePFJetEmulator.h.

Member Typedef Documentation

◆ detaphi2_t

typedef ap_fixed<18, 23> L1SCJetEmu::detaphi2_t

Definition at line 22 of file L1SeedConePFJetEmulator.h.

◆ detaphi_t

typedef ap_int<13> L1SCJetEmu::detaphi_t

Definition at line 21 of file L1SeedConePFJetEmulator.h.

◆ etaphi_t

Definition at line 20 of file L1SeedConePFJetEmulator.h.

◆ inv_pt_t

typedef ap_ufixed<18, -2> L1SCJetEmu::inv_pt_t
private

Definition at line 43 of file L1SeedConePFJetEmulator.h.

◆ Particle

Definition at line 24 of file L1SeedConePFJetEmulator.h.

◆ pt_etaphi_t

typedef ap_fixed<22, 22> L1SCJetEmu::pt_etaphi_t

Definition at line 23 of file L1SeedConePFJetEmulator.h.

◆ pt_t

Definition at line 19 of file L1SeedConePFJetEmulator.h.

Constructor & Destructor Documentation

◆ L1SCJetEmu()

L1SCJetEmu::L1SCJetEmu ( bool  debug,
float  coneSize,
unsigned  nJets 
)

Definition at line 3 of file L1SeedConePFJetEmulator.cc.

References inv_pt_table_.

4  : debug_(debug),
6  nJets_(nJets),
8  init_invert_table<pt_t, inv_pt_t, N_table_inv_pt>(inv_pt_table_);
9 }
static constexpr int nJets
constexpr float ETAPHI_LSB
Definition: datatypes.h:144
#define debug
Definition: HDRShower.cc:19
inv_pt_t inv_pt_table_[N_table_inv_pt]

Member Function Documentation

◆ ceillog2()

static constexpr int L1SCJetEmu::ceillog2 ( int  x)
inlinestaticprivate

Definition at line 47 of file L1SeedConePFJetEmulator.h.

References ceillog2(), and x.

Referenced by ceillog2(), idx_from_real_val(), and real_val_from_idx().

47 { return (x <= 2) ? 1 : 1 + ceillog2((x + 1) / 2); }
static constexpr int ceillog2(int x)

◆ deltaPhi()

L1SCJetEmu::detaphi_t L1SCJetEmu::deltaPhi ( L1SCJetEmu::Particle  a,
L1SCJetEmu::Particle  b 
)
staticprivate

Definition at line 11 of file L1SeedConePFJetEmulator.cc.

References a, b, l1ct::Scales::INTPHI_PI, and l1ct::Scales::INTPHI_TWOPI.

Referenced by inCone(), and makeJet_HW().

11  {
12  detaphi_t dphi = detaphi_t(a.hwPhi) - detaphi_t(b.hwPhi);
13  // phi wrap
14  detaphi_t dphi0 =
16  detaphi_t dphi1 =
18  detaphi_t dphiw = dphi > detaphi_t(0) ? dphi0 : dphi1;
19  return dphiw;
20 }
constexpr int INTPHI_PI
Definition: datatypes.h:141
ap_int< 13 > detaphi_t
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
constexpr int INTPHI_TWOPI
Definition: datatypes.h:142

◆ emulateEvent()

std::vector< L1SCJetEmu::Jet > L1SCJetEmu::emulateEvent ( std::vector< Particle > &  parts) const

Definition at line 92 of file L1SeedConePFJetEmulator.cc.

References dbgCout(), debug_, inCone(), PDWG_EXODelayedJetMET_cff::jets, makeJet_HW(), nJets_, op_max, reduce(), fileCollector::seed, HcalDetIdTransform::transform(), and edmIntegrityCheck::work.

Referenced by L1SeedConePFJetProducer::processEvent_HW().

92  {
93  // The fixed point algorithm emulation
94  std::vector<Particle> work;
95  work.resize(parts.size());
96  std::transform(parts.begin(), parts.end(), work.begin(), [](const Particle& part) { return part; });
97 
98  std::vector<Jet> jets;
99  jets.reserve(nJets_);
100  while (!work.empty() && jets.size() < nJets_) {
101  // Take the highest pt candidate as a seed
102  // Use the firmware reduce function to find the same seed as the firmware
103  // in case there are multiple seeds with the same pT
105 
106  // Get the particles within a coneSize_ of the seed
107  std::vector<Particle> particlesInCone;
108  std::copy_if(work.begin(), work.end(), std::back_inserter(particlesInCone), [&](const Particle& part) {
109  return inCone(seed, part);
110  });
111  if (debug_) {
112  dbgCout() << "Seed: " << seed.hwPt << ", " << seed.hwEta << ", " << seed.hwPhi << std::endl;
113  std::for_each(particlesInCone.begin(), particlesInCone.end(), [&](Particle& part) {
114  dbgCout() << " Part: " << part.hwPt << ", " << part.hwEta << ", " << part.hwPhi << std::endl;
115  inCone(seed, part);
116  });
117  }
118  jets.push_back(makeJet_HW(particlesInCone));
119  // remove the clustered particles
120  work.erase(std::remove_if(work.begin(), work.end(), [&](const Particle& part) { return inCone(seed, part); }),
121  work.end());
122  }
123  return jets;
124 }
static T reduce(std::vector< T > x, Op op)
std::ostream & dbgCout()
Definition: dbgPrintf.h:25
part
Definition: HCALResponse.h:20
static constexpr OpPuppiObjMax op_max
Jet makeJet_HW(const std::vector< Particle > &parts) const
bool inCone(Particle seed, Particle part) const
unsigned transform(const HcalDetId &id, unsigned transformCode)

◆ floorlog2()

static constexpr int L1SCJetEmu::floorlog2 ( int  x)
inlinestaticprivate

Definition at line 49 of file L1SeedConePFJetEmulator.h.

References floorlog2(), and x.

Referenced by floorlog2(), and reduce().

49 { return (x < 2) ? 0 : 1 + floorlog2(x / 2); }
static constexpr int floorlog2(int x)

◆ idx_from_real_val()

template<class data_T , int N>
static unsigned L1SCJetEmu::idx_from_real_val ( data_T  x)
inlinestaticprivate

Definition at line 102 of file L1SeedConePFJetEmulator.h.

References ceillog2(), ALPAKA_ACCELERATOR_NAMESPACE::brokenline::constexpr(), N, x, and y.

102  {
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  }
#define N
Definition: blowfish.cc:9
static constexpr int ceillog2(int x)

◆ inCone()

bool L1SCJetEmu::inCone ( L1SCJetEmu::Particle  seed,
L1SCJetEmu::Particle  part 
) const
private

Definition at line 22 of file L1SeedConePFJetEmulator.cc.

References dbgCout(), debug_, deltaPhi(), diffTwoXMLs::r2, rCone2_, runTheMatrix::ret, and fileCollector::seed.

Referenced by emulateEvent().

22  {
23  // scale the particle eta, phi to hardware units
24  detaphi_t deta = detaphi_t(seed.hwEta) - detaphi_t(part.hwEta);
25  detaphi_t dphi = deltaPhi(seed, part);
26  bool ret = deta * deta + dphi * dphi < rCone2_;
27  //bool ret = r2 < cone2;
28  if (debug_) {
29  detaphi2_t r2 = detaphi2_t(deta) * detaphi2_t(deta) + detaphi2_t(dphi) * detaphi2_t(dphi);
30  dbgCout() << " part eta, seed eta: " << part.hwEta << ", " << seed.hwEta << std::endl;
31  dbgCout() << " part phi, seed phi: " << part.hwPhi << ", " << seed.hwPhi << std::endl;
32  dbgCout() << " pt, deta, dphi, r2, cone2, lt: " << part.hwPt << ", " << deta << ", " << dphi << ", "
33  << deta * deta + dphi * dphi << ", " << rCone2_ << ", " << ret << std::endl;
34  }
35  return ret;
36 }
ret
prodAgent to be discontinued
ap_fixed< 18, 23 > detaphi2_t
std::ostream & dbgCout()
Definition: dbgPrintf.h:25
static detaphi_t deltaPhi(Particle a, Particle b)
ap_int< 13 > detaphi_t
part
Definition: HCALResponse.h:20

◆ init_invert_table()

template<class data_T , class table_T , int N>
static void L1SCJetEmu::init_invert_table ( table_T  table_out[N])
inlinestaticprivate

Definition at line 112 of file L1SeedConePFJetEmulator.h.

References mps_fire::i, N, and x.

112  {
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  }
#define N
Definition: blowfish.cc:9

◆ invert_with_shift()

template<class in_t , class table_t , int N>
static table_t L1SCJetEmu::invert_with_shift ( const in_t  in,
const table_t  inv_table[N],
bool  debug = false 
)
inlinestaticprivate

Definition at line 122 of file L1SeedConePFJetEmulator.h.

References b, dbgCout(), debug, nano_mu_digi_cff::float, heavyIonCSV_trainingSettings::idx, recoMuon::in, and MillePedeFileConverter_cfg::out.

122  {
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  }
std::ostream & dbgCout()
Definition: dbgPrintf.h:25
#define debug
Definition: HDRShower.cc:19
double b
Definition: hdecay.h:120

◆ makeJet_HW()

L1SCJetEmu::Jet L1SCJetEmu::makeJet_HW ( const std::vector< Particle > &  parts) const
private

Definition at line 38 of file L1SeedConePFJetEmulator.cc.

References a, b, dbgCout(), debug_, deltaPhi(), PVValHelper::eta, inv_pt_table_, metsig::jet, op_max, phi, DiDispStaMuonMonitor_cfi::pt, reduce(), fileCollector::seed, HcalDetIdTransform::transform(), and x.

Referenced by emulateEvent().

38  {
39  // Seed Cone Jet algorithm with ap_fixed types and hardware emulation
41 
42  // Event with saturation, order of terms doesn't matter since they're all positive
43  auto sumpt = [](pt_t(a), const Particle& b) { return a + b.hwPt; };
44 
45  // Sum the pt
46  pt_t pt = std::accumulate(parts.begin(), parts.end(), pt_t(0), sumpt);
47  inv_pt_t inv_pt = invert_with_shift<pt_t, inv_pt_t, N_table_inv_pt>(pt, inv_pt_table_, false);
48 
49  // pt weighted d eta
50  std::vector<pt_etaphi_t> pt_deta;
51  pt_deta.resize(parts.size());
52  std::transform(parts.begin(), parts.end(), pt_deta.begin(), [&seed](const Particle& part) {
53  // In the firmware we calculate the per-particle pt-weighted deta
54  return pt_etaphi_t(part.hwPt * detaphi_t(part.hwEta - seed.hwEta));
55  });
56  // Accumulate the pt-weighted etas. Init to 0, include seed in accumulation
57  pt_etaphi_t sum_pt_eta = std::accumulate(pt_deta.begin(), pt_deta.end(), pt_etaphi_t(0));
58  etaphi_t eta = seed.hwEta + etaphi_t(sum_pt_eta * inv_pt);
59 
60  // pt weighted d phi
61  std::vector<pt_etaphi_t> pt_dphi;
62  pt_dphi.resize(parts.size());
63  std::transform(parts.begin(), parts.end(), pt_dphi.begin(), [&seed](const Particle& part) {
64  // In the firmware we calculate the per-particle pt-weighted dphi
65  return pt_etaphi_t(part.hwPt * deltaPhi(part, seed));
66  });
67  // Accumulate the pt-weighted phis. Init to 0, include seed in accumulation
68  pt_etaphi_t sum_pt_phi = std::accumulate(pt_dphi.begin(), pt_dphi.end(), pt_etaphi_t(0));
69  etaphi_t phi = seed.hwPhi + etaphi_t(sum_pt_phi * inv_pt);
70 
71  Jet jet;
72  jet.hwPt = pt;
73  jet.hwEta = eta;
74  jet.hwPhi = phi;
75  jet.constituents = parts;
76 
77  if (debug_) {
78  std::for_each(pt_dphi.begin(), pt_dphi.end(), [](pt_etaphi_t& x) { dbgCout() << "pt_dphi: " << x << std::endl; });
79  std::for_each(pt_deta.begin(), pt_deta.end(), [](pt_etaphi_t& x) { dbgCout() << "pt_deta: " << x << std::endl; });
80  dbgCout() << " sum_pt_eta: " << sum_pt_eta << ", 1/pt: " << inv_pt
81  << ", sum_pt_eta * 1/pt: " << etaphi_t(sum_pt_eta * inv_pt) << std::endl;
82  dbgCout() << " sum_pt_phi: " << sum_pt_phi << ", 1/pt: " << inv_pt
83  << ", sum_pt_phi * 1/pt: " << etaphi_t(sum_pt_phi * inv_pt) << std::endl;
84  dbgCout() << " uncorr eta: " << seed.hwEta << ", phi: " << seed.hwPhi << std::endl;
85  dbgCout() << " corr eta: " << eta << ", phi: " << phi << std::endl;
86  dbgCout() << " pt: " << pt << std::endl;
87  }
88 
89  return jet;
90 }
static T reduce(std::vector< T > x, Op op)
Definition: Jet.py:1
ap_ufixed< 18, -2 > inv_pt_t
std::ostream & dbgCout()
Definition: dbgPrintf.h:25
l1ct::glbeta_t etaphi_t
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
double a
Definition: hdecay.h:121
inv_pt_t inv_pt_table_[N_table_inv_pt]
ap_fixed< 22, 22 > pt_etaphi_t
ap_fixed< 10, 4 > etaphi_t
Definition: TauNNIdHW.h:14
ap_ufixed< 16, 14 > pt_t
Definition: TauNNIdHW.h:13
unsigned transform(const HcalDetId &id, unsigned transformCode)

◆ pow()

template<int B>
static constexpr int L1SCJetEmu::pow ( int  x)
inlinestaticprivate

Definition at line 52 of file L1SeedConePFJetEmulator.h.

References B, and x.

52  {
53  return x == 0 ? 1 : B * pow<B>(x - 1);
54  }
Definition: APVGainStruct.h:7

◆ pow2()

static constexpr int L1SCJetEmu::pow2 ( int  x)
inlinestaticprivate

Definition at line 56 of file L1SeedConePFJetEmulator.h.

References x.

Referenced by reduce().

56 { return pow<2>(x); }

◆ real_val_from_idx()

template<class data_T , int N>
static float L1SCJetEmu::real_val_from_idx ( unsigned  i)
inlinestaticprivate

Definition at line 90 of file L1SeedConePFJetEmulator.h.

References ceillog2(), ALPAKA_ACCELERATOR_NAMESPACE::brokenline::constexpr(), mps_fire::i, N, and x.

90  {
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  }
#define N
Definition: blowfish.cc:9
static constexpr int ceillog2(int x)

◆ reduce()

template<class T , class Op >
static T L1SCJetEmu::reduce ( std::vector< T x,
Op  op 
)
inlinestaticprivate

Definition at line 67 of file L1SeedConePFJetEmulator.h.

References floorlog2(), N, findAndChange::op, pow2(), and x.

Referenced by emulateEvent(), and makeJet_HW().

67  {
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  }
static constexpr int pow2(int x)
static constexpr int floorlog2(int x)
#define N
Definition: blowfish.cc:9

Member Data Documentation

◆ coneSize_

float L1SCJetEmu::coneSize_
private

Definition at line 38 of file L1SeedConePFJetEmulator.h.

◆ debug_

bool L1SCJetEmu::debug_
private

Definition at line 37 of file L1SeedConePFJetEmulator.h.

Referenced by emulateEvent(), inCone(), and makeJet_HW().

◆ inv_pt_table_

inv_pt_t L1SCJetEmu::inv_pt_table_[N_table_inv_pt]
private

Definition at line 45 of file L1SeedConePFJetEmulator.h.

Referenced by L1SCJetEmu(), and makeJet_HW().

◆ N_table_inv_pt

constexpr int L1SCJetEmu::N_table_inv_pt = 1024
staticprivate

Definition at line 44 of file L1SeedConePFJetEmulator.h.

◆ nJets_

unsigned L1SCJetEmu::nJets_
private

Definition at line 39 of file L1SeedConePFJetEmulator.h.

Referenced by emulateEvent().

◆ op_max

constexpr OpPuppiObjMax L1SCJetEmu::op_max {}
staticprivate

Definition at line 87 of file L1SeedConePFJetEmulator.h.

Referenced by emulateEvent(), and makeJet_HW().

◆ rCone2_

detaphi2_t L1SCJetEmu::rCone2_
private

Definition at line 40 of file L1SeedConePFJetEmulator.h.

Referenced by inCone().