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