Logo ROOT  
Reference Guide
rf108_plotbinning.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook
4 ## Basic functionality: plotting unbinned data with alternate and variable binnings
5 ##
6 ## \macro_code
7 ##
8 ## \date February 2018
9 ## \authors Clemens Lange, Wouter Verkerke (C++ version)
10 
11 import ROOT
12 
13 # Set up model
14 # ---------------------
15 
16 # Build a B decay pdf with mixing
17 dt = ROOT.RooRealVar("dt", "dt", -20, 20)
18 dm = ROOT.RooRealVar("dm", "dm", 0.472)
19 tau = ROOT.RooRealVar("tau", "tau", 1.547)
20 w = ROOT.RooRealVar("w", "mistag rate", 0.1)
21 dw = ROOT.RooRealVar("dw", "delta mistag rate", 0.)
22 
23 mixState = ROOT.RooCategory("mixState", "B0/B0bar mixing state")
24 mixState.defineType("mixed", -1)
25 mixState.defineType("unmixed", 1)
26 tagFlav = ROOT.RooCategory("tagFlav", "Flavour of the tagged B0")
27 tagFlav.defineType("B0", 1)
28 tagFlav.defineType("B0bar", -1)
29 
30 # Build a gaussian resolution model
31 dterr = ROOT.RooRealVar("dterr", "dterr", 0.1, 1.0)
32 bias1 = ROOT.RooRealVar("bias1", "bias1", 0)
33 sigma1 = ROOT.RooRealVar("sigma1", "sigma1", 0.1)
34 gm1 = ROOT.RooGaussModel("gm1", "gauss model 1", dt, bias1, sigma1)
35 
36 # Construct Bdecay (x) gauss
37 bmix = ROOT.RooBMixDecay("bmix", "decay", dt, mixState, tagFlav,
38  tau, dm, w, dw, gm1, ROOT.RooBMixDecay.DoubleSided)
39 
40 # Sample data from model
41 # --------------------------------------------
42 
43 # Sample 2000 events in (dt,mixState,tagFlav) from bmix
44 data = bmix.generate(ROOT.RooArgSet(dt, mixState, tagFlav), 2000)
45 
46 # Show dt distribution with custom binning
47 # -------------------------------------------------------------------------------
48 
49 # Make plot of dt distribution of data in range (-15,15) with fine binning
50 # for dt>0 and coarse binning for dt<0
51 
52 # Create binning object with range (-15,15)
53 tbins = ROOT.RooBinning(-15, 15)
54 
55 # Add 60 bins with uniform spacing in range (-15,0)
56 tbins.addUniform(60, -15, 0)
57 
58 # Add 15 bins with uniform spacing in range (0,15)
59 tbins.addUniform(15, 0, 15)
60 
61 # Make plot with specified binning
62 dtframe = dt.frame(ROOT.RooFit.Range(-15, 15),
63  ROOT.RooFit.Title("dt distribution with custom binning"))
64 data.plotOn(dtframe, ROOT.RooFit.Binning(tbins))
65 bmix.plotOn(dtframe)
66 
67 # NB: Note that bin density for each bin is adjusted to that of default frame binning as shown
68 # in Y axis label (100 bins -. Events/0.4*Xaxis-dim) so that all bins
69 # represent a consistent density distribution
70 
71 # Show mixstate asymmetry with custom binning
72 # ------------------------------------------------------------------------------------
73 
74 # Make plot of dt distribution of data asymmetry in 'mixState' with
75 # variable binning
76 
77 # Create binning object with range (-10,10)
78 abins = ROOT.RooBinning(-10, 10)
79 
80 # Add boundaries at 0, (-1,1), (-2,2), (-3,3), (-4,4) and (-6,6)
81 abins.addBoundary(0)
82 abins.addBoundaryPair(1)
83 abins.addBoundaryPair(2)
84 abins.addBoundaryPair(3)
85 abins.addBoundaryPair(4)
86 abins.addBoundaryPair(6)
87 
88 # Create plot frame in dt
89 aframe = dt.frame(ROOT.RooFit.Range(-10, 10), ROOT.RooFit.Title(
90  "mixState asymmetry distribution with custom binning"))
91 
92 # Plot mixState asymmetry of data with specified customg binning
93 data.plotOn(aframe, ROOT.RooFit.Asymmetry(
94  mixState), ROOT.RooFit.Binning(abins))
95 
96 # Plot corresponding property of pdf
97 bmix.plotOn(aframe, ROOT.RooFit.Asymmetry(mixState))
98 
99 # Adjust vertical range of plot to sensible values for an asymmetry
100 aframe.SetMinimum(-1.1)
101 aframe.SetMaximum(1.1)
102 
103 # NB: For asymmetry distributions no density corrects are needed (and are
104 # thus not applied)
105 
106 # Draw plots on canvas
107 c = ROOT.TCanvas("rf108_plotbinning", "rf108_plotbinning", 800, 400)
108 c.Divide(2)
109 c.cd(1)
110 ROOT.gPad.SetLeftMargin(0.15)
111 dtframe.GetYaxis().SetTitleOffset(1.6)
112 dtframe.Draw()
113 c.cd(2)
114 ROOT.gPad.SetLeftMargin(0.15)
115 aframe.GetYaxis().SetTitleOffset(1.6)
116 aframe.Draw()
117 
118 c.SaveAs("rf108_plotbinning.png")