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 
11 from __future__ import print_function
12 import ROOT
13 
14 
15 # Create pdf
16 # ------------------
17 
18 # Construct gauss(x,m,s)
19 x = ROOT.RooRealVar("x", "x", -10, 10)
20 m = ROOT.RooRealVar("m", "m", 0, -10, 10)
21 s = ROOT.RooRealVar("s", "s", 1, -10, 10)
22 gauss = ROOT.RooGaussian("g", "g", x, m, s)
23 
24 # Construct poly(x,p0)
25 p0 = ROOT.RooRealVar("p0", "p0", 0.01, 0., 1.)
26 poly = ROOT.RooPolynomial("p", "p", x, ROOT.RooArgList(p0))
27 
28 # model = f*gauss(x) + (1-f)*poly(x)
29 f = ROOT.RooRealVar("f", "f", 0.5, 0., 1.)
30 model = ROOT.RooAddPdf("model", "model", ROOT.RooArgList(
31  gauss, poly), ROOT.RooArgList(f))
32 
33 # Fit model to toy data
34 # -----------------------------------------
35 
36 d = model.generate(ROOT.RooArgSet(x), 1000)
37 model.fitTo(d)
38 
39 # Write parameters to ASCII file
40 # -----------------------------------------------------------
41 
42 # Obtain set of parameters
43 params = model.getParameters(ROOT.RooArgSet(x))
44 
45 # Write parameters to file
46 params.writeToFile("rf505_asciicfg_example.txt")
47 
48 # Read parameters from ASCII file
49 # ----------------------------------------------------------------
50 
51 # Read parameters from file
52 params.readFromFile("rf505_asciicfg_example.txt")
53 params.Print("v")
54 
55 configFile = ROOT.gROOT.GetTutorialDir().Data() + "/roofit/rf505_asciicfg.txt"
56 
57 # Read parameters from section 'Section2' of file
58 params.readFromFile(configFile, "", "Section2")
59 params.Print("v")
60 
61 # Read parameters from section 'Section3' of file. Mark all
62 # variables that were processed with the "READ" attribute
63 params.readFromFile(configFile, "READ", "Section3")
64 
65 # Print the list of parameters that were not read from Section3
66 print("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
72 params.readFromFile(configFile, "", "Section4")
73 params.Print("v")