Rf 4 0 7_Latextables

Data and categories: latex printing of lists and sets of RooArgSets

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Saturday, November 28, 2020 at 10:53 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooChebychev.h"
#include "RooAddPdf.h"
#include "RooExponential.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;

Setup composite pdf

Declare observable x

In [3]:
RooRealVar x("x", "x", 0, 10);
RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby 
                Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
                All rights reserved, please read http://roofit.sourceforge.net/license.txt

Create two gaussian pdfs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters

In [4]:
RooRealVar mean("mean", "mean of gaussians", 5);
RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
RooRealVar sigma2("sigma2", "width of gaussians", 1);
RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
[#0] WARNING:InputArguments -- The parameter 'sigma1' with range [-1e+30, 1e+30] of the RooGaussian 'sig1' exceeds the safe range of (0, inf). Advise to limit its range.
[#0] WARNING:InputArguments -- The parameter 'sigma2' with range [-1e+30, 1e+30] of the RooGaussian 'sig2' exceeds the safe range of (0, inf). Advise to limit its range.

Sum the signal components into a composite signal pdf

In [5]:
RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);

Build chebychev polynomial pdf

In [6]:
RooRealVar a0("a0", "a0", 0.5, 0., 1.);
RooRealVar a1("a1", "a1", 0.2, 0., 1.);
RooChebychev bkg1("bkg1", "Background 1", x, RooArgSet(a0, a1));

Build expontential pdf

In [7]:
RooRealVar alpha("alpha", "alpha", -1);
RooExponential bkg2("bkg2", "Background 2", x, alpha);

Sum the background components into a composite background pdf

In [8]:
RooRealVar bkg1frac("sig1frac", "fraction of component 1 in background", 0.2, 0., 1.);
RooAddPdf bkg("bkg", "Signal", RooArgList(bkg1, bkg2), sig1frac);

Sum the composite signal and background

In [9]:
RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);

Make list of parameters before and after fit

Make list of model parameters

In [10]:
RooArgSet *params = model.getParameters(x);

Save snapshot of prefit parameters

In [11]:
RooArgSet *initParams = (RooArgSet *)params->snapshot();

Do fit to data, to obtain error estimates on parameters

