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