Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:21:30

0001 #include "FWCore/Framework/interface/Frameworkfwd.h"
0002 #include "FWCore/Framework/interface/stream/EDProducer.h"
0003 #include "FWCore/Framework/interface/Event.h"
0004 #include "FWCore/Framework/interface/MakerMacros.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Utilities/interface/InputTag.h"
0007 
0008 #include "DataFormats/L1TParticleFlow/interface/PFJet.h"
0009 #include "DataFormats/JetReco/interface/Jet.h"
0010 #include "DataFormats/L1TParticleFlow/interface/PFCandidate.h"
0011 #include "L1Trigger/Phase2L1ParticleFlow/interface/JetId.h"
0012 #include "DataFormats/Common/interface/ValueMap.h"
0013 
0014 #include "DataFormats/L1Trigger/interface/VertexWord.h"
0015 
0016 #include <cmath>
0017 #include <vector>
0018 
0019 #include <string>
0020 #include "ap_fixed.h"
0021 #include "hls4ml/emulator.h"
0022 
0023 using namespace l1t;
0024 
0025 class TOoLLiPProducer : public edm::stream::EDProducer<> {
0026 public:
0027   explicit TOoLLiPProducer(const edm::ParameterSet&);
0028   ~TOoLLiPProducer() override = default;
0029 
0030   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0031 
0032 private:
0033   std::unique_ptr<JetId> fJetId_;
0034   void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0035 
0036   edm::EDGetTokenT<edm::View<l1t::PFJet>> const jets_;
0037   bool const fUseRawPt_;
0038   double const fMinPt_;
0039   double const fMaxEta_;
0040   unsigned int const fMaxJets_;
0041   int const fNParticles_;
0042   edm::EDGetTokenT<std::vector<l1t::VertexWord>> const fVtxEmu_;
0043 
0044   hls4mlEmulator::ModelLoader loader;
0045   std::shared_ptr<hls4mlEmulator::Model> model;
0046 };
0047 
0048 TOoLLiPProducer::TOoLLiPProducer(const edm::ParameterSet& cfg)
0049     : jets_(consumes<edm::View<l1t::PFJet>>(cfg.getParameter<edm::InputTag>("jets"))),
0050       fUseRawPt_(cfg.getParameter<bool>("useRawPt")),
0051       fMinPt_(cfg.getParameter<double>("minPt")),
0052       fMaxEta_(cfg.getParameter<double>("maxEta")),
0053       fMaxJets_(cfg.getParameter<int>("maxJets")),
0054       fNParticles_(cfg.getParameter<int>("nParticles")),
0055       fVtxEmu_(consumes<std::vector<l1t::VertexWord>>(cfg.getParameter<edm::InputTag>("vtx"))),
0056       loader(hls4mlEmulator::ModelLoader(cfg.getParameter<string>("TOoLLiPVersion"))) {
0057   model = loader.load_model();
0058   fJetId_ = std::make_unique<JetId>(
0059       cfg.getParameter<std::string>("NNInput"), cfg.getParameter<std::string>("NNOutput"), model, fNParticles_);
0060   produces<edm::ValueMap<float>>("L1PFLLPJets");
0061 }
0062 
0063 void TOoLLiPProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0064   edm::Handle<edm::View<l1t::PFJet>> jets;
0065   iEvent.getByToken(jets_, jets);
0066   float vz = 0.;
0067   double ptsum = 0;
0068   edm::Handle<std::vector<l1t::VertexWord>> vtxEmuHandle;
0069   iEvent.getByToken(fVtxEmu_, vtxEmuHandle);
0070   for (const auto& vtx : *vtxEmuHandle) {
0071     if (ptsum == 0 || vtx.pt() > ptsum) {
0072       ptsum = vtx.pt();
0073       vz = vtx.z0();
0074     }
0075   }
0076 
0077   std::vector<float> LLPScores;
0078   for (const auto& srcjet : *jets) {
0079     if (((fUseRawPt_ ? srcjet.rawPt() : srcjet.pt()) < fMinPt_) || std::abs(srcjet.eta()) > fMaxEta_ ||
0080         LLPScores.size() >= fMaxJets_) {
0081       LLPScores.push_back(-1.);
0082       continue;
0083     }
0084     ap_fixed<16, 6> LLPScore = fJetId_->computeFixed(srcjet, vz, fUseRawPt_);
0085     LLPScores.push_back(LLPScore);
0086   }
0087 
0088   auto outT = std::make_unique<edm::ValueMap<float>>();
0089   edm::ValueMap<float>::Filler fillerT(*outT);
0090   fillerT.insert(jets, LLPScores.begin(), LLPScores.end());
0091   fillerT.fill();
0092 
0093   iEvent.put(std::move(outT), "L1PFLLPJets");
0094 }
0095 
0096 void TOoLLiPProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0097   edm::ParameterSetDescription desc;
0098   desc.add<edm::InputTag>("jets", edm::InputTag("scPFL1Puppi"));
0099   desc.add<bool>("useRawPt", true);
0100   desc.add<std::string>("TOoLLiPVersion", std::string("TOoLLiP_v1"));
0101   desc.add<std::string>("NNInput", "input:0");
0102   desc.add<std::string>("NNOutput", "sequential/dense_2/Sigmoid");
0103   desc.add<int>("maxJets", 10);
0104   desc.add<int>("nParticles", 10);
0105   desc.add<double>("minPt", 20);
0106   desc.add<double>("maxEta", 2.4);
0107   desc.add<edm::InputTag>("vtx", edm::InputTag("L1VertexFinderEmulator", "L1VerticesEmulation"));
0108   descriptions.add("TOoLLiPProducer", desc);
0109 }
0110 
0111 #include "FWCore/Framework/interface/MakerMacros.h"
0112 DEFINE_FWK_MODULE(TOoLLiPProducer);