Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving leptonSF code to compiled class #110

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added data/leptonSF2017/RunBCDEF_SF_ID.root
Binary file not shown.
Binary file added data/leptonSF2017/RunBCDEF_SF_ID_syst.root
Binary file not shown.
Binary file added data/leptonSF2017/RunBCDEF_SF_ISO.root
Binary file not shown.
Binary file added data/leptonSF2017/RunBCDEF_SF_ISO_syst.root
Binary file not shown.
Binary file added data/leptonSF2017/RunBCDEF_data_ID.root
Binary file not shown.
Binary file added data/leptonSF2017/RunBCDEF_data_ISO.root
Binary file not shown.
Binary file added data/leptonSF2017/RunBCDEF_mc_ID.root
Binary file not shown.
Binary file added data/leptonSF2017/RunBCDEF_mc_ISO.root
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
33 changes: 33 additions & 0 deletions interface/LeptonEfficiencyCorrectorCppWorker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef PhysicsTools_NanoAODTools_LeptonEfficiencyCorrectorCppWorker_h
#define PhysicsTools_NanoAODTools_LeptonEfficiencyCorrectorCppWorker_h

#include <iostream>
#include <string>
#include <vector>
#include <TH2.h>
#include <TFile.h>

#include "PhysicsTools/NanoAODTools/interface/WeightCalculatorFromHistogram.h"

class LeptonEfficiencyCorrectorCppWorker {
public:

LeptonEfficiencyCorrectorCppWorker() {effmaps_.clear();}
LeptonEfficiencyCorrectorCppWorker(std::vector<std::string> files, std::vector<std::string> histos);
~LeptonEfficiencyCorrectorCppWorker() {}

void setLeptons(int nLep, int *lepPdgId, float *lepPt, float *lepEta);

float getSF(int pdgid, float pt, float eta);
float getSFErr(unsigned type, int pdgid, float pt, float eta);
const std::vector<float> & run();

private:
std::vector<TH2F*> effmaps_;
std::vector<float> ret_;
int nLep_;
float *Lep_eta_, *Lep_pt_;
int *Lep_pdgId_;
};

#endif
75 changes: 54 additions & 21 deletions python/postprocessing/modules/common/lepSFProducer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# -----------------------------------------------------------
#Updated by AMMagnan -- compiled version that works in crab + include other WP
#@FIXME: just doing loose and tight, still needs extending to other WP and proper error implementation...
# --- 24/10/2018.
# -----------------------------------------------------------
import ROOT
import os
import numpy as np
Expand All @@ -7,17 +12,41 @@
from PhysicsTools.NanoAODTools.postprocessing.framework.eventloop import Module

class lepSFProducer(Module):
def __init__(self, muonSelectionTag, electronSelectionTag):
if muonSelectionTag=="LooseWP_2016":
mu_f=["Mu_Trg.root","Mu_ID.root","Mu_Iso.root"]
mu_h = ["IsoMu24_OR_IsoTkMu24_PtEtaBins/pt_abseta_ratio",
"MC_NUM_LooseID_DEN_genTracks_PAR_pt_eta/pt_abseta_ratio",
"LooseISO_LooseID_pt_eta/pt_abseta_ratio"]
if electronSelectionTag=="GPMVA90_2016":
el_f = ["EGM2D_eleGSF.root","EGM2D_eleMVA90.root"]
el_h = ["EGamma_SF2D", "EGamma_SF2D"]
mu_f = ["%s/src/PhysicsTools/NanoAODTools/python/postprocessing/data/leptonSF/" % os.environ['CMSSW_BASE'] + f for f in mu_f]
el_f = ["%s/src/PhysicsTools/NanoAODTools/python/postprocessing/data/leptonSF/" % os.environ['CMSSW_BASE'] + f for f in el_f]
def __init__(self, era, muonSelectionTag, electronSelectionTag):

self.era = era
self.inputFilePath = os.environ['CMSSW_BASE'] + "/src/PhysicsTools/NanoAODTools/data/leptonSF" + self.era + "/"

self.muonSelectionTag = muonSelectionTag
self.electronSelectionTag = electronSelectionTag

mu_f=["RunBCDEF_SF_ID.root","RunBCDEF_SF_ISO.root"]
if "Loose" in muonSelectionTag:
mu_h = ["NUM_LooseID_DEN_genTracks_pt_abseta","NUM_LooseRelIso_DEN_LooseID_pt_abseta"]
elif "Tight" in muonSelectionTag:
mu_h = ["NUM_TightID_DEN_genTracks_pt_abseta","NUM_TightRelIso_DEN_TightIDandIPCut_pt_abseta"]


