Logo ROOT  
Reference Guide
rf305_condcorrprod.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook
4 ## Multidimensional models: multi-dimensional pdfs with conditional pdfs in product
5 ##
6 ## `pdf = gauss(x,f(y),sx | y ) * gauss(y,ms,sx)` with `f(y) = a0 + a1*y`
7 ##
8 ## \macro_code
9 ##
10 ## \date February 2018
11 ## \authors Clemens Lange, Wouter Verkerke (C++ version)
12 
13 import ROOT
14 
15 # Create conditional pdf gx(x|y)
16 # -----------------------------------------------------------
17 
18 # Create observables
19 x = ROOT.RooRealVar("x", "x", -5, 5)
20 y = ROOT.RooRealVar("y", "y", -5, 5)
21 
22 # Create function f(y) = a0 + a1*y
23 a0 = ROOT.RooRealVar("a0", "a0", -0.5, -5, 5)
24 a1 = ROOT.RooRealVar("a1", "a1", -0.5, -1, 1)
25 fy = ROOT.RooPolyVar("fy", "fy", y, ROOT.RooArgList(a0, a1))
26 
27 # Create gaussx(x,f(y),sx)
28 sigmax = ROOT.RooRealVar("sigma", "width of gaussian", 0.5)
29 gaussx = ROOT.RooGaussian(
30  "gaussx", "Gaussian in x with shifting mean in y", x, fy, sigmax)
31 
32 # Create pdf gy(y)
33 # -----------------------------------------------------------
34 
35 # Create gaussy(y,0,5)
36 gaussy = ROOT.RooGaussian(
37  "gaussy",
38  "Gaussian in y",
39  y,
40  ROOT.RooFit.RooConst(0),
41  ROOT.RooFit.RooConst(3))
42 
43 # Create product gx(x|y)*gy(y)
44 # -------------------------------------------------------
45 
46 # Create gaussx(x,sx|y) * gaussy(y)
47 model = ROOT.RooProdPdf(
48  "model",
49  "gaussx(x|y)*gaussy(y)",
50  ROOT.RooArgSet(gaussy),
51  ROOT.RooFit.Conditional(
52  ROOT.RooArgSet(gaussx),
53  ROOT.RooArgSet(x)))
54 
55 # Sample, fit and plot product pdf
56 # ---------------------------------------------------------------
57 
58 # Generate 1000 events in x and y from model
59 data = model.generate(ROOT.RooArgSet(x, y), 10000)
60 
61 # Plot x distribution of data and projection of model x = Int(dy)
62 # model(x,y)
63 xframe = x.frame()
64 data.plotOn(xframe)
65 model.plotOn(xframe)
66 
67 # Plot x distribution of data and projection of model y = Int(dx)
68 # model(x,y)
69 yframe = y.frame()
70 data.plotOn(yframe)
71 model.plotOn(yframe)
72 
73 # Make two-dimensional plot in x vs y
74 hh_model = model.createHistogram("hh_model", x, ROOT.RooFit.Binning(
75  50), ROOT.RooFit.YVar(y, ROOT.RooFit.Binning(50)))
76 hh_model.SetLineColor(ROOT.kBlue)
77 
78 # Make canvas and draw ROOT.RooPlots
79 c = ROOT.TCanvas("rf305_condcorrprod", "rf05_condcorrprod", 1200, 400)
80 c.Divide(3)
81 c.cd(1)
82 ROOT.gPad.SetLeftMargin(0.15)
83 xframe.GetYaxis().SetTitleOffset(1.6)
84 xframe.Draw()
85 c.cd(2)
86 ROOT.gPad.SetLeftMargin(0.15)
87 yframe.GetYaxis().SetTitleOffset(1.6)
88 yframe.Draw()
89 c.cd(3)
90 ROOT.gPad.SetLeftMargin(0.20)
91 hh_model.GetZaxis().SetTitleOffset(2.5)
92 hh_model.Draw("surf")
93 
94 c.SaveAs("rf305_condcorrprod.png")