Skip to content

Commit

Permalink
Merge pull request #8 from violatingcp/L1PFTau_10_5_X_v4
Browse files Browse the repository at this point in the history
Added NN Tau
  • Loading branch information
gpetruc authored May 9, 2019
2 parents f370a7f + eb27d31 commit 23d1834
Show file tree
Hide file tree
Showing 14 changed files with 405 additions and 7 deletions.
35 changes: 35 additions & 0 deletions DataFormats/Phase2L1ParticleFlow/interface/PFTau.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef DataFormats_Phase2L1ParticleFlow_PFTau_h
#define DataFormats_Phase2L1ParticleFlow_PFTau_h

#include <algorithm>
#include <vector>
#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<l1t::PFTau> PFTauCollection;
}
#endif

9 changes: 9 additions & 0 deletions DataFormats/Phase2L1ParticleFlow/src/PFTau.cc
Original file line number Diff line number Diff line change
@@ -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) {
}

3 changes: 3 additions & 0 deletions DataFormats/Phase2L1ParticleFlow/src/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -13,11 +14,13 @@ namespace DataFormats_Phase2L1ParticleFlow {
l1t::PFTrack l1trk;
l1t::PFCandidate l1pfc;
l1t::PFJet l1pfj;
l1t::PFTau l1pft;

edm::Wrapper<l1t::PFClusterCollection> wl1PFClusterCollection;
edm::Wrapper<l1t::PFTrackCollection> wl1PFTrackCollection;
edm::Wrapper<l1t::PFCandidateCollection> wl1PFCandidateCollection;
edm::Wrapper<l1t::PFJetCollection> wl1PFJetCollection;
edm::Wrapper<l1t::PFTauCollection> wl1PFTauCollection;

};
}
Expand Down
4 changes: 4 additions & 0 deletions DataFormats/Phase2L1ParticleFlow/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<class name="l1t::PFJetCollection" />
<class name="edm::Wrapper<l1t::PFJetCollection>" />

<class name="l1t::PFTau" />
<class name="l1t::PFTauCollection" />
<class name="edm::Wrapper<l1t::PFTauCollection>" />


</lcgdict>

3 changes: 3 additions & 0 deletions L1Trigger/Phase2L1Taus/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
<use name="DataFormats/L1Trigger"/>
<use name="DataFormats/L1CaloTrigger"/>
<use name="DataFormats/Phase2L1CaloTrig"/>
<use name="DataFormats/Phase2L1ParticleFlow"/>
<use name="DataFormats/EcalDigi"/>
<use name="DataFormats/L1TrackTrigger"/>
<use name="Geometry/HcalTowerAlgo"/>
<use name="tensorflow-cc"/>
<use name="PhysicsTools/TensorFlow"/>
<export>
<lib name="1"/>
</export>
41 changes: 41 additions & 0 deletions L1Trigger/Phase2L1Taus/interface/L1NNTauProducer.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef L1TRIGGER_PHASE2L1TAU_L1NNTAU_H
#define L1TRIGGER_PHASE2L1TAU_L1NNTAU_H

#include <iostream>
#include <vector>
#include <TLorentzVector.h>


#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<PFTauCollection> &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<l1t::PFCandidate> > fL1PFToken;
};


#endif
3 changes: 2 additions & 1 deletion L1Trigger/Phase2L1Taus/interface/L1PFTauProducer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -95,7 +96,7 @@ class L1PFTauProducer : public edm::EDProducer {
tauMapperCollection tauCandidates;

void createTaus(tauMapperCollection &inputCollection);
void tau_cand_sort(tauMapperCollection tauCandidates, std::unique_ptr<L1PFTauCollection> &newL1PFTauCollection, unsigned int nCands);
void tau_cand_sort(tauMapperCollection tauCandidates, std::unique_ptr<PFTauCollection> &newL1PFTauCollection, unsigned int nCands);

/// ///////////////// ///
/// MANDATORY METHODS ///
Expand Down
5 changes: 3 additions & 2 deletions L1Trigger/Phase2L1Taus/interface/TauMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TauMapper{
TauMapper();

~TauMapper();

l1t::L1PFTau l1PFTau;

typedef struct
Expand Down Expand Up @@ -96,6 +96,8 @@ class TauMapper{
void ClearSeedHadron(){
seedHadronSet = false;;
}
float sumEGIso;

private:

bool seedHadronSet;
Expand All @@ -104,7 +106,6 @@ class TauMapper{
l1t::PFCandidate prong3;

float sumChargedIso;
float sumEGIso;
float strip_pt;
float strip_eta;
float strip_phi;
Expand Down
31 changes: 31 additions & 0 deletions L1Trigger/Phase2L1Taus/interface/TauNNId.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef L1TRIGGER_PHASE2L1TAUS_TAUNNID_H
#define L1TRIGGER_PHASE2L1TAUS_TAuNNID_H

#include <string>
#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<float> NNvectorVar_;
};
#endif
92 changes: 92 additions & 0 deletions L1Trigger/Phase2L1Taus/plugins/L1NNTauProducer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "L1Trigger/Phase2L1Taus/interface/L1NNTauProducer.hh"

L1NNTauProducer::L1NNTauProducer(const edm::ParameterSet& cfg) :
fSeedPt( cfg.getParameter<double>("seedpt")),
fConeSize( cfg.getParameter<double>("conesize")),
fTauSize( cfg.getParameter<double>("tausize")),
fMaxTaus ( cfg.getParameter<int> ("maxtaus")),
fNParticles ( cfg.getParameter<int> ("nparticles")),
fL1PFToken( consumes< vector<l1t::PFCandidate> >(cfg.getParameter<edm::InputTag>("L1PFObjects"))) {
std::string lNNFile = cfg.getParameter<std::string>("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<l1t::PFTauCollection>();
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<l1t::PFTauCollection> &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);

10 changes: 6 additions & 4 deletions L1Trigger/Phase2L1Taus/plugins/L1PFTauProducer.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

// -*- C++ -*-
//
// Package: L1PFTauProducer
Expand All @@ -20,12 +21,12 @@ L1PFTauProducer::L1PFTauProducer(const edm::ParameterSet& cfg) :
L1PFToken_( consumes< vector<l1t::PFCandidate> >(cfg.getParameter<edm::InputTag>("L1PFObjects"))),
L1NeutralToken_( consumes< vector<l1t::PFCandidate> >(cfg.getParameter<edm::InputTag>("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<L1PFTauCollection> newL1PFTauCollection(new L1PFTauCollection);
std::unique_ptr<l1t::PFTauCollection> newL1PFTauCollection(new l1t::PFTauCollection);

edm::Handle< l1t::PFCandidateCollection > l1PFCandidates;
iEvent.getByToken( L1PFToken_, l1PFCandidates);
Expand Down Expand Up @@ -111,11 +112,12 @@ void L1PFTauProducer::createTaus(tauMapperCollection &inputCollection){
}


void L1PFTauProducer::tau_cand_sort(tauMapperCollection tauCandidates, std::unique_ptr<L1PFTauCollection> &newL1PFTauCollection, unsigned int nCands){
void L1PFTauProducer::tau_cand_sort(tauMapperCollection tauCandidates, std::unique_ptr<l1t::PFTauCollection> &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);
}

}
Expand Down
22 changes: 22 additions & 0 deletions L1Trigger/Phase2L1Taus/python/L1NNTauProducer_cff.py
Original file line number Diff line number Diff line change
@@ -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")
)
Loading

0 comments on commit 23d1834

Please sign in to comment.