Belle II Software  release-05-02-19
plotting_utils.py
1 # !/usr/bin/env python3
2 
3 """ plotting_utils.py : utility functions for plotting """
4 
5 __author__ = "Marco Milesi"
6 __email__ = "marco.milesi@unimelb.edu.au"
7 __maintainer__ = "Marco Milesi"
8 
9 import os
10 import math
11 import ROOT
12 
13 
14 def get_ndf(data, fitres):
15  """
16  Return the number of degrees of freedom from RooDataHist and RooFitResult
17  """
18  nbins = data.numEntries()
19  params = fitres.floatParsFinal().getSize()
20  return nbins - params
21 
22 
23 def draw_plots(**kwargs):
24 
25  hypodir = "pdg{0}{1}".format(abs(kwargs["hypo"]), kwargs["append"])
26 
27  baseplotdir = "{0}/FitPlots/{1}".format(kwargs["outputplots"], hypodir)
28  for plottype in ["LINY", "LOGY"]:
29  plotdir = "{0}/{1}".format(baseplotdir, plottype)
30  if not os.path.exists(plotdir):
31  os.makedirs(plotdir)
32 
33  pdfplot = ROOT.TCanvas("pdfplot", "pdfplot", 1200, 1000)
34 
35  pad1 = ROOT.TPad("pad1", "", 0, 0.25, 1, 1)
36  pad2 = ROOT.TPad("pad2", "", 0, 0, 1, 0.25)
37  pad1.SetBottomMargin(0.02)
38  pad2.SetBottomMargin(0.4)
39  pad1.Draw()
40  pad2.Draw()
41 
42  frame1 = kwargs["frame1"]
43 
44  pad1.cd()
45  frame1.GetYaxis().SetTitleSize(0.05)
46  frame1.GetYaxis().SetTitleOffset(0.8)
47  frame1.GetXaxis().SetLabelSize(0)
48  frame1.GetXaxis().SetLabelOffset(999)
49  frame1.getAttText('pdf_paramBox').SetLineWidth(0) # To remove fit results box border.
50  frame1.getAttText('pdf_paramBox').SetFillStyle(0) # To make the fit results box transparent.
51  frame1.Draw()
52 
53  chi2 = frame1.chiSquare()
54 
55  t = ROOT.TLatex()
56  t.SetNDC()
57  t.SetTextColor(ROOT.kBlack)
58  t.SetTextFont(43)
59  t.SetTextSize(35)
60  t.DrawLatex(0.15, 0.8, "#frac{{#chi^{{2}}}}{{ndf}} = #frac{{{0:.1f}}}{{{1}}}".format(chi2 * kwargs["ndf"], kwargs["ndf"]))
61 
62  frame2 = kwargs["frame2"]
63 
64  pad2.cd()
65  frame2.GetYaxis().SetTitle(frame2.GetTitle())
66  frame2.SetTitle("")
67  frame2.GetYaxis().SetTitleSize(0.14)
68  frame2.GetYaxis().SetTitleOffset(0.3)
69  frame2.GetYaxis().SetLabelSize(0.1)
70  frame2.GetXaxis().SetTitleSize(0.15)
71  frame2.GetXaxis().SetLabelSize(0.15)
72  frame2.GetXaxis().SetNdivisions(kwargs["ndiv"])
73  frame2.Draw()
74 
75  badfit = "__BADFIT" if (chi2 > 500 or math.isnan(chi2)) else ""
76 
77  outname = "{0}/LINY/{1}_{2}_{3}{4}".format(baseplotdir,
78  hypodir,
79  kwargs["idx_p"],
80  kwargs["idx_theta"],
81  badfit)
82  pdfplot.Print("{0}.pdf".format(outname))
83 
84  # Make log plot as well
85  pad1.cd()
86  pad1.SetLogy()
87  outname = outname.replace("LINY", "LOGY")
88  pdfplot.Print("{0}_LOGY.pdf".format(outname))
89 
90  pdfplot.Close()