In [12]:
RooDataSet *data = model.generate(x, 1000);
model.fitTo(*data);
[#1] INFO:Minization -- RooMinimizer::optimizeConst: activating const optimization
[#1] INFO:Minization --  The following expressions have been identified as constant and will be precalculated and cached: (bkg2,sig1,sig2)
[#1] INFO:Minization --  The following expressions will be evaluated in cache-and-track mode: (bkg1)
 **********
 **    1 **SET PRINT           1
 **********
 **********
 **    2 **SET NOGRAD
 **********
 PARAMETER DEFINITIONS:
    NO.   NAME         VALUE      STEP SIZE      LIMITS
     1 a0           5.00000e-01  1.00000e-01    0.00000e+00  1.00000e+00
     2 a1           2.00000e-01  1.00000e-01    0.00000e+00  1.00000e+00
     3 bkgfrac      5.00000e-01  1.00000e-01    0.00000e+00  1.00000e+00
     4 sig1frac     8.00000e-01  1.00000e-01    0.00000e+00  1.00000e+00
 **********
 **    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=1963.41 FROM MIGRAD    STATUS=INITIATE       14 CALLS          15 TOTAL
                     EDM= unknown      STRATEGY= 1      NO ERROR MATRIX       
  EXT PARAMETER               CURRENT GUESS       STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  a0           5.00000e-01   1.00000e-01   2.01358e-01   5.45557e+00
   2  a1           2.00000e-01   1.00000e-01   2.57889e-01   2.12468e+00
   3  bkgfrac      5.00000e-01   1.00000e-01   2.01358e-01   2.95374e+01
   4  sig1frac     8.00000e-01   1.00000e-01   2.57889e-01   1.90239e+01
                               ERR DEF= 0.5
 MIGRAD MINIMIZATION HAS CONVERGED.
 MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
 COVARIANCE MATRIX CALCULATED SUCCESSFULLY
 FCN=1960.64 FROM MIGRAD    STATUS=CONVERGED     100 CALLS         101 TOTAL
                     EDM=3.75265e-06    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  a0           6.45917e-01   2.21997e-01   7.59449e-03  -6.13466e-03
   2  a1           2.39717e-01   1.63607e-01   8.68922e-03   8.68189e-04
   3  bkgfrac      4.48723e-01   2.86509e-02   1.18450e-03   2.52740e-02
   4  sig1frac     7.07467e-01   5.79941e-02   1.94490e-03  -2.93455e-02
                               ERR DEF= 0.5
 EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  4    ERR DEF=0.5
  5.330e-02 -1.068e-02 -1.772e-03 -1.095e-02 
 -1.068e-02  2.819e-02 -2.571e-03  1.770e-04 
 -1.772e-03 -2.571e-03  8.218e-04  8.391e-04 
 -1.095e-02  1.770e-04  8.391e-04  3.382e-03 
 PARAMETER  CORRELATION COEFFICIENTS  
       NO.  GLOBAL      1      2      3      4
        1  0.85615   1.000 -0.276 -0.268 -0.815
        2  0.68971  -0.276  1.000 -0.534  0.018
        3  0.74062  -0.268 -0.534  1.000  0.503
        4  0.86784  -0.815  0.018  0.503  1.000
 **********
 **    7 **SET ERR         0.5
 **********
 **********
 **    8 **SET PRINT           1
 **********
 **********
 **    9 **HESSE        2000
 **********
 COVARIANCE MATRIX CALCULATED SUCCESSFULLY
 FCN=1960.64 FROM HESSE     STATUS=OK             23 CALLS         124 TOTAL
                     EDM=3.75168e-06    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                INTERNAL      INTERNAL  
  NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE   
   1  a0           6.45917e-01   2.21223e-01   1.51890e-03   2.96143e-01
   2  a1           2.39717e-01   1.63005e-01   3.47569e-04  -5.47513e-01
   3  bkgfrac      4.48723e-01   2.85510e-02   2.36899e-04  -1.02734e-01
   4  sig1frac     7.07467e-01   5.77889e-02   3.88981e-04   4.27870e-01
                               ERR DEF= 0.5
 EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  4    ERR DEF=0.5
  5.289e-02 -1.058e-02 -1.763e-03 -1.085e-02 
 -1.058e-02  2.797e-02 -2.538e-03  1.785e-04 
 -1.763e-03 -2.538e-03  8.161e-04  8.330e-04 
 -1.085e-02  1.785e-04  8.330e-04  3.358e-03 
 PARAMETER  CORRELATION COEFFICIENTS  
       NO.  GLOBAL      1      2      3      4
        1  0.85497   1.000 -0.275 -0.268 -0.814
        2  0.68674  -0.275  1.000 -0.531  0.018
        3  0.73847  -0.268 -0.531  1.000  0.503
        4  0.86683  -0.814  0.018  0.503  1.000
[#1] INFO:Minization -- RooMinimizer::optimizeConst: deactivating const optimization

Print parameter list in latex for (one column with names, one column with values)

In [13]:
params->printLatex();
\begin{tabular}{lc}
$\verb+a0+ $ & $  0.6\pm 0.2$\\
$\verb+a1+ $ & $  0.2\pm 0.2$\\
$\verb+alpha+ $ & $ -1.00$\\
$\verb+bkgfrac+ $ & $  0.45\pm 0.03$\\
$\verb+mean+ $ & $  5$\\
$\verb+sig1frac+ $ & $  0.71\pm 0.06$\\
$\verb+sigma1+ $ & $  0.5$\\
$\verb+sigma2+ $ & $  1$\\
\end{tabular}

Print parameter list in latex for (names values|names values)

In [14]:
params->printLatex(Columns(2));
\begin{tabular}{lc|lc}
$\verb+a0+ $ & $  0.6\pm 0.2$ & $\verb+mean+ $ & $  5$\\
$\verb+a1+ $ & $  0.2\pm 0.2$ & $\verb+sig1frac+ $ & $  0.71\pm 0.06$\\
$\verb+alpha+ $ & $ -1.00$ & $\verb+sigma1+ $ & $  0.5$\\
$\verb+bkgfrac+ $ & $  0.45\pm 0.03$ & $\verb+sigma2+ $ & $  1$\\
\end{tabular}

Print two parameter lists side by side (name values initvalues)

In [15]:
params->printLatex(Sibling(*initParams));
\begin{tabular}{lcc}
$\verb+a0+ $ & $  0.6\pm 0.2$ & $ 0.5$\\
$\verb+a1+ $ & $  0.2\pm 0.2$ & $ 0.2$\\
$\verb+alpha+ $ & $ -1.00$ & $-1.00$\\
$\verb+bkgfrac+ $ & $  0.45\pm 0.03$ & $ 0.5$\\
$\verb+mean+ $ & $  5$ & $ 5$\\
$\verb+sig1frac+ $ & $  0.71\pm 0.06$ & $ 0.8$\\
$\verb+sigma1+ $ & $  0.5$ & $ 0.5$\\
$\verb+sigma2+ $ & $  1$ & $ 1$\\
\end{tabular}

Print two parameter lists side by side (name values initvalues|name values initvalues)

In [16]:
params->printLatex(Sibling(*initParams), Columns(2));
\begin{tabular}{lcc|lcc}
$\verb+a0+ $ & $  0.6\pm 0.2$ & $ 0.5$ & $\verb+mean+ $ & $  5$ & $ 5$\\
$\verb+a1+ $ & $  0.2\pm 0.2$ & $ 0.2$ & $\verb+sig1frac+ $ & $  0.71\pm 0.06$ & $ 0.8$\\
$\verb+alpha+ $ & $ -1.00$ & $-1.00$ & $\verb+sigma1+ $ & $  0.5$ & $ 0.5$\\
$\verb+bkgfrac+ $ & $  0.45\pm 0.03$ & $ 0.5$ & $\verb+sigma2+ $ & $  1$ & $ 1$\\
\end{tabular}

Write latex table to file

In [17]:
params->printLatex(Sibling(*initParams), OutputFile("rf407_latextables.tex"));