# rf604_constraints¶

Likelihood and minimization: fitting with constraints

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, November 30, 2022 at 11:23 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooPolynomial.h"
#include "RooProdPdf.h"
#include "RooFitResult.h"
#include "RooPlot.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TH1.h"
using namespace RooFit;


## Create model and dataset¶

Construct a Gaussian pdf

In [2]:
RooRealVar x("x", "x", -10, 10);

RooRealVar m("m", "m", 0, -10, 10);
RooRealVar s("s", "s", 2, 0.1, 10);
RooGaussian gauss("gauss", "gauss(x,m,s)", x, m, s);


Construct a flat pdf (polynomial of 0th order)

In [3]:
RooPolynomial poly("poly", "poly(x)", x);


Construct model = fgauss + (1-f)poly

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


Generate small dataset for use in fitting below

In [5]:
RooDataSet *d = model.generate(x, 50);


## Create constraint pdf¶

Construct Gaussian constraint pdf on parameter f at 0.8 with resolution of 0.1

In [6]:
RooGaussian fconstraint("fconstraint", "fconstraint", f, RooConst(0.8), RooConst(0.2));


## METHOD 1 - Add internal constraint to model¶

Multiply constraint term with regular pdf using RooProdPdf Specify in fitTo() that internal constraints on parameter f should be used

Multiply constraint with pdf

In [7]:
RooProdPdf modelc("modelc", "model with constraint", RooArgSet(model, fconstraint));


Fit model (without use of constraint term)

In [8]:
RooFitResult *r1 = model.fitTo(*d, Save(), PrintLevel(-1));

[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization --  The following expressions have been identified as constant and will be precalculated and cached: (poly)
[#1] INFO:Minimization --  The following expressions will be evaluated in cache-and-track mode: (gauss)
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization


Fit modelc with constraint term on parameter f

In [9]:
RooFitResult *r2 = modelc.fitTo(*d, Constrain(f), Save(), PrintLevel(-1));

[#1] INFO:Minimization --  Including the following constraint terms in minimization: (fconstraint)
[#1] INFO:Minimization -- The global observables are not defined , normalize constraints with respect to the parameters (f)
[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_modelc_modelData_with_constr) Summation contains a RooNLLVar, using its error level
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization --  The following expressions have been identified as constant and will be precalculated and cached: (poly)
[#1] INFO:Minimization --  The following expressions will be evaluated in cache-and-track mode: (gauss)
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization


## METHOD 2 - Specify external constraint when fitting¶

Construct another Gaussian constraint pdf on parameter f at 0.2 with resolution of 0.1

In [10]:
RooGaussian fconstext("fconstext", "fconstext", f, RooConst(0.2), RooConst(0.1));


Fit with external constraint

In [11]:
RooFitResult *r3 = model.fitTo(*d, ExternalConstraints(fconstext), Save(), PrintLevel(-1));

[#1] INFO:Minimization --  Including the following constraint terms in minimization: (fconstext)
[#1] INFO:Minimization -- The global observables are not defined , normalize constraints with respect to the parameters (f,m,s)
[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_modelData_with_constr) Summation contains a RooNLLVar, using its error level
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization --  The following expressions have been identified as constant and will be precalculated and cached: (poly)
[#1] INFO:Minimization --  The following expressions will be evaluated in cache-and-track mode: (gauss)
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization


Print the fit results

In [12]:
cout << "fit result without constraint (data generated at f=0.5)" << endl;
r1->Print("v");
cout << "fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)" << endl;
r2->Print("v");
cout << "fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)" << endl;
r3->Print("v");

fit result without constraint (data generated at f=0.5)

RooFitResult: minimized FCN value: 134.849, estimated distance to minimum: 4.23315e-05
covariance matrix quality: Full, accurate covariance matrix
Status : MINIMIZE=0 HESSE=0

Floating Parameter  InitialValue    FinalValue +/-  Error     GblCorr.
--------------------  ------------  --------------------------  --------
f    5.0000e-01    6.4987e-01 +/-  1.21e-01  <none>
m    0.0000e+00    7.1824e-01 +/-  4.97e-01  <none>
s    2.0000e+00    2.1880e+00 +/-  4.61e-01  <none>

fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)

RooFitResult: minimized FCN value: 134.19, estimated distance to minimum: 7.15613e-07
covariance matrix quality: Full, accurate covariance matrix
Status : MINIMIZE=0 HESSE=0

Floating Parameter  InitialValue    FinalValue +/-  Error     GblCorr.
--------------------  ------------  --------------------------  --------
f    6.4987e-01    6.9039e-01 +/-  1.01e-01  <none>
m    7.1824e-01    6.9529e-01 +/-  4.99e-01  <none>
s    2.1880e+00    2.2698e+00 +/-  4.76e-01  <none>

fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)

RooFitResult: minimized FCN value: 137.195, estimated distance to minimum: 4.81665e-05
covariance matrix quality: Full, accurate covariance matrix
Status : MINIMIZE=0 HESSE=0

Floating Parameter  InitialValue    FinalValue +/-  Error     GblCorr.
--------------------  ------------  --------------------------  --------
f    6.9039e-01    3.6200e-01 +/-  8.15e-02  <none>
m    6.9529e-01    7.0620e-01 +/-  5.88e-01  <none>
s    2.2698e+00    1.7083e+00 +/-  4.75e-01  <none>