File indexing completed on 2024-04-06 12:21:28
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 using namespace l1t;
0020
0021 class L1BJetProducer : public edm::stream::EDProducer<edm::GlobalCache<BJetTFCache>> {
0022 public:
0023 explicit L1BJetProducer(const edm::ParameterSet&, const BJetTFCache*);
0024 ~L1BJetProducer() override = default;
0025
0026 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0027 static std::unique_ptr<BJetTFCache> initializeGlobalCache(const edm::ParameterSet&);
0028 static void globalEndJob(const BJetTFCache*);
0029
0030 private:
0031 std::unique_ptr<JetId> fBJetId_;
0032 void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0033
0034 edm::EDGetTokenT<edm::View<l1t::PFJet>> const jets_;
0035 bool const fUseRawPt_;
0036 double const fMinPt_;
0037 double const fMaxEta_;
0038 unsigned int const fMaxJets_;
0039 int const fNParticles_;
0040 edm::EDGetTokenT<std::vector<l1t::VertexWord>> const fVtxEmu_;
0041 };
0042
0043 L1BJetProducer::L1BJetProducer(const edm::ParameterSet& cfg, const BJetTFCache* cache)
0044 : jets_(consumes<edm::View<l1t::PFJet>>(cfg.getParameter<edm::InputTag>("jets"))),
0045 fUseRawPt_(cfg.getParameter<bool>("useRawPt")),
0046 fMinPt_(cfg.getParameter<double>("minPt")),
0047 fMaxEta_(cfg.getParameter<double>("maxEta")),
0048 fMaxJets_(cfg.getParameter<int>("maxJets")),
0049 fNParticles_(cfg.getParameter<int>("nParticles")),
0050 fVtxEmu_(consumes<std::vector<l1t::VertexWord>>(cfg.getParameter<edm::InputTag>("vtx"))) {
0051 fBJetId_ = std::make_unique<JetId>(
0052 cfg.getParameter<std::string>("NNInput"), cfg.getParameter<std::string>("NNOutput"), cache, fNParticles_);
0053 produces<edm::ValueMap<float>>("L1PFBJets");
0054 }
0055 std::unique_ptr<BJetTFCache> L1BJetProducer::initializeGlobalCache(const edm::ParameterSet& cfg) {
0056 edm::FileInPath fp(cfg.getParameter<edm::FileInPath>("NNFileName"));
0057 auto cache = std::make_unique<BJetTFCache>(fp.fullPath());
0058 return cache;
0059 }
0060 void L1BJetProducer::globalEndJob(const BJetTFCache* cache) {}
0061 void L1BJetProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0062 edm::Handle<edm::View<l1t::PFJet>> jets;
0063 iEvent.getByToken(jets_, jets);
0064
0065 float vz = 0.;
0066 double ptsum = 0;
0067 edm::Handle<std::vector<l1t::VertexWord>> vtxEmuHandle;
0068 iEvent.getByToken(fVtxEmu_, vtxEmuHandle);
0069 for (const auto& vtx : *vtxEmuHandle) {
0070 if (ptsum == 0 || vtx.pt() > ptsum) {
0071 ptsum = vtx.pt();
0072 vz = vtx.z0();
0073 }
0074 }
0075
0076 std::vector<float> bScores;
0077
0078 for (const auto& srcjet : *jets) {
0079 if (((fUseRawPt_ ? srcjet.rawPt() : srcjet.pt()) < fMinPt_) || std::abs(srcjet.eta()) > fMaxEta_ ||
0080 bScores.size() >= fMaxJets_) {
0081 bScores.push_back(-1.);
0082 continue;
0083 }
0084 float NN = fBJetId_->compute(srcjet, vz, fUseRawPt_);
0085 bScores.push_back(NN);
0086 }
0087
0088 auto outT = std::make_unique<edm::ValueMap<float>>();
0089 edm::ValueMap<float>::Filler fillerT(*outT);
0090 fillerT.insert(jets, bScores.begin(), bScores.end());
0091 fillerT.fill();
0092
0093 iEvent.put(std::move(outT), "L1PFBJets");
0094 }
0095
0096 void L1BJetProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0097
0098 edm::ParameterSetDescription desc;
0099 desc.add<edm::InputTag>("jets", edm::InputTag("scPFL1Puppi"));
0100 desc.add<bool>("useRawPt", true);
0101 desc.add<edm::FileInPath>("NNFileName",
0102 edm::FileInPath("L1Trigger/Phase2L1ParticleFlow/data/modelTT_PUP_Off_dXY_XYCut_Graph.pb"));
0103 desc.add<std::string>("NNInput", "input:0");
0104 desc.add<std::string>("NNOutput", "sequential/dense_2/Sigmoid");
0105 desc.add<int>("maxJets", 10);
0106 desc.add<int>("nParticles", 10);
0107 desc.add<double>("minPt", 20);
0108 desc.add<double>("maxEta", 2.4);
0109 desc.add<edm::InputTag>("vtx", edm::InputTag("L1VertexFinderEmulator", "L1VerticesEmulation"));
0110 descriptions.add("L1BJetProducer", desc);
0111 }
0112
0113 #include "FWCore/Framework/interface/MakerMacros.h"
0114 DEFINE_FWK_MODULE(L1BJetProducer);