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))
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))
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))
h.Draw('colz') h.GetListOfFunctions().FindObject("palette").SetX2NDC(0.92) title = TitleAsLatex('[4#mu VR] ' + h.title) title.Draw() canvas.SaveAs('{}/ch4mu_vr_dphiisopre.pdf'.format(outdir)) canvas.Clear() h.Draw('colz') h.GetListOfFunctions().FindObject("palette").SetX2NDC(0.92) title.Draw() dphi_bounds = [0.7 * math.pi, 0.8 * math.pi, 0.9 * math.pi] vlines = [] for i, b in enumerate(dphi_bounds): vline = Line(b, h.yaxis.GetXmin(), b, h.yaxis.GetXmax()) vline.color = COLORS[i] vline.linewidth = 2 vlines.append(vline) for l in vlines: l.Draw() canvas.SaveAs('{}/ch4mu_vr_dphiisopre_vline.pdf'.format(outdir)) canvas.Clear() h.Draw('colz') h.GetListOfFunctions().FindObject("palette").SetX2NDC(0.92) title.Draw() iso_bounds = [0.2, 0.3, 0.4] hlines = [] for i, b in enumerate(iso_bounds): hline = Line(h.xaxis.GetXmin(), b, h.xaxis.GetXmax(), b) hline.color = COLORS[i] hline.linewidth = 2
if args.logx: axes[0].SetMoreLogLabels() mainPad.SetGrid() # 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)
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 __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 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()
def __init__(self, width=None, height=None, ratio_height=0.2, ratio_margin=0.05, ratio_range=(0, 2), ratio_divisions=4, ratio_line_values=(1,), ratio_line_width=2, ratio_line_style='dashed', xtitle=None, ytitle=None, ratio_title=None, tick_length=20): style = ROOT.gStyle # plot dimensions in pixels if height is not None: figheight = baseheight = height else: figheight = baseheight = style.GetCanvasDefH() if width is not None: figwidth = basewidth = width else: figwidth = basewidth = style.GetCanvasDefW() # margins left_margin = style.GetPadLeftMargin() bottom_margin = style.GetPadBottomMargin() top_margin = style.GetPadTopMargin() right_margin = style.GetPadRightMargin() figheight += (ratio_height + ratio_margin) * figheight ratio_height += bottom_margin + ratio_margin / 2. super(RatioPlot, self).__init__( width=int(figwidth), height=int(figheight)) self.SetMargin(0, 0, 0, 0) # top pad for histograms with self: main = Pad(0., ratio_height, 1., 1.) main.SetBottomMargin(ratio_margin / 2.) main.SetTopMargin(top_margin) main.SetLeftMargin(left_margin) main.SetRightMargin(right_margin) main.Draw() # bottom pad for ratio plot with self: ratio = Pad(0, 0, 1, ratio_height) ratio.SetBottomMargin(bottom_margin / ratio_height) ratio.SetTopMargin(ratio_margin / (2. * ratio_height)) ratio.SetLeftMargin(left_margin) ratio.SetRightMargin(right_margin) 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() * figheight / baseheight) # 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() / ratio_height) xaxis.SetTitleOffset( xaxis.GetTitleOffset() / ratio_height) # adjust y-axis title spacing yaxis.SetTitleOffset( yaxis.GetTitleOffset() * figheight / baseheight) if ratio_range is not None: yaxis.SetLimits(*ratio_range) yaxis.SetRangeUser(*ratio_range) 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_range = ratio_range