# Rf 5 0 5_Asciicfg¶

Organisation and simultaneous fits: reading and writing ASCII configuration files

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, January 17, 2022 at 10:04 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooPolynomial.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"

In [2]:
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;


## Create pdf¶

Construct gauss(x,m,s)

In [3]:
RooRealVar x("x", "x", -10, 10);
RooRealVar m("m", "m", 0, -10, 10);
RooRealVar s("s", "s", 1, -10, 10);
RooGaussian gauss("g", "g", x, m, s);

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University

[#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 [4]:
RooRealVar p0("p0", "p0", 0.01, 0., 1.);
RooPolynomial poly("p", "p", x, p0);


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

In [5]:
RooRealVar f("f", "f", 0.5, 0., 1.);
RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);


## Fit model to toy data¶

In [6]:
RooDataSet *d = model.generate(x, 1000);
model.fitTo(*d);

[#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
**********
**********
**********
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
**********
**********
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 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 [7]:
RooArgSet *params = model.getParameters(x);


Write parameters to file

In [8]:
params->writeToFile("rf505_asciicfg_example.txt");

TString dir1 = gROOT->GetTutorialDir() ;
dir1.Append("/roofit/rf505_asciicfg.txt") ;
TString dir2 = "rf505_asciicfg_example.txt";


## Read parameters from ascii file¶

In [9]:
params->readFromFile(dir2);
params->Print("v");

  1) 0x7f4cd3bb30d0 RooRealVar::  f = 0.50735 +/- 0.020971  L(0 - 1)  "f"
2) 0x7f4cd3bb23d8 RooRealVar::  m = 0.0062321 +/- 0.053675  L(-10 - 10)  "m"
3) 0x7f4cec92a028 RooRealVar:: p0 = 0.0072987 +/- 0.0078458  L(0 - 1)  "p0"
4) 0x7f4cd3bb27b0 RooRealVar::  s = 0.9649 +/- 0.047032  L(-10 - 10)  "s"


Read parameters from section 'section2' of file

In [10]:
params->readFromFile(dir1, 0, "Section2");
params->Print("v");

[#1] INFO:InputArguments -- RooArgSet::readFromStream(parameters): processing include file rf505_asciicfg_example.txt
1) 0x7f4cd3bb30d0 RooRealVar::  f = 0.45 +/- 0.03  L(0 - 1)  "f"
2) 0x7f4cd3bb23d8 RooRealVar::  m = 0.025 +/- 0.02  L(-10 - 10)  "m"
3) 0x7f4cec92a028 RooRealVar:: p0 = 0.0022 +/- 0.0001  L(0 - 1)  "p0"
4) 0x7f4cd3bb27b0 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 [11]:
params->readFromFile(dir1, "READ", "Section3");

[#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 [12]:
cout << "The following parameters of the were _not_ read from Section3: "

The following parameters of the were _not_ read from Section3: (p0,s)


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

In [13]:
params->readFromFile(dir1, 0, "Section4");
params->Print("v");

[#1] INFO:InputArguments -- RooArgSet::readFromStream(parameters): processing include file rf505_asciicfg_example.txt
1) 0x7f4cd3bb30d0 RooRealVar::  f = 0.372 C  L(0 - 1)  "f"
2) 0x7f4cd3bb23d8 RooRealVar::  m = 0.195 C  L(-10 - 10)  "m"
3) 0x7f4cec92a028 RooRealVar:: p0 = 0.0022 +/- 0.0001  L(0 - 1)  "p0"
4) 0x7f4cd3bb27b0 RooRealVar::  s = 0.98 +/- 0.03  L(-10 - 10)  "s"