Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf407_ComputationalGraphVisualization.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit_main
3## \notebook -nodraw
4## Data and categories: Visualing computational graph model before fitting, and latex printing of lists and sets of RooArgSets after fitting
5##
6## \macro_code
7## \macro_output
8##
9## \date February 2018
10## \authors Clemens Lange, Wouter Verkerke (C++ version)
11
12import ROOT
13
14
15# Setup composite pdf
16# --------------------------------------
17
18# Declare observable x
19x = ROOT.RooRealVar("x", "x", 0, 10)
20
21# Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
22# their parameters
23mean = ROOT.RooRealVar("mean", "mean of gaussians", 5)
24sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
25sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
26sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
27sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
28
29# Sum the signal components into a composite signal pdf
30sig1frac = ROOT.RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8, 0.0, 1.0)
31sig = ROOT.RooAddPdf("sig", "Signal", [sig1, sig2], [sig1frac])
32
33# Build Chebychev polynomial pdf
34a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0.0, 1.0)
35a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0.0, 1.0)
36bkg1 = ROOT.RooChebychev("bkg1", "Background 1", x, [a0, a1])
37
38# Build expontential pdf
39alpha = ROOT.RooRealVar("alpha", "alpha", -1)
40bkg2 = ROOT.RooExponential("bkg2", "Background 2", x, alpha)
41
42# Sum the background components into a composite background pdf
43bkg1frac = ROOT.RooRealVar("sig1frac", "fraction of component 1 in background", 0.2, 0.0, 1.0)
44bkg = ROOT.RooAddPdf("bkg", "Signal", [bkg1, bkg2], [sig1frac])
45
46# Sum the composite signal and background
47bkgfrac = ROOT.RooRealVar("bkgfrac", "fraction of background", 0.5, 0.0, 1.0)
48model = ROOT.RooAddPdf("model", "g1+g2+a", [bkg, sig], [bkgfrac])
49
50# Print composite tree in ASCII
51# -----------------------------------------------------------
52
53# Print tree to stdout
54model.Print("t")
55
56# Print tree to file
57model.printCompactTree("", "rf206_asciitree.txt")
58
59# Draw composite tree graphically
60# -------------------------------------------------------------
61
62# Print GraphViz DOT file with representation of tree
63model.graphVizTree("rf206_model.dot")
64
65# Make list of parameters before and after fit
66# ----------------------------------------------------------------------------------------
67
68# Make list of model parameters
69params = model.getParameters({x})
70
71# Save snapshot of prefit parameters
72initParams = params.snapshot()
73
74# Do fit to data, obtain error estimates on parameters
75data = model.generate({x}, 1000)
76model.fitTo(data, PrintLevel=-1)
77
78# Print LateX table of parameters of pdf
79# --------------------------------------------------------------------------
80
81# Print parameter list in LaTeX for (one column with names, column with
82# values)
84
85# Print parameter list in LaTeX for (names values|names values)
86params.printLatex(Columns=2)
87
88# Print two parameter lists side by side (name values initvalues)
89params.printLatex(Sibling=initParams)
90
91# Print two parameter lists side by side (name values initvalues|name
92# values initvalues)
93params.printLatex(Sibling=initParams, Columns=2)
94
95# Write LaTex table to file
96params.printLatex(Sibling=initParams, OutputFile="rf407_latextables.tex")
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.