Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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## \macro_output
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.0, 1.0)
27poly = ROOT.RooPolynomial("p", "p", x, [p0])
28
29# model = f*gauss(x) + (1-f)*poly(x)
30f = ROOT.RooRealVar("f", "f", 0.5, 0.0, 1.0)
31model = ROOT.RooAddPdf("model", "model", [gauss, poly], [f])
32
33# Fit model to toy data
34# -----------------------------------------
35
36d = model.generate({x}, 1000)
37model.fitTo(d, PrintLevel=-1)
38
39# Write parameters to ASCII file
40# -----------------------------------------------------------
41
42# Obtain set of parameters
43params = model.getParameters({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: ", params.selectByAttrib("READ", False))
67
68# Read parameters from section 'Section4' of file, contains
69# 'include file' statement of rf505_asciicfg_example.txt
70# so that we effective read the same
71params.readFromFile(configFile, "", "Section4")
72params.Print("v")