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.0, 1.0)
26poly = ROOT.RooPolynomial("p", "p", x, [p0])
27
28# model = f*gauss(x) + (1-f)*poly(x)
29f = ROOT.RooRealVar("f", "f", 0.5, 0.0, 1.0)
30model = ROOT.RooAddPdf("model", "model", [gauss, poly], [f])
31
32# Fit model to toy data
33# -----------------------------------------
34
35d = model.generate({x}, 1000)
36model.fitTo(d)
37
38# Write parameters to ASCII file
39# -----------------------------------------------------------
40
41# Obtain set of parameters
42params = model.getParameters({x})
43
44# Write parameters to file
45params.writeToFile("rf505_asciicfg_example.txt")
46
47# Read parameters from ASCII file
48# ----------------------------------------------------------------
49
50# Read parameters from file
51params.readFromFile("rf505_asciicfg_example.txt")
52params.Print("v")
53
54configFile = ROOT.gROOT.GetTutorialDir().Data() + "/roofit/rf505_asciicfg.txt"
55
56# Read parameters from section 'Section2' of file
57params.readFromFile(configFile, "", "Section2")
58params.Print("v")
59
60# Read parameters from section 'Section3' of file. Mark all
61# variables that were processed with the "READ" attribute
62params.readFromFile(configFile, "READ", "Section3")
63
64# Print the list of parameters that were not read from Section3
65print("The following parameters of the were _not_ read from Section3: ", params.selectByAttrib("READ", False))
66
67# Read parameters from section 'Section4' of file, contains
68# 'include file' statement of rf505_asciicfg_example.txt
69# so that we effective read the same
70params.readFromFile(configFile, "", "Section4")
71params.Print("v")