if "withTrg" in muonSelectionTag:
mu_f.append("Mu_Trg.root")
mu_h.append("IsoMu24_OR_IsoTkMu24_PtEtaBins/pt_abseta_ratio")

el_f = ["egammaEffi.txt_EGM2D_runBCDEF_passingRECO.root","egammaEffi.txt_EGM2D_runBCDEF_passingRECO_lowEt.root"]
el_h = ["EGamma_SF2D","EGamma_SF2D"]

if electronSelectionTag=="Veto":
el_f.append("egammaEffi.txt_EGM2D_runBCDEF_passingVeto94X.root")
el_h.append("EGamma_SF2D")

if electronSelectionTag=="Tight":
el_f.append("egammaEffi.txt_EGM2D_runBCDEF_passingTight94X.root")
el_h.append("EGamma_SF2D")




mu_f = [self.inputFilePath + f for f in mu_f]
el_f = [self.inputFilePath + f for f in el_f]

self.mu_f = ROOT.std.vector(str)(len(mu_f))
self.mu_h = ROOT.std.vector(str)(len(mu_f))
Expand All @@ -26,18 +55,20 @@ def __init__(self, muonSelectionTag, electronSelectionTag):
self.el_h = ROOT.std.vector(str)(len(el_f))
for i in range(len(el_f)): self.el_f[i] = el_f[i]; self.el_h[i] = el_h[i];

if "/LeptonEfficiencyCorrector_cc.so" not in ROOT.gSystem.GetLibraries():
print "Load C++ Worker"
ROOT.gROOT.ProcessLine(".L %s/src/PhysicsTools/NanoAODTools/python/postprocessing/helpers/LeptonEfficiencyCorrector.cc+" % os.environ['CMSSW_BASE'])
for library in [ "libCondFormatsJetMETObjects", "libPhysicsToolsNanoAODTools" ]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we checking for METObjects?

if library not in ROOT.gSystem.GetLibraries():
print("Load Library '%s'" % library.replace("lib", ""))
ROOT.gSystem.Load(library)

def beginJob(self):
self._worker_mu = ROOT.LeptonEfficiencyCorrector(self.mu_f,self.mu_h)
self._worker_el = ROOT.LeptonEfficiencyCorrector(self.el_f,self.el_h)
self._worker_mu = ROOT.LeptonEfficiencyCorrectorCppWorker(self.mu_f,self.mu_h)
self._worker_el = ROOT.LeptonEfficiencyCorrectorCppWorker(self.el_f,self.el_h)
def endJob(self):
pass
def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
self.out = wrappedOutputTree
self.out.branch("Muon_effSF", "F", lenVar="nMuon")
self.out.branch("Electron_effSF", "F", lenVar="nElectron")
self.out.branch("Muon_effSF_%s"%(self.muonSelectionTag), "F", lenVar="nMuon")
self.out.branch("Electron_effSF_%s"%(self.electronSelectionTag), "F", lenVar="nElectron")
def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
pass
def analyze(self, event):
Expand All @@ -46,11 +77,13 @@ def analyze(self, event):
electrons = Collection(event, "Electron")
sf_el = [ self._worker_el.getSF(el.pdgId,el.pt,el.eta) for el in electrons ]
sf_mu = [ self._worker_mu.getSF(mu.pdgId,mu.pt,mu.eta) for mu in muons ]
self.out.fillBranch("Muon_effSF", sf_mu)
self.out.fillBranch("Electron_effSF", sf_el)
self.out.fillBranch("Muon_effSF_%s"%(self.muonSelectionTag), sf_mu)
self.out.fillBranch("Electron_effSF_%s"%(self.electronSelectionTag), sf_el)
return True

# define modules using the syntax 'name = lambda : constructor' to avoid having them loaded when not needed

lepSF = lambda : lepSFProducer( "LooseWP_2016", "GPMVA90_2016")
lepSFveto = lambda : lepSFProducer( '2017', 'Loose', 'Veto')
#lepSFvetotrig = lambda : lepSFProducer( '2017', 'Loose_withTrg', 'Veto')
lepSFtight = lambda : lepSFProducer( '2017', 'Tight', 'Tight')

Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
#ifndef PhysicsTools_NanoAODTools_LeptonEfficiencyCorrector_h
#define PhysicsTools_NanoAODTools_LeptonEfficiencyCorrector_h
#include "PhysicsTools/NanoAODTools/interface/LeptonEfficiencyCorrectorCppWorker.h"

#include <iostream>
#include <string>
#include <vector>
#include <TH2.h>
#include <TFile.h>

#include "PhysicsTools/NanoAODTools/src/WeightCalculatorFromHistogram.cc"

