def log_s_over_b(fileList): #-------------- # log s over b #-------------- histosL={} s_b_d_histos = {} for file in fileList: print file name = '%s' %file histosL[name] = [] for th1 in get_th1(file): #th1.Sumw2() if 'VVLF' in th1.GetName(): th1.SetName('VV') if 'Zj1b' in th1.GetName(): th1.SetName('Zj2b') if 'Wj1b' in th1.GetName(): th1.SetName('Wj2b') histosL[name].append(th1) i = 0 for hist in histosL[name]: if 'VH' in hist.GetName() and not 'VVHF' in hist.GetName(): #if 'VVHF' in hist.GetName(): hSignal = hist.Clone() elif 'data_obs' in hist.GetName(): hData = hist.Clone() else: if i == 0: hBkg = hist.Clone() else: hBkg.Add(hist) i += 1 s_b_d_histos[name] = {'b': hBkg, 's': hSignal, 'd': hData} bmin=-4 bmax=0 nbins=16 log_s_over_b_b = ROOT.TH1F("log_s_over_b_b","log_s_over_b_b",nbins,bmin,bmax) log_s_over_b_b.SetFillColor(4) log_s_over_b_b.GetXaxis().SetTitle("log(S/B)") log_s_over_b_b.GetYaxis().SetTitle("Events") log_s_over_b_s = ROOT.TH1F("log_s_over_b_s","log_s_over_b_s",nbins,bmin,bmax) log_s_over_b_s.SetFillColor(2) log_s_over_b_d = ROOT.TH1F("log_s_over_b_d","log_s_over_b_d",nbins,bmin,bmax) log_s_over_b = ROOT.THStack("log_s_over_b","log_s_over_b") stack_log_s_over_b = ROOT.THStack("stack_log_s_over_b","stack_log_s_over_b") for key, s_b_d in s_b_d_histos.iteritems(): for bin in range(0,s_b_d['b'].GetNbinsX()+1): s = s_b_d['s'].GetBinContent(bin) b = s_b_d['b'].GetBinContent(bin) d = s_b_d['d'].GetBinContent(bin) sErr = s_b_d['s'].GetBinError(bin) bErr = s_b_d['b'].GetBinError(bin) dErr = s_b_d['d'].GetBinError(bin) logsb = -3.9 if b > 0. and s > 0.: logsb = log10(s/b) elif s > 0.: logsb = -0. #print logsb newBin = log_s_over_b_b.FindBin(logsb) log_s_over_b_b.SetBinContent(newBin, b+log_s_over_b_b.GetBinContent(newBin)) log_s_over_b_s.SetBinContent(newBin, s+log_s_over_b_s.GetBinContent(newBin)) log_s_over_b_d.SetBinContent(newBin, d+log_s_over_b_d.GetBinContent(newBin)) log_s_over_b_b.SetBinError(newBin, sqrt(bErr*bErr+log_s_over_b_b.GetBinError(newBin)*log_s_over_b_b.GetBinError(newBin))) log_s_over_b_s.SetBinError(newBin, sqrt(sErr*sErr+log_s_over_b_s.GetBinError(newBin)*log_s_over_b_s.GetBinError(newBin))) log_s_over_b_d.SetBinError(newBin, sqrt(dErr*dErr+log_s_over_b_d.GetBinError(newBin)*log_s_over_b_d.GetBinError(newBin))) stack = StackMaker(config,'logSB','plot1',False) stack.setup = ['VH','BKG'] stack.typs = ['VH','BKG'] stack.lumi = 18940. stack.histos = [log_s_over_b_s,log_s_over_b_b] stack.datas = [log_s_over_b_d] stack.datanames='data_obs' stack.overlay = log_s_over_b_s stack.doPlot()
def log_s_over_b(fileList): print '-----> Running def log_s_over_b()...' #-------------- # log s over b #-------------- histosL={} s_b_d_histos = {} for file in fileList: print file name = '%s' %file histosL[name] = [] for th1 in get_th1(file): #print '\n\t Retrieving TH1 from file: ', th1 #th1.Sumw2() if 'VVLF' in th1.GetName(): th1.SetName('VV') if 'Zj1b' in th1.GetName(): th1.SetName('Zj2b') if 'Wj1b' in th1.GetName(): th1.SetName('Wj2b') histosL[name].append(th1) i = 0 j = 0 for hist in histosL[name]: if 'ZH' in hist.GetName() or 'ggZH' in hist.GetName(): #print '\n\t Setting ',hist, ' as signal...' if j == 0: hSignal = hist.Clone() else: hSignal.Add(hist) j += 1 elif 'data_obs' in hist.GetName(): hData = hist.Clone() else: if i == 0: hBkg = hist.Clone() else: #print '\n\t Setting ',hist, ' as background...' hBkg.Add(hist) i += 1 # temp hack hData = hSignal s_b_d_histos[name] = {'b': hBkg, 's': hSignal, 'd': hData} bmin=-4 bmax=0 nbins=16 log_s_over_b_b = ROOT.TH1F("log_s_over_b_b","log_s_over_b_b",nbins,bmin,bmax) log_s_over_b_b.SetFillColor(4) log_s_over_b_b.GetXaxis().SetTitle("log(S/B)") log_s_over_b_b.GetYaxis().SetTitle("Events") log_s_over_b_s = ROOT.TH1F("log_s_over_b_s","log_s_over_b_s",nbins,bmin,bmax) log_s_over_b_s.SetFillColor(2) log_s_over_b_d = ROOT.TH1F("log_s_over_b_d","log_s_over_b_d",nbins,bmin,bmax) log_s_over_b = ROOT.THStack("log_s_over_b","log_s_over_b") stack_log_s_over_b = ROOT.THStack("stack_log_s_over_b","stack_log_s_over_b") print '\ns_b_d_histos',s_b_d_histos for key, s_b_d in s_b_d_histos.iteritems(): print '\n-----> Looping over Histogram: ',s_b_d for bin in range(0,s_b_d['b'].GetNbinsX()+1): s = s_b_d['s'].GetBinContent(bin) b = s_b_d['b'].GetBinContent(bin) d = s_b_d['d'].GetBinContent(bin) sErr = s_b_d['s'].GetBinError(bin) bErr = s_b_d['b'].GetBinError(bin) dErr = s_b_d['d'].GetBinError(bin) logsb = -3.9 if b > 0. and s > 0.: logsb = log10(s/b) elif s > 0.: logsb = -0. print '\n\t Calculating log_s/b for bin', bin print '\t\t sig:',s,' bgk:',b print '\t\t Log_s/b',logsb newBin = log_s_over_b_b.FindBin(logsb) log_s_over_b_b.SetBinContent(newBin, b+log_s_over_b_b.GetBinContent(newBin)) log_s_over_b_s.SetBinContent(newBin, s+log_s_over_b_s.GetBinContent(newBin)) log_s_over_b_d.SetBinContent(newBin, d+log_s_over_b_d.GetBinContent(newBin)) log_s_over_b_b.SetBinError(newBin, sqrt(bErr*bErr+log_s_over_b_b.GetBinError(newBin)*log_s_over_b_b.GetBinError(newBin))) log_s_over_b_s.SetBinError(newBin, sqrt(sErr*sErr+log_s_over_b_s.GetBinError(newBin)*log_s_over_b_s.GetBinError(newBin))) log_s_over_b_d.SetBinError(newBin, sqrt(dErr*dErr+log_s_over_b_d.GetBinError(newBin)*log_s_over_b_d.GetBinError(newBin))) stack = StackMaker(config,'logSB','plot1',False) stack.setup = ['ZH','BKG'] stack.typs = ['ZH','BKG'] stack.lumi = 10000. stack.histos = [log_s_over_b_s,log_s_over_b_b] stack.datas = [log_s_over_b_d] stack.datanames='data_obs' stack.overlay = log_s_over_b_s stack.doPlot()
def drawFromDC(): config = BetterConfigParser() config.read(opts.config) region = opts.region print "\nopts.config:",opts.config print "opts:", opts print "var:", opts.var print "bin:", opts.bin dataname = 'Zll' if 'Zmm' in opts.bin: dataname = 'Zmm' elif 'Zee' in opts.bin: dataname = 'Zee' elif 'Wmunu' in opts.bin: dataname = 'Wmn' elif 'Wenu' in opts.bin: dataname = 'Wen' elif 'Znunu' in opts.bin: dataname = 'Znn' elif 'Wtn' in opts.bin: dataname = 'Wtn' if (opts.var == ''): var = 'BDT' if dataname == 'Zmm' or dataname == 'Zee': var = 'BDT_Zll' elif dataname == 'Wmn' or dataname == 'Wen': var = 'BDT_Wln' elif dataname == 'Znn': if 'HighPt' in opts.bin: var = 'BDT_ZnnHighPt' if 'LowPt' in opts.bin: var = 'BDT_ZnnLowPt' if 'LowCSV' in opts.bin: var = 'BDT_ZnnLowCSV' if dataname == '' or var == 'BDT': raise RuntimeError, 'Did not recognise mode or var from '+opts.bin else: var = opts.var if 'BDT' in var: region = 'BDT' else: region = opts.bin ws_var = config.get('plotDef:%s'%var,'relPath') if region == 'BDT': ws_var = ROOT.RooRealVar(ws_var,ws_var,-1.,1.) else: ws_var = ROOT.RooRealVar(ws_var,ws_var, 0, 1.) blind = eval(config.get('Plot:%s'%region,'blind')) print 'config:', config print 'var: ', var print 'region: ', region Stack=StackMaker(config,var,region,True) if 'LowPt' in opts.bin or 'ch1_Wenu' == opts.bin or 'ch2_Wmunu' == opts.bin: Stack.addFlag2 = 'Low p_{T}(V)' elif 'MedPt' in opts.bin or 'ch1_Wenu2' == opts.bin or 'ch2_Wmunu2' == opts.bin: Stack.addFlag2 = 'Intermediate p_{T}(V)' elif 'HighPt' in opts.bin or 'ch1_Wenu3' == opts.bin or 'ch2_Wmunu3' == opts.bin: Stack.addFlag2 = 'High p_{T}(V)' # check for pre or post fit options preFit = False addName = 'PostFit_%s' %(opts.fit) if not opts.mlfit: addName = 'PreFit' preFit = True print '\n-----> Fit Type(opts.fit) : ', opts.fit print ' (opts.mlfit): ', opts.mlfit print ' preFit : ', preFit Stack.options['pdfName'] = '%s_%s_%s.pdf' %(var,opts.bin,addName) log = eval(config.get('Plot:%s'%region,'log')) setup = config.get('Plot_general','setup').split(',') if dataname == 'Zmm' or dataname == 'Zee': try: setup.remove('W1b') setup.remove('W2b') setup.remove('Wlight') setup.remove('WH') except: print '@INFO: Wb / Wligh / WH not present in the datacard' if not dataname == 'Znn' and 'QCD' in setup: setup.remove('QCD') Stack.setup = setup Dict = eval(config.get('LimitGeneral','Dict')) lumi = eval(config.get('Plot_general','lumi')) options = copy(opts) options.dataname = "data_obs" options.mass = 0 options.format = "%8.3f +/- %6.3f" options.channel = opts.bin options.excludeSyst = [] options.norm = False options.stat = False options.bin = True # fake that is a binary output, so that we parse shape lines options.out = "tmp.root" options.fileName = args[0] options.cexpr = False options.fixpars = False options.libs = [] options.verbose = 0 options.poisson = 0 options.nuisancesToExclude = [] options.noJMax = None theBinning = ROOT.RooFit.Binning(Stack.nBins,Stack.xMin,Stack.xMax) file = open(opts.dc, "r") os.chdir(os.path.dirname(opts.dc)) print '\nDC Path:', os.path.dirname(opts.dc) DC = parseCard(file, options) if not DC.hasShapes: DC.hasShapes = True MB = ShapeBuilder(DC, options) theShapes = {} theSyst = {} nuiVar = {} print '\n\n ------> Mlfit File: ', opts.mlfit if opts.mlfit: nuiVar = readBestFit(opts.mlfit) if not opts.bin in DC.bins: raise RuntimeError, "Cannot open find %s in bins %s of %s" % (opts.bin,DC.bins,opts.dc) print '\n-----> Looping over bins in datacard...' for b in DC.bins: print ' bin: ', b if options.channel != None and (options.channel != b): continue exps = {} expNui = {} shapeNui = {} reducedShapeNui = {} for (p,e) in DC.exp[b].items(): # so that we get only self.DC.processes contributing to this bin exps[p] = [ e, [] ] expNui[p] = [ e, [] ] print '\n-----> Datacard systematics: ', DC.systs for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs: print '\n-----> Looping over systematics in datacard: ', (lsyst,nofloat,pdf,pdfargs,errline) if pdf in ('param', 'flatParam'): continue # begin skip systematics skipme = False for xs in options.excludeSyst: if re.search(xs, lsyst): skipme = True if skipme: print '\n-----> skipping systematics...' continue # end skip systematics counter = 0 print '\n\t-----> Looping over keys in datacard: ', DC.exp[b].keys() for p in DC.exp[b].keys(): # so that we get only self.DC.processes contributing to this bin print '\n\t-----> Looping over process in this bin: ', p if errline[b][p] == 0: continue if p == 'QCD' and not 'QCD' in setup: continue if pdf == 'gmN': exps[p][1].append(1/sqrt(pdfargs[0]+1)); elif pdf == 'gmM': exps[p][1].append(errline[b][p]); elif type(errline[b][p]) == list: kmax = max(errline[b][p][0], errline[b][p][1], 1.0/errline[b][p][0], 1.0/errline[b][p][1]); exps[p][1].append(kmax-1.); elif pdf == 'lnN': lnNVar = max(errline[b][p], 1.0/errline[b][p])-1. if not nuiVar.has_key('%s_%s'%(opts.fit,lsyst)): nui = 0. else: nui= nuiVar['%s_%s'%(opts.fit,lsyst)][0] lnNVar = lnNVar*nuiVar['%s_%s'%(opts.fit,lsyst)][1] exps[p][1].append(lnNVar) expNui[p][1].append(abs(1-errline[b][p])*nui); elif 'shape' in pdf: print '\n\t-----> Filling the Shapes for this process...' #print 'shape %s %s: %s'%(pdf,p,lsyst) s0 = MB.getShape(b,p) sUp = MB.getShape(b,p,lsyst+"Up") sDown = MB.getShape(b,p,lsyst+"Down") if (s0.InheritsFrom("RooDataHist")): s0 = ROOT.RooAbsData.createHistogram(s0,p,ws_var,theBinning) s0.SetName(p) sUp = ROOT.RooAbsData.createHistogram(sUp,p+lsyst+'Up',ws_var,theBinning) sUp.SetName(p+lsyst+'Up') sDown = ROOT.RooAbsData.createHistogram(sDown,p+lsyst+'Down',ws_var,theBinning) sDown.SetName(p+lsyst+'Down') theShapes[p] = s0.Clone() theShapes[p+lsyst+'Up'] = sUp.Clone() theShapes[p+lsyst+'Down'] = sDown.Clone() if not nuiVar.has_key('%s_%s'%(opts.fit,lsyst)): nui = 0. reducedNui = 1. else: nui= nuiVar['%s_%s'%(opts.fit,lsyst)][0] reducedNui= nuiVar['%s_%s'%(opts.fit,lsyst)][1] shapeNui[p+lsyst] = nui reducedShapeNui[lsyst] = reducedNui if not 'CMS_vhbb_stat' in lsyst: if counter == 0: theSyst[lsyst] = s0.Clone() theSyst[lsyst+'Up'] = sUp.Clone() theSyst[lsyst+'Down'] = sDown.Clone() else: theSyst[lsyst].Add(s0) theSyst[lsyst+'Up'].Add(sUp.Clone()) theSyst[lsyst+'Down'].Add(sDown.Clone()) counter += 1 procs = DC.exp[b].keys(); procs.sort() if not 'QCD' in setup and 'QCD' in procs: procs.remove('QCD') if not 'W2b' in setup and 'WjHF' in procs: procs.remove('WjHF') if not 'Wlight' in setup and 'WjLF' in procs: procs.remove('WjLF') fmt = ("%%-%ds " % max([len(p) for p in procs]))+" "+options.format; #Compute norm uncertainty and best fit theNormUncert = {} theBestFit = {} print '\n-----> Computing norm uncertaint and best fit...' for p in procs: relunc = sqrt(sum([x*x for x in exps[p][1]])) print fmt % (p, exps[p][0], exps[p][0]*relunc) theNormUncert[p] = relunc absBestFit = sum([x for x in expNui[p][1]]) theBestFit[p] = 1.+absBestFit histos = [] typs = [] setup2=copy(setup) shapesUp = [[] for _ in range(0,len(setup2))] shapesDown = [[] for _ in range(0,len(setup2))] sigCount = 0 signalList = ['ZH','WH'] # for shape analysis? for p in procs: b = opts.bin print 'process: ', p print 'setup:',setup print 'Dict:', Dict print 'theShapes:', theShapes for s in setup: print '-----> Fillings the shapes for: ', s if not Dict[s] == p: continue if s in signalList: if sigCount ==0: Overlay=copy(theShapes[Dict[s]]) else: Overlay.Add(theShapes[Dict[s]]) sigCount += 1 else: histos.append(theShapes[Dict[s]]) typs.append(s) for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs: if errline[b][p] == 0: continue if ("shape" in pdf) and not 'CMS_vhbb_stat' in lsyst: print 'syst %s'%lsyst shapesUp[setup2.index(s)].append(theShapes[Dict[s]+lsyst+'Up']) shapesDown[setup2.index(s)].append(theShapes[Dict[s]+lsyst+'Down']) #------------- #Compute absolute uncertainty from shapes counter = 0 for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs: sumErr = 0 for p in procs: sumErr += errline[b][p] print '---> PDF:',pdf, lsyst if ("shape" in pdf) and not 'CMS_vhbb_stat' in lsyst and not sumErr == 0: theSystUp = theSyst[lsyst+'Up'].Clone() theSystUp.Add(theSyst[lsyst].Clone(),-1.) theSystUp.Multiply(theSystUp) theSystDown = theSyst[lsyst+'Down'].Clone() theSystDown.Add(theSyst[lsyst].Clone(),-1.) theSystDown.Multiply(theSystDown) theSystUp.Scale(reducedShapeNui[lsyst]) theSystDown.Scale(reducedShapeNui[lsyst]) if counter == 0: theAbsSystUp = theSystUp.Clone() theAbsSystDown = theSystDown.Clone() else: theAbsSystUp.Add(theSystUp.Clone()) theAbsSystDown.Add(theSystDown.Clone()) counter +=1 #------------- #Best fit for shapes if not preFit: # Set the preFit as an overlay print '\n Making prefit overlay...' print procs i = 0 for hist in theShapes: if hist not in procs: continue print 'Process:', hist print 'Shape:', theShapes[hist] print 'i:', i if i == 0: prefit_overlay=copy(theShapes[hist]) #prefit_overlay=theShapes[hist] print 'First Integral:', theShapes[hist].Integral() i+=1 else: #prefit_overlay.Add(theShapes[hist], 1.0) prefit_overlay.Add(theShapes[hist]) print 'Integral:', theShapes[hist].Integral() print 'prefit_overlay:', prefit_overlay print 'Integral:', prefit_overlay.Integral() print '\n-----> Getting best fit shapes(for postFit)...' histos, Overlay, typs = getBestFitShapes(procs,theShapes,shapeNui,theBestFit,DC,setup,opts,Dict) counter = 0 errUp=[] total=[] errDown=[] nBins = histos[0].GetNbinsX() #print histos # temp hack to get histo names right #names = ['ggZH','DY2B', 'DY1B', 'DYlight', 'TT', 'VV'] #for name,i in enumerate(histos): # i.SetName(names[name]) #Overlay.SetName('ZH') # end hack print '\n total bins %s'%nBins print '\n histos: ',histos print '\n theNormUncert: ',theNormUncert print '\n Overlay: ', Overlay Error = ROOT.TGraphAsymmErrors(histos[0]) theTotalMC = histos[0].Clone() for h in range(1,len(histos)): theTotalMC.Add(histos[h]) total = [[]]*nBins errUp = [[]]*nBins errDown = [[]]*nBins print '\n\n\t\t -----> The Histos: ', histos for bin in range(1,nBins+1): binError = theTotalMC.GetBinError(bin) if math.isnan(binError): binError = 0. total[bin-1]=theTotalMC.GetBinContent(bin) #Stat uncertainty of the MC outline errUp[bin-1] = [binError] errDown[bin-1] = [binError] # Temp hack to fix theNormUncert naming temp_theNormUncert = {} for i,hist in enumerate(histos): for x in theNormUncert: #print '\nx: ', x if x in histos[i].GetName(): temp_theNormUncert[histos[i].GetName()] = theNormUncert[x] #print temp_theNormUncert #Relative norm uncertainty of the individual MC for h in range(0,len(histos)): #errUp[bin-1].append(histos[h].GetBinContent(bin)*theNormUncert[histos[h].GetName()]) #errDown[bin-1].append(histos[h].GetBinContent(bin)*theNormUncert[histos[h].GetName()]) errUp[bin-1].append(histos[h].GetBinContent(bin)*temp_theNormUncert[histos[h].GetName()]) errDown[bin-1].append(histos[h].GetBinContent(bin)*temp_theNormUncert[histos[h].GetName()]) #Shape uncertainty of the MC for bin in range(1,nBins+1): #print sqrt(theSystUp.GetBinContent(bin)) errUp[bin-1].append(sqrt(theAbsSystUp.GetBinContent(bin))) errDown[bin-1].append(sqrt(theAbsSystDown.GetBinContent(bin))) #Add all in quadrature totErrUp=[sqrt(sum([x**2 for x in bin])) for bin in errUp] totErrDown=[sqrt(sum([x**2 for x in bin])) for bin in errDown] #Make TGraph with errors for bin in range(1,nBins+1): if not total[bin-1] == 0: point=histos[0].GetXaxis().GetBinCenter(bin) Error.SetPoint(bin-1,point,1) Error.SetPointEYlow(bin-1,totErrDown[bin-1]/total[bin-1]) #print 'down %s'%(totErrDown[bin-1]/total[bin-1]) Error.SetPointEYhigh(bin-1,totErrUp[bin-1]/total[bin-1]) #print 'up %s'%(totErrUp[bin-1]/total[bin-1]) #----------------------- #Read data data0 = MB.getShape(opts.bin,'data_obs') if (data0.InheritsFrom("RooDataHist")): data0 = ROOT.RooAbsData.createHistogram(data0,'data_obs',ws_var,theBinning) data0.SetName('data_obs') datas=[data0] datatyps = [None] datanames=[dataname] print '\nDATA HIST:', data0 print 'Data name:', dataname if blind and 'BDT' in var: for bin in range(10,datas[0].GetNbinsX()+1): datas[0].SetBinContent(bin,0) #for bin in range(0,datas[0].GetNbinsX()+1): # print 'Data in bin x:', datas[0].GetBinContent(bin) histos.append(copy(Overlay)) if 'ZH' in signalList and 'WH' in signalList: typs.append('ZH') if 'ZH' in Stack.setup: Stack.setup.remove('ZH') if 'WH' in Stack.setup: Stack.setup.remove('WH') Stack.setup.insert(0,'ZH') elif 'ZH' in signalList: typs.append('ZH') elif 'WH' in signalList: typs.append('WH') elif 'VVb' in signalList: typs.append('VVb') print '\n-----> Stack.setup(double check)...' print 'Histos:', histos print 'typs:', typs Stack.histos = histos Stack.typs = typs Stack.datas = datas Stack.datatyps = datatyps Stack.datanames= datanames Stack.prefit_overlay = [prefit_overlay] if region == 'BDT': Stack.overlay = [Overlay] print '\n\n\t\t Overlay: ',Stack.overlay Stack.AddErrors=Error if dataname == 'Wtn': lumi = 18300. Stack.lumi = lumi Stack.doPlot() print 'i am done!\n'
def drawFromDC(): config = BetterConfigParser() config.read(opts.config) region = opts.region print "\nopts.config:", opts.config print "opts:", opts print "var:", opts.var print "bin:", opts.bin dataname = 'Zll' if 'Zuu' in opts.bin: dataname = 'Zuu' elif 'Zee' in opts.bin: dataname = 'Zee' elif 'Wmn' in opts.bin: dataname = 'Wmn' elif 'Wen' in opts.bin: dataname = 'Wen' elif 'Znn' in opts.bin: dataname = 'Znn' elif 'Wtn' in opts.bin: dataname = 'Wtn' if (opts.var == ''): var = 'BDT' if dataname == 'Zmm' or dataname == 'Zee': var = 'BDT_Zll' elif dataname == 'Wmn' or dataname == 'Wen': var = 'BDT_Wln' elif dataname == 'Znn': var = 'BDT_Znn' #if 'HighPt' in opts.bin: var = 'BDT_ZnnHighPt' #if 'LowPt' in opts.bin: var = 'BDT_ZnnLowPt' #if 'LowCSV' in opts.bin: var = 'BDT_ZnnLowCSV' if dataname == '' or var == 'BDT': raise RuntimeError, 'Did not recognise mode or var from ' + opts.bin else: var = opts.var if opts.var == 'BDT': if 'LowPt' in opts.bin: var = 'gg_plus_ZH125_low_Zpt' elif 'MedPt' in opts.bin: var = 'gg_plus_ZH125_med_Zpt' elif 'HighPt' in opts.bin: var = 'gg_plus_ZH125_high_Zpt' elif 'VV' in opts.bin: var = 'VV_bdt' else: var = 'gg_plus_ZH125_high_Zpt' #if 'BDT' in var: # region = 'BDT' #else: region = opts.bin ws_var = config.get('plotDef:%s' % var, 'relPath') print 'ws_var:', ws_var if opts.var == 'BDT': ws_var = ROOT.RooRealVar(ws_var, ws_var, -1., 1.) else: ws_var = ROOT.RooRealVar(ws_var, ws_var, 0, 1.) blind = eval(config.get('Plot:%s' % region, 'blind')) #blind = True # If Zvv change region to relevant CR if 'Znn' in region: if 'QCD' in opts.dc: region = 'Zvv_QCD' if 'TT' in opts.dc: region = 'Zvv_TT' if 'Zbb' in opts.dc: region = 'Zvv_Zbb' if 'Zlight' in opts.dc: region = 'Zvv_Zlight' print 'config:', config print 'var: ', var print 'region: ', region Stack = StackMaker(config, var, region, True) if 'low' in opts.bin or 'ch1_Wenu' == opts.bin or 'ch2_Wmunu' == opts.bin: Stack.addFlag2 = 'Low p_{T}(V)' elif 'MedPt' in opts.bin or 'ch1_Wenu2' == opts.bin or 'ch2_Wmunu2' == opts.bin: Stack.addFlag2 = 'Intermediate p_{T}(V)' elif 'high' in opts.bin or 'ch1_Wenu3' == opts.bin or 'ch2_Wmunu3' == opts.bin: Stack.addFlag2 = 'High p_{T}(V)' # check for pre or post fit options preFit = False addName = 'PostFit_%s' % (opts.fit) if not opts.mlfit: addName = 'PreFit' preFit = True print '\n-----> Fit Type(opts.fit) : ', opts.fit print ' (opts.mlfit): ', opts.mlfit print ' preFit : ', preFit print ' opts.bin : ', opts.bin Stack.options['pdfName'] = '%s_%s_%s.pdf' % (var, opts.bin, addName) log = eval(config.get('Plot:%s' % region, 'log')) if 'Zll' in opts.bin or 'Zee' in opts.bin or 'Zuu' in opts.bin or 'minCMVA' in opts.var or 'Zmm' in opts.bin: #setup = config.get('Plot_general','setup').split(',') setup = [ 'ZH', 'ggZH', 'DY2b', 'DY1b', 'DYlight', 'TT', 'VVHF', 'VVLF', 'ST' ] signalList = ['ZH'] #channel = 'ZllHbb' # For my own fits channel = '' if 'Zee' in opts.bin: lep_channel = 'Zee' elif 'Zuu' in opts.bin: lep_channel = 'Zmm' region_dic = { 'BDT': 'SIG', ' Zlf': 'Zlf', 'Zhf': 'Zhf', 'TT': 'TT', '13TeV': 'SIG' } region_name = [ region_dic[key] for key in region_dic if (key in opts.bin) ] if 'minCMVA' not in opts.var: region_name = region_name[0] else: if 'Zlf' in opts.bin: region_name = 'Zlf' if 'Zhf' in opts.bin: region_name = 'Zhf' if 'ttbar' in opts.bin: region_name = 'TT' pt_region_dic = { 'lowpt': 'low', 'highpt': 'high', 'LowPt': 'low', 'HighPt': 'high' } pt_region_name = [ pt_region_dic[key] for key in pt_region_dic if (key in opts.bin) ] #pt_region_name = pt_region_name[0] if 'low' in opts.bin: pt_region_name = 'low' if 'high' in opts.bin: pt_region_name = 'high' print 'region_name is', region_name print 'pt region_name is', pt_region_name if 'Wmn' in opts.bin or 'Wen' in opts.bin: setup = [ 'WH', 'ZH', 'DY2b', 'DY1b', 'DYlight', 'TT', 'VVHF', 'VVLF', 'ST', 'Wj0b', 'Wj1b', 'Wj2b' ] signalList = ['ZH', 'WH'] if 'Znn' in opts.bin: setup = [ 'ZH', 'ggZH', 'DY2b', 'DY1b', 'DYlight', 'TT', 'VVHF', 'ST', 'WH', 'Wj0b', 'Wj1b', 'Wj2b' ] signalList = ['ZH'] lep_channel = 'Znn' pt_region_name = 'HighPt' if opts.var == 'BDT': region_name = '' region_type = 'SR' print 'region_name is', region_name print 'region_type is', region_type print 'pt region_name is', pt_region_name print 'Lepton channel:', lep_channel if dataname == 'Zmm' or dataname == 'Zee': try: setup.remove('W1b') setup.remove('W2b') setup.remove('Wlight') setup.remove('WH') except: print '@INFO: Wb / Wligh / WH not present in the datacard' if not dataname == 'Znn' and 'QCD' in setup: setup.remove('QCD') Stack.setup = setup Dict = eval(config.get('LimitGeneral', 'Dict')) lumi = eval(config.get('Plot_general', 'lumi')) options = copy(opts) options.dataname = "data_obs" options.mass = 0 options.format = "%8.3f +/- %6.3f" options.channel = opts.bin options.excludeSyst = [] options.norm = False options.stat = False options.bin = True # fake that is a binary output, so that we parse shape lines options.out = "tmp.root" options.fileName = args[0] options.filename = region options.cexpr = False options.fixpars = False options.libs = [] options.verbose = 0 options.poisson = 0 options.nuisancesToExclude = [] options.noJMax = None theBinning = ROOT.RooFit.Binning(Stack.nBins, Stack.xMin, Stack.xMax) if 'Wmn' in opts.bin or 'Wen' in opts.bin or 'Znn' in opts.bin: # SET: WLV MINCSV BINNING if 'CSV' in opts.var: Stack.nBins = 15 print '\n\t Changing Wlv/Zvv CSV bins to ', 15 theBinning = ROOT.RooFit.Binning(15, Stack.xMin, Stack.xMax) print '/n----> The Binning:' print 'nBins:', Stack.nBins print 'xMin:', Stack.xMin print 'xMax:', Stack.xMax error_histos = [] histos = [] typs = [] shapes = {} shapesUp = [[] for _ in range(0, len(setup))] shapesDown = [[] for _ in range(0, len(setup))] sigCount = 0 Overlay = [] prefit_overlay = [] dirname = '' #### #Open the mlfit.root and retrieve the mc file = ROOT.TFile.Open(opts.mlfit) if file == None: raise RuntimeError, "Cannot open file %s" % opts.mlfit print '\n\n-----> Fit File: ', file if not ROOT.gDirectory.cd('shapes_fit_s'): print '@ERROR: didn\'t find the shapes_fit_s directory. Aborting' sys.exit() for dir in ROOT.gDirectory.GetListOfKeys(): dirinfo = dir.GetName().split('_') print 'dirinfo:', dirinfo if 'Znn' in opts.bin and 'BDT' in opts.var: print 'lepton channel, pt_region_name, region_type:', dirinfo[ 0], dirinfo[1], dirinfo[2] if not (dirinfo[0] == lep_channel and dirinfo[1] == pt_region_name and dirinfo[2] == region_type): continue elif len(dirinfo) == 5: print 'channel, lepton channel, region_name, pt_region_name:', dirinfo[ 0], dirinfo[1], dirinfo[2], dirinfo[3], dirinfo[4] if not (dirinfo[0] == channel and dirinfo[2] == lep_channel and dirinfo[3] == region_name and dirinfo[4] == pt_region_name): continue elif len(dirinfo) == 4: print 'channel, lepton channel, region_name, pt_region_name:', dirinfo[ 0], dirinfo[1], dirinfo[2], dirinfo[3] if not (dirinfo[1] == lep_channel and dirinfo[2] == region_name and dirinfo[3] == pt_region_name): continue print 'Directory:', dir.GetName() dirname = dir.GetName() ROOT.gDirectory.cd(dirname) for s in setup: found = False for subdir in ROOT.gDirectory.GetListOfKeys(): print 'subdir name is', subdir.GetName() if subdir.GetName() == Dict[s]: found = True # Set Histos postFit shapes and preFit errors hist = rebinHist( ROOT.gDirectory.Get(subdir.GetName()).Clone(), Stack.nBins, Stack.xMin, Stack.xMax) histos.append(hist) #error_histos.append(PostFit_Errors) typs.append(s) print 's is', s print 'signalList is', signalList if s in signalList: hist.SetTitle(s) Overlay.append(hist) print 'the Histogram title is', hist.GetTitle() if not found: print '@ERROR: didn\'t find the postfit histogram. Aborting' sys.exit() ROOT.gDirectory.cd('/shapes_prefit/' + dirname) total = rebinHist( ROOT.gDirectory.Get('total').Clone(), Stack.nBins, Stack.xMin, Stack.xMax) total.SetTitle('prefit') prefit_overlay.append(total) break # ================================================= ##### Read data print 'file is ', opts.dc dc_file = open(opts.dc, "r") os.chdir(os.path.dirname(opts.dc)) DC = parseCard(dc_file, options) if not DC.hasShapes: DC.hasShapes = True MB = ShapeBuilder(DC, options) data0 = MB.getShape(opts.bin, 'data_obs') if (data0.InheritsFrom("RooDataHist")): data0 = ROOT.RooAbsData.createHistogram(data0, 'data_obs', ws_var, theBinning) data0.SetName('data_obs') datas = [data0] datatyps = [None] datanames = [dataname] print '\nDATA HIST:', data0 print 'Data name:', dataname if opts.var == 'BDT': for bin in range(11, datas[0].GetNbinsX() + 1): datas[0].SetBinContent(bin, 0) # ======================================================= #if 'VV' in opts.bin: if isVV: signalList = ['VVHF', ' VVHF'] print 'Signal List:', signalList #histos.append(copy(Overlay)) if 'ZH' in signalList and 'WH' in signalList: #typs.append('WH') #if 'ZH' in Stack.setup: Stack.setup.remove('ZH') #if 'WH' in Stack.setup: Stack.setup.remove('WH') #Stack.setup.insert(0,'WH') #print 'Stack.setup:', Stack.setup typs.append('WH') typs.append('ZH') #elif 'ZH' in signalList: #Stack.setup.remove('WH') #typs.append('ggZH') # typs.append('ZH') if 'VVb' in signalList or 'VVHF' in signalList: #typs.append('WH') typs.append('ZH') typs.append('VVHF') if 'VVHF' in Stack.setup: Stack.setup.remove('VVHF') Stack.setup.insert(0, 'VVHF') if 'ZH' in Stack.setup: Stack.setup.remove('ZH') Stack.setup.insert(-1, 'ZH') if 'WH' in Stack.setup: Stack.setup.remove('WH') Stack.setup.insert(-1, 'WH') if 'ggZH' in Stack.setup: Stack.setup.remove('ggZH') Stack.setup.insert(-1, 'ggZH') print '\n-----> Stack.setup(double check)...', Stack print 'Post Histos:', histos print 'Datas:', datas print 'typs:', typs Stack.histos = histos Stack.typs = typs Stack.datas = datas Stack.datatyps = datatyps Stack.datanames = datanames Stack.filename = region #if opts.var is not 'BDT': #Stack.prefit_overlay = [prefit_overlay] #if '13TeV' in region: #Stack.overlay = [Overlay] print '\n\n\t\t Overlay: ', Stack.overlay # Add custom postFit errors #Stack.AddErrors = theErrorGraph if dataname == 'Wtn': lumi = 18300. Stack.lumi = lumi Stack.doPlot() print 'i am done!\n'