Logo ROOT  
Reference Guide
rf205_compplot.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook
4 ##
5 ## Addition and convolution: options for plotting components of composite p.d.f.s.
6 ##
7 ## \macro_code
8 ##
9 ## \date February 2018
10 ## \authors Clemens Lange, Wouter Verkerke (C++ version)
11 
12 import ROOT
13 
14 # Set up composite pdf
15 # --------------------------------------
16 
17 # Declare observable x
18 x = ROOT.RooRealVar("x", "x", 0, 10)
19 
20 # Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
21 # their parameters
22 mean = ROOT.RooRealVar("mean", "mean of gaussians", 5)
23 sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
24 sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
25 sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
26 sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
27 
28 # Sum the signal components into a composite signal p.d.f.
29 sig1frac = ROOT.RooRealVar(
30  "sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.)
31 sig = ROOT.RooAddPdf(
32  "sig", "Signal", ROOT.RooArgList(sig1, sig2), ROOT.RooArgList(sig1frac))
33 
34 # Build Chebychev polynomial p.d.f.
35 a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0., 1.)
36 a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0., 1.)
37 bkg1 = ROOT.RooChebychev("bkg1", "Background 1",
38  x, ROOT.RooArgList(a0, a1))
39 
40 # Build expontential pdf
41 alpha = ROOT.RooRealVar("alpha", "alpha", -1)
42 bkg2 = ROOT.RooExponential("bkg2", "Background 2", x, alpha)
43 
44 # Sum the background components into a composite background p.d.f.
45 bkg1frac = ROOT.RooRealVar(
46  "sig1frac", "fraction of component 1 in background", 0.2, 0., 1.)
47 bkg = ROOT.RooAddPdf(
48  "bkg", "Signal", ROOT.RooArgList(bkg1, bkg2), ROOT.RooArgList(sig1frac))
49 
50 # Sum the composite signal and background
51 bkgfrac = ROOT.RooRealVar("bkgfrac", "fraction of background", 0.5, 0., 1.)
52 model = ROOT.RooAddPdf(
53  "model", "g1+g2+a", ROOT.RooArgList(bkg, sig), ROOT.RooArgList(bkgfrac))
54 
55 # Set up basic plot with data and full pdf
56 # ------------------------------------------------------------------------------
57 
58 # Generate a data sample of 1000 events in x from model
59 data = model.generate(ROOT.RooArgSet(x), 1000)
60 
61 # Plot data and complete PDF overlaid
62 xframe = x.frame(ROOT.RooFit.Title(
63  "Component plotting of pdf=(sig1+sig2)+(bkg1+bkg2)"))
64 data.plotOn(xframe)
65 model.plotOn(xframe)
66 
67 # Clone xframe for use below
68 xframe2 = xframe.Clone("xframe2")
69 
70 # Make component by object reference
71 # --------------------------------------------------------------------
72 
73 # Plot single background component specified by object reference
74 ras_bkg = ROOT.RooArgSet(bkg)
75 model.plotOn(xframe, ROOT.RooFit.Components(
76  ras_bkg), ROOT.RooFit.LineColor(ROOT.kRed))
77 
78 # Plot single background component specified by object reference
79 ras_bkg2 = ROOT.RooArgSet(bkg2)
80 model.plotOn(xframe, ROOT.RooFit.Components(ras_bkg2), ROOT.RooFit.LineStyle(
81  ROOT.kDashed), ROOT.RooFit.LineColor(ROOT.kRed))
82 
83 # Plot multiple background components specified by object reference
84 # Note that specified components may occur at any level in object tree
85 # (e.g bkg is component of 'model' and 'sig2' is component 'sig')
86 ras_bkg_sig2 = ROOT.RooArgSet(bkg, sig2)
87 model.plotOn(xframe, ROOT.RooFit.Components(ras_bkg_sig2),
88  ROOT.RooFit.LineStyle(ROOT.kDotted))
89 
90 # Make component by name/regexp
91 # ------------------------------------------------------------
92 
93 # Plot single background component specified by name
94 model.plotOn(xframe2, ROOT.RooFit.Components(
95  "bkg"), ROOT.RooFit.LineColor(ROOT.kCyan))
96 
97 # Plot multiple background components specified by name
98 model.plotOn(
99  xframe2,
100  ROOT.RooFit.Components("bkg1,sig2"),
101  ROOT.RooFit.LineStyle(
102  ROOT.kDotted),
103  ROOT.RooFit.LineColor(
104  ROOT.kCyan))
105 
106 # Plot multiple background components specified by regular expression on
107 # name
108 model.plotOn(
109  xframe2,
110  ROOT.RooFit.Components("sig*"),
111  ROOT.RooFit.LineStyle(
112  ROOT.kDashed),
113  ROOT.RooFit.LineColor(
114  ROOT.kCyan))
115 
116 # Plot multiple background components specified by multiple regular
117 # expressions on name
118 model.plotOn(
119  xframe2,
120  ROOT.RooFit.Components("bkg1,sig*"),
121  ROOT.RooFit.LineStyle(
122  ROOT.kDashed),
123  ROOT.RooFit.LineColor(
124  ROOT.kYellow),
125  ROOT.RooFit.Invisible())
126 
127 # Draw the frame on the canvas
128 c = ROOT.TCanvas("rf205_compplot", "rf205_compplot", 800, 400)
129 c.Divide(2)
130 c.cd(1)
131 ROOT.gPad.SetLeftMargin(0.15)
132 xframe.GetYaxis().SetTitleOffset(1.4)
133 xframe.Draw()
134 c.cd(2)
135 ROOT.gPad.SetLeftMargin(0.15)
136 xframe2.GetYaxis().SetTitleOffset(1.4)
137 xframe2.Draw()
138 
139 c.SaveAs("rf205_compplot.png")