""" __author__ = "Maoqiang JING <*****@*****.**>" __copyright__ = "Copyright (c) Maoqiang JING" __created__ = "[2019-09-05 Thu 00:04]" import ROOT from ROOT import TCanvas, gStyle from ROOT import TFile, TH1F, TLegend import sys, os import logging from math import * from tools import convert_name, width_rawm_D, width_rm_Dpi logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s') gStyle.SetOptTitle(0) gStyle.SetOptTitle(0) def usage(): sys.stdout.write(''' NAME plot_chi2_kf.py SYNOPSIS ./plot_chi2_kf.py [ecms] [mode] [D_sample] [D_type] AUTHOR Maoqiang JING <*****@*****.**> DATE
def overlayhisto(dR_histo1, dR_histo2, dR_histo3, is1b, dirPath="plots"): c = PlotTemplates.myCanvas() gPad.GetUymax() #gPad.SetLogy() gStyle.SetOptStat(0) gStyle.SetOptTitle(0) leg = PlotTemplates.SetLegend(coordinate_=[.47, .73, .79, .88]) dR_histo1.SetLineWidth(3) dR_histo1.SetMarkerStyle(20) dR_histo1.SetMarkerSize(0.5) dR_histo1.SetLineColor(1) dR_histo1.SetMarkerColor(1) dR_histo1.GetXaxis().SetTitle("Delta R") dR_histo1.GetYaxis().SetTitle("Events/Bin") leg.AddEntry(dR_histo1, "Total (Before Cut)") dR_histo2.SetLineWidth(3) dR_histo2.SetMarkerStyle(21) dR_histo2.SetMarkerSize(0.5) dR_histo2.SetLineColor(2) dR_histo2.SetMarkerColor(2) leg.AddEntry(dR_histo2, "AB (Before Cut)") dR_histo3.SetLineWidth(3) dR_histo3.SetMarkerStyle(22) dR_histo3.SetMarkerSize(0.5) dR_histo3.SetLineColor(4) dR_histo3.SetMarkerColor(4) leg.AddEntry(dR_histo3, "CD (Before Cut)") #dR_histo4.SetLineWidth(3) #dR_histo4.SetMarkerStyle(23) #dR_histo4.SetMarkerSize(0.5) #dR_histo4.SetLineColor(3) #dR_histo4.SetMarkerColor(3) #dR_histo4.GetXaxis().SetTitle(xAxisName[i]) #leg.AddEntry(dR_histo4, "CD (After Cut)") dR_histo1.Draw("hist") dR_histo2.Draw("histsame") dR_histo3.Draw("histsame") #dR_histo4.Draw("histsame") leg.Draw() energy = PlotTemplates.drawenergy(is2017=False) for e in energy: e.Draw() if is1b: nb = "1b" nb_ext = "1b_dR_jet1_ele" if not is1b: nb = "2b" nb_ext = "2b_dR_jet1_ele" pt = TPaveText(0.1577181, 0.815, 0.3580537, 0.875, "brNDC") pt.SetBorderSize(0) pt.SetTextAlign(12) pt.SetFillStyle(0) pt.SetTextFont(52) pt.AddText("Top (e#nu) + " + nb + " CR") pt.Draw() dt = datetime.now() dt_str = dt.strftime("%Y%m%d_%H%M") subdirPath = dirPath + "/deltaR" subsubdirPath = subdirPath + "/TopenuCR_" + dt_str dirPDF = subsubdirPath + "/pdf" dirPNG = subsubdirPath + "/png" if not (os.path.exists(subdirPath)): os.mkdir(subdirPath) if not (os.path.exists(subsubdirPath)): os.mkdir(subsubdirPath) if not (os.path.exists(dirPDF)): os.mkdir(dirPDF) if not (os.path.exists(dirPNG)): os.mkdir(dirPNG) c.Update() c.Modified() c.SaveAs(dirPDF + "/TopenuCR_" + nb_ext + ".pdf") c.SaveAs(dirPNG + "/TopenuCR_" + nb_ext + ".png")
def CLICdpStyle(): gROOT.SetStyle("Plain") gStyle.SetCanvasColor(root.kWhite) gStyle.SetFrameFillColor(root.kWhite) gStyle.SetStatColor(root.kWhite) gStyle.SetPadColor(root.kWhite) gStyle.SetFillColor(10) gStyle.SetTitleFillColor(root.kWhite) gStyle.SetPaperSize(20, 26) gStyle.SetDrawBorder(0) gStyle.SetCanvasBorderMode(0) gStyle.SetPadBorderMode(0) gStyle.SetFrameBorderMode(0) gStyle.SetLegendBorderSize(0) gStyle.SetTextSize(0.05) gStyle.SetTitleSize(0.06,"xyz") gStyle.SetLabelSize(0.06,"xyz") gStyle.SetLabelOffset(0.015,"xyz") gStyle.SetTitleOffset(1.2,"yz") gStyle.SetTitleOffset(1.17,"x") font = 42 gStyle.SetTitleFont(font) gStyle.SetTitleFontSize(0.06) gStyle.SetStatFont(font) gStyle.SetStatFontSize(0.07) gStyle.SetTextFont(font) gStyle.SetLabelFont(font,"xyz") gStyle.SetTitleFont(font,"xyz") gStyle.SetTitleBorderSize(0) gStyle.SetStatBorderSize(1) gStyle.SetMarkerStyle(1) gStyle.SetLineWidth(2) gStyle.SetMarkerSize(1.2) gStyle.SetPalette(1) gStyle.SetOptTitle(0) gStyle.SetOptStat(0) gStyle.SetOptFit(0) gStyle.SetEndErrorSize(5) gStyle.SetHistLineWidth(2) gStyle.SetFrameLineWidth(2) gStyle.SetFuncWidth(2) gStyle.SetHistLineColor(root.kBlack) gStyle.SetFuncColor(root.kBlack) gStyle.SetLabelColor(root.kBlack,"xyz") gStyle.SetPadBottomMargin(0.18) gStyle.SetPadTopMargin(0.11) gStyle.SetPadRightMargin(0.08) gStyle.SetPadLeftMargin(0.17) gStyle.SetNdivisions(506, "xy") gStyle.SetPadGridX(0) gStyle.SetPadGridY(0) gStyle.SetPadTickX(1) gStyle.SetPadTickY(1) gStyle.SetCanvasDefW(800) gStyle.SetCanvasDefH(700) gROOT.ForceStyle()
""" Plot energy of other tracks and showers """ __author__ = "Maoqiang JING <*****@*****.**>" __copyright__ = "Copyright (c) Maoqiang JING" __created__ = "[2020-10-18 Sun 19:56]" import ROOT from ROOT import TCanvas, gStyle, TTree, THStack from ROOT import TFile, TH1F, TLegend, TArrow import sys, os import logging from math import * logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s') gStyle.SetOptTitle(0) # quench title gStyle.SetPadTickX(1) # dicide on boxing on or not of x and y axis gStyle.SetPadTickY(1) # dicide on boxing on or not of x and y axis def usage(): sys.stdout.write(''' NAME plot_E_othertrksshws.py SYNOPSIS ./plot_E_othertrksshws.py [ecms] [num_charm] AUTHOR Maoqiang JING <*****@*****.**> DATE
def plot1D(histname, histnum2D, histden2D, year, channel, title="", log=False): """Plot efficiency.""" dir = ensureDirectory('plots/%d' % year) name = "%s/%s_%s" % (dir, histname, channel) if log: name += "_log" header = "" xtitle = 'jet p_{T} [GeV]' ytitle = 'b tag efficiencies' if '_b_' in histname else 'b mistag rate' xmin, xmax = 20 if log else 10, histnum2D.GetXaxis().GetXmax() ymin, ymax = 5e-3 if log else 0.0, 2.0 colors = [kBlue, kRed, kOrange] x1, y1 = (0.27, 0.44) if '_b_' in histname else (0.55, 0.80) width, height = 0.3, 0.16 x2, y2 = x1 + width, y1 - height hists = createEff1D(histnum2D, histden2D) canvas = TCanvas('canvas', 'canvas', 100, 100, 800, 700) canvas.SetFillColor(0) canvas.SetBorderMode(0) canvas.SetFrameFillStyle(0) canvas.SetFrameBorderMode(0) canvas.SetTopMargin(0.04) canvas.SetBottomMargin(0.13) canvas.SetLeftMargin(0.12) canvas.SetRightMargin(0.05) canvas.SetTickx(0) canvas.SetTicky(0) canvas.SetGrid() gStyle.SetOptTitle(0) if log: canvas.SetLogx() canvas.SetLogy() canvas.cd() frame = hists[0] for i, hist in enumerate(hists): hist.SetLineColor(colors[i % len(colors)]) hist.SetMarkerColor(colors[i % len(colors)]) hist.SetLineWidth(2) hist.SetMarkerSize(2) hist.SetMarkerStyle(1) hist.Draw('PE0SAME') frame.GetXaxis().SetTitle(xtitle) frame.GetYaxis().SetTitle(ytitle) frame.GetXaxis().SetLabelSize(0.048) frame.GetYaxis().SetLabelSize(0.048) frame.GetXaxis().SetTitleSize(0.058) frame.GetYaxis().SetTitleSize(0.058) frame.GetXaxis().SetTitleOffset(1.03) frame.GetYaxis().SetTitleOffset(1.04) frame.GetXaxis().SetLabelOffset(-0.004 if log else 0.005) frame.GetXaxis().SetRangeUser(xmin, xmax) frame.SetMinimum(ymin) frame.SetMaximum(ymax) if title: latex = TLatex() latex.SetTextSize(0.04) latex.SetTextAlign(13) latex.SetTextFont(62) latex.SetNDC(True) latex.DrawLatex(0.15, 0.94, title) legend = TLegend(x1, y1, x2, y2) legend.SetTextSize(0.04) legend.SetBorderSize(0) legend.SetFillStyle(0) legend.SetFillColor(0) if header: legend.SetTextFont(62) legend.SetHeader(header) legend.SetTextFont(42) for hist in hists: legend.AddEntry(hist, hist.GetTitle(), 'lep') legend.Draw() canvas.SaveAs(name + '.pdf') canvas.SaveAs(name + '.png') canvas.Close()
def plot2D(histname, hist, year, channel, log=False): """Plot 2D efficiency.""" dir = ensureDirectory('plots/%d' % year) name = "%s/%s_%s" % (dir, histname, channel) if log: name += "_log" xtitle = 'jet p_{T} [GeV]' ytitle = 'jet #eta' ztitle = 'b tag efficiencies' if '_b_' in histname else 'b mistag rate' xmin, xmax = 20, hist.GetXaxis().GetXmax() zmin, zmax = 5e-3 if log else 0.0, 1.0 angle = 22 if log else 77 canvas = TCanvas('canvas', 'canvas', 100, 100, 800, 700) canvas.SetFillColor(0) canvas.SetBorderMode(0) canvas.SetFrameFillStyle(0) canvas.SetFrameBorderMode(0) canvas.SetTopMargin(0.07) canvas.SetBottomMargin(0.13) canvas.SetLeftMargin(0.12) canvas.SetRightMargin(0.17) canvas.SetTickx(0) canvas.SetTicky(0) canvas.SetGrid() gStyle.SetOptTitle(0) #FontSize(0.04) if log: canvas.SetLogx() canvas.SetLogz() canvas.cd() hist.GetXaxis().SetTitle(xtitle) hist.GetYaxis().SetTitle(ytitle) hist.GetZaxis().SetTitle(ztitle) hist.GetXaxis().SetLabelSize(0.048) hist.GetYaxis().SetLabelSize(0.048) hist.GetZaxis().SetLabelSize(0.048) hist.GetXaxis().SetTitleSize(0.058) hist.GetYaxis().SetTitleSize(0.058) hist.GetZaxis().SetTitleSize(0.056) hist.GetXaxis().SetTitleOffset(1.03) hist.GetYaxis().SetTitleOffset(1.04) hist.GetZaxis().SetTitleOffset(1.03) hist.GetXaxis().SetLabelOffset(-0.004 if log else 0.005) hist.GetZaxis().SetLabelOffset(-0.005 if log else 0.005) hist.GetXaxis().SetRangeUser(xmin, xmax) hist.SetMinimum(zmin) hist.SetMaximum(zmax) hist.Draw('COLZTEXT%d' % angle) gStyle.SetPaintTextFormat('.2f') hist.SetMarkerColor(kRed) hist.SetMarkerSize(1.8 if log else 1) #gPad.Update() #gPad.RedrawAxis() latex = TLatex() latex.SetTextSize(0.048) latex.SetTextAlign(23) latex.SetTextFont(42) latex.SetNDC(True) latex.DrawLatex(0.475, 0.99, hist.GetTitle()) # to prevent typesetting issues canvas.SaveAs(name + '.pdf') canvas.SaveAs(name + '.png') canvas.Close()
def DrawOverlap(fileVec, histVec, titleVec, legendtext, pngname, logstatus=[0, 0], xRange=[-99999, 99999, 1]): gStyle.SetOptTitle(0) gStyle.SetOptStat(0) gStyle.SetTitleOffset(1.1, "Y") gStyle.SetTitleOffset(0.9, "X") gStyle.SetLineWidth(3) gStyle.SetFrameLineWidth(3) i = 0 histList_ = [] histList = [] histList1 = [] maximum = [] ## Legend leg = TLegend(0.1, 0.70, 0.89, 0.89) #,NULL,"brNDC"); leg.SetBorderSize(0) leg.SetNColumns(3) leg.SetLineColor(1) leg.SetLineStyle(1) leg.SetLineWidth(1) leg.SetFillColor(0) leg.SetFillStyle(0) leg.SetTextFont(22) leg.SetTextSize(0.035) c = TCanvas("c1", "c1", 0, 0, 500, 500) #c.SetBottomMargin(0.15) #c.SetLeftMargin(0.15) #c.SetLogy(0) #c.SetLogx(0) c1_2 = TPad("c1_2", "newpad", 0.04, 0.13, 1, 0.994) c1_2.Draw() print("you have provided " + str(len(fileVec)) + " files and " + str(len(histVec)) + " histograms to make a overlapping plot") print "opening rootfiles" c.cd() c1_2.SetBottomMargin(0.013) c1_2.SetLogy(logstatus[1]) c1_2.SetLogx(logstatus[0]) c1_2.cd() ii = 0 inputfile = {} print str(fileVec[(len(fileVec) - 1)]) for ifile_ in range(len(fileVec)): print("opening file " + fileVec[ifile_]) inputfile[ifile_] = TFile(fileVec[ifile_]) print "fetching histograms" for ihisto_ in range(len(histVec)): print("printing histo " + str(histVec[ihisto_])) histo = inputfile[ifile_].Get(histVec[ihisto_]) #status_ = type(histo) is TGraphAsymmErrors histList.append(histo) # for ratio plot as they should nt be normalize histList1.append(histo) #print histList[ii].Integral() #histList[ii].Rebin(xRange[2]) histList[ii].Scale(1.0 / histList[ii].Integral()) maximum.append(histList[ii].GetMaximum()) maximum.sort() ii = ii + 1 print histList for ih in range(len(histList)): tt = type(histList[ih]) if logstatus[1] is 1: histList[ih].SetMaximum(100) #1.4 for log histList[ih].SetMinimum(0.01) #1.4 for log if logstatus[1] is 0: histList[ih].SetMaximum(1.4) #1.4 for log histList[ih].SetMinimum(0.001) #1.4 for log # print "graph_status =" ,(tt is TGraphAsymmErrors) # print "hist status =", (tt is TH1D) or (tt is TH1F) if ih == 0: if tt is TGraphAsymmErrors: histList[ih].Draw("APL") if (tt is TH1D) or (tt is TH1F) or (tt is TH1) or (tt is TH1I): histList[ih].Draw("hist") if ih > 0: #histList[ih].SetLineWidth(2) if tt is TGraphAsymmErrors: histList[ih].Draw("PL same") if (tt is TH1D) or (tt is TH1F) or (tt is TH1) or (tt is TH1I): histList[ih].Draw("hist same") if tt is TGraphAsymmErrors: histList[ih].SetMaximum(100) histList[ih].SetMarkerColor(colors[ih]) histList[ih].SetLineColor(colors[ih]) histList[ih].SetMarkerStyle(markerStyle[ih]) histList[ih].SetMarkerSize(1) leg.AddEntry(histList[ih], legendtext[ih], "PL") if (tt is TH1D) or (tt is TH1F) or (tt is TH1) or (tt is TH1I): histList[ih].SetLineStyle(linestyle[ih]) histList[ih].SetLineColor(colors[ih]) histList[ih].SetLineWidth(3) leg.AddEntry(histList[ih], legendtext[ih], "L") histList[ih].GetYaxis().SetTitle(titleVec[1]) histList[ih].GetYaxis().SetTitleSize(0.052) histList[ih].GetYaxis().SetTitleOffset(0.88) histList[ih].GetYaxis().SetTitleFont(22) histList[ih].GetYaxis().SetLabelFont(22) histList[ih].GetYaxis().SetLabelSize(.052) histList[ih].GetXaxis().SetRangeUser(xRange[0], xRange[1]) histList[ih].GetXaxis().SetLabelSize(0.0000) histList[ih].GetXaxis().SetTitle(titleVec[0]) histList[ih].GetXaxis().SetLabelSize(0.052) histList[ih].GetXaxis().SetTitleSize(0.052) histList[ih].GetXaxis().SetTitleOffset(1.04) histList[ih].GetXaxis().SetTitleFont(22) histList[ih].GetXaxis().SetTickLength(0.07) histList[ih].GetXaxis().SetLabelFont(22) histList[ih].GetYaxis().SetLabelFont(22) histList[ih].GetXaxis().SetNdivisions(508) #histList[ih].GetXaxis().SetMoreLogLabels(); #histList[ih].GetXaxis().SetNoExponent(); #histList[ih].GetTGaxis().SetMaxDigits(3); i = i + 1 pt = TPaveText(0.0877181, 0.9, 0.9580537, 0.96, "brNDC") pt.SetBorderSize(0) pt.SetTextAlign(12) pt.SetFillStyle(0) pt.SetTextFont(22) pt.SetTextSize(0.046) text = pt.AddText(0.2, 0.5, "CMS Internal") text = pt.AddText(0.65, 0.5, "#sqrt{s} = 13 TeV(2016) ") pt.Draw() # t2a = TPaveText(0.0877181,0.81,0.9580537,0.89,"brNDC") # t2a.SetBorderSize(0) # t2a.SetFillStyle(0) # t2a.SetTextSize(0.040) # t2a.SetTextAlign(12) # t2a.SetTextFont(62) # histolabel1= str(fileVec[(len(fileVec)-1)]) # text1 = t2a.AddText(0.06,0.5,"CMS Internal") # t2a.Draw() leg.Draw() # # c.cd() outputdirname = 'plots/' histname = outputdirname + pngname c.SaveAs(histname + '.png') c.SaveAs(histname + '.pdf')
def DrawAll(self, normtodata): gStyle.SetOptTitle(0) for prop in self.Props: self.Props[prop].Draw(normtodata)