rf505_asciicfg

Organization and simultaneous fits: reading and writing ASCII configuration files

Author: Clemens Lange, Wouter Verkerke (C++ version)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Sunday, November 27, 2022 at 11:07 AM.

In [1]:
from __future__ import print_function
import ROOT
Welcome to JupyROOT 6.27/01

Create pdf

Construct gauss(x,m,s)

In [2]:
x = ROOT.RooRealVar("x", "x", -10, 10)
m = ROOT.RooRealVar("m", "m", 0, -10, 10)
s = ROOT.RooRealVar("s", "s", 1, -10, 10)
gauss = ROOT.RooGaussian("g", "g", x, m, s)
[#0] WARNING:InputArguments -- The parameter 's' with range [-10, 10] of the RooGaussian 'g' exceeds the safe range of (0, inf). Advise to limit its range.

Construct poly(x,p0)

In [3]:
p0 = ROOT.RooRealVar("p0", "p0", 0.01, 0.0, 1.0)
poly = ROOT.RooPolynomial("p", "p", x, [p0])

model = fgauss(x) + (1-f)poly(x)

In [4]:
f = ROOT.RooRealVar("f", "f", 0.5, 0.0, 1.0)
model = ROOT.RooAddPdf("model", "model", [gauss, poly], [f])

Fit model to toy data

In [5]:
d = model.generate({x}, 1000)
model.fitTo(d)
Out[5]:
<cppyy.gbl.RooFitResult object at 0x(nil)>
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization --  The following expressions will be evaluated in cache-and-track mode: (g,p)
 **********
 **    1 **SET PRINT           1
 **********
 **********
 **    2 **SET NOGRAD
 **********
 PARAMETER DEFINITIONS:
    NO.   NAME         VALUE      STEP SIZE      LIMITS
     1 f            5.00000e-01  1.00000e-01    0.00000e+00  1.00000e+00
     2 m            0.00000e+00  2.00000e+00   -1.00000e+01  1.00000e+01
     3 p0           1.00000e-02  5.00000e-03    0.00000e+00  1.00000e+00
     4 s            1.00000e+00  2.00000e+00   -1.00000e+01  1.00000e+01
 **********
 **    3 **SET ERR         0.5
 **********
 **********
 **    4 **SET PRINT           1
 **********
 **********
 **    5 **SET STR           1
 **********
 NOW USING STRATEGY  1: TRY TO BALANCE SPEED AGAINST RELIABILITY
 **********
 **    6 **MIGRAD        2000           1
 **********
 FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
 START MIGRAD MINIMIZATION.  STRATEGY  1.  CONVERGENCE WHEN EDM .LT. 1.00e-03
 FCN=2578.08 FROM MIGRAD    STATUS=INITIATE       16 CALLS          17 TOTAL
                     EDM= unknown      STRATEGY= 1      NO ERROR MATRIX       
  EXT PARAMETER               CURRENT GUESS       STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  f            5.00000e-01   1.00000e-01   2.01358e-01  -1.53716e+01
   2  m            0.00000e+00   2.00000e+00   2.01358e-01  -2.26705e+01
   3  p0           1.00000e-02   5.00000e-03   5.20130e-02   4.33944e+00
   4  s            1.00000e+00   2.00000e+00   2.02430e-01   1.92608e+02
                               ERR DEF= 0.5
 MIGRAD MINIMIZATION HAS CONVERGED.
 MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
 COVARIANCE MATRIX CALCULATED SUCCESSFULLY
 FCN=2577.55 FROM MIGRAD    STATUS=CONVERGED      72 CALLS          73 TOTAL
                     EDM=8.24282e-07    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  f            5.07346e-01   2.09674e-02   1.38917e-03  -5.25373e-03
   2  m            6.23214e-03   5.36796e-02   1.87122e-04  -5.00083e-02
   3  p0           7.29872e-03   7.84582e-03   3.20927e-03   6.94096e-03
   4  s            9.64897e-01   4.70258e-02   1.56237e-04  -8.82679e-02
                               ERR DEF= 0.5
 EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  4    ERR DEF=0.5
  4.399e-04  5.188e-05 -1.462e-06  3.236e-04 
  5.188e-05  2.882e-03 -3.245e-05  1.903e-04 
 -1.462e-06 -3.245e-05  6.173e-05 -5.278e-06 
  3.236e-04  1.903e-04 -5.278e-06  2.211e-03 
 PARAMETER  CORRELATION COEFFICIENTS  
       NO.  GLOBAL      1      2      3      4
        1  0.32882   1.000  0.046 -0.009  0.328
        2  0.10924   0.046  1.000 -0.077  0.075
        3  0.07746  -0.009 -0.077  1.000 -0.014
        4  0.33367   0.328  0.075 -0.014  1.000
 **********
 **    7 **SET ERR         0.5
 **********
 **********
 **    8 **SET PRINT           1
 **********
 **********
 **    9 **HESSE        2000
 **********
 COVARIANCE MATRIX CALCULATED SUCCESSFULLY
 FCN=2577.55 FROM HESSE     STATUS=OK             23 CALLS          96 TOTAL
                     EDM=8.2344e-07    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                INTERNAL      INTERNAL  
  NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE   
   1  f            5.07346e-01   2.09713e-02   2.77834e-04   1.46934e-02
   2  m            6.23214e-03   5.36750e-02   3.74243e-05   6.23214e-04
   3  p0           7.29872e-03   7.84576e-03   1.28371e-04  -1.39972e+00
   4  s            9.64897e-01   4.70322e-02   6.24947e-06   9.66401e-02
                               ERR DEF= 0.5
 EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  4    ERR DEF=0.5
  4.401e-04  5.204e-05 -1.457e-06  3.242e-04 
  5.204e-05  2.881e-03 -3.217e-05  1.889e-04 
 -1.457e-06 -3.217e-05  6.173e-05 -5.241e-06 
  3.242e-04  1.889e-04 -5.241e-06  2.212e-03 
 PARAMETER  CORRELATION COEFFICIENTS  
       NO.  GLOBAL      1      2      3      4
        1  0.32931   1.000  0.046 -0.009  0.329
        2  0.10847   0.046  1.000 -0.076  0.075
        3  0.07679  -0.009 -0.076  1.000 -0.014
        4  0.33403   0.329  0.075 -0.014  1.000
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization

Write parameters to ASCII file

Obtain set of parameters

In [6]:
params = model.getParameters({x})

Write parameters to file

In [7]:
params.writeToFile("rf505_asciicfg_example.txt")

Read parameters from ASCII file

Read parameters from file

In [8]:
params.readFromFile("rf505_asciicfg_example.txt")
params.Print("v")

configFile = ROOT.gROOT.GetTutorialDir().Data() + "/roofit/rf505_asciicfg.txt"
  1) 0x28e9d10 RooRealVar::  f = 0.50735 +/- 0.020971  L(0 - 1)  "f"
  2) 0x7bd6710 RooRealVar::  m = 0.0062321 +/- 0.053675  L(-10 - 10)  "m"
  3) 0x77b4d80 RooRealVar:: p0 = 0.0072987 +/- 0.0078458  L(0 - 1)  "p0"
  4) 0x7bb4240 RooRealVar::  s = 0.9649 +/- 0.047032  L(-10 - 10)  "s"

