Logo ROOT  
Reference Guide
rf201_composite.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook
4 ## Addition and convolution: composite pdf with signal and background component
5 ##
6 ## ```
7 ## pdf = f_bkg * bkg(x,a0,a1) + (1-fbkg) * (f_sig1 * sig1(x,m,s1 + (1-f_sig1) * sig2(x,m,s2)))
8 ## ```
9 ##
10 ## \macro_code
11 ##
12 ## \date February 2018
13 ## \authors Clemens Lange, Wouter Verkerke (C++ version)
14 
15 import ROOT
16 
17 # Setup component pdfs
18 # ---------------------------------------
19 
20 # Declare observable x
21 x = ROOT.RooRealVar("x", "x", 0, 10)
22 
23 # Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
24 # their parameters
25 mean = ROOT.RooRealVar("mean", "mean of gaussians", 5)
26 sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
27 sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
28 
29 sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
30 sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
31 
32 # Build Chebychev polynomial pdf
33 a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0.0, 1.0)
34 a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0.0, 1.0)
35 bkg = ROOT.RooChebychev("bkg", "Background", x, ROOT.RooArgList(a0, a1))
36 
37 
38 # Method 1 - Two RooAddPdfs
39 # ------------------------------------------
40 # Add signal components
41 
42 # Sum the signal components into a composite signal pdf
43 sig1frac = ROOT.RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8, 0.0, 1.0)
44 sig = ROOT.RooAddPdf("sig", "Signal", ROOT.RooArgList(sig1, sig2), ROOT.RooArgList(sig1frac))
45 
46 # Add signal and background
47 # ------------------------------------------------
48 
49 # Sum the composite signal and background
50 bkgfrac = ROOT.RooRealVar("bkgfrac", "fraction of background", 0.5, 0.0, 1.0)
51 model = ROOT.RooAddPdf("model", "g1+g2+a", ROOT.RooArgList(bkg, sig), ROOT.RooArgList(bkgfrac))
52 
53 # Sample, fit and plot model
54 # ---------------------------------------------------
55 
56 # Generate a data sample of 1000 events in x from model
57 data = model.generate(ROOT.RooArgSet(x), 1000)
58 
59 # Fit model to data
60 model.fitTo(data)
61 
62 # Plot data and PDF overlaid
63 xframe = x.frame(Title="Example of composite pdf=(sig1+sig2)+bkg")
64 data.plotOn(xframe)
65 model.plotOn(xframe)
66 
67 # Overlay the background component of model with a dashed line
68 ras_bkg = ROOT.RooArgSet(bkg)
69 model.plotOn(xframe, Components=ras_bkg, LineStyle=ROOT.kDashed)
70 
71 # Overlay the background+sig2 components of model with a dotted line
72 ras_bkg_sig2 = ROOT.RooArgSet(bkg, sig2)
73 model.plotOn(xframe, Components=ras_bkg_sig2, LineStyle=ROOT.kDotted)
74 
75 # Print structure of composite pdf
76 model.Print("t")
77 
78 # Method 2 - One RooAddPdf with recursive fractions
79 # ---------------------------------------------------
80 
81 # Construct sum of models on one go using recursive fraction interpretations
82 #
83 # model2 = bkg + (sig1 + sig2)
84 #
85 model2 = ROOT.RooAddPdf("model", "g1+g2+a", ROOT.RooArgList(bkg, sig1, sig2), ROOT.RooArgList(bkgfrac, sig1frac), True)
86 
87 # NB: Each coefficient is interpreted as the fraction of the
88 # left-hand component of the i-th recursive sum, i.e.
89 #
90 # sum4 = A + ( B + ( C + D) with fraction fA, and fC expands to
91 #
92 # sum4 = fA*A + (1-fA)*(fB*B + (1-fB)*(fC*C + (1-fC)*D))
93 
94 # Plot recursive addition model
95 # ---------------------------------------------------------
96 model2.plotOn(xframe, LineColor=ROOT.kRed, LineStyle=ROOT.kDashed)
97 model2.plotOn(xframe, Components=ras_bkg_sig2, LineColor=ROOT.kRed, LineStyle=ROOT.kDashed)
98 model2.Print("t")
99 
100 # Draw the frame on the canvas
101 c = ROOT.TCanvas("rf201_composite", "rf201_composite", 600, 600)
102 ROOT.gPad.SetLeftMargin(0.15)
103 xframe.GetYaxis().SetTitleOffset(1.4)
104 xframe.Draw()
105 
106 c.SaveAs("rf201_composite.png")