Skip to content

Commit

Permalink
Merge pull request cms-sw#97 from drankincms/l1bjets_CMSSW_1230pre4
Browse files Browse the repository at this point in the history
Adding L1 BJet NN
  • Loading branch information
gpetruc authored Sep 9, 2022
2 parents ad701bc + 85a3d37 commit 8aaee45
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 16 deletions.
7 changes: 4 additions & 3 deletions DataFormats/L1TParticleFlow/interface/PFJet.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

#include <vector>
#include "DataFormats/L1Trigger/interface/L1Candidate.h"
#include "DataFormats/L1TParticleFlow/interface/PFCandidate.h"
#include "DataFormats/Common/interface/Ptr.h"

namespace l1t {

class PFJet : public L1Candidate {
public:
/// constituent information. note that this is not going to be available in the hardware!
typedef std::vector<edm::Ptr<l1t::L1Candidate>> Constituents;
typedef std::vector<edm::Ptr<l1t::PFCandidate>> Constituents;

PFJet() {}
PFJet(float pt, float eta, float phi, float mass = 0, int hwpt = 0, int hweta = 0, int hwphi = 0)
Expand All @@ -28,13 +29,13 @@ namespace l1t {
/// constituent information. note that this is not going to be available in the hardware!
const Constituents& constituents() const { return constituents_; }
/// adds a candidate to this cluster; note that this only records the information, it's up to you to also set the 4-vector appropriately
void addConstituent(const edm::Ptr<l1t::L1Candidate>& cand) { constituents_.emplace_back(cand); }
void addConstituent(const edm::Ptr<l1t::PFCandidate>& cand) { constituents_.emplace_back(cand); }

// candidate interface
size_t numberOfDaughters() const override { return constituents_.size(); }
const reco::Candidate* daughter(size_type i) const override { return constituents_[i].get(); }
using reco::LeafCandidate::daughter; // avoid hiding the base
edm::Ptr<l1t::L1Candidate> daughterPtr(size_type i) const { return constituents_[i]; }
edm::Ptr<l1t::PFCandidate> daughterPtr(size_type i) const { return constituents_[i]; }

// Get and set the encodedJet_ bits. The Jet is encoded in 128 bits as a 2-element array of uint64_t
std::array<uint64_t, 2> encodedJet() { return encodedJet_; }
Expand Down
3 changes: 2 additions & 1 deletion DataFormats/L1TParticleFlow/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
<class name="l1t::RegionalOutput<l1t::PFCandidateCollection>" />
<class name="edm::Wrapper<l1t::RegionalOutput<l1t::PFCandidateCollection>>" />

<class name="l1t::PFJet" ClassVersion="4">
<class name="l1t::PFJet" ClassVersion="5">
<version ClassVersion="5" checksum="2270932343"/>
<version ClassVersion="4" checksum="1424452548"/>
<version ClassVersion="3" checksum="133342988"/>
</class>
Expand Down
9 changes: 9 additions & 0 deletions L1Trigger/Configuration/python/L1Trigger_EventContent_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def _appendPhase2Digis(obj):
'keep *_scPFL1PF_*_*',
'keep *_scPFL1Puppi_*_*',
'keep *_scPFL1PuppiCorrectedEmulator_*_*',
'keep *_scPFL1PuppiExtended_*_*',
'keep *_scPFL1PuppiExtendedCorrectedEmulator_*_*',
'keep *_scPFL1PuppiCorrectedEmulatorMHT_*_*',
'keep *_Phase1L1TJetProducer_*_*',
'keep *_Phase1L1TJetCalibrator_*_*',
Expand All @@ -199,10 +201,17 @@ def _appendPhase2Digis(obj):
'keep *_l1ctLayer1HGCalNoTK_*_*',
'keep *_l1ctLayer1HF_*_*',
'keep *_l1ctLayer1_*_*',
'keep *_l1ctLayer1BarrelExtended_*_*',
'keep *_l1ctLayer1HGCalExtended_*_*',
'keep *_l1ctLayer1Extended_*_*',
'keep *_l1ctLayer1EG_*_*',
'keep *_pfTracksFromL1Tracks_*_*',
'keep *_pfTracksFromL1TracksExtended_*_*',
'keep *_L1MetPfProducer_*_*',
'keep *_l1NNTauProducer_*_*',
'keep *_l1NNTauProducerPuppi_*_*',
'keep *_l1BJetProducerPuppi_*_*',
'keep *_l1BJetProducerPuppiCorrectedEmulator_*_*',
'keep *_TTStubsFromPhase2TrackerDigis_*_*',
'keep *_TTClustersFromPhase2TrackerDigis_*_*',
'keep *_TTTracksFromExtendedTrackletEmulation_*_*',
Expand Down
5 changes: 5 additions & 0 deletions L1Trigger/Configuration/python/SimL1Emulator_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@
#_phase2_siml1emulator.add(L1NNTauProducerPuppi2Vtx)
_phase2_siml1emulator.add(tau2VtxTaskHW)

# BJets
# ########################################################################
from L1Trigger.Phase2L1ParticleFlow.L1BJetProducer_cff import *
_phase2_siml1emulator.add(l1BJetsTask)


# --> add modules
from Configuration.Eras.Modifier_phase2_trigger_cff import phase2_trigger
Expand Down
Binary file not shown.
42 changes: 42 additions & 0 deletions L1Trigger/Phase2L1ParticleFlow/interface/BJetId.h
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 L1Trigger/Phase2L1ParticleFlow/plugins/L1BJetPFJetProducer.cc
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);
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class L1TCorrectedPFJetProducer : public edm::global::EDProducer<> {
private:
void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;

edm::EDGetTokenT<edm::View<reco::Candidate>> jets_;
edm::EDGetTokenT<edm::View<l1t::PFJet>> jets_;
l1tpf::corrector corrector_;
};

L1TCorrectedPFJetProducer::L1TCorrectedPFJetProducer(const edm::ParameterSet& iConfig)
: jets_(consumes<edm::View<reco::Candidate>>(iConfig.getParameter<edm::InputTag>("jets"))),
: jets_(consumes<edm::View<l1t::PFJet>>(iConfig.getParameter<edm::InputTag>("jets"))),
corrector_(iConfig.getParameter<std::string>("correctorFile"),
iConfig.getParameter<std::string>("correctorDir")) {
produces<std::vector<l1t::PFJet>>();
Expand All @@ -35,7 +35,7 @@ L1TCorrectedPFJetProducer::L1TCorrectedPFJetProducer(const edm::ParameterSet& iC
L1TCorrectedPFJetProducer::~L1TCorrectedPFJetProducer() {}

void L1TCorrectedPFJetProducer::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup&) const {
edm::Handle<edm::View<reco::Candidate>> jets;
edm::Handle<edm::View<l1t::PFJet>> jets;
iEvent.getByToken(jets_, jets);
auto out = std::make_unique<std::vector<l1t::PFJet>>();

Expand All @@ -44,11 +44,9 @@ void L1TCorrectedPFJetProducer::produce(edm::StreamID, edm::Event& iEvent, const
out->emplace_back(srcjet.p4());
auto& jet = out->back();
// copy daughters
//if (copyDaughters_) {
// for (const auto& dau : srcjet.daughterPtrVector()) {
// jet.addConstituent(edm::Ptr<l1t::L1Candidate>(dau));
// }
//}
for (const auto& dau : srcjet.constituents()) {
jet.addConstituent(dau);
}
// apply corrections
jet.calibratePt(corrector_.correctedPt(jet.pt(), jet.eta()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
nPuppiSecondBuffers = cms.uint32(32),
nPuppiThirdBuffers = cms.uint32(64)
)

DeregionizerProducerExtended = DeregionizerProducer.clone(RegionalPuppiCands = cms.InputTag("l1ctLayer1Extended","PuppiRegional"))
24 changes: 24 additions & 0 deletions L1Trigger/Phase2L1ParticleFlow/python/L1BJetProducer_cff.py
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
)
20 changes: 18 additions & 2 deletions L1Trigger/Phase2L1ParticleFlow/python/l1ctLayer1_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import math

from L1Trigger.Phase2L1ParticleFlow.pfTracksFromL1Tracks_cfi import pfTracksFromL1Tracks
from L1Trigger.Phase2L1ParticleFlow.pfTracksFromL1Tracks_cfi import pfTracksFromL1Tracks, pfTracksFromL1TracksExtended
from L1Trigger.Phase2L1ParticleFlow.pfClustersFromL1EGClusters_cfi import pfClustersFromL1EGClusters
from L1Trigger.Phase2L1ParticleFlow.pfClustersFromCombinedCalo_cff import pfClustersFromCombinedCaloHCal, pfClustersFromCombinedCaloHF
from L1Trigger.Phase2L1ParticleFlow.pfClustersFromHGC3DClusters_cfi import pfClustersFromHGC3DClusters
Expand Down Expand Up @@ -137,6 +137,8 @@
)
)

l1ctLayer1BarrelExtended = l1ctLayer1Barrel.clone(tracks = cms.InputTag('pfTracksFromL1TracksExtended'))

_hgcalSectors = cms.VPSet(
cms.PSet(
etaBoundaries = cms.vdouble(-3.0, -1.5),
Expand Down Expand Up @@ -286,6 +288,7 @@
writeRawHgcalCluster = cms.untracked.bool(True)
)

l1ctLayer1HGCalExtended = l1ctLayer1HGCal.clone(tracks = cms.InputTag('pfTracksFromL1TracksExtended'))

l1ctLayer1HGCalNoTK = cms.EDProducer("L1TCorrelatorLayer1Producer",
tracks = cms.InputTag(''),
Expand Down Expand Up @@ -469,6 +472,15 @@
regionalLabelsToMerge = cms.vstring("Puppi"),
)

l1ctLayer1Extended = l1ctLayer1.clone(
pfProducers = cms.VInputTag(
cms.InputTag("l1ctLayer1BarrelExtended"),
cms.InputTag("l1ctLayer1HGCalExtended"),
cms.InputTag("l1ctLayer1HGCalNoTK"),
cms.InputTag("l1ctLayer1HF")
)
)

l1ctLayer1EG = cms.EDProducer(
"L1TEGMultiMerger",
tkElectrons=cms.VPSet(
Expand Down Expand Up @@ -516,14 +528,18 @@
pfClustersFromCombinedCaloHCal,
pfClustersFromCombinedCaloHF,
pfClustersFromHGC3DClusters,
pfTracksFromL1Tracks
pfTracksFromL1Tracks,
pfTracksFromL1TracksExtended
)

l1ctLayer1Task = cms.Task(
l1ctLayer1Barrel,
l1ctLayer1BarrelExtended,
l1ctLayer1HGCal,
l1ctLayer1HGCalExtended,
l1ctLayer1HGCalNoTK,
l1ctLayer1HF,
l1ctLayer1,
l1ctLayer1Extended,
l1ctLayer1EG
)
Loading

0 comments on commit 8aaee45

Please sign in to comment.