Read parameters from section 'Section2' of file

In [9]:
params.readFromFile(configFile, "", "Section2")
params.Print("v")
[#1] INFO:InputArguments -- RooArgSet::readFromStream(parameters): processing include file rf505_asciicfg_example.txt
  1) 0x28e9d10 RooRealVar::  f = 0.45 +/- 0.03  L(0 - 1)  "f"
  2) 0x7bd6710 RooRealVar::  m = 0.025 +/- 0.02  L(-10 - 10)  "m"
  3) 0x77b4d80 RooRealVar:: p0 = 0.0022 +/- 0.0001  L(0 - 1)  "p0"
  4) 0x7bb4240 RooRealVar::  s = 0.98 +/- 0.03  L(-10 - 10)  "s"

Read parameters from section 'Section3' of file. Mark all variables that were processed with the "READ" attribute

In [10]:
params.readFromFile(configFile, "READ", "Section3")
Out[10]:
False
[#1] INFO:InputArguments -- RooArgSet::readFromStream(parameters): processing include file rf505_asciicfg_example.txt

Print the list of parameters that were not read from Section3

In [11]:
print("The following parameters of the were _not_ read from Section3: ", params.selectByAttrib("READ", False))
The following parameters of the were _not_ read from Section3:  { @0x86b0e80, @0x86b0e88 }

Read parameters from section 'Section4' of file, contains 'include file' statement of rf505_asciicfg_example.txt so that we effective read the same

In [12]:
params.readFromFile(configFile, "", "Section4")
params.Print("v")
[#1] INFO:InputArguments -- RooArgSet::readFromStream(parameters): processing include file rf505_asciicfg_example.txt
  1) 0x28e9d10 RooRealVar::  f = 0.372 C  L(0 - 1)  "f"
  2) 0x7bd6710 RooRealVar::  m = 0.195 C  L(-10 - 10)  "m"
  3) 0x77b4d80 RooRealVar:: p0 = 0.0022 +/- 0.0001  L(0 - 1)  "p0"
  4) 0x7bb4240 RooRealVar::  s = 0.98 +/- 0.03  L(-10 - 10)  "s"