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