def drawRatio(histRatio,stack,Region,BLINDEDLIST,nbins,xmin,xmax): #histRatio = datastack.sum.Clone() #histRatio.merge_bins([(0, 1), (-2, -1)]) tempDataStack = histRatio.Clone() ratioMinimum=0 ratioMaximum=2 histRatio.SetMinimum(ratioMinimum) histRatio.SetMaximum(ratioMaximum) histRatio.yaxis.divisions = 5 histRatio.Divide(stack.sum) if Region not in BLINDEDLIST: histRatio.Draw("PE") line = Line(float(xmin)+1e-2*float(xmax),1.,float(xmax)-1e-2*float(xmax),1.); line.SetLineWidth(4); line.SetLineColor("red"); line.Draw(); if Region not in BLINDEDLIST: histRatio.Draw("SAME EP") MCerrorband = stack.sum.Clone() MCerrorband.SetLineWidth(10) MCerrorband.SetFillStyle(3244) MCerrorband.SetFillColor(922) for i in range(1,int(nbins)+1): if stack.sum.GetBinContent(i) != 0: if tempDataStack.GetBinContent(i)/stack.sum.GetBinContent(i) >= 2: xcoord = stack.sum.GetBinCenter(i) arrow = Arrow(xcoord,1.67,xcoord,1.87,0.015,"|>") arrow.SetAngle(50) arrow.SetLineWidth(6) arrow.SetLineColor(2) arrow.SetFillColor(2) arrow.Draw() if tempDataStack.GetBinContent(i)/stack.sum.GetBinContent(i) <= -2: xcoord = stack.sum.GetBinCenter(i) arrow = Arrow(xcoord,0.33,xcoord,0.13,0.015,"|>") arrow.SetAngle(50) arrow.SetLineWidth(6) arrow.SetLineColor(2) arrow.SetFillColor(2) arrow.Draw(); mcerror = 0.0 content = stack.sum.GetBinContent(i) error = stack.sum.GetBinError(i) if content != 0 : mcerror = (content+error)/content - 1.0 MCerrorband.SetBinContent(i,1) MCerrorband.SetBinError(i,mcerror) MCerrorband.Draw("E2PSAME")
def draw_curve_7(_func, name, title, ylow, yhigh): """ Draw 7TeV trigger efficiency curves """ graphs = [] for (trigger, color) in triggers: tool = ROOT.TauTriggerCorrections( os.path.join(base, 'triggerSF_%s.root' % trigger)) func = getattr(tool, _func) eff = map(lambda x: func(x, 0), pt) eff_low = map(lambda x: func(x, -1), pt) eff_high = map(lambda x: func(x, 1), pt) graph = Graph(len(pt), name=trigger) for i, (p, e, e_low, e_high) in enumerate(zip(pt, eff, eff_low, eff_high)): graph.SetPoint(i, p / 1000, e) graph.SetPointError(i, 0.4, 0.4, e - e_low, e_high - e) graph.linecolor = color graph.linewidth = 2 graph.fillstyle = '/' graph.fillcolor = color graphs.append(graph) c = Canvas() leg = Legend(len(graphs), pad=c, topmargin=0.4, leftmargin=0.3, textsize=25, margin=0.2) for i, g in enumerate(graphs): if i == 0: g.Draw('3AC') g.xaxis.title = '#font[52]{p}_{T} [GeV]' g.xaxis.SetLimits(20, 100) g.yaxis.SetLimits(ylow, yhigh) g.yaxis.SetRangeUser(ylow, yhigh) g.yaxis.title = title else: g.Draw('3C SAME') leg.AddEntry(g, g.name, 'L') leg.Draw() lines = [] for thresh in (25, 35): line = Line(thresh, ylow, thresh, yhigh) line.linestyle = 'dashed' line.linewidth = 2 line.Draw() lines.append(line) c.SaveAs('trigger_{0}.png'.format(name)) c.SaveAs('trigger_{0}.eps'.format(name))
# ROOT.gPad.Update() # ROOT.gPad.RedrawAxis('G') legend.Draw() title = TitleAsLatex('[{}] {}'.format(chan.replace('mu', '#mu'), htitle.split(';')[0])) title.Draw() draw_labels('59.74 fb^{-1} (13 TeV)', cms_position='left', extra_text='work-in-progress') if args.vline is not None: vline = Line(args.vline, mainPad.GetUymin(), args.vline, axes[1].GetXmax()) vline.color = 'black' vline.linewidth = 2 vline.linestyle = 'dashed' vline.Draw() if args.dataset == 'all': # draw ratio on subpad subPad.cd() _ratio = hData.clone() _ratio.Divide(sumHistStack(hstack)) _ratio.SetMarkerSize(0.8) _ratio.yaxis.SetTitle('Data/MC') _ratio.yaxis.SetTitleOffset(0.35) _ratio.yaxis.CenterTitle() _ratio.yaxis.SetTitleSize(0.1) _ratio.yaxis.SetLabelSize(0.1) _ratio.xaxis.SetTitleSize(0.1) _ratio.xaxis.SetLabelSize(0.1)
def __init__(self, width=None, height=None, offset=0, ratio_height=None, ratio_margin=26, ratio_limits=(0, 2), ratio_divisions=4, prune_ratio_ticks=False, ratio_line_values=(1, ), ratio_line_width=2, ratio_line_style='dashed', xtitle=None, ytitle=None, ratio_title=None, tick_length=15, logy=False): # first init as normal canvas super(RatioPlot, self).__init__(width=width, height=height) # get margins in pixels left, right, bottom, top = self.margin_pixels default_height = self.height default_frame_height = default_height - bottom - top if ratio_height is None: ratio_height = default_height / 4. self.height += int(ratio_height) + ratio_margin + offset self.margin = (0, 0, 0, 0) main_height = default_frame_height + top + ratio_margin / 2. + offset ratio_height += ratio_margin / 2. + bottom # top pad for histograms with self: main = Pad(0., ratio_height / self.height, 1., 1.) if logy: main.SetLogy() main.margin_pixels = (left, right, ratio_margin / 2., top) main.Draw() # bottom pad for ratio plot with self: ratio = Pad(0, 0, 1, ratio_height / self.height) ratio.margin_pixels = (left, right, bottom, ratio_margin / 2.) ratio.Draw() # draw main axes with main: main_hist = Hist(1, 0, 1) main_hist.Draw('AXIS') # hide x-axis labels and title on main pad xaxis, yaxis = main_hist.xaxis, main_hist.yaxis xaxis.SetLabelOffset(1000) xaxis.SetTitleOffset(1000) # adjust y-axis title spacing yaxis.SetTitleOffset(yaxis.GetTitleOffset() * self.height / default_height) # draw ratio axes with ratio: ratio_hist = Hist(1, 0, 1) ratio_hist.Draw('AXIS') # adjust x-axis label and title spacing xaxis, yaxis = ratio_hist.xaxis, ratio_hist.yaxis xaxis.SetLabelOffset(xaxis.GetLabelOffset() * self.height / ratio_height) xaxis.SetTitleOffset(xaxis.GetTitleOffset() * self.height / ratio_height) # adjust y-axis title spacing yaxis.SetTitleOffset(yaxis.GetTitleOffset() * self.height / default_height) if ratio_limits is not None: low, high = ratio_limits if prune_ratio_ticks: delta = 0.01 * (high - low) / float(ratio_divisions % 100) low += delta high -= delta yaxis.SetLimits(low, high) yaxis.SetRangeUser(low, high) yaxis.SetNdivisions(ratio_divisions) if xtitle is not None: ratio_hist.xaxis.title = xtitle if ytitle is not None: main_hist.yaxis.title = ytitle if ratio_title is not None: ratio_hist.yaxis.title = ratio_title # set the tick lengths tick_length_pixels(main, main_hist.xaxis, main_hist.yaxis, tick_length) tick_length_pixels(ratio, ratio_hist.xaxis, ratio_hist.yaxis, tick_length) # draw ratio lines lines = [] if ratio_line_values: with ratio: for value in ratio_line_values: line = Line(0, value, 1, value) line.linestyle = ratio_line_style line.linewidth = ratio_line_width line.Draw() lines.append(line) self.lines = lines self.main = main self.main_hist = main_hist self.ratio = ratio self.ratio_hist = ratio_hist self.ratio_limits = ratio_limits self.logy = logy
def plotratio(datastack, stack): histRatio = datastack.sum.Clone() #histRatio.merge_bins([(0, 1), (-2, -1)]) ratioMinimum = 0 ratioMaximum = 2 histRatio.SetMinimum(ratioMinimum) histRatio.SetMaximum(ratioMaximum) histRatio.xaxis.SetTitle(xtitle) histRatio.yaxis.SetTitle("Data/SM") histRatio.yaxis.CenterTitle() histRatio.yaxis.divisions = 5 histRatio.Divide(stack.sum) histRatio.xaxis.SetTitleOffset(3.4) histRatio.yaxis.SetTitleOffset(2) histRatio.xaxis.set_label_size(int(canvasheight * labelscale * 1.5)) histRatio.yaxis.set_label_size(int(canvasheight * labelscale * 1.5)) if plotData: histRatio.Draw("PE") line = Line( float(xmin) + 1e-2 * float(xmax), 1., float(xmax) - 1e-2 * float(xmax), 1.) line.SetLineWidth(4) line.SetLineColor("red") if plotData: line.Draw("Same") else: line.Draw() if plotData: histRatio.Draw("SAME EP") MCerrorband = stack.sum.Clone() MCerrorband.SetLineWidth(10) MCerrorband.SetFillStyle(3244) MCerrorband.SetFillColor(922) for i in range(1, int(nbins) + 1): if stack.sum.GetBinContent(i) != 0: if datastack.sum.GetBinContent(i) / stack.sum.GetBinContent( i) >= 2: xcoord = stack.sum.GetBinCenter(i) arrow = Arrow(xcoord, 1.67, xcoord, 1.87, 0.015, "|>") arrow.SetAngle(50) arrow.SetLineWidth(6) arrow.SetLineColor(2) arrow.SetFillColor(2) arrow.Draw() if datastack.sum.GetBinContent(i) / stack.sum.GetBinContent( i) <= -2: xcoord = stack.sum.GetBinCenter(i) arrow = Arrow(xcoord, 0.33, xcoord, 0.13, 0.015, "|>") arrow.SetAngle(50) arrow.SetLineWidth(6) arrow.SetLineColor(2) arrow.SetFillColor(2) arrow.Draw() mcerror = 0.0 content = stack.sum.GetBinContent(i) error = stack.sum.GetBinError(i) if content != 0: mcerror = (content + error) / content - 1.0 MCerrorband.SetBinContent(i, 1) MCerrorband.SetBinError(i, mcerror) MCerrorband.Draw("E2PSAME")
def draw_curve_8(_func, name, title, ylow, yhigh, num_errs=1): """ Draw 8TeV trigger efficiency curves """ graphs = [] for (trigger, color) in triggers: tool = ROOT.TrigTauEfficiency() tool.loadInputFile( os.path.join(base, 'triggerSF_{0}.root'.format(trigger))) func = getattr(tool, _func) eff = np.array( map(lambda x: func(x, eta, 0, period, prong, wpflag, eveto), pt)) errs_low = [] errs_high = [] for ierr in xrange(num_errs): eff_low = np.array( map(lambda x: func(x, eta, -1, period, prong, wpflag, eveto), pt)) eff_high = np.array( map(lambda x: func(x, eta, 1, period, prong, wpflag, eveto), pt)) errs_low.append(eff_low) errs_high.append(eff_high) # quadrature sum of error eff_low = np.sqrt( np.sum([np.power(err, 2) for err in errs_low], axis=0)) eff_high = np.sqrt( np.sum([np.power(err, 2) for err in errs_high], axis=0)) graph = Graph(len(pt), name=trigger) for i, (p, e, e_low, e_high) in enumerate(zip(pt, eff, eff_low, eff_high)): graph.SetPoint(i, p / 1000, e) graph.SetPointError(i, 0.4, 0.4, e_low, e_high) graph.linecolor = color graph.linewidth = 2 graph.fillstyle = '/' graph.fillcolor = color graphs.append(graph) c = Canvas() leg = Legend(len(graphs), pad=c, topmargin=0.6, leftmargin=0.3, textsize=25, margin=0.2) for i, g in enumerate(graphs): if i == 0: g.Draw('3AL') g.xaxis.title = '#font[52]{p}_{T} [GeV]' g.xaxis.SetLimits(20, 100) g.yaxis.SetLimits(ylow, yhigh) g.yaxis.SetRangeUser(ylow, yhigh) g.yaxis.title = title else: g.Draw('3L SAME') leg.AddEntry(g, g.name, 'L') leg.Draw() lines = [] for thresh in (25, 35): line = Line(thresh, ylow, thresh, yhigh) line.linestyle = 'dashed' line.linewidth = 2 line.Draw() lines.append(line) c.SaveAs('trigger_{0}.png'.format(name)) c.SaveAs('trigger_{0}.eps'.format(name))
leg = Legend( 3, pad=canvas, topmargin=0.05, margin=0.2, entryheight=0.02, entrysep=0.01, textsize=12, ) leg.AddEntry(d0inc, label='inclusive') leg.AddEntry(d0wbj, label='N_{bjet}#geq1') leg.AddEntry(d0wbj_c, label='N_{bjet}#geq1 (scaled)') leg.Draw() title = TitleAsLatex( '[4#mu VR] muon-type lepton-jet min |d_{0}| shape comparison') title.Draw() frame = canvas.FindObject('TFrame') lo, hi = frame.GetY1(), frame.GetY2() vline = Line(100, lo, 100, hi) vline.color = 'black' vline.linewidth = 2 vline.linestyle = 'dashed' vline.Draw() canvas.SaveAs('{}/ch4mu_muljd0Shape.pdf'.format(outdir)) canvas.clear() pf.close()