def RatioCanvas(canvas_name, canvas_title, canw=500, canh=600, ratio_size_as_fraction=0.35): from ROOT import TCanvas, TPad c = TCanvas(canvas_name, canvas_title, canw, canh) c.cd() top = TPad("pad_top", "This is the top pad", 0.0, ratio_size_as_fraction, 1.0, 1.0) top.SetBottomMargin(0.02 / float(top.GetHNDC())) top.SetTopMargin(0.04 / float(top.GetHNDC())) top.SetRightMargin(0.05) top.SetLeftMargin(0.16) top.SetFillColor(0) top.Draw() tobject_collector.append(top) c.cd() bot = TPad("pad_bot", "This is the bottom pad", 0.0, 0.0, 1.0, ratio_size_as_fraction) bot.SetBottomMargin(0.11 / float(bot.GetHNDC())) bot.SetTopMargin(0.02 / float(bot.GetHNDC())) bot.SetRightMargin(0.05) bot.SetLeftMargin(0.16) bot.SetFillColor(0) bot.Draw() tobject_collector.append(bot) return c
def prepare_double_canvas(name, title, factor = 1): c = TCanvas(name, title, 1200, int(900 + 900 / factor)) pad1 = TPad("pad1","pad1",0,0,1,factor / (1. + factor)) pad2 = TPad("pad2","pad2",0,factor / (1. + factor),1,1) print("heights:",factor / (1. + factor) ) pad2.SetBottomMargin(0.01) pad2.SetRightMargin(0.03) pad2.SetLeftMargin(0.15) pad1.SetBorderMode(0) pad1.SetTopMargin(0.01) pad1.SetRightMargin(0.03) pad1.SetLeftMargin(0.15) pad1.SetBottomMargin(0.15) pad2.SetBorderMode(0) pad2.SetGridy() pad1.SetTickx(1) pad2.SetTickx(1) pad1.SetTicky(1) pad2.SetTicky(1) pad1.Draw() pad2.Draw() ROOT.SetOwnership(c,False) ROOT.SetOwnership(pad1,False) ROOT.SetOwnership(pad2,False) pad1.cd() return c, pad1, pad2
def createCanvasPads4(): c = TCanvas("c", "canvas", 800, 1100) # Upper histogram plot is pad1 pad1 = TPad("pad1", "pad1", 0.02, 0.575, 1, 1.0) pad1.SetBottomMargin(0) # joins upper and lower plot pad1.SetLeftMargin(0.13) #pad1.SetGridx() pad1.Draw() c.cd() pad2 = TPad("pad2", "pad2", 0.02, 0.41, 1, 0.57) pad2.SetBottomMargin(0) pad2.SetLeftMargin(0.13) pad2.SetGridx() pad2.SetGridy() pad2.Draw() # Lower ratio plot is pad2 c.cd() # returns to main canvas before defining pad2 pad3 = TPad("pad3", "pad3", 0.02, 0.26, 1, 0.40) pad3.SetTopMargin(0) # joins upper and lower plot pad3.SetBottomMargin(0) pad3.SetLeftMargin(0.13) pad3.SetGridx() pad3.SetGridy() pad3.Draw() c.cd() pad4 = TPad("pad4", "pad4", 0.02, 0.03, 1, 0.25) pad4.SetTopMargin(0) pad4.SetBottomMargin(0.5) pad4.SetLeftMargin(0.13) pad4.SetGridx() pad4.SetGridy() pad4.Draw() return c, pad1, pad2, pad3, pad4
def createCanvasPads(): c = TCanvas("c", "canvas", 680, 750) # Upper histogram plot is pad1 c.SetBorderSize(0) #c.SetLeftMargin(0.1); c.SetRightMargin(0.02) c.SetTopMargin(0.04) c.SetBottomMargin(0.0) pad1 = TPad("pad1", "pad1", 0.05, 0.35, 1, 1.0) pad1.SetTopMargin(0.04) # joins upper and lower plot pad1.SetBottomMargin(0.0) pad1.SetLeftMargin(0.17) pad1.SetRightMargin(0.02) pad1.SetTickx(1) pad1.SetTicky(1) #pad1.SetGridy() pad1.Draw() # Lower ratio plot is pad2 c.cd() # returns to main canvas before defining pad2 pad2 = TPad("pad2", "pad2", 0.05, 0.02, 1.0, 0.35) pad2.SetTopMargin(0) # joins upper and lower plot pad2.SetBottomMargin(0.5) pad2.SetLeftMargin(0.17) pad2.SetRightMargin(0.02) pad2.SetGridy() pad2.SetTickx(1) pad2.SetTicky(1) pad2.Draw() return c, pad1, pad2
def plot_histograms(histos, use_log_y=False, ratio=False, legend_titles=None, title="", x_label="", y_label_up="", y_label_ratio="", save_path="./plot.eps", **kwargs): """ Throws all given histograms into one canvas. If desired, a ratio plot will be added. """ linestyles = kwargs.get("linestyles", None) markerstyles = kwargs.get("markerstyles", None) colors = kwargs.get("colors", None) canvas_name = kwargs.get("canvas_name", "Canvas") style_histograms(histos, linestyles, markerstyles, colors) canvas = TCanvas('canvas', canvas_name, 800, 800) pad_up_start = 0.4 if ratio else 0. pad_up = TPad("pad_up", "", 0., pad_up_start, 1., 1.) if ratio: pad_up.SetBottomMargin(0.) pad_up.Draw() put_in_pad(pad_up, use_log_y, histos, title, "", y_label_up) pad_up.cd() legend = None if legend_titles is not None: legend = TLegend(.45, .65, .85, .85) legend.SetBorderSize(0) legend.SetFillColor(0) legend.SetFillStyle(0) legend.SetTextFont(42) legend.SetTextSize(0.02) for h, l in zip(histos, legend_titles): legend.AddEntry(h, l) legend.Draw() canvas.cd() pad_ratio = None histos_ratio = None if ratio: histos_ratio = divide_all_by_first(histos) pad_ratio = TPad("pad_ratio", "", 0., 0.05, 1., pad_up_start) pad_ratio.SetTopMargin(0.) pad_ratio.SetBottomMargin(0.3) pad_ratio.Draw() put_in_pad(pad_ratio, False, histos_ratio, "", x_label, y_label_ratio) canvas.SaveAs(save_path) canvas.Close()
def makeAndDivideCanvas(width, height, lower_pad_fraction): c = TCanvas("", "", width, height) upper_pad = TPad("", "", 0, lower_pad_fraction, 1, 1) upper_pad.SetBottomMargin(0.03) lower_pad = TPad("", "", 0, 0, 1, lower_pad_fraction) lower_pad.SetTopMargin(0.01) lower_pad.SetBottomMargin(0.4) return c, upper_pad, lower_pad
def residPullDivide(pad, topMargin=0.025, bottomMargin=0.1, leftMargin=0.2, rightMargin=0.05): ''' Divides the given pad in 3 sub-pads that are on top of each other w/o spaces, share the x-axis and the sizes of their plot area along y are in the ratio 2:1:1. This is meant to show a fit to data on top, residuals in the middle and pulls in the bottom. The optional arguments are margins as fractions of the original pad and are recomputed and applied to the sub-pads. The three sub-pads given on return have to be assign to a variable in order for the pads not to be destroyed by python's garbage collector.''' y1 = 0.5 * (1 - topMargin + bottomMargin) y2 = 0.25 * (1 - topMargin + 3 * bottomMargin) padsav = gPad pad.cd() name = pad.GetName() + '_1' pad1 = TPad(name, name, 0, y1, 1, 1) pad1.SetNumber(1) pad1.SetTopMargin(topMargin / (1. - y1)) pad1.SetBottomMargin(0) pad1.SetLeftMargin(leftMargin) pad1.SetRightMargin(rightMargin) pad1.Draw() name = pad.GetName() + '_2' pad2 = TPad(name, name, 0, y2, 1, y1) pad2.SetNumber(2) pad2.SetTopMargin(0) pad2.SetBottomMargin(0) pad2.SetLeftMargin(leftMargin) pad2.SetRightMargin(rightMargin) pad2.Draw() name = pad.GetName() + '_3' pad3 = TPad(name, name, 0, 0, 1, y2) pad3.SetNumber(3) pad3.SetTopMargin(0) pad3.SetBottomMargin(bottomMargin / y2) pad3.SetLeftMargin(leftMargin) pad3.SetRightMargin(rightMargin) pad3.Draw() # Clean up and exit pad.Modified() if padsav: padsav.cd() return pad1, pad2, pad3
def createCanvasPads(): c = TCanvas("c", "canvas", 900, 700) # Upper histogram plot is pad1 pad1 = TPad("pad1", "pad1", 0, 0.3, 1, 1.0) pad1.SetBottomMargin(0) # joins upper and lower plot pad1.Draw() # Lower ratio plot is pad2 c.cd() # returns to main canvas before defining pad2 pad2 = TPad("pad2", "pad2", 0, 0.05, 1, 0.3) pad2.SetTopMargin(0) # joins upper and lower plot pad2.SetBottomMargin(0.3) pad2.Draw() return c, pad1, pad2
def draw_ratio(c_title, h_top=[], h_bottom=[], draw_top=[], draw_bottom=[]): c = TCanvas(c_title) c.cd() pad_bottom = TPad("pad_bottom", "", 0, 0.02105263, 1, 0.4) pad_bottom.SetFrameBorderMode(0) pad_bottom.SetFrameBorderMode(0) pad_bottom.SetBorderMode(0) pad_bottom.SetTopMargin(0) pad_bottom.SetBottomMargin(0.3) pad_bottom.Draw() pad_bottom.cd() h_bottom[0].SetStats(0) pad_bottom.SetGridy() h_bottom[0].Draw(draw_bottom[0]) h_bottom[0].GetYaxis().SetRangeUser(0.91, 1.09) h_bottom[0].GetYaxis().SetNdivisions(5, 0, 5) h_bottom[0].GetYaxis().SetLabelSize(0.10) h_bottom[0].GetYaxis().SetTitleSize(0.13) h_bottom[0].GetYaxis().SetTitleOffset(0.35) h_bottom[0].GetXaxis().SetLabelSize(0.10) h_bottom[0].GetXaxis().SetTitleSize(0.13) h_bottom[0].GetXaxis().SetTitleOffset(0.85) h_bottom[0].GetYaxis().SetTitle("Data/MC") h_bottom[0].GetYaxis().CenterTitle() h_bottom[0].SetMarkerStyle(20) h_bottom[0].SetLineColor(1) for i, h in enumerate(h_bottom[1:]): h.Draw("same" + draw_bottom[i + 1]) c.cd() pad_top = TPad("pad_top", "", 0, 0.4, 1, 1) pad_top.SetBottomMargin(0) pad_top.SetTopMargin(0.15) pad_top.Draw() pad_top.cd() h_top[0].SetStats(0) h_top[0].DrawCopy(draw_top[0]) h_top[0].GetYaxis().SetTitleOffset(0.59) h_top[0].GetYaxis().SetTitleSize(0.07) h_top[0].SetFillStyle(3001) h_top[0].Draw("e2same") for i, h in enumerate(h_top[1:]): h.Draw("same" + draw_top[i + 1]) c.Update() return c
def buildCanvas(): global can, pad, padr can = TCanvas('can','',800,800) can.cd() can.Draw() sep = 0.35 pad = TPad('pad','',0.01,sep,0.99, 0.99) pad.SetBottomMargin(0.04) padr = TPad('padr','',0.01, 0.01, 0.99, sep) padr.SetTopMargin(0.04) padr.SetBottomMargin(0.3) pad.Draw() padr.Draw() return can, pad, padr
def buildCanvas(self): can = TCanvas('can_{num}'.format(num=self.__class__.CAN_NUM), self.name, 600, 600) self.__class__.CAN_NUM += 1 can.cd() can.Draw() sep = 0.35 pad = TPad('pad', '', 0.01, sep, 0.99, 0.99) pad.SetBottomMargin(0.04) padr = TPad('padr', '', 0.01, 0.01, 0.99, sep + 0.02) padr.SetTopMargin(0.04) padr.SetBottomMargin(0.3) padr.SetGridy() return can, pad, padr
def no_temp_file_create_canvas_two_pads(): c1 = ROOT.TCanvas("c1", "c1", 0, 0, 1000, 700) pad1 = TPad("pad1", "pad1", 0, 0.3, 1, 1.0) pad1.SetBottomMargin(0) pad1.SetGridx() pad1.Draw() c1.cd() # returns to main canvas before defining pad2 pad2 = TPad("pad2", "pad2", 0, 0.01, 1, 0.3) pad2.SetTopMargin(0) # joins upper and lower plot pad2.SetBottomMargin(0.35) pad2.SetGridx() pad2.Draw() pad1.cd() return c1, pad1, pad2
def plotEA(h_selected,h_total,label_selected,label_total,fileOut): c = Canvas('c') pad1 = TPad('pad1', 'pad1', 0., 0.3, 1., 1.) pad1.SetBottomMargin(0.005) pad1.SetTicks(1,1) pad1.SetGrid(1,1) pad1.Draw() c.cd() pad2 = TPad('pad2', 'pad2', 0., 0.05, 1., 0.29) pad2.SetTopMargin(0.005) pad2.SetBottomMargin(0.3) pad2.SetTicks(1,1) pad2.Draw() pad1.cd() h_total.SetTitle('') h_total.GetYaxis().SetTitle('Events') h_total.SetLineColor(ROOT.kBlue) h_total.SetFillColorAlpha(ROOT.kBlue,0.2) h_total.Draw('HIST') h_selected.SetTitle('') h_selected.SetLineColor(ROOT.kRed) h_selected.SetFillColorAlpha(ROOT.kRed,0.2) h_selected.Draw('HIST same') legend = TLegend(0.12, 0.61, 0.28, 0.8) legend.SetBorderSize(0) legend.SetFillColor(0) legend.SetFillStyle(0) legend.SetTextFont(42) legend.SetTextSize(0.031) legend.AddEntry(h_selected,label_selected, 'f') legend.AddEntry(h_total,label_total, 'f') legend.Draw() cLabel, pLabel, dLabel = condLabel(), simLabel(), descriptionLabel() cLabel.SetTextSize(0.049), pLabel.SetTextSize(0.049) cLabel.Draw(), pLabel.Draw(), dLabel.Draw() pad2.cd() pad2.SetGrid(0,1) h_new = h_selected.Clone('h_new') h_new.Sumw2() h_new.Divide(h_total) h_new.SetMarkerStyle(20) h_new.SetMarkerSize(0.9) h_new.SetLineColor(ROOT.kBlack) h_new.GetXaxis().SetTitle('#xi' if 'xi' in fileOut else 'missing mass') h_new.SetMinimum(-0.01), h_new.SetMaximum(1.05) h_new.Draw('p same') Prettify(h_new) #h_new.GetXaxis().SetTitle('Generated #xi') h_new.GetYaxis().SetTitle('Red/Blue') c.SaveAs(fileOut)
def BuildTPad(Properties, padname, xlow, ylow, xup, yup): """builds and returns a TPad taking the properties from a dictionary""" #READ FROM DICTIONARY OR TAKE DEFAULTS pad_xlow = Properties.get(padname + '_xlow', xlow) pad_ylow = Properties.get(padname + '_ylow', ylow) pad_xup = Properties.get(padname + '_xup', xup) pad_yup = Properties.get(padname + '_yup', yup) pad_settickx = Properties.get(padname + '_settickx', 1) pad_setticky = Properties.get(padname + '_setticky', 1) pad_logy = Properties.get(padname + '_logy', 0) pad_setbottommargin = Properties.get(padname + '_setbottommargin', 0.1) pad_settopmargin = Properties.get(padname + '_settopmargin', 0.1) pad_setleftmargin = Properties.get(padname + '_setleftmargin', 0.0) pad_setrightmargin = Properties.get(padname + '_setrightmargin', 0.0) # # # pad = TPad("pad", "pad", pad_xlow, pad_ylow, pad_xup, pad_yup) pad.SetTickx(pad_settickx) pad.SetTicky(pad_setticky) # pad.SetLogy(pad_logy) # pad.SetBottomMargin(pad_setbottommargin) pad.SetTopMargin(pad_settopmargin) pad.SetLeftMargin(pad_setleftmargin) pad.SetRightMargin(pad_setrightmargin) # # return pad
def overlayAndRatio(canvas, min_ratio, max_ratio, h1, h2): canvas.ResetAttPad() canvas.Clear() pad = TPad("main", "main", 0, 0.3, 1, 1) pad.SetBottomMargin(0.05) pad.Draw() pad.cd() h1.Draw() h2.SetLineColor(kOrange + 10) h2.Draw('SAME') canvas.cd() ratio = TPad("ratio", "ratio", 0, 0.05, 1, 0.3) ratio.SetTopMargin(0.05) ratio.Draw() ratio.cd() if isinstance(h1, TProfile) and isinstance(h2, TProfile): h1_p = h1.ProjectionX() h2_p = h2.ProjectionX() h1_p.Divide(h2_p) h1_p.SetMaximum(max_ratio) h1_p.SetMinimum(min_ratio) h1_p.SetMarkerStyle(kFullCircle) h1_p.SetMarkerSize(0.6) h1_p.SetTitle('') h1_p.GetXaxis().SetLabelFont(42) h1_p.GetXaxis().SetLabelSize(0.1) h1_p.GetYaxis().SetLabelFont(42) h1_p.GetYaxis().SetLabelSize(0.1) h1_p.GetYaxis().SetNdivisions(505) equality = TLine() h1_p.Draw("SAME HIST P") equality.SetLineColor(kOrange + 10) equality.DrawLine(h1_p.GetXaxis().GetXmin(), 1, h1_p.GetXaxis().GetXmax(), 1)
def plot3(key, xsections, histos, names, LogY, weights, outputDIR): c1=TCanvas(key, key, 800, 600) pad1 = TPad("pad1", "", 0.00, 0.0, 0.99, 0.99) pad1.SetGridx() pad1.SetGridy() pad1.SetFillColor(0) pad1.SetLineColor(0) pad1.SetBottomMargin(0.1) pad1.Draw() leg1 = TLegend(0.68,0.55,0.86,0.86) histos[0].GetXaxis().SetLabelSize(0) histos[0].SetMarkerStyle(20) for i in range(0,len(histos)): #if histos[i].Integral()==0: continue histos[i].Scale(xsections[i]/weights[i]) histos[i].SetLineColor(i+1) histos[i].SetLineWidth(2) pad1.cd() if LogY: pad1.SetLogy() ymax=[] histos[0].Draw('pe') for i in range(0, len(histos)): ymax.append(histos[i].GetMaximum()) ymax.sort(reverse=True) histos[0].SetMaximum(1.2* ymax[0]) #histos[0].SetMinimum(0.1) for i in range(1,len(histos)): histos[i].Draw('same h') histos[0].GetXaxis().SetTitle(key) histos[0].GetXaxis().SetTitleSize(0.04) histos[0].GetXaxis().SetTitleOffset(1.1) histos[0].GetXaxis().SetLabelSize(0.035) histos[0].GetYaxis().SetTitle("XS/bin [pb]") histos[0].GetYaxis().SetTitleSize(0.04) histos[0].GetYaxis().SetTitleOffset(1.09) histos[0].GetYaxis().SetLabelSize(0.035) leg1.AddEntry(histos[0], names[0]) for i in range(1,len(histos)): leg1.AddEntry(histos[i], names[i]) leg1.SetFillStyle(0) leg1.SetBorderSize(0) leg1.Draw() c1.Update() if LogY: c1.SaveAs(outputDIR + '/' + key + '_log.png') c1.SaveAs(outputDIR + '/' + key + '_log.pdf') else: c1.SaveAs(outputDIR + '/' + key + '.png') c1.SaveAs(outputDIR + '/' + key + '.pdf') for i in range(0,len(histos)): histos[i].Scale(weights[i]/xsections[i])
def createCanvasPads(): #creat canvas c = TCanvas("c_" + "{}".format(i), "c", 450, 450) # Upper histogram plot is pad1 pad1 = TPad("pad1", "pad1", 0.0, 0.3, 1, 1.0) pad1.SetBottomMargin(0.02) pad1.Draw() c.cd() # returns to main canvas before defining pad2 pad2 = TPad("pad2", "pad2", 0.0, 0.0, 1, 0.28) pad2.SetBottomMargin(0.3) pad2.SetTopMargin(0.02) pad2.SetGridx() pad2.SetGridy() pad2.Draw() return c, pad1, pad2
class MultiCanvas: def __init__(self, name='', yMargin = 0.2 ): self.name = name self.frame = TCanvas(name,name) # in case set the width above SetOwnership(self.frame,False) self.mainPad = TPad(name+'mainPad', '' ,0.01,yMargin,0.99,0.99) self.mainPad.SetBottomMargin(0) self.comparisonPad = TPad(name+'comparisonPad', '' ,0.01,0.01,0.99,yMargin) self.comparisonPad.SetTopMargin(0) self.comparisonPad.SetBottomMargin(0.33); self.comparisonPad.SetGridy() self.draw() def draw(self): self.mainPad.Draw() self.comparisonPad.Draw() self.frame.Modified() self.frame.Update()
def createCanvasPads(): c = TCanvas("c", "canvas", 800, 800) # Upper histogram plot is pad1 pad1 = TPad("pad1", "pad1", 0, 0.3, 1, 1.0) pad1.SetBottomMargin(0) # joins upper and lower plot #pad1.SetLeftMargin(1.55) # joins upper and lower plot pad1.SetTicks(0, 1) pad1.Draw() # Lower ratio plot is pad2 c.cd() # returns to main canvas before defining pad2 pad2 = TPad("pad2", "pad2", 0, 0.01, 1, 0.3) pad2.SetTopMargin(0) # joins upper and lower plot pad2.SetBottomMargin(0.28) pad2.SetTicks(0, 1) #pad2.SetGridx() pad2.Draw() return c, pad1, pad2
def create_canvas_three_pads(fp): fp.cd() c1 = ROOT.TCanvas("c1", "c1", 0, 0, 1000, 700) pad1 = TPad("pad1", "pad1", 0, 0.7, 1, 1.0) pad1.SetBottomMargin(0) pad1.SetGridx() pad1.Draw() c1.cd() # returns to main canvas before defining pad2 pad2 = TPad("pad2", "pad2", 0, 0.4, 1, 0.68) pad2.SetTopMargin(0) # joins upper and lower plot pad2.SetGridx() pad2.SetBottomMargin(0) pad2.Draw() pad3 = TPad("pad3", "pad3", 0, 0.01, 1, 0.38) pad3.SetTopMargin(0) # joins upper and lower plot pad3.SetGridx() pad3.SetBottomMargin(0) pad3.Draw() pad3.SetBottomMargin(0.4) pad1.cd() return c1, pad1, pad2, pad3
def draw_resoLin(histReso, histLin): pad1 = TPad("pad1", "pad1", 0, 0, 1, 0.66) pad2 = TPad("pad2", "pad2", 0, 0.66, 1, 1) pad2.SetBottomMargin(0.01) pad1.SetBorderMode(0) pad1.SetTopMargin(0.01) pad1.SetBottomMargin(0.15) pad2.SetBorderMode(0) pad1.SetTickx(1) pad2.SetTickx(1) pad1.SetTicky(1) pad2.SetTicky(1) pad1.Draw() pad2.Draw() pad1.cd() pad1.SetLogx() histReso.Draw("AP") histReso.GetXaxis().SetTitle("E [GeV]") histReso.GetYaxis().SetTitle("#sigma_{E_{tot}}/#LT E_{tot}#GT") histReso.GetXaxis().SetLimits(10, 11000) histReso.SetMinimum(0.) histReso.SetMaximum(0.15) histReso.Draw("AP") pad2.cd() pad2.SetLogx() pad2.SetGridy() histLin.GetYaxis().SetLabelSize(0.09) histLin.GetYaxis().SetTitleOffset(0.9) histLin.GetYaxis().SetTickLength(0.04) histLin.GetYaxis().SetTitleSize(0.09) histLin.GetXaxis().SetTickLength(0.04) histLin.GetXaxis().SetTitleOffset(1.1) histLin.GetXaxis().SetLabelSize(0.1) histLin.GetXaxis().SetTitleSize(0.09) histLin.GetXaxis().SetLimits(10, 11000) histLin.GetYaxis().SetRangeUser(-0.155, 0.155) histLin.Draw("AP")
def savePie(self, legend, lumi, labelx): cpie = TCanvas("cpie", "TPie test", 700, 700) pad1 = TPad("pad1", "pad1", 0.1, 0.1, 0.75, 0.75) pad1.SetBottomMargin(0.12) pad1.Draw() pad1.cd() colors = [] names = [] vals = [] for i in range(0, len(self.histos)): vals.append(self.histos[i].Integral()) colors.append(self.histos[i].GetLineColor()) names.append(self.histos[i].GetName()) v = array('d', vals) c = array('i', colors) pie4 = TPie("p4", "", len(v), v, c) pie4.SetRadius(.45) pie4.SetLabelsOffset(.01) pie4.SetLabelsOffset(100) pie4.SetEntryLineWidth(1, 2) pie4.SetEntryLineWidth(2, 2) pie4.SetEntryLineWidth(3, 2) pie4.SetEntryLineWidth(4, 2) pie4.SetEntryLineWidth(5, 2) pie4.SetEntryLineWidth(6, 2) pie4.SetEntryLineWidth(7, 2) pie4.Draw() self.makeLegend() self.myLegend.Draw() lat = TLatex() lat.SetNDC() lat.SetTextSize(0.06) lat.SetTextFont(42) lat.DrawLatex(0.12, -0.1, "Slepton signal region, " + labelx) self.banner3(True, lumi) for plotName in self.plotNames: path = 'plots/' + plotName #self.ensurePath(path) cpie.SaveAs(path) self.myLegend.IsA().Destructor(self.myLegend)
def main(progname, file_name, number): myFile1 = TFile.Open("%s" % (file_name), "READ") hist1 = myFile1.Get("Data_s%s" % (number)) myFile2 = TFile.Open("%s" % (file_name), "READ") hist2 = myFile2.Get("Model_cat1_s%s" % (number)) c1 = TCanvas("c1", "fit", 1000, 4000) hist1.Sumw2() hist1.SetLineColor(ROOT.kOrange + 10) hist1.SetMarkerStyle(20) hist1.SetLineWidth(1) hist1.SetMarkerSize(0.7) hist1.SetMarkerColorAlpha(ROOT.kOrange + 10, 1) # hist1.SetFillColorAlpha(ROOT.kOrange + 10, 0.4) hist1.SetStats(False) hist2.SetLineColor(ROOT.kViolet + 10) hist2.SetLineStyle(1) hist2.Sumw2() hist2.SetMarkerStyle(20) hist2.SetMarkerSize(0.7) hist2.SetLineWidth(1) hist2.SetMarkerColorAlpha(ROOT.kViolet + 10, 1) hist2.SetStats(False) #Create pad1 pad1 = TPad("pad1", "", 0, 0, 1, 1) pad1.SetLeftMargin(0.1) pad1.SetRightMargin(0.1) pad1.SetTopMargin(0.1) pad1.SetBottomMargin(0.1) pad1.Draw() pad1.cd() hist1.Draw("E1") hist2.Draw("E1 same") #Add the legend legend = TLegend(0.65, 0.75, 0.89, 0.89) legend.AddEntry(hist1, "LHCb data", "lpe") legend.AddEntry(hist2, "AmpGen fitter", "lpe") legend.SetTextFont(133) legend.SetTextSize(30) legend.SetLineColor(0) legend.Draw() c1.SaveAs("compare%s.png" % (number))
def myPad(): c = TCanvas("c", "", 800, 900) c.SetTopMargin(0.4) c.SetBottomMargin(0.05) c.SetRightMargin(0.1) c.SetLeftMargin(0.15) gStyle.SetOptStat(0) padMain = TPad("padMain", "", 0.0, 0.25, 1.0, 0.97) padMain.SetTopMargin(0.4) padMain.SetRightMargin(0.05) padMain.SetLeftMargin(0.17) padMain.SetBottomMargin(0) padMain.SetTopMargin(0.1) padRatio = TPad("padRatio", "", 0.0, 0.0, 1.0, 0.25) padRatio.SetRightMargin(0.05) padRatio.SetLeftMargin(0.17) padRatio.SetTopMargin(0.0) padRatio.SetBottomMargin(0.3) padMain.Draw() padRatio.Draw() return [c, padMain, padRatio]
def createCanvasPadsgt(): c = TCanvas("c", "canvas", 800, 1200) pad1 = TPad("pad1", "pad1", 0.02, 0.76, 1, 0.95) pad1.SetBottomMargin(0) pad1.SetTopMargin(0.11) pad1.SetLeftMargin(0.13) pad1.SetGridx() pad1.SetGridy() pad1.Draw() pad2 = TPad("pad2", "pad2", 0.02, 0.61, 1, 0.75) pad2.SetBottomMargin(0) pad2.SetTopMargin(0) pad2.SetLeftMargin(0.13) pad2.SetGridx() pad2.SetGridy() pad2.Draw() pad3 = TPad("pad3", "pad3", 0.02, 0.46, 1, 0.60) pad3.SetBottomMargin(0) pad3.SetTopMargin(0) pad3.SetLeftMargin(0.13) pad3.SetGridx() pad3.SetGridy() pad3.Draw() pad4 = TPad("pad4", "pad4", 0.02, 0.30, 1, 0.45) pad4.SetBottomMargin(0) pad4.SetTopMargin(0) pad4.SetLeftMargin(0.13) pad4.SetGridx() pad4.SetGridy() pad4.Draw() pad5 = TPad("pad5", "pad5", 0.02, 0.011, 1, 0.29) pad5.SetBottomMargin(0) pad5.SetTopMargin(0) pad5.SetLeftMargin(0.13) pad5.SetBottomMargin(0.45) pad5.SetGridx() pad5.SetGridy() pad5.Draw() return c, pad1, pad2, pad3, pad4, pad5
def show(self, outDir): if len(self.mc) == 0: print '%s has no MC!' % self.name return htype = self.mc[0].ClassName() if htype.find('TH2') >= 0: print 'Skipping TH2' return ROOT.gStyle.SetOptTitle(0) ROOT.gStyle.SetOptStat(0) ROOT.gROOT.SetBatch(1) canvas = TCanvas('c_' + self.name, 'C', 800, 800) canvas.cd() t1 = TPad("t1", "t1", 0.0, 0.20, 1.0, 1.0) t1.SetBottomMargin(0) t1.Draw() t1.cd() self._garbageList.append(t1) frame = None # Decide which backgrounds are visible maxint = max([x.Integral() for x in self.mc]) hists_to_add = [h for h in self.mc if h.Integral() > 0.005 * maxint] # Make the legend with the correct size leg = TLegend(0.75, 0.74 - 0.04 * max(len(hists_to_add) - 2, 0), .89, 0.89) leg.SetBorderSize(0) leg.SetFillStyle(0) leg.SetTextFont(43) leg.SetTextSize(20) nlegCols = 0 maxY = 1.0 if self.data is not None: leg.AddEntry(self.data, self.data.GetTitle(), 'p') frame = self.dataH.Clone('frame') self._garbageList.append(frame) maxY = self.dataH.GetMaximum() frame.Reset('ICE') elif self.dataH is not None: leg.AddEntry(self.dataH, self.dataH.GetTitle(), 'p') frame = self.dataH.Clone('frame') self._garbageList.append(frame) maxY = self.dataH.GetMaximum() frame.Reset('ICE') # Add the legend entries for the visible backgrounds for h in sorted(hists_to_add, key=lambda x: x.Integral(), reverse=True): leg.AddEntry(h, h.GetTitle(), 'f') nlegCols = nlegCols + 1 # Build the stack to plot from all backgrounds totalMC = None stack = THStack('mc', 'mc') for h in sorted(self.mc, key=lambda x: x.Integral()): stack.Add(h, 'hist') if totalMC is None: totalMC = h.Clone('totalmc') self._garbageList.append(totalMC) totalMC.SetDirectory(0) else: totalMC.Add(h) if totalMC is not None: maxY = max(totalMC.GetMaximum(), maxY) if frame is None: frame = totalMC.Clone('frame') frame.Reset('ICE') self._garbageList.append(frame) if self.data is not None or self.dataH is not None: nlegCols = nlegCols + 1 if nlegCols == 0: print '%s is empty' % self.name return frame.GetYaxis().SetTitleSize(0.045) frame.GetYaxis().SetLabelSize(0.04) frame.GetYaxis().SetRangeUser(0.5, 1.2 * maxY) frame.GetYaxis().SetNoExponent() frame.SetDirectory(0) frame.Draw() frame.GetYaxis().SetTitleOffset(1.6) stack.Draw('hist same') if self.data is not None: self.data.Draw('P') elif self.dataH is not None: self.dataH.Draw('e1same') # leg.SetNColumns(nlegCols) leg.Draw() # redrawBorder(t1) ## Draw CMS Preliminary label CMS_lumi(pad=t1, iPeriod=2, iPosX=0, extraText='') #Work in Progress') if self.normalizedToData: txt = TLatex() txt.SetNDC(True) txt.SetTextFont(42) txt.SetTextColor(ROOT.kGray + 1) txt.SetTextSize(0.035) txt.SetTextAngle(90) txt.SetTextAlign(12) txt.DrawLatex(0.05, 0.05, '#it{Normalized to data}') if totalMC is None or (self.data is None and self.dataH is None): t1.SetPad(0, 0, 1, 1) t1.SetBottomMargin(0.12) else: canvas.cd() t2 = TPad("t2", "t2", 0.0, 0.0, 1.0, 0.2) self._garbageList.append(t2) t2.SetTopMargin(0) t2.SetBottomMargin(0.4) t2.SetGridy() t2.Draw() t2.cd() ratioframe = self.dataH.Clone('ratioframe') ratioframe.Reset('ICE') ratioframe.Draw() ratioframe.GetYaxis().SetRangeUser(self.ratiorange[0], self.ratiorange[1]) ratioframe.GetYaxis().SetTitle('Obs./Exp.') ratioframe.GetYaxis().SetNdivisions(5) ratioframe.GetYaxis().SetLabelSize(0.15) ratioframe.GetXaxis().SetLabelSize(0.15) ratioframe.GetYaxis().SetTitleSize(0.18) ratioframe.GetXaxis().SetLabelSize(0.18) ratioframe.GetXaxis().SetTitleSize(0.18) ratioframe.GetYaxis().SetTitleOffset(0.4) ratioframe.GetXaxis().SetTitleOffset(0.9) gr = ROOT.TGraphAsymmErrors() gr.SetName("data2bkg") gr.SetMarkerStyle(self.dataH.GetMarkerStyle()) gr.SetMarkerSize(0.7 * self.dataH.GetMarkerSize()) gr.SetMarkerColor(self.dataH.GetMarkerColor()) gr.SetLineColor(self.dataH.GetLineColor()) gr.SetLineWidth(self.dataH.GetLineWidth()) bkgUncGr = ROOT.TGraphErrors() bkgUncGr.SetName('bkgunc') bkgUncGr.SetMarkerColor(920) bkgUncGr.SetMarkerStyle(1) bkgUncGr.SetLineColor(920) bkgUncGr.SetFillColor(920) bkgUncGr.SetFillStyle(3001) for xbin in xrange(1, self.dataH.GetXaxis().GetNbins() + 1): x = self.dataH.GetXaxis().GetBinCenter(xbin) dx = self.dataH.GetXaxis().GetBinWidth(xbin) dataCts = self.dataH.GetBinContent(xbin) if self.data: data_err_low = self.data.GetErrorYlow( xbin - 1) #get errors from the graph data_err_up = self.data.GetErrorYhigh(xbin - 1) else: data_err_low = self.dataH.GetBinError(xbin) data_err_up = data_err_low bkgCts = totalMC.GetBinContent(xbin) bkgCts_err = totalMC.GetBinError(xbin) if bkgCts == 0: continue errLo = math.sqrt( math.pow(data_err_low * bkgCts, 2) + math.pow(dataCts * bkgCts_err, 2)) / math.pow(bkgCts, 2) errHi = math.sqrt( math.pow(data_err_up * bkgCts, 2) + math.pow(dataCts * bkgCts_err, 2)) / math.pow(bkgCts, 2) np = gr.GetN() gr.SetPoint(np, x, dataCts / bkgCts) gr.SetPointError(np, 0, 0, errLo, errHi) bkgUncGr.SetPoint(np, x, 1) bkgUncGr.SetPointError(np, dx, bkgCts_err / bkgCts) bkgUncGr.Draw('2') gr.Draw('p') # redrawBorder(t2) canvas.cd() canvas.Modified() canvas.Update() for ext in self.plotformats: canvas.SaveAs(os.path.join(outDir, self.name + '.' + ext)) if self.savelog: t1.cd() t1.SetLogy() frame.GetYaxis().SetRangeUser(70, 2 * maxY) # frame.GetYaxis().SetRangeUser(1000,10*maxY) canvas.cd() for ext in self.plotformats: canvas.SaveAs(os.path.join(outDir, self.name + '_log.' + ext))
def showme(figure, data, theory, ratio, X, Y, YB): """A function to draw and and theory as well as their ratio. S.Chekanov @param figure Figure file @param: data Histogram with data @param: theory Histogram with theory @param: ratio TGraph or Histogram with for bottom plot @param: X X-axis attributes (Min,Max,Name,IsLog) @param: Y Y-axis attributes (Min,Max,Name,IsLog) @param: YB Bottom (lower) panel attributes (Min,Max,Name,IsLog) @author S.Chekanov (ANL) """ if (X[0] > X[1]): print("X min larger than X max") return if (Y[0] > Y[1]): print("X min larger than X mas") return if (YB[0] > YB[1]): print("X min larger than X mas") return inp = "plot" if (len(sys.argv) == 2): inp = sys.argv[1] print "Mode=", inp c1 = TCanvas("cv", "", 600, 500) c1.Divide() c1.SetTickx() c1.SetTicky() gROOT.SetStyle("Plain") gStyle.SetOptStat(0) cv1 = TPad("cv_a", "", 0.0, 0.20, 1.0, 1.0) cv1.SetTickx() cv1.SetTicky() cv1.SetTopMargin(0.05) cv1.SetBottomMargin(0.001) cv1.SetLeftMargin(0.12) cv1.SetRightMargin(0.05) cv1.Draw() cv2 = TPad("cv_b", "", 0.0, 0.0, 1.0, 0.275) cv2.SetTopMargin(0.0) cv2.SetTickx() cv2.SetTicky() cv2.SetLeftMargin(0.12) cv2.SetRightMargin(0.05) cv2.SetBottomMargin(0.35) cv2.Draw() if (Y[3] == 1): cv1.cd().SetLogy() if (X[3] == 1): cv1.cd().SetLogx() if (YB[3] == 1): cv2.cd().SetLogy() h1 = gPad.DrawFrame(X[0], Y[0], X[1], Y[1]) h1.Draw() ay = h1.GetYaxis() ay.SetTitleOffset(1.1) ay.SetTitle(Y[2]) data.Draw("same pe") theory.Draw("histo same") ######################################### #cv2.cd().SetGridy() cv2.cd().SetGridx() h2 = gPad.DrawFrame(X[0], YB[0], X[1], YB[1]) h2.Draw() ax = h2.GetXaxis() ax.SetTitleOffset(0.8) ax.SetTitle(X[2]) ax.SetLabelSize(0.12) ax.SetTitleSize(0.14) ay = h2.GetYaxis() #ay.SetTitle("OOOO" ) ay.SetNdivisions(505) ay.SetLabelSize(0.12) ay.SetTitleSize(0.14) ax.SetTitleOffset(1.1) ay.SetTitleOffset(0.0) ax.Draw("same") ay.Draw("same") ratio.SetMarkerColor(1) ratio.SetMarkerStyle(20) ratio.SetMarkerSize(1.0) ratio.SetMarkerColor(2) # plot depending on style if (type(ratio) == TGraphErrors or type(ratio) == TGraph or type(ratio) == TGraphAsymmErrors): ratio.Draw("P same") elif (type(ratio) == TH1F or type(ratio) == TH1D): ratio.SetFillColor(2) ratio.SetLineWidth(1) ratio.SetLineColor(1) ratio.Draw("same histo ][") else: ratio.Draw("P same") l1 = TLatex() l1.SetTextAngle(90) l1.SetTextSize(0.12) l1.SetNDC() l1.SetTextColor(1) l1.DrawLatex(0.06, 0.5, YB[2]) # draw line x1 = c1.XtoPad(X[0]) x2 = c1.XtoPad(X[1]) ar5 = TLine(x1, 0, x2, 0) ar5.SetLineWidth(2) ar5.SetLineStyle(2) ar5.Draw("same") c1.Print(figure) if (inp != "-b"): if (raw_input("Press any key to exit") != "-9999"): c1.Close() sys.exit(1)
c.SetFrameBorderMode(0) c.SetLeftMargin(L / W) c.SetRightMargin(R / W) c.SetTopMargin(T / H) c.SetBottomMargin(B / H) c.SetTickx(0) c.SetTicky(0) c.Draw() c.cd() pad1 = TPad("zxc_p1", "zxc_p1", 0, padRatio - padOverlap, 1, 1) pad2 = TPad("qwe_p2", "qwe_p2", 0, 0, 1, padRatio + padOverlap) pad1.SetLeftMargin(L / W) pad1.SetRightMargin(R / W) pad1.SetTopMargin(T / H / (1 - padRatio + padOverlap)) pad1.SetBottomMargin( (padOverlap + padGap) / (1 - padRatio + padOverlap)) pad1.SetFillColor(0) pad1.SetBorderMode(0) pad1.SetFrameFillStyle(0) pad1.SetFrameBorderMode(0) pad1.SetTickx(0) pad1.SetTicky(0) pad2.SetLeftMargin(L / W) pad2.SetRightMargin(R / W) pad2.SetTopMargin((padOverlap) / (padRatio + padOverlap)) pad2.SetBottomMargin(B / H / (padRatio + padOverlap)) pad2.SetFillColor(0) pad2.SetFillStyle(4000) pad2.SetBorderMode(0) pad2.SetFrameFillStyle(0)
particleType = sys.argv[3] fO = TFile("BN_ComparisonPlots_" + particleType + ".hist.root", "RECREATE") gROOT.SetBatch(kTRUE) gStyle.SetOptStat(0) for folder in GetKeyNames(f1): for histo in GetKeyNames(f1, folder): c1 = TCanvas() mainPad = TPad("mainPad", "top", 0.00, 0.254758, 1.00, 1.00) mainPad.SetLeftMargin(0.12) mainPad.SetRightMargin(0.04) mainPad.SetTopMargin(0.02) mainPad.SetBottomMargin(0.02) mainPad.SetTicky(0) mainPad.SetTickx(0) mainPad.Draw() c1.Update() ratioPad = TPad("ratioPad", "bottom", 0.00, 0.00, 1.00, 0.25) ratioPad.SetLeftMargin(0.12) ratioPad.SetRightMargin(0.04) ratioPad.SetTopMargin(0.03) ratioPad.SetTickx(0) ratioPad.SetBottomMargin(0.36) ratioPad.Draw() c1.Update()
#!/usr/bin/env python3 # Test ratio plot for systematics import ROOT from ROOT import TCanvas, TColor, TGaxis, TH1F, TPad from ana_util import * fStd = ROOT.TFile('std.root') fSys = ROOT.TFile('syspid.root') c = ROOT.TCanvas('cMC','Systematics', 800, 800) pad1 = TPad("pad1", "pad1", 0, 0.3, 1, 1.0) pad1.SetBottomMargin(0) # joins upper and lower plot pad1.SetGrid() pad1.SetLogy() pad1.Draw() pad1.cd() lgd = ROOT.TLegend(0.5, 0.12, 0.85, 0.45) fStd.hJpsiEffPrompt.Draw('PE') lgd.AddEntry(fStd.hJpsiEffPrompt, 'Prompt (Std. exclude hadrons)') fStd.hJpsiEffBdecay.Draw('same') lgd.AddEntry(fStd.hJpsiEffBdecay, 'Non-prompt (Std. exclude hadrons)') hPsys = fSys.hJpsiEffPrompt.Clone('hPsys') hPsys.SetMarkerStyle(kRoundHollow) hPsys.Draw('same') lgd.AddEntry(hPsys, 'Prompt (compare)')