From 6271c2eb05e096860932b20c77413ec2f93be371 Mon Sep 17 00:00:00 2001 From: violatingcp Date: Tue, 26 Mar 2019 05:07:15 -0500 Subject: [PATCH 1/3] Added NN Tau --- .../Phase2L1ParticleFlow/interface/PFTau.h | 34 +++++++ DataFormats/Phase2L1ParticleFlow/src/PFTau.cc | 9 ++ .../Phase2L1ParticleFlow/src/classes.h | 3 + .../Phase2L1ParticleFlow/src/classes_def.xml | 4 + L1Trigger/Phase2L1Taus/BuildFile.xml | 3 + .../Phase2L1Taus/interface/L1NNTauProducer.hh | 41 +++++++++ .../Phase2L1Taus/interface/L1PFTauProducer.hh | 3 +- L1Trigger/Phase2L1Taus/interface/TauMapper.h | 5 +- .../Phase2L1Taus/plugins/L1NNTauProducer.cc | 92 +++++++++++++++++++ .../Phase2L1Taus/plugins/L1PFTauProducer.cc | 10 +- 10 files changed, 197 insertions(+), 7 deletions(-) create mode 100644 DataFormats/Phase2L1ParticleFlow/interface/PFTau.h create mode 100644 DataFormats/Phase2L1ParticleFlow/src/PFTau.cc create mode 100644 L1Trigger/Phase2L1Taus/interface/L1NNTauProducer.hh create mode 100644 L1Trigger/Phase2L1Taus/plugins/L1NNTauProducer.cc diff --git a/DataFormats/Phase2L1ParticleFlow/interface/PFTau.h b/DataFormats/Phase2L1ParticleFlow/interface/PFTau.h new file mode 100644 index 0000000000000..e3dd3eeb3fd94 --- /dev/null +++ b/DataFormats/Phase2L1ParticleFlow/interface/PFTau.h @@ -0,0 +1,34 @@ +#ifndef DataFormats_Phase2L1ParticleFlow_PFTau_h +#define DataFormats_Phase2L1ParticleFlow_PFTau_h + +#include +#include "DataFormats/L1Trigger/interface/L1Candidate.h" + +namespace l1t +{ + + class PFTau : public L1Candidate { + public: + PFTau() {} + enum { unidentified=0, oneprong=1, oneprongpi0=2, threeprong=3}; + PFTau(const LorentzVector & p, float iso=-1, float fulliso=-1, int id=0,int hwpt=0, int hweta=0, int hwphi=0) : + PFTau(PolarLorentzVector(p), iso, id, hwpt, hweta, hwphi) {} + PFTau(const PolarLorentzVector & p, float iso=-1, float fulliso=-1,int id=0, int hwpt=0, int hweta=0, int hwphi=0) ; + float chargedIso() const { return iso_; } + float fullIso() const { return fullIso_; } + int id() const { return id_; } + bool passLooseNN() const { return iso_*(0.1+0.2*(min(pt,100)))*1./20.1 > 0.05;} + bool passLoosePF() const { return fullIso_ < 10.0;} + bool passTightNN() const { return iso_*(0.1+0.2*(min(pt,100)))*1./20.1 > 0.25;} + bool passTightPF() const { return fullIso_ < 5.0;} + + private: + float iso_; + float fullIso_; + int id_; + }; + + typedef std::vector PFTauCollection; +} +#endif + diff --git a/DataFormats/Phase2L1ParticleFlow/src/PFTau.cc b/DataFormats/Phase2L1ParticleFlow/src/PFTau.cc new file mode 100644 index 0000000000000..bfcd994ad9bd7 --- /dev/null +++ b/DataFormats/Phase2L1ParticleFlow/src/PFTau.cc @@ -0,0 +1,9 @@ +#include "DataFormats/Phase2L1ParticleFlow/interface/PFTau.h" + +l1t::PFTau::PFTau(const PolarLorentzVector & p, float iso, float fulliso, int id, int hwpt, int hweta, int hwphi) : + L1Candidate(p, hwpt, hweta, hwphi, /*hwQuality=*/int(0)), + iso_(iso), + fullIso_(fulliso), + id_(id) { + } + diff --git a/DataFormats/Phase2L1ParticleFlow/src/classes.h b/DataFormats/Phase2L1ParticleFlow/src/classes.h index 9d459a650e03b..015e993b5bacb 100644 --- a/DataFormats/Phase2L1ParticleFlow/src/classes.h +++ b/DataFormats/Phase2L1ParticleFlow/src/classes.h @@ -5,6 +5,7 @@ #include "DataFormats/Phase2L1ParticleFlow/interface/PFTrack.h" #include "DataFormats/Phase2L1ParticleFlow/interface/PFCandidate.h" #include "DataFormats/Phase2L1ParticleFlow/interface/PFJet.h" +#include "DataFormats/Phase2L1ParticleFlow/interface/PFTau.h" namespace DataFormats_Phase2L1ParticleFlow { @@ -13,11 +14,13 @@ namespace DataFormats_Phase2L1ParticleFlow { l1t::PFTrack l1trk; l1t::PFCandidate l1pfc; l1t::PFJet l1pfj; + l1t::PFTau l1pft; edm::Wrapper wl1PFClusterCollection; edm::Wrapper wl1PFTrackCollection; edm::Wrapper wl1PFCandidateCollection; edm::Wrapper wl1PFJetCollection; + edm::Wrapper wl1PFTauCollection; }; } diff --git a/DataFormats/Phase2L1ParticleFlow/src/classes_def.xml b/DataFormats/Phase2L1ParticleFlow/src/classes_def.xml index ebe2fdc614950..45e4add773eec 100644 --- a/DataFormats/Phase2L1ParticleFlow/src/classes_def.xml +++ b/DataFormats/Phase2L1ParticleFlow/src/classes_def.xml @@ -27,6 +27,10 @@ + + + + diff --git a/L1Trigger/Phase2L1Taus/BuildFile.xml b/L1Trigger/Phase2L1Taus/BuildFile.xml index b7023a5315e46..34d1a516ec1cb 100644 --- a/L1Trigger/Phase2L1Taus/BuildFile.xml +++ b/L1Trigger/Phase2L1Taus/BuildFile.xml @@ -4,9 +4,12 @@ + + + diff --git a/L1Trigger/Phase2L1Taus/interface/L1NNTauProducer.hh b/L1Trigger/Phase2L1Taus/interface/L1NNTauProducer.hh new file mode 100644 index 0000000000000..9f09e0b5a3b7c --- /dev/null +++ b/L1Trigger/Phase2L1Taus/interface/L1NNTauProducer.hh @@ -0,0 +1,41 @@ +#ifndef L1TRIGGER_PHASE2L1TAU_L1NNTAU_H +#define L1TRIGGER_PHASE2L1TAU_L1NNTAU_H + +#include +#include +#include + + +#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 "DataFormats/Phase2L1ParticleFlow/interface/PFTau.h" +#include "DataFormats/Phase2L1ParticleFlow/interface/PFCandidate.h" +#include "L1Trigger/Phase2L1Taus/interface/TauNNId.h" + +using namespace l1t; + +class L1NNTauProducer : public edm::stream::EDProducer<> { +public: + explicit L1NNTauProducer(const edm::ParameterSet&); + ~L1NNTauProducer(); + +private: + TauNNId *fTauNNId; + void addTau(l1t::PFCandidate &iCand,const l1t::PFCandidateCollection &iParts, std::unique_ptr &outputTaus); + float deltaR(auto iPart1, auto iPart2); + virtual void produce( edm::Event& iEvent, const edm::EventSetup& iSetup ) override; + + double fSeedPt; + double fConeSize; + double fTauSize; + int fMaxTaus; + int fNParticles; + edm::EDGetTokenT< vector > fL1PFToken; +}; + + +#endif diff --git a/L1Trigger/Phase2L1Taus/interface/L1PFTauProducer.hh b/L1Trigger/Phase2L1Taus/interface/L1PFTauProducer.hh index 22d8d4cf0a811..5ad4fc9eb99b7 100644 --- a/L1Trigger/Phase2L1Taus/interface/L1PFTauProducer.hh +++ b/L1Trigger/Phase2L1Taus/interface/L1PFTauProducer.hh @@ -45,6 +45,7 @@ #include "Geometry/HcalTowerAlgo/interface/HcalTrigTowerGeometry.h" #include "Geometry/HcalTowerAlgo/interface/HcalGeometry.h" +#include "DataFormats/Phase2L1ParticleFlow/interface/PFTau.h" #include "DataFormats/L1Trigger/interface/L1PFTau.h" #include "DataFormats/Phase2L1ParticleFlow/interface/PFCandidate.h" #include "L1Trigger/Phase2L1Taus/interface/TauMapper.h" @@ -95,7 +96,7 @@ class L1PFTauProducer : public edm::EDProducer { tauMapperCollection tauCandidates; void createTaus(tauMapperCollection &inputCollection); - void tau_cand_sort(tauMapperCollection tauCandidates, std::unique_ptr &newL1PFTauCollection, unsigned int nCands); + void tau_cand_sort(tauMapperCollection tauCandidates, std::unique_ptr &newL1PFTauCollection, unsigned int nCands); /// ///////////////// /// /// MANDATORY METHODS /// diff --git a/L1Trigger/Phase2L1Taus/interface/TauMapper.h b/L1Trigger/Phase2L1Taus/interface/TauMapper.h index 97cda20026bea..52b861920f36f 100644 --- a/L1Trigger/Phase2L1Taus/interface/TauMapper.h +++ b/L1Trigger/Phase2L1Taus/interface/TauMapper.h @@ -25,7 +25,7 @@ class TauMapper{ TauMapper(); ~TauMapper(); - + l1t::L1PFTau l1PFTau; typedef struct @@ -96,6 +96,8 @@ class TauMapper{ void ClearSeedHadron(){ seedHadronSet = false;; } + float sumEGIso; + private: bool seedHadronSet; @@ -104,7 +106,6 @@ class TauMapper{ l1t::PFCandidate prong3; float sumChargedIso; - float sumEGIso; float strip_pt; float strip_eta; float strip_phi; diff --git a/L1Trigger/Phase2L1Taus/plugins/L1NNTauProducer.cc b/L1Trigger/Phase2L1Taus/plugins/L1NNTauProducer.cc new file mode 100644 index 0000000000000..f780ab2ae3f58 --- /dev/null +++ b/L1Trigger/Phase2L1Taus/plugins/L1NNTauProducer.cc @@ -0,0 +1,92 @@ +#include "L1Trigger/Phase2L1Taus/interface/L1NNTauProducer.hh" + +L1NNTauProducer::L1NNTauProducer(const edm::ParameterSet& cfg) : + fSeedPt( cfg.getParameter("seedpt")), + fConeSize( cfg.getParameter("conesize")), + fTauSize( cfg.getParameter("tausize")), + fMaxTaus ( cfg.getParameter ("maxtaus")), + fNParticles ( cfg.getParameter ("nparticles")), + fL1PFToken( consumes< vector >(cfg.getParameter("L1PFObjects"))) { + std::string lNNFile = cfg.getParameter("NNFileName");//,"L1Trigger/Phase2L1Taus/data/tau_3layer.pb"); + fTauNNId = new TauNNId(); + if(lNNFile.find("v0") == std::string::npos) fTauNNId->initialize("input_1:0",lNNFile,fNParticles); + if(lNNFile.find("v0") != std::string::npos) fTauNNId->initialize("dense_1_input:0",lNNFile,fNParticles); + + produces< l1t::PFTauCollection >( "L1PFTausNN" ); +} +void L1NNTauProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) { + edm::Handle< l1t::PFCandidateCollection > l1PFCandidates; + iEvent.getByToken( fL1PFToken, l1PFCandidates); + + l1t::PFCandidateCollection pfChargedHadrons; + l1t::PFCandidateCollection pfChargedHadrons_sort; + l1t::PFCandidateCollection pfChargedHadrons_seeds; + //for(auto l1PFCand : *l1PFCandidates) if((l1PFCand.id() == l1t::PFCandidate::ChargedHadron || l1PFCand.id() == l1t::PFCandidate::Electron || l1PFCand.id() == l1t::PFCandidate::Photon) && fabs(l1PFCand.eta()) < 2.5) pfChargedHadrons_sort.push_back(l1PFCand); + for(auto l1PFCand : *l1PFCandidates) if((l1PFCand.id() == l1t::PFCandidate::ChargedHadron || l1PFCand.id() == l1t::PFCandidate::Electron) && fabs(l1PFCand.eta()) < 2.5) pfChargedHadrons_sort.push_back(l1PFCand); + std::sort(pfChargedHadrons_sort.begin(), pfChargedHadrons_sort.end(), [](l1t::PFCandidate i,l1t::PFCandidate j){return(i.pt() > j.pt());}); + auto lTaus = std::make_unique(); + if(pfChargedHadrons_sort.size() == 0) { + if(lTaus->size() == 0) { + PFTau dummy; + lTaus->push_back(dummy); + } + iEvent.put( std::move(lTaus) , "L1PFTausNN" ); + return; + } + pfChargedHadrons_seeds.push_back(pfChargedHadrons_sort[0]); + for(unsigned int i0 = 1; i0 < pfChargedHadrons_sort.size(); i0++) { + bool pMatch = false; + for(unsigned int i1 = 0; i1 < pfChargedHadrons_seeds.size(); i1++) { + if(deltaR(pfChargedHadrons_seeds[i1],pfChargedHadrons_sort[i0]) < 0.4) pMatch = true; + } + if(pMatch) continue; + pfChargedHadrons_seeds.push_back(pfChargedHadrons_sort[i0]); + if(int(pfChargedHadrons_seeds.size()) > fMaxTaus-1) break; + } + for(unsigned int i0 = 0; i0 < pfChargedHadrons_seeds.size(); i0++ ) { + addTau(pfChargedHadrons_seeds[i0],(*l1PFCandidates),lTaus); + } + if(lTaus->size() == 0) { + PFTau dummy; + lTaus->push_back(dummy); + } + std::sort(lTaus->begin(), lTaus->end(), [](l1t::PFTau i,l1t::PFTau j){return(i.pt() > j.pt());}); + iEvent.put( std::move(lTaus) , "L1PFTausNN" ); +} + +// create taus based on grid structure +void L1NNTauProducer::addTau(l1t::PFCandidate &iCand,const l1t::PFCandidateCollection &iParts, std::unique_ptr &outputTaus){ + l1t::PFCandidateCollection pfTauCands; + TLorentzVector lTot; lTot.SetPtEtaPhiM(0,0,0,0); + TLorentzVector lCand; lCand.SetPtEtaPhiM(0,0,0,0); + int lId = 0; + for(auto l1PFCand : iParts) { + if(deltaR(iCand,l1PFCand) > fConeSize) continue; + TLorentzVector pVec; pVec.SetPtEtaPhiM(l1PFCand.pt(),l1PFCand.eta(),l1PFCand.phi(),0); + lTot += pVec; + if(deltaR(iCand,l1PFCand) < fTauSize && (l1PFCand.id() == l1t::PFCandidate::Electron || l1PFCand.id() == l1t::PFCandidate::ChargedHadron || l1PFCand.id() == l1t::PFCandidate::Photon) ) { + lId++; + if( l1PFCand.id() == l1t::PFCandidate::Electron || l1PFCand.id() == l1t::PFCandidate::ChargedHadron ) lCand += pVec; + } + pfTauCands.push_back(l1PFCand); + } + if(lTot.Pt() < fSeedPt) return; + std::sort(pfTauCands.begin(), pfTauCands.end(), [](l1t::PFCandidate i,l1t::PFCandidate j){return(i.pt() > j.pt());}); + float NN = fTauNNId->compute(iCand,pfTauCands); + math::PtEtaPhiMLorentzVector tempP4(lCand.Pt(),lCand.Eta(),lCand.Phi(),lCand.M()); + l1t::PFTau l1PFTau(tempP4,NN,0,lId); + outputTaus->push_back(l1PFTau); +} +float L1NNTauProducer::deltaR(auto iPart1, auto iPart2){ + float delta_r = 20; + float pDPhi = fabs(iPart1.phi() - iPart2.phi()); + if(pDPhi > 2.*TMath::Pi()-pDPhi) pDPhi = 2.*TMath::Pi()-pDPhi; + delta_r = sqrt((iPart1.eta() - iPart2.eta())*(iPart1.eta() - iPart2.eta()) + pDPhi*pDPhi); + return delta_r; +} +L1NNTauProducer::~L1NNTauProducer(){} + + +#include "FWCore/Framework/interface/MakerMacros.h" +DEFINE_FWK_MODULE(L1NNTauProducer); + diff --git a/L1Trigger/Phase2L1Taus/plugins/L1PFTauProducer.cc b/L1Trigger/Phase2L1Taus/plugins/L1PFTauProducer.cc index b2092f5f62e80..e99d3a75588fb 100644 --- a/L1Trigger/Phase2L1Taus/plugins/L1PFTauProducer.cc +++ b/L1Trigger/Phase2L1Taus/plugins/L1PFTauProducer.cc @@ -1,3 +1,4 @@ + // -*- C++ -*- // // Package: L1PFTauProducer @@ -20,12 +21,12 @@ L1PFTauProducer::L1PFTauProducer(const edm::ParameterSet& cfg) : L1PFToken_( consumes< vector >(cfg.getParameter("L1PFObjects"))), L1NeutralToken_( consumes< vector >(cfg.getParameter("L1Neutrals")) ) { - produces< L1PFTauCollection >( "L1PFTaus" ).setBranchAlias("L1PFTaus"); + produces< l1t::PFTauCollection >( "L1PFTaus" ).setBranchAlias("L1PFTaus"); } void L1PFTauProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) { - std::unique_ptr newL1PFTauCollection(new L1PFTauCollection); + std::unique_ptr newL1PFTauCollection(new l1t::PFTauCollection); edm::Handle< l1t::PFCandidateCollection > l1PFCandidates; iEvent.getByToken( L1PFToken_, l1PFCandidates); @@ -111,11 +112,12 @@ void L1PFTauProducer::createTaus(tauMapperCollection &inputCollection){ } -void L1PFTauProducer::tau_cand_sort(tauMapperCollection tauCandidates, std::unique_ptr &newL1PFTauCollection, unsigned int nCands){ +void L1PFTauProducer::tau_cand_sort(tauMapperCollection tauCandidates, std::unique_ptr &newL1PFTauCollection, unsigned int nCands){ std::sort(tauCandidates.begin(), tauCandidates.end(), [](TauMapper i,TauMapper j){return(i.l1PFTau.pt() > j.l1PFTau.pt());}); for(unsigned int i = 0; i < nCands && i < tauCandidates.size(); i++){ - newL1PFTauCollection->push_back(tauCandidates.at(i).l1PFTau); + l1t::PFTau pTau(tauCandidates.at(i).l1PFTau.polarP4(),tauCandidates.at(i).l1PFTau.chargedIso(),tauCandidates.at(i).sumEGIso+tauCandidates.at(i).l1PFTau.chargedIso(),tauCandidates.at(i).l1PFTau.tauType()); + newL1PFTauCollection->push_back(pTau); } } From 6f70398eb564651cc24c11da7e16ead52c575330 Mon Sep 17 00:00:00 2001 From: violatingcp Date: Tue, 26 Mar 2019 05:32:04 -0500 Subject: [PATCH 2/3] Fixed small bug --- DataFormats/Phase2L1ParticleFlow/interface/PFTau.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DataFormats/Phase2L1ParticleFlow/interface/PFTau.h b/DataFormats/Phase2L1ParticleFlow/interface/PFTau.h index e3dd3eeb3fd94..fa35c8e702d0f 100644 --- a/DataFormats/Phase2L1ParticleFlow/interface/PFTau.h +++ b/DataFormats/Phase2L1ParticleFlow/interface/PFTau.h @@ -1,6 +1,7 @@ #ifndef DataFormats_Phase2L1ParticleFlow_PFTau_h #define DataFormats_Phase2L1ParticleFlow_PFTau_h +#include #include #include "DataFormats/L1Trigger/interface/L1Candidate.h" @@ -17,9 +18,9 @@ namespace l1t float chargedIso() const { return iso_; } float fullIso() const { return fullIso_; } int id() const { return id_; } - bool passLooseNN() const { return iso_*(0.1+0.2*(min(pt,100)))*1./20.1 > 0.05;} + bool passLooseNN() const { return iso_*(0.1+0.2*(min(pt(),100.)))*1./20.1 > 0.05;} bool passLoosePF() const { return fullIso_ < 10.0;} - bool passTightNN() const { return iso_*(0.1+0.2*(min(pt,100)))*1./20.1 > 0.25;} + bool passTightNN() const { return iso_*(0.1+0.2*(min(pt(),100.)))*1./20.1 > 0.25;} bool passTightPF() const { return fullIso_ < 5.0;} private: From eb27d31d80fd2ca1f9b0d8404e04f791680ee498 Mon Sep 17 00:00:00 2001 From: violatingcp Date: Tue, 16 Apr 2019 16:25:30 -0500 Subject: [PATCH 3/3] Adding missing files --- L1Trigger/Phase2L1Taus/interface/TauNNId.h | 31 ++++++++ .../python/L1NNTauProducer_cff.py | 22 ++++++ L1Trigger/Phase2L1Taus/src/TauNNId.cc | 75 ++++++++++++++++++ L1Trigger/Phase2L1Taus/test/test-NNTaus.py | 79 +++++++++++++++++++ 4 files changed, 207 insertions(+) create mode 100644 L1Trigger/Phase2L1Taus/interface/TauNNId.h create mode 100644 L1Trigger/Phase2L1Taus/python/L1NNTauProducer_cff.py create mode 100644 L1Trigger/Phase2L1Taus/src/TauNNId.cc create mode 100644 L1Trigger/Phase2L1Taus/test/test-NNTaus.py diff --git a/L1Trigger/Phase2L1Taus/interface/TauNNId.h b/L1Trigger/Phase2L1Taus/interface/TauNNId.h new file mode 100644 index 0000000000000..17a5078938961 --- /dev/null +++ b/L1Trigger/Phase2L1Taus/interface/TauNNId.h @@ -0,0 +1,31 @@ +#ifndef L1TRIGGER_PHASE2L1TAUS_TAUNNID_H +#define L1TRIGGER_PHASE2L1TAUS_TAuNNID_H + +#include +#include "PhysicsTools/TensorFlow/interface/TensorFlow.h" +#include "DataFormats/L1Trigger/interface/L1PFTau.h" +#include "DataFormats/Phase2L1ParticleFlow/interface/PFCandidate.h" + +class TauNNId { + public: + TauNNId(); + ~TauNNId(); + + void initialize(std::string iName, const std::string iWeightFile,int iNParticles); + void SetNNVectorVar(); + float EvaluateNN(); + float compute(l1t::PFCandidate &iSeed,l1t::PFCandidateCollection &iParts); + + std::string fInput; + int fNParticles; + float *fPt; + float *fEta; + float *fPhi; + float *fId; + + private: + tensorflow::Session* session; + tensorflow::GraphDef* graphDef; + std::vector NNvectorVar_; +}; +#endif diff --git a/L1Trigger/Phase2L1Taus/python/L1NNTauProducer_cff.py b/L1Trigger/Phase2L1Taus/python/L1NNTauProducer_cff.py new file mode 100644 index 0000000000000..f6f8fa2f37e8c --- /dev/null +++ b/L1Trigger/Phase2L1Taus/python/L1NNTauProducer_cff.py @@ -0,0 +1,22 @@ +import FWCore.ParameterSet.Config as cms + +L1NNTauProducer = cms.EDProducer("L1NNTauProducer", + seedpt = cms.double(20), + conesize = cms.double(0.4), + tausize = cms.double(0.1), + maxtaus = cms.int32(5), + nparticles = cms.int32(10), + L1PFObjects = cms.InputTag("L1PFProducer","l1pfCandidates"), + NNFileName = cms.string("L1Trigger/Phase2L1Taus/data/tau_3layer.pb") + ) + + +L1NNTauProducerPuppi = cms.EDProducer("L1NNTauProducer", + seedpt = cms.double(20), + conesize = cms.double(0.4), + tausize = cms.double(0.1), + maxtaus = cms.int32(5), + nparticles = cms.int32(10), + L1PFObjects = cms.InputTag("L1PFProducer","l1pfCandidates"), + NNFileName = cms.string("L1Trigger/Phase2L1Taus/data/tau_3layer_puppi.pb") + ) diff --git a/L1Trigger/Phase2L1Taus/src/TauNNId.cc b/L1Trigger/Phase2L1Taus/src/TauNNId.cc new file mode 100644 index 0000000000000..66f0bbf0adbf3 --- /dev/null +++ b/L1Trigger/Phase2L1Taus/src/TauNNId.cc @@ -0,0 +1,75 @@ +#include "L1Trigger/Phase2L1Taus/interface/TauNNId.h" +#include +#include +#include + +TauNNId::TauNNId(){ + NNvectorVar_.clear(); +} +TauNNId::~TauNNId() { + tensorflow::closeSession(session); + delete graphDef; +} +void TauNNId::initialize(std::string iInput, const std::string iWeightFile, int iNParticles){ + std::string cmssw_base_src = getenv("CMSSW_BASE"); + graphDef= tensorflow::loadGraphDef((cmssw_base_src+"/src/"+iWeightFile).c_str()); + session = tensorflow::createSession(graphDef); + fNParticles = iNParticles; + fPt = new float[fNParticles]; + fEta = new float[fNParticles]; + fPhi = new float[fNParticles]; + fId = new float[fNParticles]; + fInput = iInput; //tensorflow::run(session, { { "input_1:0",input } }, { "dense_4/Sigmoid:0" }, &outputs); +} +void TauNNId::SetNNVectorVar(){ + NNvectorVar_.clear(); + for(int i0 = 0; i0 < fNParticles; i0++) { + NNvectorVar_.push_back(fPt[i0]) ; //pT + NNvectorVar_.push_back(fEta[i0]) ; //dEta from jet axis + NNvectorVar_.push_back(fPhi[i0]) ; //dPhi from jet axis + if(fPt[i0] == 0) { + for(int i1 = 0; i1 < 5; i1++) NNvectorVar_.push_back(0); + continue; + } + fId[i0] == l1t::PFCandidate::Photon ? NNvectorVar_.push_back(1) : NNvectorVar_.push_back(0); //Photon + fId[i0] == l1t::PFCandidate::Electron ? NNvectorVar_.push_back(1) : NNvectorVar_.push_back(0); //Electron + fId[i0] == l1t::PFCandidate::Muon ? NNvectorVar_.push_back(1) : NNvectorVar_.push_back(0); //Muon + fId[i0] == l1t::PFCandidate::NeutralHadron ? NNvectorVar_.push_back(1) : NNvectorVar_.push_back(0); //Neutral Had + fId[i0] == l1t::PFCandidate::ChargedHadron ? NNvectorVar_.push_back(1) : NNvectorVar_.push_back(0); //Charged Had + } +} +float TauNNId::EvaluateNN(){ + tensorflow::Tensor input(tensorflow::DT_FLOAT, {1,(unsigned int)NNvectorVar_.size()});//was {1,35} but get size mismatch, CHECK + for (unsigned int i = 0; i < NNvectorVar_.size(); i++){ + //std::cout<<"i:"<()(0,i) = float(NNvectorVar_[i]); + } + std::vector outputs; + tensorflow::run(session, { { fInput,input } }, { "dense_4/Sigmoid:0" }, &outputs); + //std::cout << "===> result " << outputs[0].matrix()(0, 0) << std::endl; + float disc = outputs[0].matrix()(0, 0); + return disc; +}//end EvaluateNN + +float TauNNId::compute(l1t::PFCandidate &iSeed,l1t::PFCandidateCollection &iParts) { + for(int i0 = 0; i0 < fNParticles; i0++) { + fPt[i0] = 0; + fEta[i0] = 0; + fPhi[i0] = 0; + fId [i0] = 0; + } + std::sort(iParts.begin(), iParts.end(), [](l1t::PFCandidate i,l1t::PFCandidate j){return(i.pt() > j.pt());}); + for(unsigned int i0 = 0; i0 < iParts.size(); i0++) { + if(i0 > 10) break; + //std::cout << "===> " << i0 << " -- " << iParts[i0].pt() << std::endl; + fPt[i0] = iParts[i0].pt(); + fEta[i0] = iSeed.eta()-iParts[i0].eta(); + float lDPhi = iSeed.phi()-iParts[i0].phi(); + if(lDPhi > TMath::Pi()) lDPhi-=TMath::Pi(); + if(lDPhi < -TMath::Pi()) lDPhi+=TMath::Pi(); + fPhi[i0] = lDPhi; + fId[i0] = iParts[i0].id(); + } + SetNNVectorVar(); + return EvaluateNN(); +} diff --git a/L1Trigger/Phase2L1Taus/test/test-NNTaus.py b/L1Trigger/Phase2L1Taus/test/test-NNTaus.py new file mode 100644 index 0000000000000..a0df23980501a --- /dev/null +++ b/L1Trigger/Phase2L1Taus/test/test-NNTaus.py @@ -0,0 +1,79 @@ +# Auto generated configuration file +# using: +# Revision: 1.19 +# Source: /local/reps/CMSSW/CMSSW/Configuration/Applications/python/ConfigBuilder.py,v +# with command line options: step2 --python_filename=rerun_step2_L1_onMCL1_FEVTHLTDEBUG.py --no_exec -s L1 --datatier GEN-SIM-DIGI-RAW -n 1 --era Phase2_timing --eventcontent FEVTDEBUGHLT --filein file:/afs/cern.ch/user/r/rekovic/release/CMSSW_9_3_2/src/step2_DIGI_PU200_10ev.root --conditions 93X_upgrade2023_realistic_v2 --beamspot HLLHC14TeV --geometry Extended2023D17 --fileout file:step2_ZEE_PU200_1ev_rerun-L1-L1Ntuple.root --customise=L1Trigger/L1TNtuples/customiseL1Ntuple.L1NtupleEMU +import FWCore.ParameterSet.Config as cms +from Configuration.StandardSequences.Eras import eras + +process = cms.Process("IN2", eras.Phase2C4_trigger) +process.load('Configuration.StandardSequences.Services_cff') +process.load('Configuration.EventContent.EventContent_cff') +process.load('Configuration.Geometry.GeometryExtended2023D35Reco_cff') +process.load('Configuration.Geometry.GeometryExtended2023D35_cff') +process.load('Configuration.StandardSequences.MagneticField_cff') +process.load('Configuration.StandardSequences.EndOfProcess_cff') +process.load('SimCalorimetry.HcalTrigPrimProducers.hcaltpdigi_cff') +process.load('CalibCalorimetry.CaloTPG.CaloTPGTranscoder_cfi') +process.load('Configuration.StandardSequences.SimL1Emulator_cff') +process.load('L1Trigger.TrackFindingTracklet.L1TrackletTracks_cff') +process.VertexProducer.l1TracksInputTag = cms.InputTag("TTTracksFromTracklet", "Level1TTTracks") + +process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff') +from Configuration.AlCa.GlobalTag import GlobalTag +process.GlobalTag = GlobalTag(process.GlobalTag, '103X_upgrade2023_realistic_v2', '') + +process.maxEvents = cms.untracked.PSet( + input = cms.untracked.int32(-1) +) + +# Input source +process.source = cms.Source("PoolSource", + duplicateCheckMode = cms.untracked.string("noDuplicateCheck"), + fileNames = cms.untracked.vstring( + *( + '/store/cmst3/group/monojet/trigger/GGH/inputs_753710142.root', + '/store/cmst3/group/monojet/trigger/GGH/inputs_753710413.root') ), + inputCommands = cms.untracked.vstring("keep *", + "drop l1tHGCalTowerMapBXVector_hgcalTriggerPrimitiveDigiProducer_towerMap_HLT", + "drop *_hgcalTriggerPrimitiveDigiProducer_*_*", + "drop l1tEMTFHit2016Extras_simEmtfDigis_CSC_HLT", + "drop l1tEMTFHit2016Extras_simEmtfDigis_RPC_HLT", + "drop l1tEMTFHit2016s_simEmtfDigis__HLT", + "drop l1tEMTFTrack2016Extras_simEmtfDigis__HLT", + "drop l1tEMTFTrack2016s_simEmtfDigis__HLT") + ) +process.options = cms.untracked.PSet( +) + +# Production Info +process.configurationMetadata = cms.untracked.PSet( + annotation = cms.untracked.string('step2 nevts:1'), + name = cms.untracked.string('Applications'), + version = cms.untracked.string('$Revision: 1.19 $') +) +process.load("L1Trigger.Phase2L1Taus.L1NNTauProducer_cff") +process.L1NNTauProducer.L1PFObjects = cms.InputTag("l1pfCandidates","PF") +process.load("L1Trigger.Phase2L1Taus.L1PFTauProducer_cff") +process.L1PFTauProducer.L1PFObjects = cms.InputTag("l1pfCandidates","PF") +process.L1NNTauProducerPuppi = process.L1NNTauProducer.clone() +process.L1NNTauProducerPuppi.L1PFObjects = cms.InputTag("l1pfCandidates","Puppi") +process.L1NNTauProducerPuppi.NNFileName = cms.string("L1Trigger/Phase2L1Taus/data/tau_3layer_puppi.pb") + +process.p = cms.Path( + #process.L1TrackletTracks + + #process.SimL1Emulator + + process.L1PFTauProducer + + process.L1NNTauProducer + + process.L1PFTauProducerPuppi +) +process.out = cms.OutputModule("PoolOutputModule", + fileName = cms.untracked.string("tau104X.root"), + outputCommands = cms.untracked.vstring( + 'keep *_*_*_IN2', + #"keep *_*_L1PFTau*_*", + "keep *_genParticles_*_*", + ), + ) +process.e = cms.EndPath(process.out) +process.schedule = cms.Schedule([process.p,process.e])