CMS 3D CMS Logo

L1SeedConePFJetEmulator.cc
Go to the documentation of this file.
2 
3 L1SCJetEmu::L1SCJetEmu(bool debug, float coneSize, unsigned nJets)
4  : debug_(debug),
5  coneSize_(coneSize),
6  nJets_(nJets),
7  rCone2_(coneSize * coneSize / l1ct::Scales::ETAPHI_LSB / l1ct::Scales::ETAPHI_LSB) {
8  init_invert_table<pt_t, inv_pt_t, N_table_inv_pt>(inv_pt_table_);
9 }
10 
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 }
21 
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 }
37 
38 L1SCJetEmu::Jet L1SCJetEmu::makeJet_HW(const std::vector<Particle>& parts) const {
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 }
91 
92 std::vector<L1SCJetEmu::Jet> L1SCJetEmu::emulateEvent(std::vector<Particle>& parts) const {
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)
constexpr int INTPHI_PI
Definition: datatypes.h:145
ret
prodAgent to be discontinued
constexpr float ETAPHI_LSB
Definition: datatypes.h:148
L1SCJetEmu(bool debug, float coneSize, unsigned nJets)
Definition: Jet.py:1
ap_fixed< 18, 23 > detaphi2_t
ap_ufixed< 18, -2 > inv_pt_t
std::ostream & dbgCout()
Definition: dbgPrintf.h:25
l1ct::glbeta_t etaphi_t
std::vector< Jet > emulateEvent(std::vector< Particle > &parts) const
#define debug
Definition: HDRShower.cc:19
static detaphi_t deltaPhi(Particle a, Particle b)
ap_int< 13 > detaphi_t
part
Definition: HCALResponse.h:20
double b
Definition: hdecay.h:118
static constexpr OpPuppiObjMax op_max
Jet makeJet_HW(const std::vector< Particle > &parts) const
double a
Definition: hdecay.h:119
inv_pt_t inv_pt_table_[N_table_inv_pt]
constexpr int INTPHI_TWOPI
Definition: datatypes.h:146
bool inCone(Particle seed, Particle part) const
ap_fixed< 22, 22 > pt_etaphi_t
Definition: datatypes.h:19
unsigned transform(const HcalDetId &id, unsigned transformCode)