From 2431d5342a2ac3bdfa845282858dcc827fad789d Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Fri, 22 Dec 2023 20:22:21 +0100 Subject: [PATCH 01/13] add JetID --- .../Phase2L1ParticleFlow/interface/JetId.h | 51 ++++++ L1Trigger/Phase2L1ParticleFlow/src/JetId.cc | 157 ++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 L1Trigger/Phase2L1ParticleFlow/interface/JetId.h create mode 100644 L1Trigger/Phase2L1ParticleFlow/src/JetId.cc diff --git a/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h b/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h new file mode 100644 index 0000000000000..0261aaaba03a8 --- /dev/null +++ b/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h @@ -0,0 +1,51 @@ +#ifndef L1TRIGGER_PHASE2L1PARTICLEFLOWS_JETID_H +#define L1TRIGGER_PHASE2L1PARTICLEFLOWS_JETID_H + +#include +#include "PhysicsTools/TensorFlow/interface/TensorFlow.h" +#include "DataFormats/L1TParticleFlow/interface/PFCandidate.h" +#include "DataFormats/L1TParticleFlow/interface/PFJet.h" + +//HLS4ML compiled emulator modeling +#include +#include "ap_fixed.h" +#include "hls4ml/emulator.h" + +struct BJetTFCache { + BJetTFCache(const std::string &graphPath) : graphDef(tensorflow::loadGraphDef(graphPath)) { + session = tensorflow::createSession(graphDef.get()); + } + ~BJetTFCache() { tensorflow::closeSession(session); } + std::unique_ptr graphDef; + tensorflow::Session *session; +}; + +class JetId { +public: + JetId(const std::string &iInput, const std::string &iOutput, const std::shared_ptr model, int iNParticles); + JetId(const std::string &iInput, const std::string &iOutput, const BJetTFCache *cache, int iNParticles); + ~JetId(); + + void setNNVectorVar(); + float EvaluateNN(); + ap_fixed<16,6> EvaluateNNFixed(); + float compute (const l1t::PFJet &iJet, float vz, bool useRawPt); + ap_fixed<16,6> computeFixed(const l1t::PFJet &iJet, float vz, bool useRawPt); + +private: + std::vector NNvectorVar_; + std::string fInput_; + std::string fOutput_; + int fNParticles_; + unique_ptr fPt_; + unique_ptr fEta_; + unique_ptr fPhi_; + unique_ptr fId_; + unique_ptr fCharge_; + unique_ptr fDZ_; + unique_ptr fDX_; + unique_ptr fDY_; + tensorflow::Session *sessionRef_; + std::shared_ptr modelRef_; +}; +#endif diff --git a/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc b/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc new file mode 100644 index 0000000000000..212b367383c4e --- /dev/null +++ b/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc @@ -0,0 +1,157 @@ +#include "L1Trigger/Phase2L1ParticleFlow/interface/JetId.h" +#include "DataFormats/Math/interface/deltaPhi.h" +#include + +JetId::JetId(const std::string &iInput, const std::string &iOutput, const std::shared_ptr model, int iNParticles) + :modelRef_(model){ + NNvectorVar_.clear(); + fNParticles_ = iNParticles; + + fPt_ = std::make_unique(fNParticles_); + fEta_ = std::make_unique(fNParticles_); + fPhi_ = std::make_unique(fNParticles_); + fId_ = std::make_unique(fNParticles_); + fCharge_ = std::make_unique(fNParticles_); + fDZ_ = std::make_unique(fNParticles_); + fDX_ = std::make_unique(fNParticles_); + fDY_ = std::make_unique(fNParticles_); + fInput_ = iInput; + fOutput_ = iOutput; +} + +//--BJet algo specific constructor +JetId::JetId(const std::string &iInput, const std::string &iOutput, const BJetTFCache *cache, int iNParticles) + : sessionRef_(cache->session) { + NNvectorVar_.clear(); + fNParticles_ = iNParticles; + + fPt_ = std::make_unique(fNParticles_); + fEta_ = std::make_unique(fNParticles_); + fPhi_ = std::make_unique(fNParticles_); + fId_ = std::make_unique(fNParticles_); + fCharge_ = std::make_unique(fNParticles_); + fDZ_ = std::make_unique(fNParticles_); + fDX_ = std::make_unique(fNParticles_); + fDY_ = std::make_unique(fNParticles_); + fInput_ = iInput; + fOutput_ = iOutput; +} + +JetId::~JetId() {} + +void JetId::setNNVectorVar() { + NNvectorVar_.clear(); + for (int i0 = 0; i0 < fNParticles_; i0++) { + if (fPt_.get()[i0] == 0) { + for (int i1 = 0; i1 < 13; i1++) + NNvectorVar_.push_back(0); + continue; + } + NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Electron && fCharge_.get()[i0] < 0); // Electron + NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Electron && fCharge_.get()[i0] > 0); // Positron + NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Muon && fCharge_.get()[i0] < 0); // Muon + NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Muon && fCharge_.get()[i0] > 0); // Anti-Muon + NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Photon); // Photon + NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::NeutralHadron); // Neutral Had + NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::ChargedHadron && fCharge_.get()[i0] < 0); // Pion + NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::ChargedHadron && fCharge_.get()[i0] > 0); // Anti-Pion + NNvectorVar_.push_back(fDZ_.get()[i0]); //dZ + NNvectorVar_.push_back(std::hypot(fDX_.get()[i0], fDY_.get()[i0])); //d0 + NNvectorVar_.push_back(fPt_.get()[i0]); //pT as a fraction of jet pT + NNvectorVar_.push_back(fEta_.get()[i0]); //dEta from jet axis + NNvectorVar_.push_back(fPhi_.get()[i0]); //dPhi from jet axis + } +} +float JetId::EvaluateNN() { + tensorflow::Tensor input(tensorflow::DT_FLOAT, {1, (unsigned int)NNvectorVar_.size(), 1}); + for (unsigned int i = 0; i < NNvectorVar_.size(); i++) { + input.tensor()(0, i, 0) = float(NNvectorVar_[i]); + } + std::vector outputs; + tensorflow::run(sessionRef_, {{fInput_, input}}, {fOutput_}, &outputs); + return outputs[0].matrix()(0, 0); +} //end EvaluateNN + +ap_fixed<16,6> JetId::EvaluateNNFixed() { + ap_fixed<16,6> modelInput[140]={}; + for (unsigned int i = 0; i < NNvectorVar_.size(); i++) { + modelInput[i] = NNvectorVar_[i]; + } + ap_fixed<16,6> modelResult = -1; + //for (int i = 0; i < 1; i++) { + // modelResult[i] = -1; + //} + + modelRef_->prepare_input(modelInput); + modelRef_->predict(); + modelRef_->read_result(modelResult); + return modelResult; +} //end EvaluateNN + +float JetId::compute(const l1t::PFJet &iJet, float vz, bool useRawPt) { + for (int i0 = 0; i0 < fNParticles_; i0++) { + fPt_.get()[i0] = 0; + fEta_.get()[i0] = 0; + fPhi_.get()[i0] = 0; + fId_.get()[i0] = 0; + fCharge_.get()[i0] = 0; + fDZ_.get()[i0] = 0; + fDX_.get()[i0] = 0; + fDY_.get()[i0] = 0; + } + auto iParts = iJet.constituents(); + std::sort(iParts.begin(), iParts.end(), [](edm::Ptr i, edm::Ptr j) { + return (i->pt() > j->pt()); + }); + float jetpt = useRawPt ? iJet.rawPt() : iJet.pt(); + for (unsigned int i0 = 0; i0 < iParts.size(); i0++) { + if (i0 >= (unsigned int)fNParticles_) + break; + fPt_.get()[i0] = iParts[i0]->pt() / jetpt; + fEta_.get()[i0] = iParts[i0]->eta() - iJet.eta(); + fPhi_.get()[i0] = deltaPhi(iParts[i0]->phi(), iJet.phi()); + fId_.get()[i0] = iParts[i0]->id(); + fCharge_.get()[i0] = iParts[i0]->charge(); + if (iParts[i0]->pfTrack().isNonnull()) { + fDX_.get()[i0] = iParts[i0]->pfTrack()->vx(); + fDY_.get()[i0] = iParts[i0]->pfTrack()->vy(); + fDZ_.get()[i0] = iParts[i0]->pfTrack()->vz() - vz; + } + } + setNNVectorVar(); + return EvaluateNN(); +} + +ap_fixed<16, 6> JetId::computeFixed(const l1t::PFJet &iJet, float vz, bool useRawPt) { + for (int i0 = 0; i0 < fNParticles_; i0++) { + fPt_.get()[i0] = 0; + fEta_.get()[i0] = 0; + fPhi_.get()[i0] = 0; + fId_.get()[i0] = 0; + fCharge_.get()[i0] = 0; + fDZ_.get()[i0] = 0; + fDX_.get()[i0] = 0; + fDY_.get()[i0] = 0; + } + auto iParts = iJet.constituents(); + std::sort(iParts.begin(), iParts.end(), [](edm::Ptr i, edm::Ptr j) { + return (i->pt() > j->pt()); + }); + float jetpt = useRawPt ? iJet.rawPt() : iJet.pt(); + for (unsigned int i0 = 0; i0 < iParts.size(); i0++) { + if (i0 >= (unsigned int)fNParticles_) + break; + fPt_.get()[i0] = iParts[i0]->pt() / jetpt; + fEta_.get()[i0] = iParts[i0]->eta() - iJet.eta(); + fPhi_.get()[i0] = deltaPhi(iParts[i0]->phi(), iJet.phi()); + fId_.get()[i0] = iParts[i0]->id(); + fCharge_.get()[i0] = iParts[i0]->charge(); + if (iParts[i0]->pfTrack().isNonnull()) { + fDX_.get()[i0] = iParts[i0]->pfTrack()->vx(); + fDY_.get()[i0] = iParts[i0]->pfTrack()->vy(); + fDZ_.get()[i0] = iParts[i0]->pfTrack()->vz() - vz; + } + } + setNNVectorVar(); + return EvaluateNNFixed(); +} From 19815dc977da4685e7683fae7c6baeff1b357954 Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Fri, 22 Dec 2023 20:34:07 +0100 Subject: [PATCH 02/13] Modify L1BJetProducer.cc to use JetID, change BJetID to JetID --- .../Phase2L1ParticleFlow/interface/BJetId.h | 42 --------- .../Phase2L1ParticleFlow/interface/JetId.h | 2 +- .../plugins/L1BJetProducer.cc | 6 +- L1Trigger/Phase2L1ParticleFlow/src/BJetId.cc | 88 ------------------- 4 files changed, 4 insertions(+), 134 deletions(-) delete mode 100644 L1Trigger/Phase2L1ParticleFlow/interface/BJetId.h delete mode 100644 L1Trigger/Phase2L1ParticleFlow/src/BJetId.cc diff --git a/L1Trigger/Phase2L1ParticleFlow/interface/BJetId.h b/L1Trigger/Phase2L1ParticleFlow/interface/BJetId.h deleted file mode 100644 index cffbafce13e9c..0000000000000 --- a/L1Trigger/Phase2L1ParticleFlow/interface/BJetId.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef L1TRIGGER_PHASE2L1PARTICLEFLOWS_BJETID_H -#define L1TRIGGER_PHASE2L1PARTICLEFLOWS_BJETID_H - -#include -#include "PhysicsTools/TensorFlow/interface/TensorFlow.h" -#include "DataFormats/L1TParticleFlow/interface/PFCandidate.h" -#include "DataFormats/L1TParticleFlow/interface/PFJet.h" - -struct BJetTFCache { - BJetTFCache(const std::string &graphPath) : graphDef(tensorflow::loadGraphDef(graphPath)) { - session = tensorflow::createSession(graphDef.get()); - } - ~BJetTFCache() { tensorflow::closeSession(session); } - std::unique_ptr graphDef; - tensorflow::Session *session; -}; - -class BJetId { -public: - BJetId(const std::string &iInput, const std::string &iOutput, const BJetTFCache *cache, int iNParticles); - ~BJetId(); - - void setNNVectorVar(); - float EvaluateNN(); - float compute(const l1t::PFJet &iJet, float vz, bool useRawPt); - -private: - std::vector NNvectorVar_; - std::string fInput_; - std::string fOutput_; - int fNParticles_; - unique_ptr fPt_; - unique_ptr fEta_; - unique_ptr fPhi_; - unique_ptr fId_; - unique_ptr fCharge_; - unique_ptr fDZ_; - unique_ptr fDX_; - unique_ptr fDY_; - tensorflow::Session *sessionRef_; -}; -#endif diff --git a/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h b/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h index 0261aaaba03a8..6657cc7be64b2 100644 --- a/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h +++ b/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h @@ -29,7 +29,7 @@ class JetId { void setNNVectorVar(); float EvaluateNN(); ap_fixed<16,6> EvaluateNNFixed(); - float compute (const l1t::PFJet &iJet, float vz, bool useRawPt); + float compute(const l1t::PFJet &iJet, float vz, bool useRawPt); ap_fixed<16,6> computeFixed(const l1t::PFJet &iJet, float vz, bool useRawPt); private: diff --git a/L1Trigger/Phase2L1ParticleFlow/plugins/L1BJetProducer.cc b/L1Trigger/Phase2L1ParticleFlow/plugins/L1BJetProducer.cc index 3db89a84a276e..f3a160a36a769 100644 --- a/L1Trigger/Phase2L1ParticleFlow/plugins/L1BJetProducer.cc +++ b/L1Trigger/Phase2L1ParticleFlow/plugins/L1BJetProducer.cc @@ -8,7 +8,7 @@ #include "DataFormats/L1TParticleFlow/interface/PFJet.h" #include "DataFormats/JetReco/interface/Jet.h" #include "DataFormats/L1TParticleFlow/interface/PFCandidate.h" -#include "L1Trigger/Phase2L1ParticleFlow/interface/BJetId.h" +#include "L1Trigger/Phase2L1ParticleFlow/interface/JetId.h" #include "DataFormats/Common/interface/ValueMap.h" #include "DataFormats/L1Trigger/interface/VertexWord.h" @@ -28,7 +28,7 @@ class L1BJetProducer : public edm::stream::EDProducer fBJetId_; + std::unique_ptr fBJetId_; void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override; edm::EDGetTokenT> const jets_; @@ -48,7 +48,7 @@ L1BJetProducer::L1BJetProducer(const edm::ParameterSet& cfg, const BJetTFCache* fMaxJets_(cfg.getParameter("maxJets")), fNParticles_(cfg.getParameter("nParticles")), fVtxEmu_(consumes>(cfg.getParameter("vtx"))) { - fBJetId_ = std::make_unique( + fBJetId_ = std::make_unique( cfg.getParameter("NNInput"), cfg.getParameter("NNOutput"), cache, fNParticles_); produces>("L1PFBJets"); } diff --git a/L1Trigger/Phase2L1ParticleFlow/src/BJetId.cc b/L1Trigger/Phase2L1ParticleFlow/src/BJetId.cc deleted file mode 100644 index eaf8087bf85e6..0000000000000 --- a/L1Trigger/Phase2L1ParticleFlow/src/BJetId.cc +++ /dev/null @@ -1,88 +0,0 @@ -#include "L1Trigger/Phase2L1ParticleFlow/interface/BJetId.h" -#include "DataFormats/Math/interface/deltaPhi.h" -#include - -BJetId::BJetId(const std::string &iInput, const std::string &iOutput, const BJetTFCache *cache, int iNParticles) - : sessionRef_(cache->session) { - NNvectorVar_.clear(); - fNParticles_ = iNParticles; - - fPt_ = std::make_unique(fNParticles_); - fEta_ = std::make_unique(fNParticles_); - fPhi_ = std::make_unique(fNParticles_); - fId_ = std::make_unique(fNParticles_); - fCharge_ = std::make_unique(fNParticles_); - fDZ_ = std::make_unique(fNParticles_); - fDX_ = std::make_unique(fNParticles_); - fDY_ = std::make_unique(fNParticles_); - fInput_ = iInput; - fOutput_ = iOutput; -} - -BJetId::~BJetId() {} -void BJetId::setNNVectorVar() { - NNvectorVar_.clear(); - for (int i0 = 0; i0 < fNParticles_; i0++) { - if (fPt_.get()[i0] == 0) { - for (int i1 = 0; i1 < 13; i1++) - NNvectorVar_.push_back(0); - continue; - } - NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Electron && fCharge_.get()[i0] < 0); // Electron - NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Electron && fCharge_.get()[i0] > 0); // Positron - NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Muon && fCharge_.get()[i0] < 0); // Muon - NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Muon && fCharge_.get()[i0] > 0); // Anti-Muon - NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::Photon); // Photon - NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::NeutralHadron); // Neutral Had - NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::ChargedHadron && fCharge_.get()[i0] < 0); // Pion - NNvectorVar_.push_back(fId_.get()[i0] == l1t::PFCandidate::ChargedHadron && fCharge_.get()[i0] > 0); // Anti-Pion - NNvectorVar_.push_back(fDZ_.get()[i0]); //dZ - NNvectorVar_.push_back(std::hypot(fDX_.get()[i0], fDY_.get()[i0])); //d0 - NNvectorVar_.push_back(fPt_.get()[i0]); //pT as a fraction of jet pT - NNvectorVar_.push_back(fEta_.get()[i0]); //dEta from jet axis - NNvectorVar_.push_back(fPhi_.get()[i0]); //dPhi from jet axis - } -} -float BJetId::EvaluateNN() { - tensorflow::Tensor input(tensorflow::DT_FLOAT, {1, (unsigned int)NNvectorVar_.size(), 1}); - for (unsigned int i = 0; i < NNvectorVar_.size(); i++) { - input.tensor()(0, i, 0) = float(NNvectorVar_[i]); - } - std::vector outputs; - tensorflow::run(sessionRef_, {{fInput_, input}}, {fOutput_}, &outputs); - return outputs[0].matrix()(0, 0); -} //end EvaluateNN - -float BJetId::compute(const l1t::PFJet &iJet, float vz, bool useRawPt) { - for (int i0 = 0; i0 < fNParticles_; i0++) { - fPt_.get()[i0] = 0; - fEta_.get()[i0] = 0; - fPhi_.get()[i0] = 0; - fId_.get()[i0] = 0; - fCharge_.get()[i0] = 0; - fDZ_.get()[i0] = 0; - fDX_.get()[i0] = 0; - fDY_.get()[i0] = 0; - } - auto iParts = iJet.constituents(); - std::sort(iParts.begin(), iParts.end(), [](edm::Ptr i, edm::Ptr j) { - return (i->pt() > j->pt()); - }); - float jetpt = useRawPt ? iJet.rawPt() : iJet.pt(); - for (unsigned int i0 = 0; i0 < iParts.size(); i0++) { - if (i0 >= (unsigned int)fNParticles_) - break; - fPt_.get()[i0] = iParts[i0]->pt() / jetpt; - fEta_.get()[i0] = iParts[i0]->eta() - iJet.eta(); - fPhi_.get()[i0] = deltaPhi(iParts[i0]->phi(), iJet.phi()); - fId_.get()[i0] = iParts[i0]->id(); - fCharge_.get()[i0] = iParts[i0]->charge(); - if (iParts[i0]->pfTrack().isNonnull()) { - fDX_.get()[i0] = iParts[i0]->pfTrack()->vx(); - fDY_.get()[i0] = iParts[i0]->pfTrack()->vy(); - fDZ_.get()[i0] = iParts[i0]->pfTrack()->vz() - vz; - } - } - setNNVectorVar(); - return EvaluateNN(); -} From 796a477e498689bc1e167e79084ef1e393c1c3bf Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Fri, 22 Dec 2023 22:11:14 +0100 Subject: [PATCH 03/13] adding producer, everything compiles --- .../plugins/TOoLLiPProducer.cc | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc diff --git a/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc b/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc new file mode 100644 index 0000000000000..38e448e5fab60 --- /dev/null +++ b/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc @@ -0,0 +1,117 @@ +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/stream/EDProducer.h" +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/MakerMacros.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/Utilities/interface/InputTag.h" + +#include "DataFormats/L1TParticleFlow/interface/PFJet.h" +#include "DataFormats/JetReco/interface/Jet.h" +#include "DataFormats/L1TParticleFlow/interface/PFCandidate.h" +#include "L1Trigger/Phase2L1ParticleFlow/interface/JetId.h" +#include "DataFormats/Common/interface/ValueMap.h" + +#include "DataFormats/L1Trigger/interface/VertexWord.h" + +#include +#include + +//HLS4ML compiled emulator modeling +#include +#include "ap_fixed.h" +#include "hls4ml/emulator.h" + +using namespace l1t; + +class TOoLLiPProducer : public edm::stream::EDProducer<> { +public: + explicit TOoLLiPProducer(const edm::ParameterSet&); + ~TOoLLiPProducer() override = default; + + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); + +private: + std::unique_ptr fJetId_; + void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override; + + edm::EDGetTokenT> const jets_; + bool const fUseRawPt_; + double const fMinPt_; + double const fMaxEta_; + unsigned int const fMaxJets_; + int const fNParticles_; + edm::EDGetTokenT> const fVtxEmu_; + + //HLS4ML emulator objects + hls4mlEmulator::ModelLoader loader; + std::shared_ptr model; +}; + +TOoLLiPProducer::TOoLLiPProducer(const edm::ParameterSet& cfg) + : jets_(consumes>(cfg.getParameter("jets"))), + fUseRawPt_(cfg.getParameter("useRawPt")), + fMinPt_(cfg.getParameter("minPt")), + fMaxEta_(cfg.getParameter("maxEta")), + fMaxJets_(cfg.getParameter("maxJets")), + fNParticles_(cfg.getParameter("nParticles")), + fVtxEmu_(consumes>(cfg.getParameter("vtx"))), + loader(hls4mlEmulator::ModelLoader(cfg.getParameter("TOoLLiPVersion"))) + { + //load model and feed to JetID + model = loader.load_model(); + fJetId_ = std::make_unique(cfg.getParameter("NNInput"), cfg.getParameter("NNOutput"), model, fNParticles_); + //produces("L1LLPScores"); + produces>("L1PFLLPJets"); +} + +void TOoLLiPProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) { + edm::Handle> jets; + iEvent.getByToken(jets_, jets); + float vz = 0.; + double ptsum = 0; + edm::Handle> vtxEmuHandle; + iEvent.getByToken(fVtxEmu_, vtxEmuHandle); + for (const auto& vtx : *vtxEmuHandle) { + if (ptsum == 0 || vtx.pt() > ptsum) { + ptsum = vtx.pt(); + vz = vtx.z0(); + } + } + + std::vector LLPScores; + for (const auto& srcjet : *jets) { + if (((fUseRawPt_ ? srcjet.rawPt() : srcjet.pt()) < fMinPt_) || std::abs(srcjet.eta()) > fMaxEta_ || + LLPScores.size() >= fMaxJets_) { + LLPScores.push_back(-1.); + continue; + } + ap_fixed<16, 6> LLPScore = fJetId_->computeFixed(srcjet, vz, fUseRawPt_); + LLPScores.push_back(LLPScore); + } + + auto outT = std::make_unique>(); + edm::ValueMap::Filler fillerT(*outT); + fillerT.insert(jets, LLPScores.begin(), LLPScores.end()); + fillerT.fill(); + + iEvent.put(std::move(outT), "L1PFLLPJets"); +} + +void TOoLLiPProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { + edm::ParameterSetDescription desc; + desc.add("jets", edm::InputTag("scPFL1Puppi")); + desc.add("useRawPt", true); + //change for LLP + desc.add("TOoLLiPVersion", std::string("/src/L1Trigger/Phase2L1ParticleFlow/test/TOoLLip_emulator_v1.so")); + desc.add("NNInput", "input:0"); + desc.add("NNOutput", "sequential/dense_2/Sigmoid"); + desc.add("maxJets", 10); + desc.add("nParticles", 10); + desc.add("minPt", 20); + desc.add("maxEta", 2.4); + desc.add("vtx", edm::InputTag("L1VertexFinderEmulator", "L1VerticesEmulation")); + descriptions.add("TOoLLiPProducer", desc); +} + +#include "FWCore/Framework/interface/MakerMacros.h" +DEFINE_FWK_MODULE(TOoLLiPProducer); From 1f1b97f0787ac1c4802558c57460ac8404200ced Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Sat, 23 Dec 2023 00:04:40 +0100 Subject: [PATCH 04/13] code check updates --- L1Trigger/Phase2L1ParticleFlow/interface/JetId.h | 9 ++++++--- .../plugins/TOoLLiPProducer.cc | 15 ++++++++------- L1Trigger/Phase2L1ParticleFlow/src/JetId.cc | 15 +++++++++------ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h b/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h index 6657cc7be64b2..751eaa3898290 100644 --- a/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h +++ b/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h @@ -22,15 +22,18 @@ struct BJetTFCache { class JetId { public: - JetId(const std::string &iInput, const std::string &iOutput, const std::shared_ptr model, int iNParticles); + JetId(const std::string &iInput, + const std::string &iOutput, + const std::shared_ptr model, + int iNParticles); JetId(const std::string &iInput, const std::string &iOutput, const BJetTFCache *cache, int iNParticles); ~JetId(); void setNNVectorVar(); float EvaluateNN(); - ap_fixed<16,6> EvaluateNNFixed(); + ap_fixed<16, 6> EvaluateNNFixed(); float compute(const l1t::PFJet &iJet, float vz, bool useRawPt); - ap_fixed<16,6> computeFixed(const l1t::PFJet &iJet, float vz, bool useRawPt); + ap_fixed<16, 6> computeFixed(const l1t::PFJet &iJet, float vz, bool useRawPt); private: std::vector NNvectorVar_; diff --git a/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc b/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc index 38e448e5fab60..a1db97a3b1b07 100644 --- a/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc +++ b/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc @@ -55,11 +55,11 @@ TOoLLiPProducer::TOoLLiPProducer(const edm::ParameterSet& cfg) fMaxJets_(cfg.getParameter("maxJets")), fNParticles_(cfg.getParameter("nParticles")), fVtxEmu_(consumes>(cfg.getParameter("vtx"))), - loader(hls4mlEmulator::ModelLoader(cfg.getParameter("TOoLLiPVersion"))) - { + loader(hls4mlEmulator::ModelLoader(cfg.getParameter("TOoLLiPVersion"))) { //load model and feed to JetID model = loader.load_model(); - fJetId_ = std::make_unique(cfg.getParameter("NNInput"), cfg.getParameter("NNOutput"), model, fNParticles_); + fJetId_ = std::make_unique( + cfg.getParameter("NNInput"), cfg.getParameter("NNOutput"), model, fNParticles_); //produces("L1LLPScores"); produces>("L1PFLLPJets"); } @@ -78,14 +78,14 @@ void TOoLLiPProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) } } - std::vector LLPScores; - for (const auto& srcjet : *jets) { + std::vector LLPScores; + for (const auto& srcjet : *jets) { if (((fUseRawPt_ ? srcjet.rawPt() : srcjet.pt()) < fMinPt_) || std::abs(srcjet.eta()) > fMaxEta_ || LLPScores.size() >= fMaxJets_) { LLPScores.push_back(-1.); continue; } - ap_fixed<16, 6> LLPScore = fJetId_->computeFixed(srcjet, vz, fUseRawPt_); + ap_fixed<16, 6> LLPScore = fJetId_->computeFixed(srcjet, vz, fUseRawPt_); LLPScores.push_back(LLPScore); } @@ -102,7 +102,8 @@ void TOoLLiPProducer::fillDescriptions(edm::ConfigurationDescriptions& descripti desc.add("jets", edm::InputTag("scPFL1Puppi")); desc.add("useRawPt", true); //change for LLP - desc.add("TOoLLiPVersion", std::string("/src/L1Trigger/Phase2L1ParticleFlow/test/TOoLLip_emulator_v1.so")); + desc.add("TOoLLiPVersion", + std::string("/src/L1Trigger/Phase2L1ParticleFlow/test/TOoLLip_emulator_v1.so")); desc.add("NNInput", "input:0"); desc.add("NNOutput", "sequential/dense_2/Sigmoid"); desc.add("maxJets", 10); diff --git a/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc b/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc index 212b367383c4e..11e216c177781 100644 --- a/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc +++ b/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc @@ -2,8 +2,11 @@ #include "DataFormats/Math/interface/deltaPhi.h" #include -JetId::JetId(const std::string &iInput, const std::string &iOutput, const std::shared_ptr model, int iNParticles) - :modelRef_(model){ +JetId::JetId(const std::string &iInput, + const std::string &iOutput, + const std::shared_ptr model, + int iNParticles) + : modelRef_(model) { NNvectorVar_.clear(); fNParticles_ = iNParticles; @@ -72,18 +75,18 @@ float JetId::EvaluateNN() { return outputs[0].matrix()(0, 0); } //end EvaluateNN -ap_fixed<16,6> JetId::EvaluateNNFixed() { - ap_fixed<16,6> modelInput[140]={}; +ap_fixed<16, 6> JetId::EvaluateNNFixed() { + ap_fixed<16, 6> modelInput[140] = {}; for (unsigned int i = 0; i < NNvectorVar_.size(); i++) { modelInput[i] = NNvectorVar_[i]; } - ap_fixed<16,6> modelResult = -1; + ap_fixed<16, 6> modelResult = -1; //for (int i = 0; i < 1; i++) { // modelResult[i] = -1; //} modelRef_->prepare_input(modelInput); - modelRef_->predict(); + modelRef_->predict(); modelRef_->read_result(modelResult); return modelResult; } //end EvaluateNN From d49123d408eb39f09c914653644f772607c7d908 Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Fri, 23 Feb 2024 03:39:50 +0100 Subject: [PATCH 05/13] remove unnecessary --- L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc b/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc index a1db97a3b1b07..9d8c2df25f2fe 100644 --- a/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc +++ b/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc @@ -16,7 +16,6 @@ #include #include -//HLS4ML compiled emulator modeling #include #include "ap_fixed.h" #include "hls4ml/emulator.h" @@ -42,7 +41,6 @@ class TOoLLiPProducer : public edm::stream::EDProducer<> { int const fNParticles_; edm::EDGetTokenT> const fVtxEmu_; - //HLS4ML emulator objects hls4mlEmulator::ModelLoader loader; std::shared_ptr model; }; @@ -56,11 +54,9 @@ TOoLLiPProducer::TOoLLiPProducer(const edm::ParameterSet& cfg) fNParticles_(cfg.getParameter("nParticles")), fVtxEmu_(consumes>(cfg.getParameter("vtx"))), loader(hls4mlEmulator::ModelLoader(cfg.getParameter("TOoLLiPVersion"))) { - //load model and feed to JetID model = loader.load_model(); fJetId_ = std::make_unique( cfg.getParameter("NNInput"), cfg.getParameter("NNOutput"), model, fNParticles_); - //produces("L1LLPScores"); produces>("L1PFLLPJets"); } @@ -101,7 +97,6 @@ void TOoLLiPProducer::fillDescriptions(edm::ConfigurationDescriptions& descripti edm::ParameterSetDescription desc; desc.add("jets", edm::InputTag("scPFL1Puppi")); desc.add("useRawPt", true); - //change for LLP desc.add("TOoLLiPVersion", std::string("/src/L1Trigger/Phase2L1ParticleFlow/test/TOoLLip_emulator_v1.so")); desc.add("NNInput", "input:0"); From 867e2b2f0c09defc216411e7189b605540871b54 Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Fri, 23 Feb 2024 17:40:38 +0100 Subject: [PATCH 06/13] remove .so emulator file --- L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc | 2 +- L1Trigger/Phase2L1ParticleFlow/src/JetId.cc | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc b/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc index 9d8c2df25f2fe..2fd8a8615ec41 100644 --- a/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc +++ b/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc @@ -98,7 +98,7 @@ void TOoLLiPProducer::fillDescriptions(edm::ConfigurationDescriptions& descripti desc.add("jets", edm::InputTag("scPFL1Puppi")); desc.add("useRawPt", true); desc.add("TOoLLiPVersion", - std::string("/src/L1Trigger/Phase2L1ParticleFlow/test/TOoLLip_emulator_v1.so")); + std::string("TOoLLiP_v1")); desc.add("NNInput", "input:0"); desc.add("NNOutput", "sequential/dense_2/Sigmoid"); desc.add("maxJets", 10); diff --git a/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc b/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc index 11e216c177781..4935595d907ba 100644 --- a/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc +++ b/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc @@ -81,15 +81,12 @@ ap_fixed<16, 6> JetId::EvaluateNNFixed() { modelInput[i] = NNvectorVar_[i]; } ap_fixed<16, 6> modelResult = -1; - //for (int i = 0; i < 1; i++) { - // modelResult[i] = -1; - //} modelRef_->prepare_input(modelInput); modelRef_->predict(); modelRef_->read_result(modelResult); return modelResult; -} //end EvaluateNN +} //end EvaluateNNFixed float JetId::compute(const l1t::PFJet &iJet, float vz, bool useRawPt) { for (int i0 = 0; i0 < fNParticles_; i0++) { From 1e7978964fb1d53d2bb2e9e9dc1b1ddc514b9f73 Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Fri, 23 Feb 2024 18:39:27 +0100 Subject: [PATCH 07/13] adding TOoLLiP python config --- .../python/TOoLLiPProducer_cff.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 L1Trigger/Phase2L1ParticleFlow/python/TOoLLiPProducer_cff.py diff --git a/L1Trigger/Phase2L1ParticleFlow/python/TOoLLiPProducer_cff.py b/L1Trigger/Phase2L1ParticleFlow/python/TOoLLiPProducer_cff.py new file mode 100644 index 0000000000000..9eb1c774a8243 --- /dev/null +++ b/L1Trigger/Phase2L1ParticleFlow/python/TOoLLiPProducer_cff.py @@ -0,0 +1,20 @@ +import FWCore.ParameterSet.Config as cms + +from L1Trigger.Phase2L1ParticleFlow.l1pfJetMet_cff import L1TPFJetsExtendedTask + +from L1Trigger.Phase2L1ParticleFlow.TOoLLiPProducer_cfi import TOoLLiPProducer +l1tTOoLLiPProducer = TOoLLiPProducer.clone( + jets = ("l1tSC4PFL1PuppiExtended", ""), + maxJets = 6, + minPt = 10, + vtx = ("l1tVertexFinderEmulator","L1VerticesEmulation") +) + + +l1tTOoLLiPProducerCorrectedEmulator = l1tTOoLLiPProducer.clone( + jets = ("l1tSC4PFL1PuppiExtendedCorrectedEmulator", "") +) + +L1TTOoLLiPTask = cms.Task( + L1TPFJetsExtendedTask, l1tTOoLLiPProducer, l1tTOoLLiPProducerCorrectedEmulator +) From c7937dcbf63cbb282890438444fa0a849d9c05f2 Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Fri, 23 Feb 2024 18:42:36 +0100 Subject: [PATCH 08/13] adding to Emulator chain --- L1Trigger/Configuration/python/SimL1Emulator_cff.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/L1Trigger/Configuration/python/SimL1Emulator_cff.py b/L1Trigger/Configuration/python/SimL1Emulator_cff.py index 91bd0ba2e181a..6136c3cc49919 100644 --- a/L1Trigger/Configuration/python/SimL1Emulator_cff.py +++ b/L1Trigger/Configuration/python/SimL1Emulator_cff.py @@ -244,6 +244,10 @@ from L1Trigger.Phase2L1ParticleFlow.L1BJetProducer_cff import * _phase2_siml1emulator.add(L1TBJetsTask) +# LLPJets +# ######################################################################## +from L1Trigger.Phase2L1ParticleFlow.TOoLLiPProducer_cff import * +_phase2_siml1emulator.add(L1TTOoLLiPTask) # --> add modules from Configuration.Eras.Modifier_phase2_trigger_cff import phase2_trigger From 509d8d52f1940869b13c0223566b83d2dcc23602 Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Fri, 23 Feb 2024 18:44:42 +0100 Subject: [PATCH 09/13] adding to event content --- L1Trigger/Configuration/python/L1Trigger_EventContent_cff.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/L1Trigger/Configuration/python/L1Trigger_EventContent_cff.py b/L1Trigger/Configuration/python/L1Trigger_EventContent_cff.py index 17ee0b32aa165..bd302c7951555 100644 --- a/L1Trigger/Configuration/python/L1Trigger_EventContent_cff.py +++ b/L1Trigger/Configuration/python/L1Trigger_EventContent_cff.py @@ -227,6 +227,8 @@ def _appendPhase2Digis(obj): 'keep *_l1tHPSPFTauProducer_*_*', 'keep *_l1tBJetProducerPuppi_*_*', 'keep *_l1tBJetProducerPuppiCorrectedEmulator_*_*', + 'keep *_l1tTOoLLiPProducer_*_*', + 'keep *_l1tTOoLLiPProducerCorrectedEmulator_*_*', 'keep *_TTStubsFromPhase2TrackerDigis_*_*', 'keep *_TTClustersFromPhase2TrackerDigis_*_*', 'keep *_l1tTTTracksFromExtendedTrackletEmulation_*_*', From ad778504b8689588865cea860d46cf0b5ad47bae Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Fri, 23 Feb 2024 22:41:41 +0100 Subject: [PATCH 10/13] add model name in Buildfile --- L1Trigger/Phase2L1ParticleFlow/BuildFile.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/L1Trigger/Phase2L1ParticleFlow/BuildFile.xml b/L1Trigger/Phase2L1ParticleFlow/BuildFile.xml index 9c3b1442767df..e45b710a713f9 100644 --- a/L1Trigger/Phase2L1ParticleFlow/BuildFile.xml +++ b/L1Trigger/Phase2L1ParticleFlow/BuildFile.xml @@ -13,8 +13,9 @@ - + + From 3843a39e80cb5a6e07ea6e2a51bd0f168c37fd19 Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Fri, 23 Feb 2024 23:45:43 +0100 Subject: [PATCH 11/13] Fix modelResult type --- L1Trigger/Phase2L1ParticleFlow/src/JetId.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc b/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc index 4935595d907ba..383485597d487 100644 --- a/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc +++ b/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc @@ -80,12 +80,13 @@ ap_fixed<16, 6> JetId::EvaluateNNFixed() { for (unsigned int i = 0; i < NNvectorVar_.size(); i++) { modelInput[i] = NNvectorVar_[i]; } - ap_fixed<16, 6> modelResult = -1; + ap_fixed<16, 6> modelResult[1] = {-1}; modelRef_->prepare_input(modelInput); modelRef_->predict(); modelRef_->read_result(modelResult); - return modelResult; + ap_fixed<16, 6> modelResult_ = modelResult[0]; + return modelResult_; } //end EvaluateNNFixed float JetId::compute(const l1t::PFJet &iJet, float vz, bool useRawPt) { From 0d183865e1bc9f2b2e077b93ed65e703902064d5 Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Mon, 26 Feb 2024 19:03:05 +0100 Subject: [PATCH 12/13] format fix --- L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc b/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc index 2fd8a8615ec41..e33ff7040865a 100644 --- a/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc +++ b/L1Trigger/Phase2L1ParticleFlow/plugins/TOoLLiPProducer.cc @@ -97,8 +97,7 @@ void TOoLLiPProducer::fillDescriptions(edm::ConfigurationDescriptions& descripti edm::ParameterSetDescription desc; desc.add("jets", edm::InputTag("scPFL1Puppi")); desc.add("useRawPt", true); - desc.add("TOoLLiPVersion", - std::string("TOoLLiP_v1")); + desc.add("TOoLLiPVersion", std::string("TOoLLiP_v1")); desc.add("NNInput", "input:0"); desc.add("NNOutput", "sequential/dense_2/Sigmoid"); desc.add("maxJets", 10); From 35942a4cd36b8658511ef5706b9fc94ee88654aa Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Mon, 26 Feb 2024 19:12:51 +0100 Subject: [PATCH 13/13] set destructor to default --- L1Trigger/Phase2L1ParticleFlow/interface/JetId.h | 2 +- L1Trigger/Phase2L1ParticleFlow/src/JetId.cc | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h b/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h index 751eaa3898290..8dd4d14a7afcf 100644 --- a/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h +++ b/L1Trigger/Phase2L1ParticleFlow/interface/JetId.h @@ -27,7 +27,7 @@ class JetId { const std::shared_ptr model, int iNParticles); JetId(const std::string &iInput, const std::string &iOutput, const BJetTFCache *cache, int iNParticles); - ~JetId(); + ~JetId() = default; void setNNVectorVar(); float EvaluateNN(); diff --git a/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc b/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc index 383485597d487..3692ad912c6d0 100644 --- a/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc +++ b/L1Trigger/Phase2L1ParticleFlow/src/JetId.cc @@ -40,8 +40,6 @@ JetId::JetId(const std::string &iInput, const std::string &iOutput, const BJetTF fOutput_ = iOutput; } -JetId::~JetId() {} - void JetId::setNNVectorVar() { NNvectorVar_.clear(); for (int i0 = 0; i0 < fNParticles_; i0++) {