def plot(file, var, region): signalRegion = False stack = StackMaker(config, var, region, signalRegion) histosL = [] overlayL = [] print file datas = [] datasL = [] for th1 in get_th1(file): print th1.GetBinLowEdge(0) if 'VH' in th1.GetName(): overlayL.append(th1) th1.SetLineWidth(1) if 'data_obs' in th1.GetName(): datasL.append(th1) else: histosL.append(th1) print 'histoL' print histosL typs = [] typsL = [] overlay_typs = [] #append the name just once for histo in histosL: typsL.append(histo.GetName()) print histo.GetName() if 'VH' in histo.GetName(): overlay_typs.append(histo.GetName()) print typsL print 'Overlay list' print overlayL overlay_histo_dict = HistoMaker.orderandadd([{ overlay_typs[i]: overlayL[i] } for i in range(len(overlayL))], ['VH', 'VV']) overlayL2 = [] stack.histos = histosL stack.typs = typsL stack.datas = datasL stack.datanames = region for key in overlay_histo_dict: overlayL2.append(overlay_histo_dict[key]) appendix = '' stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_unweighted.' + opts.format) stack.lumi = 18940 stack.doPlot() print 'i am done!\n'
def plot(file,var,region): signalRegion = False stack = StackMaker(config,var,region,signalRegion) histosL = [] overlayL = [] print file datas = [] datasL = [] for th1 in get_th1(file): print th1.GetBinLowEdge(0) if 'VH' in th1.GetName(): overlayL.append(th1) th1.SetLineWidth(1) if 'data_obs' in th1.GetName(): datasL.append(th1) else: histosL.append(th1) print 'histoL' print histosL typs = [] typsL = [] overlay_typs=[] #append the name just once for histo in histosL: typsL.append(histo.GetName()) print histo.GetName() if 'VH' in histo.GetName(): overlay_typs.append(histo.GetName()) print typsL print 'Overlay list' print overlayL overlay_histo_dict = HistoMaker.orderandadd([{overlay_typs[i]:overlayL[i]} for i in range(len(overlayL))],['VH','VV']) overlayL2=[] stack.histos = histosL stack.typs = typsL stack.datas = datasL stack.datanames=region for key in overlay_histo_dict: overlayL2.append(overlay_histo_dict[key]) appendix = '' stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_unweighted.'+opts.format) stack.lumi = 18940 stack.doPlot() print 'i am done!\n'
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 plot(fileList): signalRegion = True region = 'plot' var = 'Hmass' stack = StackMaker(config,var,region,signalRegion) histosL = [] overlayL = [] #7-9 for the higgs #5-6 for the VV binmin=7 binmax=9 max_sb = 0 max_ssb = 0 for file in fileList: if max_sb < get_s_over_b(file,binmin,binmax): max_sb = get_s_over_b(file,binmin,binmax) if max_ssb < get_s_over_sb(file,binmin,binmax): max_ssb = get_s_over_sb(file,binmin,binmax) print max_ssb print max_sb for file in fileList: print file print get_s_over_b(file,binmin,binmax) if eval(opts.rescale) == False: max_sb = 1. max_ssb = 1. for th1 in get_th1(file): #th1.Sumw2() if 's/b' in opts.fom: th1.Scale(get_s_over_b(file,binmin,binmax)/max_sb) if 's/s+b' in opts.fom: th1.Scale(get_s_over_sb(file,binmin,binmax)/max_ssb) if 'VV' in th1.GetName(): th1.SetName('VV') if 'Zj1b' in th1.GetName(): th1.SetName('Zj2b') if 'Wj1b' in th1.GetName(): th1.SetName('Wj2b') # new stack for the overlay plot if 'VH' in th1.GetName() or 'VV' in th1.GetName(): overlayL.append(th1) histosL.append(th1) print 'histoL' print histosL typs = [] typsL = [] datas = [] datasL = [] overlay_typs=[] #append the name just once for histo in histosL: typsL.append(histo.GetName()) if 'data' in histo.GetName(): datasL.append(histo) if 'VH' in histo.GetName() or 'VV' in histo.GetName(): overlay_typs.append(histo.GetName()) #datasL.append(datas) #typsL.append(typs) print typsL print 'Overlay list' print overlayL overlay_histo_dict = HistoMaker.orderandadd([{overlay_typs[i]:overlayL[i]} for i in range(len(overlayL))],['VH','VV']) overlayL2=[] stack.histos = histosL stack.typs = typsL stack.datas = datasL # stack.datatyps = Ldatatyps[v] stack.datanames='data_obs' for key in overlay_histo_dict: overlayL2.append(overlay_histo_dict[key]) mjj_sub = eval(opts.sub) if not mjj_sub: stack.overlay = overlayL2 appendix = '' if(eval(opts.rescale) == True): appendix = '_rescaled_' if 's/s+b' in opts.fom: stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_combined78tev_postFit_s_over_sb'+appendix+'.'+opts.format) elif 's/b' in opts.fom: stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_combined78tev_postFit_s_over_b'+appendix+'.'+opts.format) else: stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_unweighted.'+opts.format) # stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_highPt_7tev.pdf') # stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_combined_postFit_s_over_b_Hpt_weight_1.pdf' stack.lumi = 18940 if mjj_sub == False: stack.doPlot() elif mjj_sub == True: stack.options['pdfName'] = stack.options['pdfName'].replace('.'+opts.format,'_subtracted.'+opts.format) stack.doSubPlot(['VH','VV']) print 'i am done!\n'
def plot(fileList): print '-----> Running def plot()...' signalRegion = True region = 'signal_Zuu_low_Zpt' var = 'HCSV_mass' # for subtracted bkg mass plot #mjj_sub = True opts.fom = 's/s+b' print '-----> Making Stacks...' stack = StackMaker(config, var, region, signalRegion) histosL = [] overlayL = [] #7-9 for the higgs #5-6 for the VV binmin = 7 binmax = 9 #binmin=5 #binmax=6 max_sb = 0 max_ssb = 0 for file in fileList: if max_sb < get_s_over_b(file, binmin, binmax): max_sb = get_s_over_b(file, binmin, binmax) if max_ssb < get_s_over_sb(file, binmin, binmax): max_ssb = get_s_over_sb(file, binmin, binmax) print '-----> max_ssb: ', max_ssb print '-----> max_sb : ', max_sb for file in fileList: #if eval(opts.rescale) == False: # max_sb = 1. # max_ssb = 1. for th1 in get_th1_allRegions(file): print 'Scaling factor for ', th1, ':', get_s_over_sb( file, binmin, binmax) if 's/b' in opts.fom: th1.Scale(get_s_over_b(th1, binmin, binmax) / max_sb) if 's/s+b' in opts.fom: print '--->Scaling by s/s+b...' #th1.Scale(get_s_over_sb(th1,binmin,binmax)/max_ssb) th1.Scale(get_s_over_sb(file, binmin, binmax)) if 'Zj1b' in th1.GetName(): th1.SetName('DY1b') if 'Zj2b' in th1.GetName(): th1.SetName('DY2b') if 'Zj0b' in th1.GetName(): th1.SetName('DYlight') if 's_Top' in th1.GetName(): th1.SetName('ST') if 'Wj1b' in th1.GetName(): th1.SetName('Wj1b') #if mjj_sub: # if 'VVHF' in th1.GetName(): # th1.SetName('VV') # if 'VH' in th1.GetName(): # th1.SetName('VH') # new stack for the overlay plot if 'VH' in th1.GetName() or 'VVHF' in th1.GetName( ) or 'ZH' in th1.GetName(): overlayL.append(th1) histosL.append(th1) print 'histoL' print histosL typs = [] typsL = [] datas = [] datasL = [] overlay_typs = [] #append the name just once for histo in histosL: typsL.append(histo.GetName()) if 'data' in histo.GetName(): datasL.append(histo) if 'VH' in histo.GetName() or 'VV' in histo.GetName( ) or 'ZH' in th1.GetName(): overlay_typs.append(histo.GetName()) if 'TT' in histo.GetName(): datasL.append(histo) # temp hack #datasL.append(datas) #typsL.append(typs) print typsL print 'Overlay list' print overlayL #overlay_histo_dict = HistoMaker.orderandadd([{overlay_typs[i]:overlayL[i]} for i in range(len(overlayL))],['VH','VV']) overlayL2 = [] stack.histos = histosL stack.typs = typsL stack.datas = datasL # stack.datatyps = Ldatatyps[v] stack.datanames = 'data_obs' #for key in overlay_histo_dict: # overlayL2.append(overlay_histo_dict[key]) mjj_sub = eval(opts.sub) #if not mjj_sub: # stack.overlay = overlayL2 appendix = '' if (eval(opts.rescale) == True): appendix = '_rescaled_' if 's/s+b' in opts.fom: stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_combined78tev_postFit_s_over_sb' + appendix + '.' + opts.format) elif 's/b' in opts.fom: stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_combined78tev_postFit_s_over_b' + appendix + '.' + opts.format) else: stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_unweighted.' + opts.format) # stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_highPt_7tev.pdf') # stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_combined_postFit_s_over_b_Hpt_weight_1.pdf' stack.lumi = 18940 if mjj_sub == False: print '\n\t----> Making Nominal Mass plot...' stack.doPlot() elif mjj_sub == True: print '\n\t----> Making Subtracted Mass plot...' stack.options['pdfName'] = stack.options['pdfName'].replace( '.' + opts.format, '_subtracted.' + opts.format) #stack.doSubPlot(['VH','VV']) stack.doSubPlot(['VVHF', 'ZH', 'ggZH']) print 'i am done!\n'
def log_s_over_b_allRegions(fileList, Region): print '-----> Running def log_s_over_b() for All Regions' #-------------- # log s over b #-------------- region_list = ['Zuu_high', 'Zee_high', 'Zuu_low', 'Zee_low'] histosL = {} s_b_d_histos = {} for file in fileList: for region in region_list: #print '\n\tRegion:', region #print file name = '%s' % file histosL[region] = [] for th1 in get_th1(file, region): if 'btag' in th1.GetName(): continue if '_stats_' in th1.GetName(): continue if '_j_' in th1.GetName(): continue if 'eff' in th1.GetName(): continue if 'LHE' in th1.GetName(): continue if 'total' in th1.GetName(): continue if 'total_signal' in th1.GetName(): continue if 'total_background' in th1.GetName(): continue #print '\n\t Retrieving TH1 from file: ', th1.GetName(), th1.Integral() histosL[region].append(th1) i = 0 j = 0 #print ' histosL:', histosL for hist in histosL[region]: if 'ZH' == hist.GetName() or 'ggZH' == 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: hBkg.Add(hist) i += 1 # temp hack hData = hBkg + hSignal s_b_d_histos[region] = {'b': hBkg, 's': hSignal, 'd': hData} bmin = -4 bmax = 0 nbins = 10 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: ', key, s_b_d #for bin in range(0,s_b_d['b'].GetNbinsX()+1): for bin in range(1, nbins + 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 print '\t\t Data Obs', d 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 = 12900. 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.filename = Region stack.doPlot()
def drawFromDC(): config = BetterConfigParser() config.read(opts.config) print "opts.config:", opts.config dataname = '' 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' print 'Variable printing' print opts.var 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' elif 'LowPt' in opts.bin: var = 'BDT_ZnnLowPt' elif 'LowCSV' in opts.bin: var = 'BDT_ZnnLowCSV' else: var = 'BDT_Znn' if dataname == '' or var == 'BDT': raise RuntimeError, "Did not recognise mode or var from %s" % opts.bin else: var = opts.var region = 'BDT' ws_var = config.get('plotDef:%s' % var, 'relPath') ws_var = ROOT.RooRealVar(ws_var, ws_var, -1., 1.) blind = eval(config.get('Plot:%s' % region, 'blind')) Stack = StackMaker(config, var, region, True) if 'LowPt' in opts.bin or 'ch1_Wenu' == opts.bin or 'ch2_Wmunu' == opts.bin: print 'Niklas %s' % 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)' else: Stack.addFlag2 = '' preFit = False addName = 'PostFit_%s' % (opts.fit) if not opts.mlfit: addName = 'PreFit' preFit = True 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)) DC = parseCard(file, options) if not DC.hasShapes: DC.hasShapes = True MB = ShapeBuilder(DC, options) theShapes = {} theSyst = {} nuiVar = {} 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) for b in DC.bins: 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, []] for (lsyst, nofloat, pdf, pdfargs, errline) in DC.systs: 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: continue # end skip systematics counter = 0 for p in DC.exp[b].keys( ): # so that we get only self.DC.processes contributing to this bin 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 '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() print "Original procs:", procs # 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 = {} 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'] #signalList = ['VVb'] for p in procs: b = opts.bin for s in setup: 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] 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: histos, Overlay, typs = getBestFitShapes(procs, theShapes, shapeNui, theBestFit, DC, setup, opts, Dict) counter = 0 errUp = [] total = [] errDown = [] nBins = histos[0].GetNbinsX() print 'total bins %s' % nBins 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 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] #Relative norm uncertainty of the individual MC for h in range(0, len(histos)): print "h:", h print "bin:", bin print "histos:", histos print "theNormUncert:", theNormUncert print "histos[h]:", histos[h] errUp[bin - 1].append(histos[h].GetBinContent(bin) * theNormUncert[histos[h].GetName()]) errDown[bin - 1].append(histos[h].GetBinContent(bin) * 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 "blind:", blind print "'BDT' in var:", 'BDT' in var if blind and 'BDT' in var: print "I'm blinding..." for bin in range(datas[0].GetNbinsX() / 2, datas[0].GetNbinsX() + 1): datas[0].SetBinContent(bin, 0) histos.append(copy(Overlay)) if 'ZH' in signalList and 'WH' in signalList: typs.append('VH') if 'ZH' in Stack.setup: Stack.setup.remove('ZH') if 'WH' in Stack.setup: Stack.setup.remove('WH') Stack.setup.insert(0, 'VH') elif 'ZH' in signalList: typs.append('ZH') elif 'WH' in signalList: typs.append('WH') elif 'VVb' in signalList: typs.append('VVb') print Stack.setup Stack.histos = histos Stack.typs = typs Stack.datas = datas Stack.datatyps = datatyps Stack.datanames = datanames Stack.overlay = [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 '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 plot(fileList): signalRegion = True #region = 'plot' region = opts.region var = 'HCSVmass' stack = StackMaker(config, var, region, signalRegion) histosL = [] overlayL = [] #7-9 for the higgs #5-6 for the VV binmin = 7 binmax = 9 max_sb = 0 max_ssb = 0 for file in fileList: if max_sb < get_s_over_b(file, binmin, binmax): max_sb = get_s_over_b(file, binmin, binmax) if max_ssb < get_s_over_sb(file, binmin, binmax): max_ssb = get_s_over_sb(file, binmin, binmax) print max_ssb print max_sb for file in fileList: print file print get_s_over_b(file, binmin, binmax) if eval(opts.rescale) == False: max_sb = 1. max_ssb = 1. for th1 in get_th1(file): #th1.Sumw2() if 's/b' in opts.fom: th1.Scale(get_s_over_b(file, binmin, binmax) / max_sb) if 's/s+b' in opts.fom: th1.Scale(get_s_over_sb(file, binmin, binmax) / max_ssb) if 'VV' in th1.GetName(): th1.SetName('VV') if 'Zj1b' in th1.GetName(): th1.SetName('Zj2b') if 'Wj1b' in th1.GetName(): th1.SetName('Wj2b') # new stack for the overlay plot if 'VH' in th1.GetName() or 'VV' in th1.GetName(): overlayL.append(th1) histosL.append(th1) print 'histoL' print histosL typs = [] typsL = [] datas = [] datasL = [] overlay_typs = [] #append the name just once for histo in histosL: typsL.append(histo.GetName()) if 'data' in histo.GetName(): datasL.append(histo) if 'VH' in histo.GetName() or 'VV' in histo.GetName(): overlay_typs.append(histo.GetName()) #datasL.append(datas) #typsL.append(typs) print typsL print 'Overlay list' print overlayL overlay_histo_dict = HistoMaker.orderandadd([{ overlay_typs[i]: overlayL[i] } for i in range(len(overlayL))], ['VH', 'VV']) overlayL2 = [] stack.histos = histosL stack.typs = typsL stack.datas = datasL # stack.datatyps = Ldatatyps[v] stack.datanames = 'data_obs' for key in overlay_histo_dict: overlayL2.append(overlay_histo_dict[key]) mjj_sub = eval(opts.sub) if not mjj_sub: stack.overlay = overlayL2 appendix = '' if (eval(opts.rescale) == True): appendix = '_rescaled_' if 's/s+b' in opts.fom: stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_combined78tev_postFit_s_over_sb' + appendix + '.' + opts.format) elif 's/b' in opts.fom: stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_combined78tev_postFit_s_over_b' + appendix + '.' + opts.format) else: stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_unweighted.' + opts.format) # stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_highPt_7tev.pdf') # stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_combined_postFit_s_over_b_Hpt_weight_1.pdf' stack.lumi = 18940 if mjj_sub == False: stack.doPlot() elif mjj_sub == True: stack.options['pdfName'] = stack.options['pdfName'].replace( '.' + opts.format, '_subtracted.' + opts.format) stack.doSubPlot(['VH', 'VV']) print 'i am done!\n'
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] = [] print 'file is', file 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 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_low_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 opts.var == 'CRBDT': if 'LowPt' in opts.bin: var = 'gg_plus_ZH125_low_Zpt_CR' elif 'MedPt' in opts.bin: var = 'gg_plus_ZH125_low_Zpt_CR' elif 'HighPt' in opts.bin: var = 'gg_plus_ZH125_high_Zpt_CR' else: var = 'gg_plus_ZH125_high_Zpt_CR' #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, -1, 1.) print 'config:', config print 'var: ', var #if 'Wen' in region: # region = 'WenHighPt' #if 'Wmn' in region: # region = 'WmnHighPt' if 'Znn' in region: region = 'Znn_13TeV_Signal' print 'region: ', region blind = eval(config.get('Plot:%s' % region, 'blind')) 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 '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' 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) ] region_name = 'SIG' if 'Zlf' in opts.bin: region_name = 'Zlf' if 'Zhf' in opts.bin: region_name = 'Zhf' if 'ttbar' in opts.bin: region_name = 'TT' if 'low' in opts.bin or 'Low' in opts.bin: pt_region_name = 'low' if 'high' in opts.bin or 'High' in opts.bin: pt_region_name = 'high' if 'med' in opts.bin or 'Med' in opts.bin: pt_region_name = 'med' # ['ZllHbb', 'ch1', 'Zmm', 'SIG', 'low'] if 'Zmm' in lep_channel and 'low' in pt_region_name: zll_index = 'ch1' elif 'Zmm' in lep_channel and 'high' in pt_region_name: zll_index = 'ch3' elif 'Zee' in lep_channel and 'high' in pt_region_name: zll_index = 'ch4' elif 'Zee' in lep_channel and 'low' in pt_region_name: zll_index = 'ch2' else: zll_index = 'ch2' # binning nBins = Stack.nBins xMin = Stack.xMin xMax = Stack.xMax # for stat hists if 'Zee' in lep_channel and 'low' in pt_region_name and 'minCMVA' not in opts.var: stat_name = opts.bin elif 'Zee' in lep_channel and 'high' in pt_region_name and 'minCMVA' not in opts.var: stat_name = opts.bin elif 'Zmm' in lep_channel and 'low' in pt_region_name and 'minCMVA' not in opts.var: stat_name = opts.bin elif 'Zmm' in lep_channel and 'high' in pt_region_name and 'minCMVA' not in opts.var: stat_name = opts.bin else: stat_name = opts.bin if 'minCMVA' in opts.var: if 'low' in pt_region_name: if 'Zmm' in lep_channel: if 'Zlf' in region_name: stat_name = 'Zlf_low_Zuu' if 'Zhf' in region_name: stat_name = 'Zhf_low_Zuu' if 'TT' in region_name: stat_name = 'ttbar_low_Zuu' else: if 'Zlf' in region_name: stat_name = 'Zlf_low_Zee' if 'Zhf' in region_name: stat_name = 'Zhf_low_Zee' if 'TT' in region_name: stat_name = 'ttbar_low_Zee' if 'high' in pt_region_name: if 'Zmm' in lep_channel: if 'Zlf' in region_name: stat_name = 'Zlf_high_Zuu' if 'Zhf' in region_name: stat_name = 'Zhf_high_Zuu' if 'TT' in region_name: stat_name = 'ttbar_high_Zuu' else: if 'Zlf' in region_name: stat_name = 'Zlf_high_Zee' if 'Zhf' in region_name: stat_name = 'Zhf_high_Zee' if 'TT' in region_name: stat_name = 'ttbar_high_Zee' if 'Wmn' in opts.bin or 'Wen' in opts.bin: setup = [ 'WH', 'ZH', 'Wj2b', 'Wj1b', 'Wj0b', 'DY2b', 'DY1b', 'DYlight', 'TT', 'VVHF', 'VVLF', 'ST' ] signalList = ['ZH', 'WH'] channel = 'WlnHbb' if 'Wen' in opts.bin: lep_channel = 'Wen' if 'tt' in region: region_name = 'ttWen' elif 'whf' in region: if 'High' in opts.bin: region_name = 'whfWenHigh' else: region_name = 'whfWenLow' elif 'wlf' in region: region_name = 'wlfWen' else: region_name = opts.bin if 'Wmn' in opts.bin: lep_channel = 'Wmn' if 'tt' in region: region_name = 'ttWmn' elif 'whf' in region: if 'High' in opts.bin: region_name = 'whfWmnHigh' else: region_name = 'whfWmnLow' elif 'wlf' in region: region_name = 'wlfWmn' else: region_name = opts.bin pt_region_name = 'none' nBins = 40 xMin = -1 xMax = 1 stat_name = 'BDT_' + opts.bin + '_' if 'Znn' in opts.bin: setup = [ 'ZH', 'ggZH', 'WH', 'DY2b', 'DY1b', 'DYlight', 'Wj2b', 'Wj1b', 'Wj0b', 'TT', 'VVHF', 'VVLF', 'ST' ] signalList = ['ZH'] channel = 'Znn' lep_channel = 'Znn' if 'nAddJet1' in opts.bin: pt_region_name = 'nAddJet1' elif 'nAddJet0' in opts.bin: pt_region_name = 'nAddJet0' else: pt_region_name = 'High' region_name = 'SR' nBins = 25 xMin = -1 xMax = 1 if 'QCD' in opts.dc: region_name = 'QCD' stat_name = 'Znn_13TeV_QCD' elif 'TT' in opts.dc: region_name = 'TT' stat_name = 'Znn_13TeV_TT' elif 'Zbb' in opts.dc: region_name = 'Zbb' stat_name = 'Znn_13TeV_Zbb' elif 'Zlight' in opts.dc: region_name = 'Zlight' stat_name = 'Znn_13TeV_Zlight' else: stat_name = opts.bin print '############' print 'Channel is', channel print 'lepton channel is', lep_channel print 'region_name is', region_name print 'pt region_name is', pt_region_name print '/n----> The Binning:' print 'nBins:', nBins print 'xMin:', xMin print 'xMax:', xMax 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(nBins, xMin, xMax) # for prefit erros prefit_error_histos = [] postfit_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 for dir in ROOT.gDirectory.GetListOfKeys(): dirinfo = dir.GetName().split('_') print 'dirinfo:', dirinfo if 'Znn' in dirinfo[0] and 'Znn' not in opts.bin: continue if 'ZllHbb' in dirinfo[0] and ('Zmm' not in lep_channel and 'Zee' not in lep_channel): continue if 'W' in dirinfo[0] and 'W' not in opts.bin: continue if 'W' in opts.bin: print 'channel, lepton channel, region_name:', channel, lep_channel, region_name #if not (dirinfo[0] == channel and dirinfo[1] == lep_channel and dirinfo[2] == region_name): if not (dirinfo[0] == region_name and dirinfo[1] == 'postfit'): continue else: print '!!! Match !!!' if 'Znn' in opts.bin: print 'channel, lepton channel, region_name, pt_region_name:', channel, lep_channel, region_name, pt_region_name #if not (dirinfo[0] == lep_channel and dirinfo[2] == pt_region_name and dirinfo[3] == 'postfit'): if not (dirinfo[0] == lep_channel and dirinfo[2] == region_name and dirinfo[3] == 'postfit'): continue else: print '!!! Match !!!' if 'Zuu' in opts.bin or 'Zee' in opts.bin: print 'channel, zll index, lepton channel, region_name, pt_region_name:', channel, zll_index, lep_channel, region_name, pt_region_name if not (dirinfo[0] == lep_channel and dirinfo[1] == region_name and dirinfo[2] == pt_region_name and dirinfo[3] == 'postfit'): continue else: print '!!! Match !!!' print 'Directory:', dir.GetName() dirname = dir.GetName() # Pull out the MC stat uncertainties first #hists_WenHighPt40.root #vhbb_WenHighPt40_13TeV.txt if 'W' in opts.bin: stat_filename = opts.dc.replace('.txt', '.root') stat_filename = stat_filename.replace('vhbb', 'hists') stat_filename = stat_filename.replace('_13TeV', '') else: stat_filename = opts.dc.replace('.txt', '.root') stat_filename = stat_filename.replace('DC_', '') print '\n Opening card for MC stat hists:', stat_filename print 'Dir name:', stat_name stat_file = ROOT.TFile.Open(stat_filename) if 'W' not in opts.bin: ROOT.gDirectory.cd(stat_name) stat_hists = {} for s in setup: for dir in ROOT.gDirectory.GetListOfKeys(): #print 'dir:', dir.GetName() if 'W' in opts.bin: wlvname = dir.GetName().replace(stat_name, '') if wlvname == Dict[s]: stat_hists[wlvname] = ROOT.gDirectory.Get( dir.GetName()).Clone() if dir.GetName() == Dict[s]: stat_hists[dir.GetName()] = ROOT.gDirectory.Get( dir.GetName()).Clone() #stat_file.Close() print '\nStat_hists:', stat_hists file = ROOT.TFile.Open(opts.mlfit) #ROOT.gDirectory.cd('shapes_fit_s') #ROOT.gDirectory.cd(fit_dir) ROOT.gDirectory.cd(dirname) subdir_list = [x for x in ROOT.gDirectory.GetListOfKeys()] for s in setup: print '\ns:', s found = False #for subdir in ROOT.gDirectory.GetListOfKeys(): for subdir in subdir_list: print 'subdir name is', subdir.GetName() print 'Dict Key is', Dict[s] if subdir.GetName() == Dict[s]: found = True # Set Histos postFit shapes and preFit errors hist = rebinHist( ROOT.gDirectory.Get(subdir.GetName()).Clone(), nBins, xMin, xMax, dirname, subdir, prefit_error_histos, postfit_error_histos, stat_hists) histos.append(hist) typs.append(s) if s in signalList: hist.SetTitle(s) Overlay.append(hist) #print 'the Histogram title is', hist.GetTitle() break if not found: print '@ERROR: didn\'t find the postfit histogram. Aborting' hist = ROOT.TH1F(Dict[s], Dict[s], nBins, xMin, xMax) histos.append(hist) typs.append(s) #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 # Get the total pre/post fit error #print '\n Calculating final pre/post fit errors' #print prefit_error_histos for i, iErrorHist in enumerate(prefit_error_histos): #print i,iErrorHist if i == 0: temp_prefit_error = iErrorHist.Clone() else: temp_prefit_error.Add(iErrorHist) for i, iErrorHist in enumerate(postfit_error_histos): #print i,iErrorHist if i == 0: temp_postfit_error = iErrorHist.Clone() else: temp_postfit_error.Add(iErrorHist) final_prefit_error = ROOT.TGraphAsymmErrors(temp_prefit_error) final_postfit_error = ROOT.TGraphAsymmErrors(temp_postfit_error) total = [[]] * nBins errUp = [[]] * nBins errDown = [[]] * nBins total_post = [[]] * nBins errUp_post = [[]] * nBins errDown_post = [[]] * nBins # rebin the final errors for bin in range(1, nBins + 1): binError = temp_prefit_error.GetBinError(bin) total[bin - 1] = temp_prefit_error.GetBinContent(bin) errUp[bin - 1] = [binError] errDown[bin - 1] = [binError] binError_post = temp_postfit_error.GetBinError(bin) total_post[bin - 1] = temp_postfit_error.GetBinContent(bin) errUp_post[bin - 1] = [binError_post] errDown_post[bin - 1] = [binError_post] #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] totErrUp_post = [sqrt(sum([x**2 for x in bin])) for bin in errUp_post] totErrDown_post = [sqrt(sum([x**2 for x in bin])) for bin in errDown_post] for bin in range(1, nBins + 1): if not total[bin - 1] == 0: point = histos[0].GetXaxis().GetBinCenter(bin) final_prefit_error.SetPoint(bin - 1, point, 1) final_prefit_error.SetPointEYlow( bin - 1, totErrDown[bin - 1] / total[bin - 1]) final_prefit_error.SetPointEYhigh( bin - 1, totErrUp[bin - 1] / total[bin - 1]) if not total_post[bin - 1] == 0: point = histos[0].GetXaxis().GetBinCenter(bin) final_postfit_error.SetPoint(bin - 1, point, 1) final_postfit_error.SetPointEYlow( bin - 1, totErrDown_post[bin - 1] / total_post[bin - 1]) final_postfit_error.SetPointEYhigh( bin - 1, totErrUp_post[bin - 1] / total_post[bin - 1]) # ================================================= ##### Read data print '\n#### Datafile 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') print data0 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': # print '!!!! Blinding !!!!' # if 'Zee' in dataname or 'Zuu' in dataname: # for bin in range(4,datas[0].GetNbinsX()+1): # datas[0].SetBinContent(bin,0) # if 'Znn' in dataname: # for bin in range(20,datas[0].GetNbinsX()+1): # datas[0].SetBinContent(bin,0) # if 'W' in dataname: # for bin in range(4,datas[0].GetNbinsX()+1): # #print datas[0].GetBinContent(bin,0) # datas[0].SetBinContent(bin,0) #else: for bin in range(0, datas[0].GetNbinsX() + 1): print datas[0].GetBinContent(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') Stack.nBins = nBins Stack.xMin = xMin Stack.xMax = xMax 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 = final_prefit_error Stack.AddErrors_Postfit = final_postfit_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 #Should Read this from the parser datanames = config.get('dc:%s'%opts.bin,'data').split(' ') print 'dataname is', datanames region = opts.bin var = opts.var ws_var = config.get('plotDef:%s'%var,'relPath') nbin = int(config.get('plotDef:%s'%var,'nBins')) xmin = float(config.get('plotDef:%s'%var,'min')) xmax = float(config.get('plotDef:%s'%var,'max')) blind = eval(opts.blind) postfit = eval(opts.postfit) print 'config:', config print 'var: ', var print 'region: ', region print 'blind: ', blind print 'postfit: ', postfit Group_dc = eval(config.get('Plot_general','Group_dc')) Stack=StackMaker(config,var,region,True) # 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')) if 'Zee' in opts.bin or 'Zuu' in opts.bin: #VH setup = ['ggZHbb', 'qqZHbb','Zbb','Zb','Z_udscg','TT','VV2b','VVlight','ST'] #VV #setup = ['VV2b','ggZHbb','qqZHbb','Z_udscg','Zb','Zbb','TT','VVlight','ST'] channel = 'ZllHbb' if 'Zee' in opts.bin: lep_channel = 'Zee' elif 'Zuu' in opts.bin: lep_channel = 'Zuu' #region_dic = {'BDT':'SIG','CRZlight':'Zlf','CRZb':'Zhf','CRttbar':'TT'} region_dic = { 'BDT':'BDT', 'CRZlight':'CRZlight', 'CRZb':'CRZb', 'CRttbar':'CRttbar', 'ZeeMass_lowpt':'ZeeMass_lowpt', 'ZeeMass_highpt':'ZeeMass_highpt', 'ZuuMass_lowpt':'ZuuMass_lowpt', 'ZuuMass_highpt':'ZuuMass_highpt', 'ZeeMassVV_lowpt':'ZeeMassVV_lowpt', 'ZeeMassVV_highpt':'ZeeMassVV_highpt', 'ZuuMassVV_lowpt':'ZuuMassVV_lowpt', 'ZuuMassVV_highpt':'ZuuMassVV_highpt', 'ZuuMass_Vptbin0':'ZuuMass_Vptbin0', 'ZuuMass_Vptbin1':'ZuuMass_Vptbin1', 'ZuuMass_Vptbin2':'ZuuMass_Vptbin2', 'ZeeMass_Vptbin0':'ZeeMass_Vptbin0', 'ZeeMass_Vptbin1':'ZeeMass_Vptbin1', 'ZeeMass_Vptbin2':'ZeeMass_Vptbin2', } print 'opts.bin is', opts.bin region_name = [region_dic[key] for key in region_dic if (key in opts.bin)] region_name = region_name[0] print 'region_name is', region_name pt_region_dic = {'lowpt':'lowpt','highpt':'highpt','bin0':'bin0','bin1':'bin1','bin2':'bin2'} 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] else: print '@ERROR: This is not a Zll region. Aborting' sys.exit() Stack.setup = setup Dict = eval(config.get('LimitGeneral','Dict')) lumi = eval(config.get('General','lumi')) Stack.nBins = nbin Stack.xMin = xmin Stack.xMax = xmax print '/n----> The Binning:' print 'nBins:', Stack.nBins print 'xMin:', Stack.xMin print 'xMax:', Stack.xMax theBinning = ROOT.RooFit.Binning(Stack.nBins,Stack.xMin,Stack.xMax) ################# #We are now skiping a large part of the orginal code, as everything that remains to be done is to read the postfit plot from the mlfit.root #How should the scale sys (lnN be handled) ? # histos = [] typs = [] shapes = {} shapesUp = [[] for _ in range(0,len(setup))] shapesDown = [[] for _ in range(0,len(setup))] #signalList = ['ggZHbb', 'qqZHbb'] #signalList = [] sigCount = 0 #Overlay ={} Overlay = [] prefit_overlay = [] postfit_from_wc = False if opts.mlfit.split('/')[-1] != 'mlfit.root': postfit_from_wc = True dirname = '' #### #Open the mlfit.root and retrieve the mc print 'opts.mlfit is', opts.mlfit file = ROOT.TFile.Open(opts.mlfit) #if file == None: raise RuntimeError, "Cannot open file %s" % theFile #print '\n\n-----> Fit File: ',file print '=====================' print 'POSTFIT IS', postfit print '=====================' if postfit: if not postfit_from_wc: if not ROOT.gDirectory.cd('shapes_fit_s'): print '@ERROR: didn\'t find the shapes_fit_s directory. Aborting' sys.exit() else: pass else: if not ROOT.gDirectory.cd('shapes_prefit'): print '@ERROR: didn\'t find the shapes_prefit directory. Aborting' sys.exit() folder_found = False for dir in ROOT.gDirectory.GetListOfKeys(): dirinfo = dir.GetName().split('_') print 'dir name is', dir.GetName().split('_') ##if not (dirinfo[0] == channel and dirinfo[2] == lep_channel and dirinfo[3] == region_name and dirinfo[4] == pt_region_name): print 'dirinfo is', dirinfo print 'lep_channel is', lep_channel print 'region_name is', region_name print 'pt_region_name is', pt_region_name if not (dirinfo[0] == lep_channel and dirinfo[1] == region_name and dirinfo[2] == pt_region_name): #for VV if not (dirinfo[2] == region_name.split('_')[0] and dirinfo[3] == pt_region_name): if not (len(dirinfo) > 3 and dirinfo[3] == region_name.split('_')[0] and dirinfo[4] == 'Vpt'+pt_region_name): continue if postfit_from_wc and 'prefit' in dirinfo: continue folder_found = True dirname = dir.GetName() #signal, use prefit for s in setup: if ('ZHbb' in s and postfit) and not postfit_from_wc: print 'ERROR' sys.exit() ROOT.gDirectory.cd('/shapes_prefit') ROOT.gDirectory.cd(dirname) found = False for subdir in ROOT.gDirectory.GetListOfKeys(): #print 'subdir name is', subdir.GetName() if subdir.GetName() == Dict[s]: found = True hist = rebinHist(gDirectory.Get(subdir.GetName()).Clone(), nbin, xmin, xmax) histos.append(hist) 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() else: #SF_ZJets = [0.95188, 0.94404, 1.0463] #SF_TTbar = 1.0373 #;Vpt high #SF_ZJets = [1.1235, 0.91368, 1.2435] #SF_TTbar = 1.0601 #Start be getting the SF print 'Gonna apply SF' scale = 1 #if 'low' in opts.dc: # if 'TT' in s: scale = 1.01 # if 'Z_udscg' in s: scale = 0.96 # if 'Zb' in s: scale = 0.99 # if 'Zbb' in s : scale = 1.04 #elif 'high' in opts.dc: # if 'TT' in s: scale = 1.01 # if 'Z_udscg' in s: scale = 1.03 # if 'Zb' in s: scale = 0.96 # if 'Zbb' in s: scale = 1.23 #else: # pass # #ROOT.gDirectory.cd('/shapes_fit_s') #if not postfit_from_wc: # ROOT.gDirectory.cd('/shapes_prefit') # print 'ERROR2' # sys.exit() #else: # scale = 1 ROOT.gDirectory.cd(dirname) found = False for subdir in ROOT.gDirectory.GetListOfKeys(): print 'subdir name is', subdir.GetName() #print 'Dict is ', Dict if subdir.GetName() == Dict[s] or (postfit_from_wc and subdir.GetName() == s): found = True hist = rebinHist(gDirectory.Get(subdir.GetName()).Clone(), nbin, xmin, xmax, scale) histos.append(hist) 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() #take prefit distr. for signal if not found: print '@ERROR: didn\'t find the postfit histogram. Aborting' sys.exit() if not postfit_from_wc: ROOT.gDirectory.cd('/shapes_prefit/'+dirname) print 'ERROR3' sys.exit() if not postfit_from_wc: total = rebinHist(gDirectory.Get('total').Clone(), nbin, xmin, xmax) total.SetTitle('prefit') prefit_overlay.append(total) break if not folder_found: print '@ERROR: Folder was not found.' print 'lep_channel', lep_channel print 'region_name', region_name print 'pt_region_name', pt_region_name sys.exit() #retrieve the data 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 #This needs to be done from the "dc" root file 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] if blind and 'BDT' in var: for bin in range(datas[0].GetNbinsX()-3,datas[0].GetNbinsX()+1): datas[0].SetBinContent(bin,0) if blind and 'Mass' in var: for bin in range(datas[0].GetNbinsX()-13,datas[0].GetNbinsX()-7): datas[0].SetBinContent(bin,0) datatyps = [None] #print '\nshapes!!!', shapes print '\nOVERLAY!!!', Overlay #Add all the histos and overlay to the stackmaker such that they can be ploted #print 'before Stack, histos are', histos #sys.exit() Stack.histos = histos Stack.typs = typs Stack.overlay = Overlay Stack.prefit_overlay = prefit_overlay Stack.datas = datas Stack.datatyps = datatyps Stack.datanames= datanames Stack.AddErrors= True Stack.lumi = lumi if 'BDT' in var: Stack.forceLog = True 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' #dataname = 'SingleElectron_Run2016B_PromptReco' dataname = ['SingleElectron_Run2016B_PromptReco','SingleElectron_Run2016C_PromptReco','SingleElectron_Run2016D_PromptReco','SingleElectron_Run2016F_PromptReco','SingleElectron_Run2016G_PromptReco'] #if 'Zuu' in opts.bin: dataname = 'Zuu' #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 opts.var == 'BDT': if 'LowPt' in opts.bin: var = 'gg_plus_ZH125_low_Zpt' if 'MedPt' in opts.bin: var = 'gg_plus_ZH125_med_Zpt' if 'HighPt' in opts.bin: var = 'gg_plus_ZH125_high_Zpt' if 'VV' in opts.bin: var = 'VV_bdt' #if 'BDT' in var: # region = 'BDT' #else: region = opts.bin var = opts.var ws_var = config.get('plotDef:%s'%var,'relPath') if 'gg_plus' in var: 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 #blind = False blind = eval(opts.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 = ['Z_udscg','Zb','Zbb','TT','VVlight','ST','VV2b','qqZHbb','ggZHbb'] 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')) #Dict = eval(config.get('Plot_general','Group')) 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 = ['Zbb','WH'] #signalList = ['Zbb'] signalList = ['ZH'] #signalList = ['ggZHbb','qqZHbb'] Overlay ={} # 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 #print Dict[s], p if Dict[s] != p: print 'not equal', p print 'not equal', Dict[s] 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] datanames= dataname print '\nDATA HIST:', data0 print 'Data name:', dataname if blind: for bin in range(12,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) if 'VV' in opts.bin: signalList = ['VVb',' VVlight'] print 'Signal List:', signalList histos.append(copy(Overlay)) if 'Zbb' in signalList and 'WH' in signalList: typs.append('Zbb') if 'Zbb' in Stack.setup: Stack.setup.remove('Zbb') if 'WH' in Stack.setup: Stack.setup.remove('WH') Stack.setup.insert(0,'Zbb') elif 'Zbb' in signalList: typs.append('Zbb') elif 'WH' in signalList: typs.append('WH') elif 'ZH' in signalList: typs.append('ZH') if 'VVb' in signalList: typs.append('VVb') #typs.append('VVlight') 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 '13TeV' in region: 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 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 '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'