17 #include "hls4ml/emulator.h" 34 typedef ap_ufixed<14, 12, AP_RND, AP_WRAP>
pt_t;
40 typedef ap_fixed<pt_t::width + 1, pt_t::iwidth + 1, AP_RND, AP_SAT>
pxy_t;
41 typedef ap_fixed<2 * pt_t::width, 2 * pt_t::iwidth, AP_RND, AP_SAT>
pt2_t;
45 const phi_t hwPiOverTwo_ = round(
M_PI / (2 * phiLSB_));
47 typedef ap_ufixed<pt_t::width, 0>
inv_t;
51 static constexpr int dropFactor_ = (1 << dropBits_);
53 static constexpr int invTableSize_ = (1 << invTableBits_);
57 std::shared_ptr<hls4mlEmulator::Model>
model;
64 static constexpr int numInputs_ = numContInputs_ + numPxPyInputs_ + numCatInputs_;
69 int EncodePdgId(
int pdgId)
const;
71 void CalcMetHLS(
const std::vector<float>&
pt,
72 const std::vector<float>& phi,
74 void CalcMlMet(
const std::vector<float>&
pt,
75 const std::vector<float>&
eta,
76 const std::vector<float>& phi,
77 const std::vector<float>& puppiWeight,
78 const std::vector<int>&
pdgId,
79 const std::vector<int>&
charge,
85 maxCands_(
cfg.getParameter<
int>(
"maxCands")),
86 modelVersion_(
cfg.getParameter<
std::
string>(
"modelVersion")) {
87 produces<std::vector<l1t::EtSum>>();
98 desc.add<
int>(
"maxCands", 128);
100 descriptions.
add(
"L1MetPfProducer",
desc);
107 std::vector<float>
pt;
108 std::vector<float>
eta;
109 std::vector<float>
phi;
110 std::vector<float> puppiWeight;
111 std::vector<int>
pdgId;
115 const auto& l1PFCand = l1PFCandidates->at(
i);
116 pt.push_back(l1PFCand.pt());
117 eta.push_back(l1PFCand.eta());
118 phi.push_back(l1PFCand.phi());
119 puppiWeight.push_back(l1PFCand.puppiWeight());
120 pdgId.push_back(l1PFCand.pdgId());
121 charge.push_back(l1PFCand.charge());
134 auto metCollection = std::make_unique<std::vector<l1t::EtSum>>(0);
157 const std::vector<float>&
eta,
158 const std::vector<float>&
phi,
159 const std::vector<float>& puppiWeight,
160 const std::vector<int>&
pdgId,
161 const std::vector<int>&
charge,
169 for (
int i = 0;
i < inputSize;
i++) {
192 double met_px = -
result[0].to_double();
193 double met_py = -
result[1].to_double();
194 metVector.SetPt(
hypot(met_px, met_py));
195 metVector.SetPhi(
atan2(met_py, met_px));
200 const std::vector<float>&
phi,
211 Project(hw_pt, hw_phi, hw_px,
true);
212 Project(hw_pt, hw_phi, hw_py,
false);
214 hw_sumx = hw_sumx - hw_px;
215 hw_sumy = hw_sumy - hw_py;
219 hw_met =
sqrt(
int(hw_met));
221 phi_t hw_met_phi = 0;
224 metVector.SetPt(hw_met.to_double());
225 metVector.SetPhi(hw_met_phi.to_double() *
phiLSB_);
243 phiQ1 =
hwPi_ - phiQ1;
248 }
else if (phiQ1 < 0) {
253 typedef ap_ufixed<14, 12, AP_RND, AP_WRAP>
pt_t;
267 if (
px == 0 &&
py == 0) {
294 inv_t a_over_b =
a * inv_b;
297 printf(
" a, b = %f, %f; index, inv = %d, %f; ratio = %f \n",
302 a_over_b.to_double());
306 int atanTableBits_ = 7;
307 int atanTableSize_ = (1 << atanTableBits_);
308 index = round(a_over_b.to_double() * atanTableSize_);
312 printf(
" atan index, phi = %d, %f (%f rad) real atan(a/b)= %f \n",
316 atan(
a.to_double() /
b.to_double()));
323 if (px < 0 && py > 0)
325 if (
px > 0 &&
py < 0)
327 if (
px < 0 &&
py < 0)
331 printf(
" phi hw, float, real = %f, %f (%f rad from x,y = %f, %f) \n",
~L1MetPfProducer() override
std::shared_ptr< hls4mlEmulator::Model > model
MPlex< T, D1, D2, N > hypot(const MPlex< T, D1, D2, N > &a, const MPlex< T, D1, D2, N > &b)
static constexpr float phiLSB_
static constexpr int numPxPyInputs_
Sin< T >::type sin(const T &t)
static constexpr float maxPt_
void Project(pt_t pt, phi_t phi, pxy_t &pxy, bool isX, bool debug=false) const
ap_ufixed< 14, 12, AP_RND, AP_WRAP > pt_t
static constexpr int dropFactor_
ap_fixed< 2 *pt_t::width, 2 *pt_t::iwidth, AP_RND, AP_SAT > pt2_t
void CalcMetHLS(const std::vector< float > &pt, const std::vector< float > &phi, reco::Candidate::PolarLorentzVector &metVector) const
static std::string const input
ap_fixed< 32, 16 > input_t
void PhiFromXY(pxy_t px, pxy_t py, phi_t &phi, bool debug=false) const
Cos< T >::type cos(const T &t)
std::string modelVersion_
static constexpr int invTableSize_
Abs< T >::type abs(const T &t)
int EncodePdgId(int pdgId) const
#define DEFINE_FWK_MODULE(type)
void produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
static constexpr int numInputs_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ap_ufixed< pt_t::width, 0 > inv_t
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void CalcMlMet(const std::vector< float > &pt, const std::vector< float > &eta, const std::vector< float > &phi, const std::vector< float > &puppiWeight, const std::vector< int > &pdgId, const std::vector< int > &charge, reco::Candidate::PolarLorentzVector &metVector) const
void add(std::string const &label, ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< vector< l1t::PFCandidate > > _l1PFToken
Log< level::Warning, false > LogWarning
L1MetPfProducer(const edm::ParameterSet &)
ap_fixed< pt_t::width+1, pt_t::iwidth+1, AP_RND, AP_SAT > pxy_t
ap_fixed< 32, 16 > result_t
static constexpr int numContInputs_
MPlex< T, D1, D2, N > atan2(const MPlex< T, D1, D2, N > &y, const MPlex< T, D1, D2, N > &x)
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.