def MakeLegend(can, x1=.8, y1=.8, x2=.9, y2=.9, textsize=18, ncolumns=1, totalentries=0, option='f', skip=[]): from ROOT import TLegend, TH1, gStyle, TGraph if can.GetPrimitive('pad_top'): MakeLegend(can.GetPrimitive('pad_top'), x1, y1, x2, y2, textsize, ncolumns, totalentries, skip=skip) return if CanvasEmpty(can): print 'Error: trying to make legend from canvas with 0 plots. Will do nothing.' return # # if a previous version exists from this function, delete it # if can.GetPrimitive('legend'): can.GetPrimitive('legend').Delete() leg = TLegend(x1, y1, x2, y2) leg.SetName('legend') tobject_collector.append(leg) leg.SetTextFont(43) leg.SetTextSize(textsize) leg.SetTextFont(43) leg.SetBorderSize(0) leg.SetFillStyle(0) leg.SetNColumns(ncolumns) # # Add by TH1 GetTitle() # the_primitives = can.GetListOfPrimitives() if can.GetPrimitive('pad_top'): the_primitives = can.GetPrimitive('pad_top').GetListOfPrimitives() if can.GetPrimitive('stack'): the_stack = list(reversed(list(can.GetPrimitive('stack').GetHists()))) the_primitives = the_stack + list(the_primitives) if type(option) == type(''): option = [option] * 100 total = 0 for i in the_primitives: if i.GetName() == 'stack': continue drawopt = i.GetDrawOption() if issubclass(type(i), TH1) or issubclass(type(i), TGraph): if i.GetTitle() in skip: continue leg.AddEntry(i, i.GetTitle(), option[total]) # plef total += 1 # # Add empty entries to ensure a standard layout # for i in range(100): if totalentries == 0: break if total >= totalentries: break leg.AddEntry(None, '', '') total += 1 # recipe for making roughly square boxes h = leg.GetY2() - leg.GetY1() w = leg.GetX2() - leg.GetX1() leg.SetMargin(leg.GetNColumns() * h / float(leg.GetNRows() * w)) can.cd() if can.GetPrimitive('pad_top'): can.GetPrimitive('pad_top').cd() leg.Draw() can.Modified() can.Update() return
def make_plot_all_rods(error_dict, rod_dict, name): leg = TLegend(0,0,0,0) if 'lock' in name: leg = TLegend(0.18,0.85,0.50,0.55) else: leg = TLegend(0.18,0.85,0.40,0.55) leg.SetLineColor(0) leg.SetFillStyle(0) leg.SetShadowColor(0) leg.SetBorderSize(0) leg.SetNColumns(3) gStyle.SetLegendTextSize(0.045) v_hists = [] #for e,c in zip(error_bits, error_colors): for e in error_bits: h = TH1F('h'+e,'h'+e, len(rod_dict), 0, len(rod_dict)) h.SetFillStyle(1001) h.SetLineWidth(0) v_hists.append(h) v_hists[-1].SetDirectory(0) if bin(int('0x'+e, 16))[2:].zfill(4) == '0111': leg.AddEntry(v_hists[-1],'GOL 3',"f"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1011': leg.AddEntry(v_hists[-1],'GOL 2',"f"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1101': leg.AddEntry(v_hists[-1],'GOL 1',"f"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1110': leg.AddEntry(v_hists[-1],'GOL 0',"f"); else: leg.AddEntry(v_hists[-1],bin(int('0x'+e, 16))[2:].zfill(4),"f"); h = leg.GetY2()-leg.GetY1(); w = leg.GetX2()-leg.GetX1()*.6; leg.SetMargin(leg.GetNColumns()*h/(leg.GetNRows()*w)) for key,val in error_dict.items(): idx_rod = 0 for i, key2 in enumerate(rod_dict): if key2 == key[:8]: idx_rod = i v_hists[int(key[11:12], 16)].Fill(idx_rod, val) stack = THStack("stack","stack") for hist in v_hists: stack.Add(hist) if 'buff' in name: c1 = TCanvas( 'c1', 'c1', 2000, 500) else: c1 = TCanvas( 'c1', 'c1', 1000, 500) h1 = TH1F('h_1','h_1', len(rod_dict), 0, len(rod_dict)) for i, key in enumerate(rod_dict): h1.GetXaxis().SetBinLabel(i+1,key) h1.SetBinContent(i+1,rod_dict[key]) h1.GetXaxis().LabelsOption("v") h1.GetXaxis().SetTitle("ROD") h1.GetXaxis().SetTitleOffset(2.2) h1.GetYaxis().SetTitle("# of rocketio errors") h1.SetLineColor(kRed) h1.SetLineWidth(1) leg.AddEntry(h1,'total',"l"); c1.SetBottomMargin(0.23) h1.GetXaxis().SetTitle("ROD") h1.Draw("HIST") stack.Draw("PFC PLC SAME HIST") h1.Draw("SAMEHIST") AtlasStyle.ATLAS_LABEL(0.19,.88, 1, "Internal") leg.Draw() c1.Update() c1.Print("plots/"+name +".pdf") c1.Clear()
def checkDataPoissonErrors(): '''Check on behaviour of Poisson errors in histograms.''' N = 100 hist0 = TH1D("data0", "default", N, 0, N) hist1 = TH1D("data1", "TH1.kPoisson", N, 0, N) hist2 = TH1D("data2", "#chi^{2} quantile", N, 0, N) for i in xrange(0, N + 2): y = max(0, i - 1) hist0.SetBinContent(i, y) hist1.SetBinContent(i, y) hist2.SetBinContent(i, y) # GET data Possoin errors graph1 = getDataPoissonErrors(hist1, drawZeroBins=True, centerBin=False, kPoisson=True) graph2 = getDataPoissonErrors(hist2, drawZeroBins=True, centerBin=False, kPoisson=False) # FILL table table = [] table.append( "%22s %14s %14s %14s" % (" ", "default".center(14), "kPoisson".center(14), "chi^2".center(14))) table.append("%4s %7s %7s %6s %6s %6s %6s %6s %6s" % ("bin", "content", "sqrt(N)", "errLow", "errUp", "errLow", "errUp", "errLow", "errUp")) for i in xrange(1, N + 1): y = hist0.GetBinContent(i) table.append( "%4d %5d %7.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f" % (i, y, sqrt(y), hist0.GetBinErrorLow(i), hist0.GetBinErrorUp(i), graph1.GetErrorYlow(i - 1), graph1.GetErrorYhigh(i - 1), graph2.GetErrorYlow(i - 1), graph2.GetErrorYhigh(i - 1))) # PRINT table for line in table: print ">>> " + line print ">>> " # GRAPHS function = TF1("function1", "sqrt(x)", 0, N) function.SetTitle("#sqrt{N}") graphElow1, graphEup1 = makeErrorGraph(graph1) graphElow2, graphEup2 = makeErrorGraph(graph2) graphElow1.SetTitle("TH1.kPoisson low") graphEup1.SetTitle("TH1.kPoisson high") graphElow2.SetTitle("N-F^{-1}_{#chi^{2}}(1-#alpha,2N)/2") graphEup2.SetTitle("F^{-1}_{#chi^{2}}(#alpha,2(N-1))/2-N") graphs = [function, graphElow1, graphEup1, graphElow2, graphEup2] # DRAW settings W, H = 800, 800 T, B = 0.04 * H, 0.14 * H L, R = 0.12 * W, 0.04 * W xmin, xmax = -N * 0.01, N * 1.01 ymax = function.GetMaximum() * 1.16 width = 0.25 legendTextSize = 0.045 height = 0.062 * (1 + len(graphs)) x2 = 0.86 x1 = x2 - width y1 = 0.20 y2 = y1 + height # CANVAS canvas = TCanvas("canvas", "canvas", 100, 100, W, H) canvas.Divide(2) canvas.cd(1) gPad.SetPad("pad1", "pad1", 0, 0.37, 1, 1, 0, -1, 0) gPad.SetTopMargin(T / H) gPad.SetBottomMargin(0.01) gPad.SetLeftMargin(L / W) gPad.SetRightMargin(R / W) gPad.SetTickx(0) gPad.SetTicky(0) gPad.SetGrid() # FRAME frame = gPad.DrawFrame(xmin, 0, xmax, ymax) frame.GetYaxis().SetTitleSize(0.055) frame.GetXaxis().SetTitleSize(0.058) frame.GetXaxis().SetLabelSize(0.050 * 0) frame.GetYaxis().SetLabelSize(0.053) frame.GetXaxis().SetLabelOffset(0.012) frame.GetXaxis().SetTitleOffset(1.02) frame.GetYaxis().SetTitleOffset(1.08) frame.GetXaxis().SetNdivisions(505) frame.GetXaxis().SetTitle("number of events") frame.GetYaxis().SetTitle("error on number of events") legend = TLegend(x1, y1, x2, y2) # DRAW colors = [kRed, kBlue, kViolet, kYellow, kGreen] for i, graph in enumerate(graphs): style = kSolid width = 2 if i < 3: width = 3 if i > 2: style = kDashed graph.SetLineColor(colors[i % len(colors)]) graph.SetLineStyle(style) graph.SetLineWidth(width) graph.Draw('L SAME') legend.AddEntry(graph, graph.GetTitle(), 'L') #CMS_lumi.CMS_lumi(gPad,13,0) gPad.SetTicks(1, 1) gPad.Modified() frame.Draw('SAME AXIS') # LEGEND legend.SetFillStyle(0) legend.SetBorderSize(0) legend.SetTextSize(legendTextSize) legend.SetTextFont(62) legend.SetHeader("method") legend.SetTextFont(42) legend.Draw() # CHI2 TEXT text1 = "#splitline{#alpha = (1-0.6827)/2}{upper #chi^{2} CDF F_{#chi^{2}}}" text = TLatex() text.SetNDC() text.SetTextFont(42) text.SetTextSize(legendTextSize) text.SetTextAlign(13) # centered: 22 xoffset = (legend.GetX2() - legend.GetX1()) * legend.GetMargin() * 1.04 text.DrawLatexNDC(legend.GetX1() + xoffset, legend.GetY1() - 0.012, text1) # RATIO plot canvas.cd(2) gPad.SetPad("pad2", "pad2", 0, 0, 1, 0.36, 0, -1, 0) gPad.SetTopMargin(0.05) gPad.SetBottomMargin(0.24) gPad.SetLeftMargin(L / W) gPad.SetRightMargin(R / W) # FRAME ratio frame_ratio = gPad.DrawFrame(xmin, 0.50, xmax, 1.50) frame_ratio.GetYaxis().CenterTitle() frame_ratio.GetYaxis().SetTitleSize(0.055 * 1.79) frame_ratio.GetXaxis().SetTitleSize(0.058 * 1.79) frame_ratio.GetXaxis().SetLabelSize(0.052 * 1.79) frame_ratio.GetYaxis().SetLabelSize(0.050 * 1.79) frame_ratio.GetXaxis().SetLabelOffset(0.012) frame_ratio.GetXaxis().SetTitleOffset(1.00) frame_ratio.GetYaxis().SetTitleOffset(0.63) frame_ratio.GetXaxis().SetNdivisions(508) frame_ratio.GetYaxis().CenterTitle(True) frame_ratio.GetYaxis().SetTitle("ratio") frame_ratio.GetXaxis().SetTitle("number of events N") frame_ratio.GetYaxis().SetNdivisions(5) # DRAW ratio ratios = [] graphf = makeGraphFromTF1(function, N) for i, graph in enumerate(graphs): if isinstance(graph, TF1): graph = makeGraphFromTF1(graph, N) ratio = makeRatioTGraphs(graph, graphf) ratio.SetLineColor(graph.GetLineColor()) ratio.SetLineStyle(graph.GetLineStyle()) ratio.SetLineWidth(graph.GetLineWidth()) ratio.Draw('L SAME') ratios.append(ratio) gPad.SetTicks(1, 1) gPad.Modified() frame_ratio.Draw('sameaxis') # SAVE canvas.SaveAs("PoissonError.png") canvas.Close()