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

5TeV WZ and ttbar #4

Open
wants to merge 16 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
40 changes: 34 additions & 6 deletions framework/AnalysisCreator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ def AddSelection(self, selection):
for l in lines:
self.selection += '%s%s\n'%(' '*addNSpaces, l)

def AddLoopCode(self, code):
code = code.replace('\t', ' ')
lines = code.split('\n')
for l in lines:
ltemp = "%s"%l
lorig = "%s"%l
ltemp = ltemp.replace(' ', '')
if ltemp != '': break
nspaces = 0
while len(lorig) >0 and lorig[0] == ' ':
nspaces += 1
lorig = lorig[1:]
addNSpaces = 6-nspaces
self.loopcode = ''
for l in lines:
self.loopcode += '%s%s\n'%(' '*addNSpaces, l)

def AddSyst(self, syst):
if isinstance(syst, list): self.syst += syst
elif syst == '': return
Expand All @@ -30,6 +47,10 @@ def AddSyst(self, syst):
def AddHeader(self, h):
self.header += h

def AddSampleOptions(self, h):
self.sampleOptions += h
if not self.sampleOptions.endswith('\n'): self.sampleOptions += '\n'

def AddInit(self, t):
self.init += t

Expand Down Expand Up @@ -61,13 +82,14 @@ def AddHisto(self, var, hname, nbins, b0 = 0, bN = 0, bins = [], weight = '', sy
if write: self.fillLine.append(fillLine)
else : return fillLine

def AddExpr(self, ename, var, expr):
def AddExpr(self, ename, var, expr, goAfter = False):
if not isinstance(var, list): var = [var]
for v in var:
if v == '': continue
st = 'fun.GetValue(t, "%s", syst)'%v
expr = re.sub(r'\b%s\b'%v, st, expr)
self.expr[ename] = expr
self.exprorder[ename] = goAfter

def CraftCut(self, cut):
return cut
Expand Down Expand Up @@ -118,9 +140,10 @@ def GetAnalysisTemplate(self):
body += 'import os,sys\nsys.path.append(os.path.abspath(__file__).rsplit("/xuAnalysis/",1)[0]+"/xuAnalysis/")\n'
body += 'from framework.analysis import analysis\nimport framework.functions as fun\nfrom ROOT import TLorentzVector\n\n'
body += self.header
body += '\nsystematics = ' + str(self.syst) + '\n'
body += '%s'%self.sampleOptions
body += 'class %s(analysis):\n'%self.analysisName
body += ' def init(self):\n'
body += ' self.systematics = ' + str(self.syst) + '\n'
body += self.init
body +=' # Create your histograms here\n'
#if len(self.syst) > 0: body +=' for syst in systematics:\n'
Expand All @@ -133,17 +156,19 @@ def GetAnalysisTemplate(self):

if len(self.cuts) > 0 and len(self.syst) == 0:
body += '\n # Requirements\n'
for cut in self.cuts: body += ' if not %s: return\n'%cut
for cut in self.cuts: body += ' if not (%s): return\n'%cut


hnames = self.vars.keys()
if len(hnames) > 0:
body += '\n # Filling the histograms\n'
if len(self.syst) > 0:
body += '\n for syst in systematics:\n'
body += '\n for syst in self.systematics:\n'
body += '\n # Requirements\n'
for cut in self.cuts: body += ' if not %s: return\n'%cut
for expr in self.expr.keys(): body += ' %s = %s\n'%(expr, self.expr[expr])
for cut in self.cuts: body += ' if not (%s): continue\n'%cut
for expr in self.expr.keys(): body += ' %s = %s\n'%(expr, self.expr[expr]) if not self.exprorder[expr] else ''
if self.loopcode != '': body += self.loopcode
for expr in self.expr.keys(): body += ' %s = %s\n'%(expr, self.expr[expr]) if self.exprorder[expr] else ''
# for h in hnames:
# var = self.vars[h]
# cut = ' ' if self.histocuts[h] == '' else ' if %s:'%self.histocuts[h]
Expand Down Expand Up @@ -218,6 +243,7 @@ def __init__(self, analysisName, cfgname = 'testcfg', path = '', weight = '', ou
self.header = ''
self.init = ''
self.selection = ''
self.loopcode = ''
self.cuts = []
self.histos = []
self.fillLine = []
Expand All @@ -226,7 +252,9 @@ def __init__(self, analysisName, cfgname = 'testcfg', path = '', weight = '', ou
self.weights = {}
self.histocuts = {}
self.syst = ['']
self.sampleOptions = ''
self.expr = {}
self.exprorder = {}


def main():
Expand Down
9 changes: 5 additions & 4 deletions framework/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ def divideFiles(self):
def craftJob(self, n0, nF, index = -1):
''' Returns a string with all the info for creating a job '''
localPath = os.getcwd() + '/'
modulname = self.__class__.__name__
modulname = self.__class__.__name__
modulfld = modulname if (self.modulefolder=='') else self.modulefolder
path = self.inpath
filename = self.fileName
outpath = self.outpath
Expand All @@ -317,7 +318,7 @@ def craftJob(self, n0, nF, index = -1):
elif os.path.isfile('/opt/root6/bin/thisroot.sh'):
t += 'source /cms/slc6_amd64_gcc530/external/gcc/5.3.0/etc/profile.d/init.sh; source /cms/slc6_amd64_gcc530/external/python/2.7.11-giojec2/etc/profile.d/init.sh; source /cms/slc6_amd64_gcc530/external/python/2.7.11-giojec2/etc/profile.d/dependencies-setup.sh; source /cms/slc6_amd64_gcc530/external/cmake/3.5.2/etc/profile.d/init.sh;source /opt/root6/bin/thisroot.sh\n'
pycom = 'python -c \''
pycom += 'from ' + modulname + '.' + modulname + ' import *; '
pycom += 'from ' + modulfld + '.' + modulname + ' import *; '
if isinstance(filename, list):
fname = filename [0]
for f in filename[1:]: fname += ', %s'%f
Expand Down Expand Up @@ -495,7 +496,7 @@ def log(self):
#############################################################################################
### Init method

def __init__(self,fname, fileName = '', xsec = 1, outpath = './temp/', nSlots = 1, eventRange = [], run = False, sendJobs = False, verbose = 1, index = -1, options = '', chooseFile = -1, treeName = 'Events',outname = '', elistf=""):
def __init__(self,fname, fileName = '', xsec = 1, outpath = './temp/', nSlots = 1, eventRange = [], run = False, sendJobs = False, verbose = 1, index = -1, options = '', chooseFile = -1, treeName = 'Events',outname = '', elistf="", modulefolder = ''):
# Default values:
self.inpath = fname
self.out = ''
Expand Down Expand Up @@ -537,4 +538,4 @@ def __init__(self,fname, fileName = '', xsec = 1, outpath = './temp/', nSlots =
if run: self.run(self.firstEvent, self.firstEvent+self.nRunEvents)
elif sendJobs: self.sendJobs()


self.modulefolder = modulefolder
15 changes: 12 additions & 3 deletions framework/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def SetPDGid(self, pdgid):
def SetSF(self, SF, error = 0):
self.SF = SF
self.SFerr = error

def SetIso(self, iso):
self.iso = iso

Expand Down Expand Up @@ -134,13 +134,15 @@ def __init__(self, pvec = TLorentzVector(), charge = 0, pdgid = -1):
#################################################################################
class lepton(object):

def __init__(self, pvec = TLorentzVector(), charge = 0, pdgid = -1, mcmatch=-1, passTightID = True):
def __init__(self, pvec = TLorentzVector(), charge = 0, pdgid = -1, mcmatch=-1, passTightID = True, passbtag = True):
self.SetP(pvec)
self.SetCharge(charge)
self.SetFlav(pdgid)
self.resetValues()
self.mcmatch = mcmatch
self.passTightID = passTightID
self.passbtag = passbtag

###############################
### Set methods

Expand Down Expand Up @@ -258,6 +260,12 @@ def CheckZpair(lep1, lep2):
if not lep1.charge*lep2.charge < 0: return 0
return InvMass(lep1,lep2)

def CheckSign(lep1, lep2):
''' Returns: 1 if Same Sign, 0 otherwise '''
if not lep1.charge*lep2.charge > 0: return 0
return 1


def GetNBtags(listOfJets):
''' Returns the number of btag jets in the list of jets '''
nbtags = 0
Expand Down Expand Up @@ -344,7 +352,8 @@ def GetValue(tree, var, syst = '', index = -1):
vart = "%s%s"%(var,syst)
if hasattr(tree, vars): return (getattr(tree, vars) if index == -1 else getattr(tree, vars)[index])
elif hasattr(tree, vart): return (getattr(tree, vart) if index == -1 else getattr(tree, vart)[index])
elif hasattr(tree, var ): return (getattr(tree, var ) if index == -1 else getattr(tree, var )[index])
elif hasattr(tree, var ):
return (getattr(tree, var ) if index == -1 else getattr(tree, var )[index])
else:
print 'ERROR: var %s not in tree!!'%var
return 0
Expand Down
46 changes: 37 additions & 9 deletions framework/looper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@

from framework.AnalysisCreator import AnalysisCreator
from run import main as run
from ROOT import TFile

class looper(AnalysisCreator):

def AddRunFolder(self, fname):
sys.path.append(fname)

def Run(self):
self.CreateAnalysis()
#cfgpath = '%s/%s.cfg'%(self.outpath, self.cfgname)
cfgpath = self.cfgname
print 'Executing analysis \'%s\' using cfg file \'%s\' in \'%s/%s/\'...'%(self.analysisName, cfgpath, mypath+self.outpath, self.analysisName)
return run('%s/%s/%s'%(mypath+self.outpath,self.analysisName,cfgpath))
odir = {}
if len(self.samples.keys()) != 0:
self.CreateAnalysis()
#cfgpath = '%s/%s.cfg'%(self.outpath, self.cfgname)
cfgpath = self.cfgname
print 'Executing analysis \'%s\' using cfg file \'%s\' in \'%s/%s/\'...'%(self.analysisName, cfgpath, mypath+self.basepath, self.analysisName)
odir = run('%s/%s/%s'%(mypath+self.basepath,self.analysisName,cfgpath))
if self.readOutput and self.loadDic != {}:
for ksamp in self.loadDic.keys():
odir[ksamp] = self.loadDic[ksamp]
return odir

def AutoRemove(self):
command = 'rm -r %s/%s'%(self.outpath, self.analysisName)
Expand All @@ -36,33 +43,54 @@ def AddToOutput(self, sampleName, histodic):
def GetAnalysisName(self):
return 'analysis_%i'%(int(time.time()*1e6)%1e12)

def __init__(self, path = '', nSlots = 1, cut = '', weight = '', nEvents = 0, year = 0, verbose = 0, options = 'merge', treeName = 'Events', processdic = {}):
def AddSample(self, sampleName, sampleList):
if self.readOutput:
if os.path.isfile('%s/%s.root'%(self.outpath, sampleName)):
if not sampleName in self.loadDic.keys(): self.loadDic[sampleName] = {}
print 'Reading %s from %s...'%(sampleName, self.outpath)
f = TFile.Open('%s/%s.root'%(self.outpath, sampleName))
hlist = f.GetListOfKeys()
for l in hlist:
hname = l.GetName()
histo = getattr(f, hname)
histo.SetDirectory(0)
self.loadDic[sampleName][hname] = histo
else: self.samples[sampleName] = sampleList
else:
self.samples[sampleName] = sampleList

def __init__(self, path = '', nSlots = 1, cut = '', weight = '', nEvents = 0, year = 0, verbose = 0, options = 'merge', treeName = 'Events', processdic = {}, outpath='./.looper/', readOutput=False, basepath = './looper/'):
self.analysisName = self.GetAnalysisName()
self.SetTreeName(treeName)
self.SetCfgname('testcfg')
self.SetOutpath('./.looper/')
self.SetBasePath('./.looper/')
self.AddRunFolder('.looper')
self.SetOutpath(outpath)
self.SetBasePath(basepath)
self.AddRunFolder(outpath)
self.SetPath(path)
self.SetNEvents(nEvents)
self.SetYear(year)
self.SetVerbose(verbose)
self.SetOptions(options)
self.SetNSlots(nSlots)
self.SetWeight(weight)
self.readOutput = readOutput
self.header = ''
self.init = ''
self.selection = ''
self.loopcode = ''
self.cuts = []
self.histos = []
self.samples = {}
self.vars = {}
self.out = {}
self.loadDic = {}
self.fillLine = []
self.weights = {}
self.histocuts = {}
self.syst = ['']
self.expr = {}
self.exprorder = {}
self.sampleOptions = ''
if cut != '': self.AddCut(cut)
if processdic != {}:
for pr in processdic:
Expand Down
25 changes: 24 additions & 1 deletion framework/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ def main():
pr.add_argument('-f','--force', action='store_true', help='If out saple exists, moves to .bck and executes the merging')
pr.add_argument('-p','--pretend', action='store_true', help='Returns the hadd commands')
pr.add_argument('-r','--rm', action='store_true', help='Returns the hadd commands')
pr.add_argument('--rmZero', action='store_true', help='Moves single name_0.root file to name.root')
pr.add_argument('--recursive', '--subfolders', action='store_true', help='Execute for all subfolders')

args = pr.parse_args()
inFolder = args.folder
rmZero = args.rmZero
recur = args.recursive
if args.verbose: verbose = True
if args.force: force = True
if args.pretend: pretend = True
Expand All @@ -86,11 +90,30 @@ def main():

if sampleName == '': ### Automatic option
print 'INFO: Automatic merge of files in folder: ' + inFolder
if recur:
paths = []
for root, subdirs, files in os.walk(inFolder):
if root in paths: continue
paths.append(root)
print("Exploring ", root)
if len(files) > 0:
listOfFiles = getDicFiles(root)
inFolder = root
outFolder = root
if len(listOfFiles) == 0: continue
for L in listOfFiles:
if rmZero and len(listOfFiles[L]) == 1:
if listOfFiles[L][0] == L+'_0.root': os.system('mv %s/%s %s/%s.root'%(inFolder, listOfFiles[L][0], outFolder, L))
hadd(listOfFiles[L], L, inFolder, outFolder, verbose, pretend, force, rm)
exit()
listOfFiles = getDicFiles(inFolder)
if len(listOfFiles) == 0:
print 'WARNING: no files to merge in "' + inFolder + '"'
exit()
for L in listOfFiles: hadd(listOfFiles[L], L, inFolder, outFolder, verbose, pretend, force, rm)
for L in listOfFiles:
if rmZero and len(listOfFiles[L]) == 1:
if listOfFiles[L][0] == L+'_0.root': os.system('mv %s/%s %s/%s.root'%(inFolder, listOfFiles[L][0], outFolder, L))
hadd(listOfFiles[L], L, inFolder, outFolder, verbose, pretend, force, rm)

else: ### hadd only one sample
listOfFiles = findValidRootfiles(inFolder, sampleName)
Expand Down
7 changes: 4 additions & 3 deletions plotter/Plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ def Draw(self, doSetLogy = False):
self.ratioh[0][0].SetMaximum(self.PlotRatioMax)
self.ratioh[0][0].SetMinimum(self.PlotRatioMin)
for h in self.ratioh:
h[0].SetFillStyle(0)
h[0].Draw(h[1] + ',same')
if h[2] != 0 and h[2] != '':
hrat = h[0].Clone()
Expand Down Expand Up @@ -648,7 +649,7 @@ class HistoUnc(HistoComp):

def AddHistoNom(self, h):
self.hNo = h
self.AddHisto(h, 'hist', 'e2' if self.DrawStatUnc else 0, 0, 1, 1)
self.AddHisto(h, 'hist', 0, 0, 1, 1)
self.rnom = self.hNo.Clone('rnom')
for b in range(0, self.rnom.GetNbinsX()+2):
bc = self.rnom.GetBinContent(b)
Expand All @@ -660,7 +661,7 @@ def AddHistoNom(self, h):
self.rnom.SetLineWidth(2)
self.rnom.SetLineColor(kGray+1)
self.rnom.SetFillColor(kTeal-9)
self.rnom.SetFillStyle(1)
self.rnom.SetFillStyle(1000)
self.AddRatioHisto(self.rnom, 'hist', 'e2')

def AddHistoUp(self, h):
Expand Down Expand Up @@ -710,7 +711,7 @@ def __init__(self, outpath = './', outname = 'temp', tag = '', doNorm = False, d
self.SetRatioPadMargins(top = 0.08, bottom = 0.25, right = 0.03, left = 0.10)
self.SetYratioTitle('Unc./Nom.', 0.10, 0.4, labSize = 0.08)
self.SetXtitle(xtit, 0.12, 0.8, labSize = 0.09)
self.SetYtitle(ytit, 0.12, 0.35, labSize = 0.08)
self.SetYtitle(ytit, 0.12, 0.42, labSize = 0.08)
self.SetTextLumi(texlumi = '%2.1f fb^{-1} (13 TeV)' if not doNorm else '', texlumiX = 0.67, texlumiY = 0.97, texlumiS = 0.05)
self.SetRatioMin(0.95); self.SetRatioMax(1.05)

Loading