# Rf 5 0 9_Wsinteractive¶

Organization and simultaneous fits: easy interactive access to workspace contents - CINT to CLING code migration

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

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

In [2]:
void fillWorkspace(RooWorkspace &w);


A helper function is created:

In [3]:
%%cpp -d
void fillWorkspace(RooWorkspace &w)
{
// C r e a t e  p d f   a n d   f i l l   w o r k s p a c e
// --------------------------------------------------------

// Declare observable x
RooRealVar x("x", "x", 0, 10);

// Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
RooRealVar mean("mean", "mean of gaussians", 5, 0, 10);
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);

// Build Chebychev polynomial pdf
RooRealVar a0("a0", "a0", 0.5, 0., 1.);
RooRealVar a1("a1", "a1", 0.2, 0., 1.);
RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));

// Sum the signal components into a composite signal pdf
RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);

// Sum the composite signal and background
RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);

w.import(model);
}


## Create and fill workspace¶

Create a workspace named 'w' With CINT w could exports its contents to a same-name C++ namespace in CINT 'namespace w'. but this does not work anymore in CLING. so this tutorial is an example on how to change the code

In [4]:
RooWorkspace *w1 = new RooWorkspace("w", kTRUE);

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

[#1] INFO:ObjectHandling -- RooWorkspace::exportToCint(w) INFO: references to all objects in this workspace will be created in CINT in 'namespace w'


Fill workspace with pdf and data in a separate function

In [5]:
fillWorkspace(*w1);

[#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.
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooAddPdf::model
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooChebychev::bkg
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::x
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::a0
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::a1
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::bkgfrac
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooAddPdf::sig
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooGaussian::sig1
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::mean
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::sigma1
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::sig1frac
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooGaussian::sig2
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::sigma2


Print workspace contents

In [6]:
w1->Print();

RooWorkspace(w) w contents

variables
---------
(a0,a1,bkgfrac,mean,sig1frac,sigma1,sigma2,x)

p.d.f.s
-------
RooChebychev::bkg[ x=x coefList=(a0,a1) ] = 0.8
RooAddPdf::model[ bkgfrac * bkg + [%] * sig ] = 0.9
RooAddPdf::sig[ sig1frac * sig1 + [%] * sig2 ] = 1
RooGaussian::sig1[ x=x mean=mean sigma=sigma1 ] = 1
RooGaussian::sig2[ x=x mean=mean sigma=sigma2 ] = 1



This does not work anymore with cling use normal workspace functionality

## Use workspace contents¶

Old syntax to use the name space prefix operator to access the workspace contents

RooDataSet d = w::model.generate(w::x,1000) ; RooFitResult r = w::model.fitTo(*d) ;

Use normal workspace methods

In [7]:
RooAbsPdf *model = w1->pdf("model");
RooRealVar *x = w1->var("x");

RooDataSet *d = model->generate(*x, 1000);
RooFitResult *r = model->fitTo(*d);

[#0] WARNING:Eval -- Evaluating RooAddPdf without a defined normalization set. This can lead to ambiguos coefficients definition and incorrect results. Use RooAddPdf::fixCoefNormalization(nset) to provide a normalization set for defining uniquely RooAddPdf coefficients!
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization --  The following expressions will be evaluated in cache-and-track mode: (bkg,sig1,sig2)
**********
**    1 **SET PRINT           1
**********
**********
**********
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 mean         5.00000e+00  1.00000e+00    0.00000e+00  1.00000e+01
5 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
**********
**********
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=1962.68 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.55984e+00
2  a1           2.00000e-01   1.00000e-01   2.57889e-01  -1.57464e+00
3  bkgfrac      5.00000e-01   1.00000e-01   2.01358e-01   1.16417e+00
4  mean         5.00000e+00   1.00000e+00   2.01358e-01  -6.91026e+01
5  sig1frac     8.00000e-01   1.00000e-01   2.57889e-01  -2.02114e+00
ERR DEF= 0.5
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=1962.14 FROM MIGRAD    STATUS=CONVERGED      92 CALLS          93 TOTAL
EDM=0.000129539    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                   STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  a0           4.38576e-01   7.35996e-02   4.50487e-03   3.73392e-02
2  a1           2.02416e-01   1.18128e-01   5.73699e-03   4.39091e-02
3  bkgfrac      5.03388e-01   3.60691e-02   1.24369e-03   4.03363e-02
4  mean         5.01610e+00   3.15061e-02   1.91674e-04   7.40455e-01
5  sig1frac     8.35115e-01   1.17067e-01   5.93647e-03   2.37022e-02
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  5    ERR DEF=0.5
5.457e-03  1.116e-03 -2.768e-04 -2.327e-04 -9.105e-04
1.116e-03  1.438e-02 -3.249e-03  5.690e-05 -9.716e-03
-2.768e-04 -3.249e-03  1.303e-03 -5.364e-05  3.285e-03
-2.327e-04  5.690e-05 -5.364e-05  9.926e-04 -1.964e-04
-9.105e-04 -9.716e-03  3.285e-03 -1.964e-04  1.419e-02
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2      3      4      5
1  0.16490   1.000  0.126 -0.104 -0.100 -0.103
2  0.77006   0.126  1.000 -0.751  0.015 -0.680
3  0.82650  -0.104 -0.751  1.000 -0.047  0.764
4  0.12324  -0.100  0.015 -0.047  1.000 -0.052
5  0.78139  -0.103 -0.680  0.764 -0.052  1.000
**********
**    7 **SET ERR         0.5
**********
**********
**    8 **SET PRINT           1
**********
**********
**    9 **HESSE        2500
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=1962.14 FROM HESSE     STATUS=OK             31 CALLS         124 TOTAL
EDM=0.000129401    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                INTERNAL      INTERNAL
NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE
1  a0           4.38576e-01   7.35914e-02   9.00974e-04  -1.23159e-01
2  a1           2.02416e-01   1.17686e-01   1.14740e-03  -6.37473e-01
3  bkgfrac      5.03388e-01   3.59545e-02   2.48738e-04   6.77613e-03
4  mean         5.01610e+00   3.15052e-02   3.83348e-05   3.22075e-03
5  sig1frac     8.35115e-01   1.16789e-01   1.18729e-03   7.34520e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  5    ERR DEF=0.5
5.456e-03  1.105e-03 -2.735e-04 -2.327e-04 -9.006e-04
1.105e-03  1.427e-02 -3.217e-03  5.664e-05 -9.623e-03
-2.735e-04 -3.217e-03  1.295e-03 -5.354e-05  3.261e-03
-2.327e-04  5.664e-05 -5.354e-05  9.926e-04 -1.946e-04
-9.006e-04 -9.623e-03  3.261e-03 -1.946e-04  1.412e-02
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2      3      4      5
1  0.16425   1.000  0.125 -0.103 -0.100 -0.103
2  0.76801   0.125  1.000 -0.748  0.015 -0.678
3  0.82527  -0.103 -0.748  1.000 -0.047  0.763
4  0.12301  -0.100  0.015 -0.047  1.000 -0.052
5  0.78016  -0.103 -0.678  0.763 -0.052  1.000
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization


Old syntax to access the variable x RooPlot* frame = w::x.frame() ;

In [8]:
RooPlot *frame = x->frame();
d->plotOn(frame);

[#0] WARNING:Plotting -- Cannot apply a bin width correction and use Poisson errors. Not correcting for bin width.


Old syntax to omit x:: NB: The 'w::' prefix can be omitted if namespace w is imported in local namespace in the usual C++ way

using namespace w; model.plotOn(frame) ; model.plotOn(frame,Components(bkg),LineStyle(kDashed)) ;

New correct syntax

In [9]:
RooAbsPdf *bkg = w1->pdf("bkg");
model->plotOn(frame);
model->plotOn(frame, Components(*bkg), LineStyle(kDashed));

[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) directly selected PDF components: (bkg)
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) indirectly selected PDF components: ()


Draw the frame on the canvas

In [10]:
new TCanvas("rf509_wsinteractive", "rf509_wsinteractive", 600, 600);

%jsroot on