def makePlot(data): c=r.TCanvas();c.SetLogx();c.SetLogy() r.gPad.SetTopMargin(0.06) r.gPad.SetBottomMargin(0.15) r.gPad.SetLeftMargin(0.2) mg=r.TMultiGraph() gs=r.TGraphErrors() obs=[r.TGraph() for i in range(len(data['graphs']))] exp=[r.TGraph() for i in range(len(data['graphs']))] stylize(gs,obs,exp) leg=r.TLegend(0.23,0.6 - (0.13 if H in [350,700] else 0),0.55,0.82 - (0.13 if H in [350,700] else 0)) leg2=r.TLegend(0.58,0.7 - (0.13 if H in [350,700] else 0),0.9,0.82 - (0.13 if H in [350,700] else 0)) leg.SetFillColor(0);leg2.SetFillColor(0) leg.SetHeader("95% CL limits:") leg2.SetTextFont(62) gempty=r.TGraph() gempty.SetMarkerColor(0) leg2.AddEntry(gempty,'m_{#tilde{q}} = '+str(data['H'])+' GeV','P') for i,graph in enumerate(data['graphs']): points = graph['points'] n=len(points) for j,(ctau,lim) in enumerate(points): gs.SetPoint(gs.GetN(),ctau,data['sigma']) gs.SetPointError(gs.GetN()-1,0,data['sigmaErr']) obs[i].SetPoint(j,ctau,lim['obs']) print data['H'], graph['X'], ctau, lim['obs']*1000 exp[i].SetPoint(j,ctau,lim['1ps']) exp[i].SetPoint(2*n-1-j,ctau,lim['1ms']) leg.AddEntry(obs[i],'m_{#tilde{#chi}^{0}_{1}}=%s GeV' %graph['X'],'PL') for i in range(len(data['graphs'])): mg.Add(exp[i],'F') for i in range(len(data['graphs'])): mg.Add(obs[i],'LP') leg.AddEntry(exp[0],'Exp. limits (\\pm 1\\sigma)','F') leg.AddEntry(gs,'#sigma_{#tilde{q}#tilde{q}*+#tilde{q}#tilde{q}} \\pm 1\\sigma_{theory}','LF') mg.Add(gs,'LE3') cmsStamp(lumi=18510,coords=(0.5,0.87),preliminary=False) mg.Draw('A') mg.SetMaximum((8 if H in [350,700] else 40)*max(data['sigma'],max([r.TMath.MaxElement(e.GetN(),e.GetY()) for e in exp]))) gmin,gmax = r.TMath.MinElement(gs.GetN(),gs.GetX()),r.TMath.MaxElement(gs.GetN(),gs.GetX()) mg.GetXaxis().SetRangeUser(0.95*gmin,2*gmax) mg.GetXaxis().SetMoreLogLabels() mg.GetYaxis().SetTitle("#sigma(#tilde{q}#tilde{q}*+#tilde{q}#tilde{q}) \ B^{2}(#tilde{#chi}^{0}_{1} #rightarrow u#bar{d}#mu) [pb]") mg.GetXaxis().SetTitle('#tilde{#chi}^{0}_{1} c#tau [cm]') mg.GetXaxis().SetTitleOffset(1.4) mg.GetYaxis().SetTitleOffset(1.8) leg.Draw('same') leg2.Draw('same') name=str(data['H']) c.SaveAs(name+'.eps') os.system('epstopdf '+name+'.eps && rm '+name+'.eps && mv '+name+'.pdf '+plotDir)
def limitPlot(MH = None,MX = None,list = None,observed=False): c=r.TCanvas() n=len(list) g=[r.TGraph(n),r.TGraph(n),r.TGraph(2*n),r.TGraph(2*n)] mg=r.TMultiGraph() for i,obj in enumerate(list): if observed : g[0].SetPoint(i,obj['ctau'],obj['obs']) g[1].SetPoint(i,obj['ctau'],obj['exp']) g[2].SetPoint(i,obj['ctau'],obj['1ps']) g[2].SetPoint(2*n-1-i,obj['ctau'],obj['1ms']) g[3].SetPoint(i,obj['ctau'],obj['2ms']) g[3].SetPoint(2*n-1-i,obj['ctau'],obj['2ps']) stylize(g) mg.Add(g[3],'F') mg.Add(g[2],'F') mg.Add(g[1],'L') mg.SetMaximum(50*r.TMath.MaxElement(g[3].GetN(),g[3].GetY())) if observed : mg.Add(g[0],'PL') c.SetLogy() c.SetLogx() mg.Draw('A') mg.GetXaxis().SetTitle('X^{0} c#tau [cm]') ctaus=sorted([obj['ctau'] for obj in list]) mg.GetXaxis().SetRangeUser(ctaus[0]*0.95,ctaus[-1]*2) if option=='ea': #mg.GetYaxis().SetTitle('#sigma #times BR #times Acceptance [pb] (95% CL)') mg.GetYaxis().SetTitle('#sigma B^{2} #times Acceptance [pb] (95% CL)') else: #mg.GetYaxis().SetTitle('#sigma #times BR [pb] (95% CL)') mg.GetYaxis().SetTitle('#sigma(H^{0} #rightarrow X^{0}X^{0}) B^{2}(X^{0} #rightarrow q#bar{q}) [pb]') leg=r.TLegend(0.23,0.55,0.60,0.79) leg.SetHeader("95% CL limits:") leg.SetFillColor(0) leg2=r.TLegend(0.55,0.65,0.85,0.79) leg2.SetFillColor(0) gempty=r.TGraph() gempty.SetMarkerColor(0) #leg.AddEntry(gempty,'95% CL limits','P') leg2.AddEntry(gempty,'m_{H^{0}} = '+str(MH)+' GeV','P') leg2.AddEntry(gempty,'m_{X^{0}} = '+str(MX)+' GeV','P') if observed : leg.AddEntry(g[0],'Observed','L') leg.AddEntry(g[1],'Expected','L') leg.AddEntry(g[2],'Expected \\pm 1\\sigma','F') leg.AddEntry(g[3],'Expected \\pm 2\\sigma','F') leg.Draw('same') leg2.Draw('same') cmsStamp(lumi=18510,coords=(0.5,0.87),preliminary=False) name=str(MH)+'_'+str(MX)+option os.chdir(plotDir) c.SaveAs(name+'.eps') os.system('epstopdf '+name+'.eps') os.system('rm '+name+'.eps') os.chdir('../../')
def makePlot(data): c = r.TCanvas() c.SetLogx() c.SetLogy() r.gPad.SetTopMargin(0.06) r.gPad.SetBottomMargin(0.15) r.gPad.SetLeftMargin(0.2) mg = r.TMultiGraph() obs = [r.TGraph() for i in range(len(data["graphs"]))] exp = [r.TGraph() for i in range(len(data["graphs"]))] stylize(obs, exp) leg = r.TLegend(0.23, 0.62 - (0.13 if H in [350, 700] else 0), 0.55, 0.82 - (0.13 if H in [350, 700] else 0)) leg2 = r.TLegend(0.58, 0.7 - (0.13 if H in [350, 700] else 0), 0.9, 0.82 - (0.13 if H in [350, 700] else 0)) leg.SetFillColor(0) leg2.SetFillColor(0) leg.SetHeader("95% CL limits:") leg2.SetTextFont(62) gempty = r.TGraph() gempty.SetMarkerColor(0) leg2.AddEntry(gempty, "m_{H^{0}} = " + str(data["H"]) + " GeV", "P") for i, graph in enumerate(data["graphs"]): points = graph["points"] n = len(points) for j, (ctau, lim) in enumerate(points): obs[i].SetPoint(j, ctau, lim["obs"]) exp[i].SetPoint(j, ctau, lim["1ps"]) exp[i].SetPoint(2 * n - 1 - j, ctau, lim["1ms"]) leg.AddEntry(obs[i], "m_{X^{0}}=%s GeV" % graph["X"], "PL") for i in range(len(data["graphs"])): mg.Add(exp[i], "F") for i in range(len(data["graphs"])): mg.Add(obs[i], "PL") leg.AddEntry(exp[0], "Exp. limits (\\pm 1\\sigma)", "F") cmsStamp(lumi=18510, coords=(0.5, 0.87), preliminary=False) mg.Draw("A") mg.SetMaximum(40 * max([r.TMath.MaxElement(e.GetN(), e.GetY()) for e in exp])) mg.GetYaxis().SetTitle("#sigma(H^{0} #rightarrow X^{0}X^{0}) B^{2}(X^{0} #rightarrow q#bar{q}) [pb]") mg.GetXaxis().SetTitle("X^{0} c#tau [cm]") mg.GetXaxis().SetTitleOffset(1.4) mg.GetYaxis().SetTitleOffset(1.8) leg.Draw("same") leg2.Draw("same") name = str(data["H"]) c.SaveAs(name + ".eps") os.system("epstopdf " + name + ".eps && rm " + name + ".eps && mv " + name + ".pdf " + plotDir)
def limitPlot(list = None): c=r.TCanvas() n=len(list) g=[r.TGraph(n),r.TGraph(n),r.TGraph(2*n),r.TGraph(2*n)] mg=r.TMultiGraph() den=list[0]['exp'] for i,obj in enumerate(list): g[1].SetPoint(i,obj['relErr'],obj['exp']/den) g[2].SetPoint(i,obj['relErr'],obj['1ps']/den) g[2].SetPoint(2*n-1-i,obj['relErr'],obj['1ms']/den) g[3].SetPoint(i,obj['relErr'],obj['2ms']/den) g[3].SetPoint(2*n-1-i,obj['relErr'],obj['2ps']/den) stylize(g) func=r.TF1('func','1+[0]*x + [1]*x*x',0,90) func.SetLineWidth(2) func.SetMarkerSize(0) g[1].Fit('func') #mg.Add(g[3],'F') mg.Add(g[2],'F') mg.Add(g[1],'L') mg.SetMaximum(1.2*r.TMath.MaxElement(g[2].GetN(),g[2].GetY())) #c.SetLogy() #c.SetLogx() mg.Draw('A') mg.GetXaxis().SetTitle('Systematic uncertainty [%]') ctaus=sorted([obj['relErr'] for obj in list]) mg.GetXaxis().SetRangeUser(ctaus[0]*0.95,ctaus[-1]*2) mg.GetYaxis().SetTitle('Limit / Limit (no uncertainty)') leg=r.TLegend(0.2,0.61,0.7,0.8) leg.SetFillColor(0) gempty=r.TGraph() gempty.SetMarkerColor(0) gempty.SetFillColor(0) gempty.SetLineColor(0) leg.AddEntry(gempty,'signal efficiency = '+str(dict['eff']*100)+'%') leg.AddEntry(g[1],'Exp. Limit','L') leg.AddEntry(g[2],'Exp. \\pm 1\\sigma','F') #leg.AddEntry(g[3],'Exp. \\pm 2\\sigma','F') leg.AddEntry(func,'1 + '+str(rnd(func.GetParameter(0),2))+' x + '+str(rnd(func.GetParameter(1),2))+' x^{2}') leg.Draw('same') cmsStamp(lumi=None,coords=(0.45,0.89)) name=str(100*eff) os.chdir(plotDir) c.SaveAs(name+'percent.eps') os.system('epstopdf '+name+'percent.eps') os.system('rm '+name+'percent.eps') os.chdir('../../')
def limitPlot(MH = None,MX = None,list = None,observed=False): if MX==148: MX=150 elif MX==494: MX=500 print MX c=r.TCanvas() n=len(list) g=[r.TGraph(n),r.TGraph(n),r.TGraph(2*n),r.TGraph(2*n),r.TGraphErrors(n)] mg=r.TMultiGraph() for i,obj in enumerate(list): if observed : g[0].SetPoint(i,obj['ctau'],obj['obs']) g[1].SetPoint(i,obj['ctau'],obj['exp']) g[2].SetPoint(i,obj['ctau'],obj['1ps']) g[2].SetPoint(2*n-1-i,obj['ctau'],obj['1ms']) g[3].SetPoint(i,obj['ctau'],obj['2ms']) g[3].SetPoint(2*n-1-i,obj['ctau'],obj['2ps']) g[4].SetPoint(i,obj['ctau'],obj['sigma']) g[4].SetPointError(i,0,obj['sigmaErr']*obj['sigma']) stylize(g) mg.Add(g[3],'F') mg.Add(g[2],'F') mg.Add(g[1],'L') mg.Add(g[4],'LE3') maxg=r.TMath.MaxElement(g[3].GetN(),g[3].GetY()) ming=r.TMath.MinElement(g[3].GetN(),g[3].GetY()) mg.SetMaximum((6 if MH in [350,700] else 40)*max(maxg,obj['sigma'])) mg.SetMinimum(0.5*min(ming,obj['sigma'])) if observed : mg.Add(g[0],'PL') c.SetLogy() c.SetLogx() mg.Draw('A') mg.GetXaxis().SetTitle('#tilde{#chi}^{0}_{1} c#tau [cm]') mg.GetXaxis().SetMoreLogLabels() mg.GetXaxis().SetNdivisions(303) ctaus=sorted([obj['ctau'] for obj in list]) mg.GetXaxis().SetRangeUser(ctaus[0]*0.95,ctaus[-1]*2) if option=='ea': #mg.GetYaxis().SetTitle('#sigma #times BR #times Acceptance [pb] (95% CL)') mg.GetYaxis().SetTitle('#sigma #times Acceptance [pb] (95% CL)') else: #mg.GetYaxis().SetTitle('#sigma #times BR [pb] (95% CL)') mg.GetYaxis().SetTitle("#sigma(#tilde{q}#tilde{q}*+#tilde{q}#tilde{q}) \ B^{2}(#tilde{#chi}^{0}_{1} #rightarrow u#bar{d}#mu) [pb]") leg=r.TLegend(0.23,0.6 - (0.13 if MH in [350,700] else 0),0.55,0.82 - (0.13 if MH in [350,700] else 0)) leg.SetFillColor(0) leg2=r.TLegend(0.58,0.6 - (0.13 if MH in [350,700] else 0),0.9,0.82 - (0.13 if MH in [350,700] else 0)) leg2.SetFillColor(0) gempty=r.TGraph() gempty.SetMarkerColor(0) leg2.AddEntry(gempty,'m_{#tilde{q}} = '+str(MH)+' GeV','P') leg2.AddEntry(gempty,'m_{#tilde{#chi}^{0}_{1}} = '+str(MX)+' GeV','P') leg2.AddEntry(g[4],'#sigma_{#tilde{q}#tilde{q}*+#tilde{q}#tilde{q}} \\pm 1\\sigma_{theory}','LF') if observed : leg.AddEntry(g[0],'Observed','PL') leg.SetHeader("95% CL limits:") leg.AddEntry(g[1],'Expected','L') leg.AddEntry(g[2],'Expected \\pm 1\\sigma','F') leg.AddEntry(g[3],'Expected \\pm 2\\sigma','F') leg.Draw('same') leg2.Draw('same') cmsStamp(lumi=18510,coords=(0.5,0.87),preliminary=False) name=str(MH)+'_'+str(MX)+option os.chdir(plotDir) c.SaveAs(name+'.eps') os.system('epstopdf '+name+'.eps') os.system('rm '+name+'.eps') os.chdir('../../')
def plotABCDscan(analysis,org,plotter,n,blind=True,onlyB=False): setupTdrStyle() plotter.pdfFileName = plotter.pdfFileName.replace(analysis.name+'.pdf','Scans_'+analysis.name+'.pdf') plotter.canvas.Clear() plotter.printCanvas("[") text1 = plotter.printTimeStamp() plotter.flushPage() # get all the counts counts = [[0]*len(analysis.scan) for sample in org.samples] for step in org.steps : for plotName in sorted(step.keys()) : if 'ABCDEFGHcounts' not in plotName: continue i = eval(plotName[:plotName.find('ABCDEFGH')]) for j in range(len(org.samples)): counts[j][i] = getCounts(step[plotName][j]) # pick points to scan scans=[] for cut in analysis.scanPrompt: scans.append((cut,cut,None)) # constant names cutNames = ['(NPrompt1,PromptFrac1)','(NPrompt2,PromptFrac2)','DiscVtx'] histNames = ['observed','FG/B','EG/C','DG/A','BE/A','CF/A','EF/D','BCD/A^{2}'] # plot scans for scan in scans: plotter.canvas=r.TCanvas("c","c",600,600) plotter.canvas.Divide(1,2) plotter.canvas.cd(1).SetPad(0.01,0.30+0.0,0.99,0.99) plotter.canvas.cd(1).SetTopMargin(0.07) plotter.canvas.cd(1).SetBottomMargin(0.) plotter.canvas.cd(2).SetPad(0.01,0.01,0.99,0.30) plotter.canvas.cd(2).SetTopMargin(0.) plotter.canvas.cd(2).SetBottomMargin(0.35) plotter.canvas.cd(1) r.gPad.SetLogy() #r.gPad.SetTicky(0) title='' #title='max Prompt Tracks = %s, max Prompt Energy Fraction = %s'%(scan[0][0],scan[0][1]) xtitle = 'Vertex/cluster discriminant' ytitle = 'Number of events' indices = [i for i,cuts in enumerate(analysis.scan) if len(listdiff(cuts,scan))<=1] labels = [string(cuts[scan.index(None)]) for i,cuts in enumerate(analysis.scan) if i in indices] # first make a plot of signal efficiency sigSamples = [sample for sample in org.samples if 'H' in sample['name']] sigeff = [r.TH1F('','',len(indices),0,1) for sample in sigSamples] for j,sample in enumerate(sigSamples): i = org.indexOfSampleWithName(sample['name']) norm = sample['xs']*org.lumi for k,idx in enumerate(indices): sigeff[j].SetBinContent(k+1,100*counts[i][idx][0][0]/float(2*norm)) sigeff[j].SetBinError(k+1,100*counts[i][idx][0][1]/float(norm)) sigeff[j].GetXaxis().SetBinLabel(k+1,labels[k]) # Data/QCD plots with signal efficiency on the same plot for j,sample in enumerate(org.samples): if 'H' in sample['name'] : continue histos = [r.TH1F(name,title,len(indices),0,1) for name in histNames] histob = r.TH1F('predicted bkg.',title,len(indices),0,1) histob0 = r.TH1F('predicted bkg.',title,len(indices),0,1) histoz = r.TH1F('','',len(indices),0,1) for h in histos: h.GetXaxis().SetNdivisions(len(indices)*100+10,False) histoz.GetXaxis().SetNdivisions(len(indices)*100+10,False) for k,idx in enumerate(indices): b,berr = getBkg(counts[j][idx],None) histob.SetBinContent(k+1,b) histob.SetBinError(k+1,berr) histob0.SetBinContent(k+1,b) histob0.SetBinError(k+1,0.00001) N=int(counts[j][idx][0][0]) if N==0: histoz.SetBinContent(k+1,-1e5) continue p=pvalue(b,berr,N,1e4) z=r.RooStats.PValueToSignificance(p) histoz.SetBinContent(k+1,z) histoz.SetBinError(k+1,0.) if onlyB: legend = r.TLegend(0.5, 0.65, 0.93, 0.83) else: legend = r.TLegend(0.7, 0.55, 0.93, 0.93) for i in reversed(range(n)): if blind and 'Data' in sample['name'] and i==0: continue for k,idx in enumerate(indices): histos[i].SetBinContent(k+1,counts[j][idx][i][0]) histos[i].SetBinError(k+1,counts[j][idx][i][1]) histos[i].GetXaxis().SetBinLabel(k+1,labels[k]) histoz.GetXaxis().SetBinLabel(k+1,labels[k]) histob.GetXaxis().SetBinLabel(k+1,'') histob0.GetXaxis().SetBinLabel(k+1,'') histos[i].GetXaxis().SetTitle(xtitle) histos[i].GetYaxis().SetTitle(ytitle) histos[i].SetStats(False) histob.SetStats(False) histos[i].GetYaxis().SetTitleOffset(1.25) histos[i].GetYaxis().SetTitleSize(0.066) histos[i].GetYaxis().SetLabelSize(0.062) histoz.GetXaxis().SetTitle(xtitle) histoz.SetTitle('') histoz.SetStats(0) histoz.SetMarkerStyle(8) histob.SetMarkerSize(0) histob0.SetMarkerSize(0) histos[i].SetMarkerStyle(25 if i!=0 else 8) if i==(n-1):histos[i].SetMarkerStyle(21) histos[i].SetMarkerColor(i+1) histos[i].SetFillColor(0) histos[i].SetLabelSize(0.07) if onlyB: if i==0: legend.AddEntry(histos[i],'Observed background') else: legend.AddEntry(histos[i],histNames[i]) option='EX0' if i==(n-1) else 'EX0same' histos[i].Draw(option) if onlyB: legend.AddEntry(histob,'Predicted background') histob.SetFillColor(r.kBlue) histob0.SetFillColor(r.kWhite) histob.SetFillStyle(3001) histob0.SetLineWidth(3) histob.SetLineWidth(3) histob0.SetLineColor(r.kBlue) histob.SetLineColor(r.kBlue) histos[0].SetLineWidth(2) histos[0].Draw('EX0') histob.Draw("E2same") histob0.Draw("Lsame") histos[0].Draw('EX0same') histos_tmp=tuple([histos[i] for i in range(n)]) plotter.setRanges(histos_tmp,*plotter.getExtremes(1,histos_tmp,[False]*n)) if onlyB: cmsStamp(lumi=18500,coords=(0.71,0.84),preliminary=False) #cmsStamp(lumi=None,coords=(0.78,0.88)) else: cmsStamp(lumi=org.lumi,coords=(0.45,0.88),simulation=True) #cmsStamp(lumi=None,coords=(0.45,0.85)) pad = r.TPad("pad2","",0,0,1,1) pad.SetFillStyle(4000) pad.SetFrameFillStyle(0) pad.SetLogy(0) pad.Draw() pad.cd() for i,sample in enumerate(sigSamples): option='histY+' + ('same' if i>0 else '') sigeff[i].SetLineWidth(1) sigeff[i].SetStats(False) sigeff[i].SetLabelSize(0.06) sigeff[i].SetLineColor(i+2) sigeff[i].SetLineStyle(2) sigeff[i].GetYaxis().SetRangeUser(0,45) sigeff[i].GetYaxis().SetTitle('efficiency [%]') sigeff[i].Draw(option) legend.AddEntry(sigeff[i],sample['name'].split('.')[0]) legend.SetFillColor(0) legend.SetBorderSize(0) legend.Draw("same") plotter.canvas.cd(2) r.gPad.SetGridy() histoz.GetYaxis().SetNdivisions(505,True) histoz.GetYaxis().SetTitleOffset(0.5) histoz.GetYaxis().SetTitleSize(0.16) histoz.GetXaxis().SetTitleOffset(1.0) histoz.GetXaxis().SetTitleSize(0.16) histoz.GetYaxis().SetLabelSize(0.14) histoz.GetYaxis().SetLabelOffset(0.015) histoz.GetYaxis().SetTickLength(1.3*histoz.GetYaxis().GetTickLength()) histoz.GetXaxis().SetTickLength(2.*histoz.GetXaxis().GetTickLength()) histoz.GetXaxis().SetLabelSize(0.19) histoz.GetXaxis().SetLabelOffset(0.02) histoz.GetYaxis().SetTitle('Significance ') #histoz.GetYaxis().CenterTitle() histoz.GetYaxis().SetRangeUser(-2.99,2.99) histoz.Draw('P') plotter.printCanvas() #plotter.canvas.Clear() plotter.printCanvas("]") print plotter.pdfFileName, "has been written."