Logo ROOT  
Reference Guide
rf301_composition.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook
4 ## Multidimensional models: multi-dimensional pdfs through composition, e.g. substituting
5 ## a pdf parameter with a function that depends on other observables
6 ##
7 ## `pdf = gauss(x,f(y),s)` with `f(y) = a0 + a1*y`
8 ##
9 ## \macro_code
10 ##
11 ## \date February 2018
12 ## \authors Clemens Lange, Wouter Verkerke (C++ version)
13 
14 import ROOT
15 
16 # Setup composed model gauss(x, m(y), s)
17 # -----------------------------------------------------------------------
18 
19 # Create observables
20 x = ROOT.RooRealVar("x", "x", -5, 5)
21 y = ROOT.RooRealVar("y", "y", -5, 5)
22 
23 # Create function f(y) = a0 + a1*y
24 a0 = ROOT.RooRealVar("a0", "a0", -0.5, -5, 5)
25 a1 = ROOT.RooRealVar("a1", "a1", -0.5, -1, 1)
26 fy = ROOT.RooPolyVar("fy", "fy", y, ROOT.RooArgList(a0, a1))
27 
28 # Creat gauss(x,f(y),s)
29 sigma = ROOT.RooRealVar("sigma", "width of gaussian", 0.5)
30 model = ROOT.RooGaussian(
31  "model", "Gaussian with shifting mean", x, fy, sigma)
32 
33 # Sample data, plot data and pdf on x and y
34 # ---------------------------------------------------------------------------------
35 
36 # Generate 10000 events in x and y from model
37 data = model.generate(ROOT.RooArgSet(x, y), 10000)
38 
39 # Plot x distribution of data and projection of model x = Int(dy)
40 # model(x,y)
41 xframe = x.frame()
42 data.plotOn(xframe)
43 model.plotOn(xframe)
44 
45 # Plot x distribution of data and projection of model y = Int(dx)
46 # model(x,y)
47 yframe = y.frame()
48 data.plotOn(yframe)
49 model.plotOn(yframe)
50 
51 # Make two-dimensional plot in x vs y
52 hh_model = model.createHistogram("hh_model", x, ROOT.RooFit.Binning(
53  50), ROOT.RooFit.YVar(y, ROOT.RooFit.Binning(50)))
54 hh_model.SetLineColor(ROOT.kBlue)
55 
56 # Make canvas and draw ROOT.RooPlots
57 c = ROOT.TCanvas("rf301_composition", "rf301_composition", 1200, 400)
58 c.Divide(3)
59 c.cd(1)
60 ROOT.gPad.SetLeftMargin(0.15)
61 xframe.GetYaxis().SetTitleOffset(1.4)
62 xframe.Draw()
63 c.cd(2)
64 ROOT.gPad.SetLeftMargin(0.15)
65 yframe.GetYaxis().SetTitleOffset(1.4)
66 yframe.Draw()
67 c.cd(3)
68 ROOT.gPad.SetLeftMargin(0.20)
69 hh_model.GetZaxis().SetTitleOffset(2.5)
70 hh_model.Draw("surf")
71 
72 c.SaveAs("rf301_composition.png")