def addSRdata(region='SR_OS', pol=''): # first get QCD estimate with data driven W if pol: groups = mu.MC + [getattr(mu, pol)] else: groups = mu.MC + [mu.signal] data = hist.get_QCDW(rf, mu, entry, 'SR_SS', rqcd=rQCD, verbose=False, groups=groups) for group in groups: if group.name == 'Wlnu': h = hist.get_dataW(rf, mu, entry, 'WCR_OS', verbose=False, groups=groups) else: h = hist.get_OS(rf, entry, group, region) data.Add(h) data.SetName('h_upsilon_SRData%s_SR_OS' % pol) for bin in xrange(data.GetNbinsX() + 1): data.SetBinError(bin, math.sqrt(data.GetBinContent(bin))) print ErrorFloat(*hist.IntegralError(data)) #check similar errors #d1 = rf.Get('h_visMass_Muons_SR_OS') #print ErrorFloat(*hist.IntegralError(d1)) data.Write()
def compare_scales(): rf = ROOT.TFile('../output/%s/%s_regions.root' % (stream + '_Zpt_NoWeight', stream)) wf = ROOT.TFile('../output/%s/%s_regions.root' % (stream + '_Zpt', stream)) tname = 'h_upsilon_Ztautau_SR_OS' lname = 'h_upsilon_LH_Ztautau_SR_OS' rname = 'h_upsilon_RH_Ztautau_SR_OS' for name in [tname, lname, rname]: t1 = rf.Get(name).Clone('t1') t2 = wf.Get(name).Clone('t2') n1 = ErrorFloat(*hist.IntegralError(t1)) n2 = ErrorFloat(*hist.IntegralError(t2)) print n1 print n2 print n2 / n1
def stat_error(mu): rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR,mu.name)) k_entry = Drawer.upsilon_28 wSR_MC = rf.Get('h_%s_%s_%s' % (k_entry.name,mu.Wlnu.name,'SR_OS')) nMC = ErrorFloat(*hist.IntegralError(wSR_MC)) # print ErrorFloat( wSR_MC.GetBinContent(10), wSR_MC.GetBinError(10) ) kW_OS = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS') # print kW_OS * nMC # wSR_MC.Scale(kW_OS.val) for bin in xrange(wSR_MC.GetNbinsX()+1): bi0 = ErrorFloat(wSR_MC.GetBinContent(bin), wSR_MC.GetBinError(bin)) bi1 = bi0 * ErrorFloat(kW_OS.val,0) bi2 = bi0 * kW_OS # print bi0,bi1,bi2 wSR_MC.SetBinContent(bin,bi2.val) wSR_MC.SetBinError(bin,bi2.err) nMCkw = ErrorFloat(*hist.IntegralError(wSR_MC)) print nMCkw
def backgrounds(mu): rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR,mu.name)) #rf = ROOT.TFile('~/panalysis/output/%s_Zpt_NoWeight/%s_regions.root' % (mu.name,mu.name)) eff = True f = False fcol=15;col=15;fmt="latex" entry = Drawer.visMass k_entry = Drawer.upsilon_CR regions = mu.cutflow #regions = mu.SR verbose = True groups = [mu.Zltau,mu.Zlljet,mu.ttbar,mu.Wlnu] kW_OS = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose) a = [] for i,region in enumerate(regions): cutgroup = [] n = [] mc = 0 for group in groups: h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region.name)) if 'W' not in group.name: cutgroup.append( '%i +/- %i' % hist.IntegralError(h)) n.append(h.Integral(0,h.GetNbinsX()+1)) else: nW = ErrorFloat(*hist.IntegralError(h))*kW_OS cutgroup.append('%i +/- %i' % (nW.val,nW.err)) n.append(nW.val) if group.name not in ['Muons','Egamma','LH_Ztautau','RH_Ztautau']: mc += h.Integral(0,h.GetNbinsX()+1) if i == 0: a = list(n); l=a else: print row(cuts[i]+'\t\t',cutgroup,fcol=fcol,col=col, fmt = fmt) if f: print row(region.name, [''] + [x/mc for x in n[1:]],fcol=fcol,col=col,fmt=fmt) if eff: print row('',[x/y for (x,y) in zip(n,a)],fcol=fcol,col=col, fmt = fmt) print row('',[x/y for (x,y) in zip(n,l)],fcol=fcol,col=col, fmt = fmt) l = n
def print_region(mu,reg, pdg=False):#,regions,groups,entry=Drawer.upsilon_CR,eff=False,twiki=False, fmt = None, f = False): a = [] rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR, mu.name)) regions = getattr(mu,reg) groups = [mu.data, mu.signal, mu.LH, mu.RH, mu.ttbar, mu.Zlljet, mu.Zltau, mu.Wlnu] entry = Drawer.upsilon_CR fmt = '' if pdg: fmt = 'latex' print row('',[group.name for group in groups],fcol=fcol,col=col,fmt=fmt) for i,region in enumerate(regions): cutgroup = [] n = [] mc = 0 nEW = 0 for group in groups: if 'Wlnu' in group.name: wreg = region.name.replace('QCD','WCR') wreg = wreg.replace('SR','WCR') #print region h = hist.get_dataW(rf,mu,entry, wreg, where = reg) #print h.Integral() else: h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region.name)) norm = h.Integral(0,h.GetNbinsX()+1) if group.isData: cutgroup.append(int(norm)) else: #print h.GetName() v,e = hist.IntegralError(h, of = True) if pdg: cutgroup.append( '%g +/- %g' % pdg_simple.pdg_round(v,e)) else: cutgroup.append( '%.2f +/- %.2f' % (v,e)) n.append(norm) if group.isData: nData = norm elif 'LH' in group.name: nL = norm elif 'RH' in group.name: nR = norm elif 'Ztautau' in group.name: nS = norm elif 'Wlnu' not in group.name: nEW += norm if i == 0: a = list(n); l=a #if twiki: print twikirow(region.name,cutgroup) print row(region.name,cutgroup,fcol=fcol,col=col, fmt = fmt)
def sim_modes(sim="Reco"): rf = ROOT.TFile('~/analysis/truth/cxx/thesis/PythiaFull30.root') entries = ['upsilon','upsilon20','upsilon40','upsilon60'] modes = ['0','3','4','5'] fracs = [] for i,mode in enumerate(modes): fracs.append([]) for j,entry in enumerate(entries): fracs[i].append( ErrorFloat(*hist.IntegralError(rf.Get('%s_%s_%s_LH' % (sim,entry,mode))))) fracs[i].append(ErrorFloat(*hist.IntegralError(rf.Get('%s_%s_%s_RH' % (sim,entry,mode))))) nTotal = list(fracs[0]) nOther = None for i,m in enumerate(modes): if i == 1: nOther = list(fracs[0]) for j in xrange(len(fracs[i])): fracs[i][j] = fracs[i][j]/nTotal[j] fracs[i][j] = round(fracs[i][j].val,2) if nOther: nOther[j]-=fracs[i][j] print row( modelegend[m], fracs[i], fmt='latex' , flt = '%.2f') print row('Other',nOther, fmt = 'latex', flt = '%.2f')
def sensitivity(rh, lh, p): c = ROOT.TCanvas() lh.Scale(1.0 / (lh.Integral())) #(0,lh.GetNbinsX()+1))) rh.Scale(1.0 / (rh.Integral())) #(0,rh.GetNbinsX()+1))) summ = lh.Clone() diff = lh.Clone() multi = lh.Clone() ratio = lh.Clone() sump = lh.Clone() summ.Add(lh, rh, 1.0, 1.0) diff.Add(rh, lh, 1.0, -1.0) scale = 1 / (summ.Integral()) #(0,summ.GetNbinsX()+1 )) summ.Scale(scale) diff.Scale(scale) summ.SetLineColor(2) diff.SetLineColor(4) summ.SetAxisRange(-1, 1) diff.SetAxisRange(-1, 1) summ.SetMinimum(1.2 * diff.GetMinimum()) summ.Draw() diff.Draw("SAME") #diff.Draw() multi.Multiply(diff, diff) sump.Add(summ, diff, 1, p) ratio.Divide(multi, sump) c.SaveAs('sensTest.gif+1') s_square = ErrorFloat(*hist.IntegralError(ratio)) s = math.sqrt(s_square.val) s_err = 0.5 * s_square.err / s_square.val return ErrorFloat(round(s, 2), round(s_err, 2))
def print_region(rf,regions,groups,entry=Drawer.upsilon,eff=False,twiki=False, fmt = None, f = False): a = [] for i,region in enumerate(regions): cutgroup = [] n = [] mc = 0 for group in groups: h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region.name)) cutgroup.append( '%i +/- %i' % hist.IntegralError(h)) n.append(h.Integral(0,h.GetNbinsX()+1)) if group.name not in ['Muons','Egamma','LH_Ztautau','RH_Ztautau']: #print group.name mc += h.Integral(0,h.GetNbinsX()+1) if i == 0: a = list(n); l=a if twiki: print twikirow(region.name,cutgroup) else: print row(region.name,cutgroup,fcol=fcol,col=col, fmt = fmt) if f: print row(region.name, [''] + [x/mc for x in n[1:]],fcol=fcol,col=col,fmt=fmt) if eff: print row('',[x/y for (x,y) in zip(n,a)],fcol=fcol,col=col, fmt = fmt) print row('',[x/y for (x,y) in zip(n,l)],fcol=fcol,col=col, fmt = fmt) l = n
def cutflow(mu, pdg = True, smnorm = False): rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR, mu.name)) eff = False do_pol = False f = False fcol=25;col=22;fmt="latex" entry = Drawer.upsilon regions = mu.noteflow pt = -0.144 groups = [mu.data, mu.signal, mu.LH, mu.RH, mu.ttbar, mu.Zlljet, mu.Zltau, mu.Wlnu] a = [] for i,region in enumerate(regions): cutgroup = [] n = [] mc = 0 nL = None for group in groups: if group.isData: h = rf.Get('h_%s_%s_%s' % ('upsilon',group.name,region.name)) else: h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region.name)) #print 'h_%s_%s_%s' % (entry.name,group.name,region.name) norm = h.Integral(0,h.GetNbinsX()+1) if group.isData: cutgroup.append(int(norm)) else: v,e = hist.IntegralError(h, of = True) if smnorm: if 'LH' in group.name: v = v*sm_lh e = e*sm_lh if 'RH' in group.name: v = v*sm_rh e = e*sm_rh if pdg: cutgroup.append( '%i +/- %i' % pdg_simple.pdg_round(v,e)) else: cutgroup.append( '%.2f +/- %.2f' % (v,e)) n.append(h.Integral(0,h.GetNbinsX()+1)) if 'L' in group.name: nL = norm elif 'R' in group.name: nR = norm if group.name not in ['Muons','Egamma','LH_Ztautau','RH_Ztautau']: #print group.name mc += h.Integral(0,h.GetNbinsX()+1) if i == 0: a = list(n); l=a print row('', [group.name for group in groups], fcol =fcol, col = col) if nL and do_pol: nL = 0.5*(1-pt)*nL nR = 0.5*(1+pt)*nR print row(cuts[i]+'\t\t',cutgroup + [(nR-nL)/(nR+nL)],fcol=fcol,col=col, fmt = fmt) else: #print row(region.name , cutgroup , fcol=fcol,col=col, fmt = fmt) print row(cuts[i], cutgroup , fcol=fcol,col=col, fmt = fmt) #print (nR-nL)/nR if f: print row(region.name, [''] + [x/mc for x in n[1:]],fcol=fcol,col=col,fmt=fmt) if eff: print row('',[x/y for (x,y) in zip(n,a)],fcol=fcol,col=col, fmt = fmt) print row('',[x/y for (x,y) in zip(n,l)],fcol=fcol,col=col, fmt = fmt) l = n
def signal_region(mu): #rf = ROOT.TFile('~/panalysis/output/%s_thesis/test/%s_regions.root' % (mu.name,mu.name)) rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR, mu.name)) region = 'SR_OS' k_entry = Drawer.upsilon_CR entry = Drawer.upsilon verbose = False fmt = 'latex' kW_OS = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose) kW_OS_L = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose, groups = mu.MC+[mu.LH]) kW_OS_R = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose, groups = mu.MC+[mu.RH]) pdg_simple.testpdg(kW_OS.val,kW_OS.err) print 'kWOS %g +/ %g' % ( pdg_simple.pdg_round(kW_OS.val,kW_OS.err)) EST = [ErrorFloat(0),ErrorFloat(0),ErrorFloat(0)] BG = [ErrorFloat(0),ErrorFloat(0),ErrorFloat(0)] for group in [mu.data,mu.Zlljet,mu.Zltau,mu.ttbar]: h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region)) nG = ErrorFloat(*hist.IntegralError(h)) if group.isData: print row( group.latex, [int(nG.val),'',''], fmt=fmt) else: print row( group.latex, ['%i +/- %i'%(nG.val,nG.err),'',''], fmt=fmt, makeint=True) if not group.isData: for i in xrange(len(EST)): EST[i]+=nG BG[i]+=nG print row('', ['SM','Left-Handed','Right-Handed'], fmt = fmt) signal = [] SB = [ErrorFloat(0),ErrorFloat(0),ErrorFloat(0)] for i,group in enumerate([mu.signal, mu.LH, mu.RH]): h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region)) nG = ErrorFloat(*hist.IntegralError(h)) signal.append('%i +/- %i'%(nG.val,nG.err)) EST[i]+=nG SB[i]=nG print row(mu.signal.latex, signal, fmt=fmt, makeint = True) wjets = [] for i,group in enumerate([mu.signal, mu.LH, mu.RH]): h = hist.get_dataW(rf, mu, Drawer.upsilon, 'WCR_OS', verbose = False, groups = mu.MC + [group]) print '**' nG = ErrorFloat(*hist.IntegralError(h)) wjets.append('%g +/- %g'%( pdg_simple.pdg_round((nG).val,(nG).err))) EST[i]+=nG BG[i]+=nG print row(mu.Wlnu.latex, wjets, fmt=fmt) #print row(group.latex, [nG]) QCD = [] for i,group in enumerate([mu.signal, mu.LH, mu.RH]): kW = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose, groups = mu.MC+[group]) kWSS = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='SS', verbose = verbose, groups = mu.MC+[group]) rqcd = hist.calc_rQCD(rf,mu,k_entry,'QCD',kW_OS = kW, kW_SS = kWSS, verbose = verbose, groups = mu.MC+[group]) print '%g +/- %g' % pdg_simple.pdg_round(rqcd.val,rqcd.err) qcd = hist.get_QCD(rf,mu,entry,'SR_SS', kW = kWSS, rqcd = rqcd, groups = mu.MC +[group]) nG = ErrorFloat(*hist.IntegralError(qcd)) QCD.append('%g +/- %g'% pdg_simple.pdg_round(nG.val,nG.err)) EST[i]+=nG BG[i]+=nG print row('Multijet', QCD, fmt=fmt) for i in xrange(len(EST)): EST[i] = '%i +/- %i'%(EST[i].val,EST[i].err) SB[i] = SB[i]/BG[i] print row('Total Estimate', EST, fmt = fmt) print row('S:B', SB, fmt = fmt)
def kinematic_plots_SR(mu, entry, ratio=True, verbose=False): rf = ROOT.TFile('~/panalysis/output/prod4/%s_fit/%s_regions.root' % (mu.name, mu.name)) region = 'SR_OS' # scale factors k_entry = upsilon_CR kW_OS = hist.calc_kX(rf, mu, k_entry, 'WCR', 'Wlnu', OS='OS', verbose=verbose) kW_SS = hist.calc_kX(rf, mu, k_entry, 'WCR', 'Wlnu', OS='SS', verbose=verbose) rQCD = hist.calc_rQCD(rf, mu, k_entry, 'QCD', kW_OS=kW_OS, kW_SS=kW_SS, verbose=verbose) # left and right handed kW_OS_L = hist.calc_kX(rf, mu, k_entry, 'WCR', 'Wlnu', OS='OS', verbose=verbose, groups=mu.MC + [mu.LH]) kW_OS_R = hist.calc_kX(rf, mu, k_entry, 'WCR', 'Wlnu', OS='OS', verbose=verbose, groups=mu.MC + [mu.RH]) kW_SS_L = hist.calc_kX(rf, mu, k_entry, 'WCR', 'Wlnu', OS='SS', verbose=verbose, groups=mu.MC + [mu.LH]) kW_SS_R = hist.calc_kX(rf, mu, k_entry, 'WCR', 'Wlnu', OS='SS', verbose=verbose, groups=mu.MC + [mu.RH]) rQCD_L = hist.calc_rQCD(rf, mu, k_entry, 'QCD', kW_OS=kW_OS_L, kW_SS=kW_SS_L, verbose=verbose, groups=mu.MC + [mu.LH]) rQCD_R = hist.calc_rQCD(rf, mu, k_entry, 'QCD', kW_OS=kW_OS_R, kW_SS=kW_SS_R, verbose=verbose, groups=mu.MC + [mu.RH]) rb = entry.rebin # Canvas if entry.blind: ratio = False ratio = False cname = '%s_%s_%s' % (mu.name, entry.name, region) pname = '%s_%s' % (mu.name, region.rstrip('_OS')) if ratio: c = ROOT.TCanvas(cname, cname, 1500, 1000) #Split for ratio plot c.Divide(1, 2) c.cd(1).SetPad(0.0, 0.2, 1.0, 1.0) c.cd(2).SetPad(0.0, 0.0, 1.0, 0.2) c.cd(1) else: c = ROOT.TCanvas(cname, cname, 1200, 800) # Data dregion = region data = rf.Get('h_%s_%s_%s' % (entry.name, mu.data.name, dregion)).Clone() data.SetMarkerStyle(20) data.SetMarkerSize(2) data.SetLabelSize(0.04, "y") data.Rebin(rb) if entry.xmax: data.SetAxisRange(entry.xmin or 0, entry.xmax) # stat. unc. on stack stacksum = data.Clone("tmp") stacksum.Reset() # Legend x0 = 0.72 y0 = 0.5 step = 0.05 nl = 4 + len(mu.MC) - 1 * entry.blind l = ROOT.TLegend(x0, y0, x0 + .17, y0 + nl * step) if entry.leg: leg = entry.leg l = ROOT.TLegend(leg[0], leg[1], leg[2], leg[3]) l.SetTextSize(0.035) l.SetBorderSize(0) l.SetFillColor(0) if not entry.blind: l.AddEntry(data, 'Data 2012', "P") # (%.0f)' % data.Integral(0,data.GetNbinsX()+1)) # Stack st = ROOT.THStack(cname, "%s; %s; " % (cname, entry.xtitle)) # QCD ESTIMATE #if region == 'SR_SS': rQCD = ErrorFloat(1.,0.) qcd = hist.get_QCD(rf, mu, entry, 'SR_SS', kW=kW_SS, rqcd=rQCD) qcd.SetLineWidth(0) qcd.SetLineColor(3) qcd.SetFillColor(3) qcd.SetMarkerStyle(0) qcd.SetMarkerColor(3) qcd.Rebin(rb) # left and right lefthand = hist.get_QCD(rf, mu, entry, 'SR_SS', kW=kW_SS_L, rqcd=rQCD_L, groups=mu.MC + [mu.LH]) righthand = hist.get_QCD(rf, mu, entry, 'SR_SS', kW=kW_SS_R, rqcd=rQCD_R, groups=mu.MC + [mu.RH]) lefthand.Rebin(rb) righthand.Rebin(rb) if not 'SR' in region: qcd.Reset() lefthand.Reset() righthand.Reset() if entry.xmax: qcd.SetAxisRange(entry.xmin or 0, entry.xmax) lefthand.SetAxisRange(entry.xmin or 0, entry.xmax) righthand.SetAxisRange(entry.xmin or 0, entry.xmax) st.Add(qcd) stacksum.Add(qcd) if verbose: total_est = ErrorFloat(*hist.IntegralError(qcd)) print 'Data: %.0f +/- %.0f' % (hist.IntegralError(data)[0], hist.IntegralError(data)[1]) print 'NQCD: %.0f +/- %.0f' % (hist.IntegralError(qcd)[0], hist.IntegralError(qcd)[1]) mc_leg = [] n_total = ErrorFloat( *hist.IntegralError(hist.get_OS(rf, entry, mu.signal, region))) groups = mu.MC + [mu.signal] for group in groups: entry.add_group(group) kOS = ErrorFloat(1.0, 0.0) kSS = ErrorFloat(1.0, 0.0) if group.name == 'Wlnu': kOS = kW_OS kSS = kW_SS if 'SS' not in region: h = hist.get_OS(rf, entry, group, region, kOS=kOS) else: h = hist.get_SS(rf, entry, group, region, kSS=kSS) h.Rebin(rb) if entry.xmax: h.SetAxisRange(entry.xmin or 0, entry.xmax) if group.name != mu.signal.name: lefthand.Add(h) righthand.Add(h) h.SetLineColor(entry[group.name]['fill']) h.SetLineWidth(0) h.SetMarkerStyle(0) h.SetMarkerColor(entry[group.name]['fill']) h.SetFillColor(entry[group.name]['fill']) h.SetLabelSize(0.04, "y") st.Add(h) stacksum.Add(h) mc_leg.append((h, group.legend, hist.IntegralError(h)[0], hist.IntegralError(h)[1])) if verbose: print '%s: %.0f +/- %.0f' % (group.name, hist.IntegralError(h)[0], hist.IntegralError(h)[1]) total_est += ErrorFloat(*hist.IntegralError(h)) if syst: print '%s: %.0f +/- %.0f' % ('up', hist.IntegralError(h_up)[0], hist.IntegralError(h_up)[1]) print '%s: %.0f +/- %.0f' % ('down', hist.IntegralError(h_down)[0], hist.IntegralError(h_down)[1]) if verbose: print 'Total Estimated: %.0f +/- %.0f' % (total_est.val, total_est.err) ## Add left and right hLeft = hist.get_OS(rf, entry, mu.LH, region) hRight = hist.get_OS(rf, entry, mu.RH, region) for h in [hLeft, hRight]: h.Rebin(rb) h.SetMarkerStyle(0) h.SetLineWidth(1) if entry.xmax: h.SetAxisRange(entry.xmin or 0, entry.xmax) lefthand.Add(hLeft) righthand.Add(hRight) lefthand.SetLineColor(mu.LH.fill) righthand.SetLineColor(mu.RH.fill) lefthand.SetLineStyle(2) righthand.SetLineStyle(2) for li in reversed(mc_leg): l.AddEntry(li[0], '%s' % (li[1])) #l.AddEntry(qcd,'QCD: %.0f +/- %.0f' % (hist.IntegralError(qcd)[0],hist.IntegralError(qcd)[1])) if 'SR' in region: l.AddEntry( qcd, 'Multijet' ) #: %.0f +/- %.0f' % (hist.IntegralError(qcd)[0],hist.IntegralError(qcd)[1])) l.AddEntry(lefthand, 'Left-Handed', "L") l.AddEntry(righthand, 'Right-Handed', "L") data.SetXTitle(entry.xtitle) data.GetXaxis().SetTitleOffset(1.25) stacksum.SetFillStyle(3354) stacksum.SetLineColor(1) stacksum.SetFillColor(12) stacksum.SetMarkerStyle(1) maxi = max(data.GetMaximum(), st.GetMaximum()) mini = 0. data.SetMinimum(1.2 * mini) data.SetMaximum(1.4 * maxi) bw = data.GetXaxis().GetBinWidth(1) data.GetYaxis().SetTitle('Events / %s %s' % (bw, entry.units)) if not entry.blind: data.Draw("PE") st.Draw("HIST,SAME") stacksum.Draw("E2,SAME") data.Draw("SAME, PE") else: data.Reset() data.Draw() st.SetTitle('') st.SetMinimum(1.2 * st.GetMinimum()) st.SetMaximum(1.5 * st.GetMaximum()) st.Draw("HIST,SAME") stacksum.Draw("E2,SAME") lefthand.Draw("HIST,SAME") righthand.Draw("HIST,SAME") data.Draw("SAME,PE") ROOT.gPad.RedrawAxis() l.Draw() atlas = ROOT.ATLASLabel(0.3, 0.87, " Work in Progress") #td = ROOT.TPaveText(0.3,0.75,0.7,0.85,"NDC") if 'SR' in region: td = ROOT.TPaveText(0.52, 0.68, 0.65, 0.85, "NDC") else: td = ROOT.TPaveText(0.19, 0.68, 0.32, 0.85, "NDC") td.SetTextAlign(13) td.SetBorderSize(0) td.SetFillColor(0) td.SetTextSize(0.04) #td.AddText('#sqrt{s} = 8 TeV #int Ldt = 20.3 fb^{-1}') #td.AddText('#sqrt{s} = 8 TeV') #td.AddText('#int Ldt = 20.3 fb^{-1}') td.Draw() # channel tm = ROOT.TPaveText(0.19, 0.85, 0.22, 0.92, "NDC") tm.SetBorderSize(0) tm.SetFillColor(0) tm.SetTextSize(0.05) if mu.name == 'mu': tm.AddText('#mu#tau_{had}') elif mu.name == 'el': tm.AddText('e#tau_{had}') tm.Draw() #atlas.Draw() if ratio: c.cd(2) ROOT.gPad.SetTopMargin(0) dataratio = data.Clone('ratio') if entry.xmax: dataratio.SetAxisRange(0, entry.xmax) dataratio.GetYaxis().SetTitle('Data / MC') dataratio.GetYaxis().SetTitleOffset(0.25) dataratio.GetYaxis().SetTitleSize(0.16) dataratio.SetLabelSize(.1, "y") dataratio.Divide(stacksum) dataratio.SetMaximum(1.5) dataratio.SetMinimum(0.5) err = stacksum.Clone('err') err.Divide(err) err.SetFillStyle(3354) err.SetLineColor(1) err.SetFillColor(12) err.SetMarkerStyle(1) xmin = dataratio.GetXaxis().GetXmin() xmax = entry.xmax or dataratio.GetXaxis().GetXmax() cl = ROOT.TLine(xmin, 1, xmax, 1) cl.SetLineStyle(3) dataratio.GetXaxis().SetLabelSize(0.04) dataratio.Draw('PE') cl.Draw("SAME") err.Draw("SAME,E2") c.Update() c.Update() c.Print('../plots/ttbar/%s.eps' % (cname))
def charged_asymmetry(mu): rf = ROOT.TFile('~/panalysis/output/prodr/%s_ttbar/%s_regions.root' % (mu.name, mu.name)) entry = upsilon_20 k_entry = upsilon_CR verbose = False region = 'TTBAR_OS' # scale factors k_entry = upsilon_CR groups = mu.MC + [mu.signal] kW_OS = hist.calc_kX(rf, mu, k_entry, 'WCR', 'Wlnu', OS='OS', verbose=verbose, groups=groups) kW_SS = hist.calc_kX(rf, mu, k_entry, 'WCR', 'Wlnu', OS='SS', verbose=verbose, groups=groups) rQCD = hist.calc_rQCD(rf, mu, k_entry, 'QCD', kW_OS=kW_OS, kW_SS=kW_SS, verbose=verbose, groups=groups) # Canvas if entry.blind: ratio = False ratio = False cname = '%s_%s_%s' % (mu.name, entry.name, region) #pname = '%s_%s_%s' % (mu.name,region.rstrip('_OS'), hand) if ratio: c = ROOT.TCanvas(cname, cname, 1500, 1000) #Split for ratio plot c.Divide(1, 2) c.cd(1).SetPad(0.0, 0.2, 1.0, 1.0) c.cd(2).SetPad(0.0, 0.0, 1.0, 0.2) c.cd(1) else: c = ROOT.TCanvas(cname, cname, 1200, 800) rb = 1 # data if 'SR_OS' in region: dregion = 'SR_OS' if 'SR_SS' in region: dregion = 'SR_SS' data = rf.Get('h_%s_%s_%s' % (entry.name, mu.data.name, dregion)).Clone() data.SetMarkerStyle(20) data.SetMarkerSize(2) data.SetLabelSize(0.04, "y") data.Rebin(rb) # Blind data data.Reset() if entry.xmax: data.SetAxisRange(entry.xmin or 0, entry.xmax) # stat. unc. on stack stacksum = data.Clone("tmp") stacksum.Reset() # Legend x0 = 0.73 y0 = 0.55 step = 0.045 nl = 4 + len(mu.MC) - 1 * entry.blind l = ROOT.TLegend(x0, y0, x0 + .16, y0 + nl * step) l.SetTextSize(0.0345) l.SetBorderSize(0) l.SetFillColor(0) # Data # Stack st = ROOT.THStack(cname, "%s; %s; " % (cname, entry.xtitle)) # QCD ESTIMATE #if region == 'SR_SS': rQCD = ErrorFloat(1.,0.) qcd = hist.get_QCDW(rf, mu, entry, 'SR_SS', rqcd=rQCD, verbose=False, groups=groups) qcd.SetLineWidth(0) qcd.SetLineColor(3) qcd.SetFillColor(3) qcd.SetMarkerStyle(0) qcd.SetMarkerColor(3) qcd.Rebin(rb) if entry.xmax: qcd.SetAxisRange(entry.xmin or 0, entry.xmax) st.Add(qcd) stacksum.Add(qcd) if verbose: total_est = ErrorFloat(*hist.IntegralError(qcd)) print 'Data: %.0f +/- %.0f' % (hist.IntegralError(data)[0], hist.IntegralError(data)[1]) print 'NQCD: %.0f +/- %.0f' % (hist.IntegralError(qcd)[0], hist.IntegralError(qcd)[1]) mc_leg = [] n_total = ErrorFloat( *hist.IntegralError(hist.get_OS(rf, entry, mu.signal, region))) for group in groups: entry.add_group(group) if group.name == 'Wlnu': h = hist.get_dataW(rf, mu, entry, 'WCR_OS', verbose=False, groups=groups) else: h = hist.get_OS(rf, entry, group, region) h.Rebin(rb) if entry.xmax: h.SetAxisRange(entry.xmin or 0, entry.xmax) h.SetLineColor(entry[group.name]['fill']) h.SetLineWidth(0) h.SetMarkerStyle(0) h.SetMarkerColor(entry[group.name]['fill']) h.SetFillColor(entry[group.name]['fill']) h.SetLabelSize(0.04, "y") st.Add(h) stacksum.Add(h) mc_leg.append((h, group.legend, hist.IntegralError(h)[0], hist.IntegralError(h)[1])) if verbose: print '%s: %.0f +/- %.0f' % (group.name, hist.IntegralError(h)[0], hist.IntegralError(h)[1]) total_est += ErrorFloat(*hist.IntegralError(h)) if verbose: print 'Total Estimated: %.0f +/- %.0f' % (total_est.val, total_est.err) for li in reversed(mc_leg): l.AddEntry(li[0], '%s' % (li[1])) l.AddEntry( qcd, 'Multijet' ) #: %.0f +/- %.0f' % (hist.IntegralError(qcd)[0],hist.IntegralError(qcd)[1])) data.SetXTitle(entry.xtitle) data.GetXaxis().SetTitleOffset(1.25) stacksum.SetFillStyle(3354) stacksum.SetLineColor(1) stacksum.SetFillColor(12) stacksum.SetMarkerStyle(1) maxi = max(data.GetMaximum(), st.GetMaximum()) maxi = 6250 mini = 0. data.SetMinimum(1.2 * mini) data.SetMaximum(maxi) bw = data.GetXaxis().GetBinWidth(1) data.GetYaxis().SetTitle('Events / %s %s' % (bw, entry.units)) if not entry.blind: data.Draw("PE") st.Draw("HIST,SAME") stacksum.Draw("E2,SAME") data.Draw("SAME, PE") else: data.Reset() data.Draw() st.SetTitle('') st.SetMinimum(1.2 * st.GetMinimum()) st.SetMaximum(1.5 * st.GetMaximum()) st.Draw("HIST,SAME") stacksum.Draw("E2,SAME") ROOT.gPad.RedrawAxis() l.Draw() atlas = ROOT.ATLASLabel(0.32, 0.86, " Work in Progress") td = ROOT.TPaveText(0.54, 0.68, 0.67, 0.85, "NDC") td.SetTextAlign(13) td.SetBorderSize(0) td.SetFillColor(0) td.SetTextSize(0.04) td.AddText('#sqrt{s} = 8 TeV') td.AddText('#int Ldt = 20.3 fb^{-1}') td.Draw() # channel tm = ROOT.TPaveText(0.19, 0.83, 0.22, 0.90, "NDC") tm.SetBorderSize(0) tm.SetFillColor(0) tm.SetTextSize(0.05) if mu.name == 'mu': tm.AddText('#mu#tau_{had}') elif mu.name == 'el': tm.AddText('e#tau_{had}') tm.Draw() c.Update() c.Print('../plots/ttbar/%s.eps' % (cname))