Logo ROOT  
Reference Guide
rf505_asciicfg.py
Go to the documentation of this file.
1## \file rf505_asciicfg.py
2## \ingroup tutorial_roofit
3## \notebook -nodraw
4## Organization and simultaneous fits: reading and writing ASCII configuration files
5##
6## \macro_code
7##
8## \date February 2018
9## \authors Clemens Lange, Wouter Verkerke (C++ version)
10
11from __future__ import print_function
12import ROOT
13
14
15# Create pdf
16# ------------------
17
18# Construct gauss(x,m,s)
19x = ROOT.RooRealVar("x", "x", -10, 10)
20m = ROOT.RooRealVar("m", "m", 0, -10, 10)
21s = ROOT.RooRealVar("s", "s", 1, -10, 10)
22gauss = ROOT.RooGaussian("g", "g", x, m, s)
23
24# Construct poly(x,p0)
25p0 = ROOT.RooRealVar("p0", "p0", 0.01, 0., 1.)
26poly = ROOT.RooPolynomial("p", "p", x, ROOT.RooArgList(p0))
27
28# model = f*gauss(x) + (1-f)*poly(x)
29f = ROOT.RooRealVar("f", "f", 0.5, 0., 1.)
30model = ROOT.RooAddPdf("model", "model", ROOT.RooArgList(
31 gauss, poly), ROOT.RooArgList(f))
32
33# Fit model to toy data
34# -----------------------------------------
35
36d = model.generate(ROOT.RooArgSet(x), 1000)
37model.fitTo(d)
38
39# Write parameters to ASCII file
40# -----------------------------------------------------------
41
42# Obtain set of parameters
43params = model.getParameters(ROOT.RooArgSet(x))
44
45# Write parameters to file
46params.writeToFile("rf505_asciicfg_example.txt")
47
48# Read parameters from ASCII file
49# ----------------------------------------------------------------
50
51# Read parameters from file
52params.readFromFile("rf505_asciicfg_example.txt")
53params.Print("v")
54
55configFile = ROOT.gROOT.GetTutorialDir().Data() + "/roofit/rf505_asciicfg.txt"
56
57# Read parameters from section 'Section2' of file
58params.readFromFile(configFile, "", "Section2")
59params.Print("v")
60
61# Read parameters from section 'Section3' of file. Mark all
62# variables that were processed with the "READ" attribute
63params.readFromFile(configFile, "READ", "Section3")
64
65# Print the list of parameters that were not read from Section3
66print("The following parameters of the were _not_ read from Section3: ",
67 params.selectByAttrib("READ", ROOT.kFALSE))
68
69# Read parameters from section 'Section4' of file, contains
70# 'include file' statement of rf505_asciicfg_example.txt
71# so that we effective read the same
72params.readFromFile(configFile, "", "Section4")
73params.Print("v")