class LeptonEfficiencyCorrector {
public:

LeptonEfficiencyCorrector() {effmaps_.clear();}
LeptonEfficiencyCorrector(std::vector<std::string> files, std::vector<std::string> histos);
~LeptonEfficiencyCorrector() {}

void setLeptons(int nLep, int *lepPdgId, float *lepPt, float *lepEta);

float getSF(int pdgid, float pt, float eta);
float getSFErr(int pdgid, float pt, float eta);
const std::vector<float> & run();

private:
std::vector<TH2F*> effmaps_;
std::vector<float> ret_;
int nLep_;
float *Lep_eta_, *Lep_pt_;
int *Lep_pdgId_;
};

LeptonEfficiencyCorrector:: LeptonEfficiencyCorrector(std::vector<std::string> files, std::vector<std::string> histos) {
LeptonEfficiencyCorrectorCppWorker::LeptonEfficiencyCorrectorCppWorker(std::vector<std::string> files, std::vector<std::string> histos) {
effmaps_.clear();
if(files.size()!=histos.size()) {
std::cout << "ERROR! There should be one histogram per input file! Returning 0 as SF." << std::endl;
Expand All @@ -56,11 +26,11 @@ LeptonEfficiencyCorrector:: LeptonEfficiencyCorrector(std::vector<std::string> f
}
}

void LeptonEfficiencyCorrector::setLeptons(int nLep, int *lepPdgId, float *lepPt, float *lepEta) {
void LeptonEfficiencyCorrectorCppWorker::setLeptons(int nLep, int *lepPdgId, float *lepPt, float *lepEta) {
nLep_ = nLep; Lep_pdgId_ = lepPdgId; Lep_pt_ = lepPt; Lep_eta_ = lepEta;
}

float LeptonEfficiencyCorrector::getSF(int pdgid, float pt, float eta) {
float LeptonEfficiencyCorrectorCppWorker::getSF(int pdgid, float pt, float eta) {
float out=1.;
float x = abs(pdgid)==13 ? pt : eta;
float y = abs(pdgid)==13 ? fabs(eta) : pt;
Expand All @@ -71,23 +41,25 @@ float LeptonEfficiencyCorrector::getSF(int pdgid, float pt, float eta) {
return out;
}

float LeptonEfficiencyCorrector::getSFErr(int pdgid, float pt, float eta) {
float out=1.;
float LeptonEfficiencyCorrectorCppWorker::getSFErr(unsigned type, int pdgid, float pt, float eta) {
float out=0.;
float x = pt;
float y = abs(pdgid)==13 ? fabs(eta) : eta;
for(std::vector<TH2F*>::iterator hist=effmaps_.begin(); hist<effmaps_.end(); ++hist) {
WeightCalculatorFromHistogram wc(*hist);
out *= wc.getWeightErr(x,y);
if (type >= effmaps_.size()) {
std::cout << " Error, asking error type " << type << " out-of-bound, max size is " << effmaps_.size() << std::endl;
return 0;
}
TH2F* hist=effmaps_[type];
WeightCalculatorFromHistogram wc(hist);
out = wc.getWeightErr(x,y);
return out;
}

const std::vector<float> & LeptonEfficiencyCorrector::run() {
const std::vector<float> & LeptonEfficiencyCorrectorCppWorker::run() {
ret_.clear();
for (int iL = 0, nL = nLep_; iL < nL; ++iL) {
ret_.push_back(getSF((Lep_pdgId_)[iL], (Lep_pt_)[iL], (Lep_eta_)[iL]));
}
return ret_;
}

#endif
2 changes: 2 additions & 0 deletions src/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include "PhysicsTools/NanoAODTools/interface/PyJetParametersWrapper.h"
#include "PhysicsTools/NanoAODTools/interface/WeightCalculatorFromHistogram.h"
#include "PhysicsTools/NanoAODTools/interface/ReduceMantissa.h"
#include "PhysicsTools/NanoAODTools/interface/LeptonEfficiencyCorrectorCppWorker.h"

PyJetResolutionWrapper jetRes;
PyJetResolutionScaleFactorWrapper jetResScaleFactor;
PyJetParametersWrapper jetParams;
WeightCalculatorFromHistogram wcalc;
ReduceMantissaToNbitsRounding red(12);
LeptonEfficiencyCorrectorCppWorker lepSF;
1 change: 1 addition & 0 deletions src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<class name="PyJetParametersWrapper"/>
<class name="WeightCalculatorFromHistogram"/>
<class name="ReduceMantissaToNbitsRounding"/>
<class name="LeptonEfficiencyCorrectorCppWorker"/>
</selection>
</lcgdict>