forked from cms-sw/cmssw
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cms-sw#97 from drankincms/l1bjets_CMSSW_1230pre4
Adding L1 BJet NN
- Loading branch information
Showing
14 changed files
with
366 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+12.4 KB
L1Trigger/Phase2L1ParticleFlow/data/modelTT_PUP_Off_dXY_XYCut_Graph.pb
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef L1TRIGGER_PHASE2L1PARTICLEFLOWS_BJETID_H | ||
#define L1TRIGGER_PHASE2L1PARTICLEFLOWS_BJETID_H | ||
|
||
#include <string> | ||
#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)) {} | ||
std::unique_ptr<tensorflow::GraphDef> graphDef; | ||
}; | ||
|
||
class BJetId { | ||
public: | ||
BJetId(const std::string &iInput, | ||
const std::string &iOutput, | ||
const BJetTFCache *cache, | ||
const std::string &iWeightFile, | ||
int iNParticles); | ||
~BJetId(); | ||
|
||
void setNNVectorVar(); | ||
float EvaluateNN(); | ||
float compute(const l1t::PFJet &iJet, float vz, bool useRawPt); | ||
|
||
private: | ||
tensorflow::Session *session_; | ||
std::vector<float> NNvectorVar_; | ||
std::string fInput_; | ||
std::string fOutput_; | ||
int fNParticles_; | ||
unique_ptr<float[]> fPt_; | ||
unique_ptr<float[]> fEta_; | ||
unique_ptr<float[]> fPhi_; | ||
unique_ptr<float[]> fId_; | ||
unique_ptr<int[]> fCharge_; | ||
unique_ptr<float[]> fDZ_; | ||
unique_ptr<float[]> fDX_; | ||
unique_ptr<float[]> fDY_; | ||
}; | ||
#endif |
141 changes: 141 additions & 0 deletions
141
L1Trigger/Phase2L1ParticleFlow/plugins/L1BJetPFJetProducer.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#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/BJetId.h" | ||
#include "DataFormats/Common/interface/ValueMap.h" | ||
|
||
#include "DataFormats/L1TCorrelator/interface/TkPrimaryVertex.h" | ||
#include "DataFormats/L1Trigger/interface/Vertex.h" | ||
#include "DataFormats/L1Trigger/interface/VertexWord.h" | ||
|
||
#include "DataFormats/Math/interface/deltaR.h" | ||
#include <cmath> | ||
#include <vector> | ||
|
||
using namespace l1t; | ||
|
||
class L1BJetProducer : public edm::stream::EDProducer<edm::GlobalCache<BJetTFCache>> { | ||
public: | ||
explicit L1BJetProducer(const edm::ParameterSet&, const BJetTFCache*); | ||
~L1BJetProducer() override; | ||
|
||
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); | ||
static std::unique_ptr<BJetTFCache> initializeGlobalCache(const edm::ParameterSet&); | ||
static void globalEndJob(const BJetTFCache*); | ||
|
||
private: | ||
std::unique_ptr<BJetId> fBJetId_; | ||
void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override; | ||
|
||
edm::EDGetTokenT<edm::View<l1t::PFJet>> jets_; | ||
bool fUseRawPt_; | ||
double fMinPt_; | ||
double fMaxEta_; | ||
unsigned int fMaxJets_; | ||
int fNParticles_; | ||
edm::EDGetTokenT<std::vector<l1t::TkPrimaryVertex>> fTkVtx_; | ||
edm::EDGetTokenT<std::vector<l1t::VertexWord>> fVtxEmu_; | ||
bool fVtxEmulation_; | ||
}; | ||
|
||
static constexpr float track_trigger_eta_max = 2.5; | ||
|
||
L1BJetProducer::L1BJetProducer(const edm::ParameterSet& cfg, const BJetTFCache* cache) | ||
: jets_(consumes<edm::View<l1t::PFJet>>(cfg.getParameter<edm::InputTag>("jets"))), | ||
fUseRawPt_(cfg.getParameter<bool>("useRawPt")), | ||
fMinPt_(cfg.getParameter<double>("minPt")), | ||
fMaxEta_(cfg.getParameter<double>("maxEta")), | ||
fMaxJets_(cfg.getParameter<int>("maxJets")), | ||
fNParticles_(cfg.getParameter<int>("nParticles")), | ||
fVtxEmulation_(cfg.getParameter<bool>("vtxEmulation")) { | ||
std::string lNNFile = cfg.getParameter<std::string>("NNFileName"); | ||
fBJetId_ = std::make_unique<BJetId>( | ||
cfg.getParameter<std::string>("NNInput"), cfg.getParameter<std::string>("NNOutput"), cache, lNNFile, fNParticles_); | ||
if (fVtxEmulation_) { | ||
fVtxEmu_ = consumes<std::vector<l1t::VertexWord>>(cfg.getParameter<edm::InputTag>("vtx")); | ||
} else { | ||
fTkVtx_ = consumes<std::vector<l1t::TkPrimaryVertex>>(cfg.getParameter<edm::InputTag>("vtx")); | ||
} | ||
produces<edm::ValueMap<float>>("L1PFBJets"); | ||
} | ||
std::unique_ptr<BJetTFCache> L1BJetProducer::initializeGlobalCache(const edm::ParameterSet& cfg) { | ||
tensorflow::setLogging("3"); | ||
std::string lNNFile = cfg.getParameter<std::string>("NNFileName"); | ||
edm::FileInPath fp(lNNFile); | ||
auto cache = std::make_unique<BJetTFCache>(fp.fullPath()); | ||
return cache; | ||
} | ||
void L1BJetProducer::globalEndJob(const BJetTFCache* cache) {} | ||
void L1BJetProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) { | ||
edm::Handle<edm::View<l1t::PFJet>> jets; | ||
iEvent.getByToken(jets_, jets); | ||
|
||
float vz = 0.; | ||
double ptsum = 0; | ||
if (fVtxEmulation_) { | ||
edm::Handle<std::vector<l1t::VertexWord>> vtxEmuHandle; | ||
iEvent.getByToken(fVtxEmu_, vtxEmuHandle); | ||
for (const auto& vtx : *vtxEmuHandle) { | ||
if (ptsum == 0 || vtx.pt() > ptsum) { | ||
ptsum = vtx.pt(); | ||
vz = vtx.z0(); | ||
} | ||
} | ||
} else { | ||
edm::Handle<std::vector<l1t::TkPrimaryVertex>> vtxHandle; | ||
iEvent.getByToken(fTkVtx_, vtxHandle); | ||
for (const l1t::TkPrimaryVertex& vtx : *vtxHandle) { | ||
if (ptsum == 0 || vtx.sum() > ptsum) { | ||
ptsum = vtx.sum(); | ||
vz = vtx.zvertex(); | ||
} | ||
} | ||
} | ||
|
||
std::vector<float> bScores; | ||
|
||
for (const auto& srcjet : *jets) { | ||
if (((fUseRawPt_ ? srcjet.rawPt() : srcjet.pt()) < fMinPt_) || std::abs(srcjet.eta()) > fMaxEta_ || | ||
bScores.size() >= fMaxJets_) { | ||
bScores.push_back(-1.); | ||
continue; | ||
} | ||
float NN = fBJetId_->compute(srcjet, vz, fUseRawPt_); | ||
bScores.push_back(NN); | ||
} | ||
|
||
auto outT = std::make_unique<edm::ValueMap<float>>(); | ||
edm::ValueMap<float>::Filler fillerT(*outT); | ||
fillerT.insert(jets, bScores.begin(), bScores.end()); | ||
fillerT.fill(); | ||
|
||
iEvent.put(std::move(outT), "L1PFBJets"); | ||
} | ||
|
||
void L1BJetProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { | ||
// L1BJetProducer | ||
edm::ParameterSetDescription desc; | ||
desc.add<edm::InputTag>("jets", edm::InputTag("scPFL1Puppi")); | ||
desc.add<bool>("useRawPt", true); | ||
desc.add<std::string>("NNFileName", "L1Trigger/Phase2L1ParticleFlow/data/modelTT_PUP_Off_dXY_XYCut_Graph.pb"); | ||
desc.add<std::string>("NNInput", "input:0"); | ||
desc.add<std::string>("NNOutput", "sequential/dense_2/Sigmoid"); | ||
desc.add<int>("maxJets", 10); | ||
desc.add<int>("nParticles", 10); | ||
desc.add<double>("minPt", 20); | ||
desc.add<double>("maxEta", 2.4); | ||
desc.add<edm::InputTag>("vtx", edm::InputTag("L1VertexFinderEmulator", "l1verticesEmulation")); | ||
desc.add<bool>("vtxEmulation", true); | ||
descriptions.add("L1BJetProducer", desc); | ||
} | ||
L1BJetProducer::~L1BJetProducer() {} | ||
|
||
#include "FWCore/Framework/interface/MakerMacros.h" | ||
DEFINE_FWK_MODULE(L1BJetProducer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
L1Trigger/Phase2L1ParticleFlow/python/L1BJetProducer_cff.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import FWCore.ParameterSet.Config as cms | ||
|
||
from L1Trigger.Phase2L1ParticleFlow.l1pfJetMet_cff import l1PFJetsExtendedTask | ||
|
||
l1BJetProducerPuppi = cms.EDProducer("L1BJetProducer", | ||
jets = cms.InputTag("scPFL1PuppiExtended", ""), | ||
useRawPt = cms.bool(True), | ||
NNFileName = cms.string("L1Trigger/Phase2L1ParticleFlow/data/modelTT_PUP_Off_dXY_XYCut_Graph.pb"), | ||
NNInput = cms.string("input:0"), | ||
NNOutput = cms.string("sequential/dense_2/Sigmoid"), | ||
maxJets = cms.int32(6), | ||
nParticles = cms.int32(10), | ||
minPt = cms.double(10), | ||
maxEta = cms.double(2.4), | ||
vtx = cms.InputTag("L1VertexFinderEmulator","l1verticesEmulation"), | ||
vtxEmulation = cms.bool(True), | ||
) | ||
l1BJetProducerPuppiCorrectedEmulator = l1BJetProducerPuppi.clone( | ||
jets = cms.InputTag("scPFL1PuppiExtendedCorrectedEmulator", ""), | ||
) | ||
|
||
l1BJetsTask = cms.Task( | ||
l1PFJetsExtendedTask, l1BJetProducerPuppi, l1BJetProducerPuppiCorrectedEmulator | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.