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

GenAnalysis module added #34

Open
wants to merge 6 commits into
base: HZZ_Analysis
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Empty file added GenAnalysis
Empty file.
169 changes: 157 additions & 12 deletions H4LCppModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ def __init__(self,year,cfgFile,isMC,isFSR):
ROOT.gSystem.Load("%s/JHUGenMELA/MELA/data/slc7_amd64_gcc700/libjhugenmela.so" % base)
ROOT.gSystem.Load("%s/JHUGenMELA/MELA/data/slc7_amd64_gcc700/libmcfm_707.so" % base)
ROOT.gSystem.Load("%s/JHUGenMELA/MELA/data/slc7_amd64_gcc700/libcollier.so" % base)
if "/GenAnalysis_cc.so" not in ROOT.gSystem.GetLibraries():
print("Load GenAnalysis C++ module")
base = "$CMSSW_BASE/src/PhysicsTools/NanoAODTools/python/postprocessing/analysis/nanoAOD_skim"
if base:
ROOT.gROOT.ProcessLine(
".L %s/src/GenAnalysis.cc+O" % base)
else:
base = "$CMSSW_BASE//src/PhysicsTools/NanoAODTools"
ROOT.gSystem.Load("libPhysicsToolsNanoAODTools.so")
ROOT.gROOT.ProcessLine(
".L %s/interface/GenAnalysis.h" % base)
if "/H4LTools_cc.so" not in ROOT.gSystem.GetLibraries():
print("Load C++ module")
print("Load H4LTools C++ module")
base = "$CMSSW_BASE/src/PhysicsTools/NanoAODTools/python/postprocessing/analysis/nanoAOD_skim"
if base:
ROOT.gROOT.ProcessLine(
Expand All @@ -26,9 +37,11 @@ def __init__(self,year,cfgFile,isMC,isFSR):
ROOT.gROOT.ProcessLine(
".L %s/interface/H4LTools.h" % base)
self.year = year
self.isMC = isMC
self.genworker = ROOT.GenAnalysis()
with open(cfgFile, 'r') as ymlfile:
cfg = yaml.load(ymlfile)
self.worker = ROOT.H4LTools(self.year)
self.worker = ROOT.H4LTools(self.year,self.isMC)
self.worker.InitializeElecut(cfg['Electron']['pTcut'],cfg['Electron']['Etacut'],cfg['Electron']['Sip3dcut'],cfg['Electron']['Loosedxycut'],cfg['Electron']['Loosedzcut'],
cfg['Electron']['Isocut'],cfg['Electron']['BDTWP']['LowEta']['LowPT'],cfg['Electron']['BDTWP']['MedEta']['LowPT'],cfg['Electron']['BDTWP']['HighEta']['LowPT'],
cfg['Electron']['BDTWP']['LowEta']['HighPT'],cfg['Electron']['BDTWP']['MedEta']['HighPT'],cfg['Electron']['BDTWP']['HighEta']['HighPT'])
Expand All @@ -41,7 +54,6 @@ def __init__(self,year,cfgFile,isMC,isFSR):
self.passtrigEvts = 0
self.passZZEvts = 0
self.cfgFile = cfgFile
self.isMC = isMC
self.worker.isFSR = isFSR
pass
def beginJob(self):
Expand Down Expand Up @@ -74,7 +86,16 @@ def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
self.initReaders(inputTree) # initReaders must be called in beginFile
self.out = wrappedOutputTree
self.out.branch("mass4l", "F")
self.out.branch("GENmass4l", "F")
self.out.branch("mass4e", "F")
self.out.branch("mass4mu", "F")
self.out.branch("mass2e2mu", "F")
self.out.branch("pT4l", "F")
self.out.branch("GENpT4l", "F")
self.out.branch("rapidity4l", "F")
self.out.branch("njets_pt30_eta4p7", "I")
self.out.branch("GENnjets_pt30_eta4p7", "I")
self.out.branch("GENrapidity4l", "F")
self.out.branch("eta4l", "F")
self.out.branch("phi4l", "F")
self.out.branch("massZ1", "F")
Expand Down Expand Up @@ -112,12 +133,30 @@ def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
self.out.branch("pTj1", "F")
self.out.branch("etaj1", "F")
self.out.branch("phij1", "F")
self.out.branch("mj2", "F")
self.out.branch("pTj2", "F")
self.out.branch("etaj2", "F")
self.out.branch("phij2", "F")


self.out.branch("mj2", "F")
self.out.branch("pileupWeight", "F")
self.out.branch("dataMCWeight_new", "F")
self.out.branch("prefiringWeight", "F")
self.out.branch("passedTrig", "O")
self.out.branch("passedFullSelection", "O")
self.out.branch("passedZ4lSelection", "O")
self.out.branch("passedZ4lZ1LSelection", "O")
self.out.branch("passedZ4lZXCRSelection", "O")
self.out.branch("passedZXCRSelection", "O")
self.out.branch("passedFiducialSelection", "O")
GENHlepNum = 4
GENZNum = 2
self.out.branch("GENlep_MomId", "I", lenVar = "nGENLeptons")
self.out.branch("GENlep_MomMomId", "I", lenVar = "nGENLeptons")
self.out.branch("GENZ_MomId", "I", lenVar = "nVECZ")
self.out.branch("GENZ_DaughtersId", "I", lenVar = "GENZNum")
self.out.branch("GENlep_Hindex", "I", lenVar = "GENHlepNum")
self.out.branch("lep_Hindex", "I", lenVar = "GENHlepNum")
self.out.branch("GENlep_id", "I", lenVar = "nGENLeptons")
self.out.branch("lep_genindex", "I", lenVar = "Lepointer")
Copy link
Owner

Choose a reason for hiding this comment

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

HI @YujiLee301 ,

Several branches are not in the output root file. You need to add them in the keep_and_drop.txt file, only then they will appear.

self.out.branch("Electron_Fsr_pt", "F", lenVar = "nElectron_Fsr")
self.out.branch("Electron_Fsr_eta", "F", lenVar = "nElectron_Fsr")
self.out.branch("Electron_Fsr_phi", "F", lenVar = "nElectron_Fsr")
Expand Down Expand Up @@ -152,6 +191,8 @@ def analyze(self, event):
self.worker.SetObjectNum(event.nElectron,event.nMuon,event.nJet,event.nFsrPhoton)
if isMC:
self.worker.SetObjectNumGen(event.nGenPart)
self.genworker.Initialize()
self.genworker.SetObjectNumGen(event.nGenPart, event.nGenJet)
keepIt = False

passedTrig=False
Expand All @@ -164,6 +205,18 @@ def analyze(self, event):
passedZXCRSelection=False
passedFiducialSelection=False
nZXCRFailedLeptons=0
prefiringWeight = 1
dataMCWeight_new = 1
pileupWeight = 1
mass4e=0
mass2e2mu=0
mass4mu=0
GENmass4l = -99
GENpT4l = -99
nVECZ = 2
GENrapidity4l = -99
GENnjets_pt30_eta4p7 = -1
nGENLeptons = 0
passedTrig = PassTrig(event, self.cfgFile)
if (passedTrig==True):
self.passtrigEvts += 1
Expand All @@ -175,10 +228,16 @@ def analyze(self, event):
jets = Collection(event, "Jet")
if isMC:
genparts = Collection(event, "GenPart")
genjets = Collection(event, "GenJet")
for xj in genjets:
self.genworker.SetGenJets(xj.pt,xj.eta,xj.phi,xj.mass)
for xg in genparts:
self.worker.SetGenParts(xg.pt)
self.genworker.SetGenParts(xg.pt,xg.eta,xg.phi,xg.mass,xg.pdgId,xg.status,xg.statusFlags,xg.genPartIdxMother)
for xm in muons:
self.worker.SetMuonsGen(xm.genPartIdx)
for xe in electrons:
self.worker.SetElectronsGen(xe.genPartIdx)
for xe in electrons:
self.worker.SetElectrons(xe.pt, xe.eta, xe.phi, xe.mass, xe.dxy,
xe.dz, xe.sip3d, xe.mvaFall17V2Iso, xe.pdgId, xe.pfRelIso03_all)
Expand All @@ -194,22 +253,31 @@ def analyze(self, event):
self.worker.LeptonSelection()
if ((self.worker.nTightEle<2)&(self.worker.nTightMu<2)):
pass


if isMC:
self.genworker.SetGenVariables()
GENmass4l = self.genworker.GENmass4l
GENpT4l = self.genworker.GENpT4l
GENrapidity4l = self.genworker.GENrapidity4l
GENnjets_pt30_eta4p7 = self.genworker.GENnjets_pt30_eta4p7
nGENLeptons = self.genworker.nGENLeptons

passedFiducialSelection = self.genworker.passedFiducialSelection

Electron_Fsr_pt_vec = self.worker.ElectronFsrPt()
Electron_Fsr_eta_vec = self.worker.ElectronFsrEta()
Electron_Fsr_phi_vec = self.worker.ElectronFsrPhi()
Muon_Fsr_pt_vec = self.worker.MuonFsrPt()
Muon_Fsr_eta_vec = self.worker.MuonFsrEta()
Muon_Fsr_phi_vec = self.worker.MuonFsrPhi()


Electron_Fsr_pt = []
Electron_Fsr_eta = []
Electron_Fsr_phi = []
Muon_Fsr_pt = []
Muon_Fsr_eta = []
Muon_Fsr_phi = []

if len(Electron_Fsr_pt_vec)>0:
for i in range(len(Electron_Fsr_pt_vec)):
Electron_Fsr_pt.append(Electron_Fsr_pt_vec[i])
Expand All @@ -220,9 +288,53 @@ def analyze(self, event):
Muon_Fsr_pt.append(Muon_Fsr_pt_vec[i])
Muon_Fsr_eta.append(Muon_Fsr_eta_vec[i])
Muon_Fsr_phi.append(Muon_Fsr_phi_vec[i])

GENlep_id = []
GENlep_Hindex = []
GENZ_DaughtersId = []
GENZ_MomId = []
GENlep_MomId = []
GENlep_MomMomId = []
if isMC:
GENlep_id_vec = self.genworker.GENlep_id
if len(GENlep_id_vec)>0:
for i in range(len(GENlep_id_vec)):
GENlep_id.append(GENlep_id_vec[i])
GENlep_Hindex_vec = self.genworker.GENlep_Hindex
if len(GENlep_Hindex_vec)>0:
for i in range(len(GENlep_Hindex_vec)):
GENlep_Hindex.append(GENlep_Hindex_vec[i])
GENZ_DaughtersId_vec = self.genworker.GENZ_DaughtersId
if len(GENZ_DaughtersId_vec)>0:
for i in range(len(GENZ_DaughtersId_vec)):
GENZ_DaughtersId.append(GENZ_DaughtersId_vec[i])
nVECZ = self.genworker.nVECZ
GENZ_MomId_vec = self.genworker.GENZ_MomId
if len(GENZ_MomId_vec)>0:
for i in range(len(GENZ_MomId_vec)):
GENZ_MomId.append(GENZ_MomId_vec[i])
GENlep_MomId_vec = self.genworker.GENlep_MomId
if len(GENlep_MomId_vec)>0:
for i in range(len(GENlep_MomId_vec)):
GENlep_MomId.append(GENlep_MomId_vec[i])
GENlep_MomMomId_vec = self.genworker.GENlep_MomMomId
if len(GENlep_MomMomId_vec)>0:
for i in range(len(GENlep_MomMomId_vec)):
GENlep_MomMomId.append(GENlep_MomMomId_vec[i])


foundZZCandidate = self.worker.ZZSelection()

Lepointer = self.worker.Lepointer
lep_Hindex = []
lep_Hindex_vec = self.worker.lep_Hindex
if len(lep_Hindex_vec)>0:
for i in range(len(lep_Hindex_vec)):
lep_Hindex.append(lep_Hindex_vec[i])
lep_genindex = []
if isMC:
lep_genindex_vec = self.worker.lep_genindex
if len(lep_genindex_vec)>0:
for i in range(len(lep_genindex_vec)):
lep_genindex.append(lep_genindex_vec[i])
if (foundZZCandidate):
keepIt = True
self.passZZEvts += 1
Expand Down Expand Up @@ -282,13 +394,30 @@ def analyze(self, event):
eta4l = self.worker.ZZsystem.Eta()
phi4l = self.worker.ZZsystem.Phi()
mass4l = self.worker.ZZsystem.M()
rapidity4l = self.worker.ZZsystem.Rapidity()
njets_pt30_eta4p7 = self.worker.njets_pt30_eta4p7
if self.worker.flag4e:
mass4e = mass4l
if self.worker.flag2e2mu:
mass4e = mass4l
if self.worker.flag4mu:
mass4mu = mass4l
if self.worker.isFSR==False:
pT4l = self.worker.ZZsystemnofsr.Pt()
eta4l = self.worker.ZZsystemnofsr.Eta()
phi4l = self.worker.ZZsystemnofsr.Phi()
mass4l = self.worker.ZZsystemnofsr.M()
self.out.fillBranch("mass4l",mass4l)
self.out.fillBranch("GENmass4l",GENmass4l)
self.out.fillBranch("mass4e",mass4e)
self.out.fillBranch("mass2e2mu",mass2e2mu)
self.out.fillBranch("mass4mu",mass4mu)
self.out.fillBranch("pT4l",pT4l)
self.out.fillBranch("GENpT4l",GENpT4l)
self.out.fillBranch("rapidity4l",rapidity4l)
self.out.fillBranch("GENrapidity4l",GENrapidity4l)
self.out.fillBranch("njets_pt30_eta4p7",njets_pt30_eta4p7)
self.out.fillBranch("GENnjets_pt30_eta4p7",GENnjets_pt30_eta4p7)
self.out.fillBranch("eta4l",eta4l)
self.out.fillBranch("phi4l",phi4l)
self.out.fillBranch("massZ1",massZ1)
Expand All @@ -305,6 +434,13 @@ def analyze(self, event):
self.out.fillBranch("D_int",D_int)
self.out.fillBranch("D_L1",D_L1)
self.out.fillBranch("D_L1Zg",D_L1Zg)
self.out.fillBranch("passedTrig", passedTrig)
self.out.fillBranch("passedFullSelection", passedFullSelection)
self.out.fillBranch("passedZ4lSelection", passedZ4lSelection)
self.out.fillBranch("passedZ4lZ1LSelection", passedZ4lZ1LSelection)
self.out.fillBranch("passedZ4lZXCRSelection", passedZ4lZXCRSelection)
self.out.fillBranch("passedZXCRSelection", passedZXCRSelection)
self.out.fillBranch("passedFiducialSelection", passedFiducialSelection)

self.out.fillBranch("massL1",massL1)
self.out.fillBranch("pTL1",pTL1)
Expand All @@ -331,10 +467,19 @@ def analyze(self, event):
self.out.fillBranch("pTj2",pTj2)
self.out.fillBranch("etaj2",etaj2)
self.out.fillBranch("phij2",phij2)
self.out.fillBranch("pileupWeight",pileupWeight)
self.out.fillBranch("dataMCWeight_new",dataMCWeight_new)
self.out.fillBranch("prefiringWeight",prefiringWeight)

# self.out.fillBranch("nElectron_Fsr", len(electrons))
# self.out.fillBranch("nMuon_Fsr", len(muons))


self.out.fillBranch("GENlep_id",GENlep_id)
self.out.fillBranch("GENlep_Hindex",GENlep_Hindex)
self.out.fillBranch("GENZ_DaughtersId",GENZ_DaughtersId)
self.out.fillBranch("GENZ_MomId",GENZ_MomId)
self.out.fillBranch("GENlep_MomId",GENlep_MomId)
self.out.fillBranch("GENlep_MomMomId",GENlep_MomMomId)
self.out.fillBranch("Electron_Fsr_pt",Electron_Fsr_pt)
self.out.fillBranch("Electron_Fsr_eta",Electron_Fsr_eta)
self.out.fillBranch("Electron_Fsr_phi",Electron_Fsr_phi)
Expand Down
Loading