# rf609_xychi2fit¶

Likelihood and minimization: setting up a chi^2 fit to an unbinned dataset with X,Y,err(Y) values (and optionally err(X) values)

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:08 AM.

In [1]:
import ROOT
import math

Welcome to JupyROOT 6.27/01


## Create dataset with X and Y values¶

Make weighted XY dataset with asymmetric errors stored The StoreError() argument is essential as it makes the dataset store the error in addition to the values of the observables. If errors on one or more observables are asymmetric, can store the asymmetric error using the StoreAsymError() argument

In [2]:
x = ROOT.RooRealVar("x", "x", -11, 11)
y = ROOT.RooRealVar("y", "y", -10, 200)
dxy = ROOT.RooDataSet("dxy", "dxy", {x, y}, StoreError={x, y})


Fill an example dataset with X,err(X),Y,err(Y) values

In [3]:
for i in range(10):
x.setVal(-10 + 2 * i)
x.setError((0.5 / 1.0) if (i < 5) else (1.0 / 1.0))

# Set Y value and error
y.setVal(x.getVal() * x.getVal() + 4 * abs(ROOT.gRandom.Gaus()))
y.setError(math.sqrt(y.getVal()))



## Perform chi2 fit to X +/- dX and Y +/- dY values¶

Make fit function

In [4]:
a = ROOT.RooRealVar("a", "a", 0.0, -10, 10)
b = ROOT.RooRealVar("b", "b", 0.0, -100, 100)
f = ROOT.RooPolyVar("f", "f", x, [b, a, 1.0])


Plot dataset in X-Y interpretation

In [5]:
frame = x.frame(Title="Chi^2 fit of function set of (X#pmdX,Y#pmdY) values")
dxy.plotOnXY(frame, YVar=y)

Out[5]:
<cppyy.gbl.RooPlot object at 0x8ee24b0>

Fit chi^2 using X and Y errors

In [6]:
f.chi2FitTo(dxy, YVar=y)

Out[6]:
<cppyy.gbl.RooFitResult object at 0x(nil)>
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
**********
**    1 **SET PRINT           1
**********
**********
**********
PARAMETER DEFINITIONS:
NO.   NAME         VALUE      STEP SIZE      LIMITS
1 a            0.00000e+00  2.00000e+00   -1.00000e+01  1.00000e+01
2 b            0.00000e+00  2.00000e+01   -1.00000e+02  1.00000e+02
**********
**    3 **SET ERR           1
**********
**********
**    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=1.98096 FROM MIGRAD    STATUS=INITIATE        8 CALLS           9 TOTAL
EDM= unknown      STRATEGY= 1      NO ERROR MATRIX
EXT PARAMETER               CURRENT GUESS       STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  a            0.00000e+00   2.00000e+00   2.01358e-01   1.57734e+01
2  b            0.00000e+00   2.00000e+01   2.01358e-01  -3.06377e+02
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=1.33213 FROM MIGRAD    STATUS=CONVERGED      38 CALLS          39 TOTAL
EDM=9.00236e-08    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                   STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  a           -2.03094e-01   5.38435e-01   3.99836e-05  -4.32054e-03
2  b            3.35422e-01   5.06306e-01   3.74488e-06  -7.46864e-02
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=1
2.902e-01 -2.655e-02
-2.655e-02  2.563e-01
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2
1  0.09735   1.000 -0.097
2  0.09735  -0.097  1.000
**********
**    7 **SET ERR           1
**********
**********
**    8 **SET PRINT           1
**********
**********
**    9 **HESSE        1000
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=1.33213 FROM HESSE     STATUS=OK             10 CALLS          49 TOTAL
EDM=9.00352e-08    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                INTERNAL      INTERNAL
NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE
1  a           -2.03094e-01   5.38426e-01   7.99672e-06  -2.03108e-02
2  b            3.35422e-01   5.06297e-01   7.48977e-07   3.35423e-03
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=1
2.902e-01 -2.651e-02
-2.651e-02  2.563e-01
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2
1  0.09718   1.000 -0.097
2  0.09718  -0.097  1.000


Overlay fitted function

In [7]:
f.plotOn(frame)

Out[7]:
<cppyy.gbl.RooPlot object at 0x8ee24b0>

Alternative: fit chi^2 integrating f(x) over ranges defined by X errors, rather than taking point at center of bin

In [8]:
f.chi2FitTo(dxy, YVar=y, Integrate=True)

Out[8]:
<cppyy.gbl.RooFitResult object at 0x(nil)>
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
**********
**   10 **SET PRINT           1
**********
**********
**********
PARAMETER DEFINITIONS:
NO.   NAME         VALUE      STEP SIZE      LIMITS
1 a           -2.03094e-01  5.38426e-01   -1.00000e+01  1.00000e+01
2 b            3.35422e-01  5.06297e-01   -1.00000e+02  1.00000e+02
**********
**   12 **SET ERR           1
**********
**********
**   13 **SET PRINT           1
**********
**********
**   14 **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=169665 FROM MIGRAD    STATUS=INITIATE        8 CALLS           9 TOTAL
EDM= unknown      STRATEGY= 1      NO ERROR MATRIX
EXT PARAMETER               CURRENT GUESS       STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  a           -2.03094e-01   5.38426e-01   5.38798e-02  -2.96995e+04
2  b            3.35422e-01   5.06297e-01   5.06302e-03   2.94073e+07
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=68428.1 FROM MIGRAD    STATUS=CONVERGED      30 CALLS          31 TOTAL
EDM=1.7609e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                   STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  a            1.68285e-01   1.59561e-02   2.03838e-04  -3.70672e-01
2  b           -3.49337e-01   2.15813e-03   2.75657e-06  -2.17257e+00
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=1
2.546e-04  4.601e-08
4.601e-08  4.658e-06
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2
1  0.00134   1.000  0.001
2  0.00134   0.001  1.000
**********
**   16 **SET ERR           1
**********
**********
**   17 **SET PRINT           1
**********
**********
**   18 **HESSE        1000
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=68428.1 FROM HESSE     STATUS=OK             10 CALLS          41 TOTAL
EDM=1.76081e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                INTERNAL      INTERNAL
NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE
1  a            1.68285e-01   1.59561e-02   4.07676e-05   1.68293e-02
2  b           -3.49337e-01   2.15813e-03   5.51315e-07  -3.49338e-03
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=1
2.546e-04  4.601e-08
4.601e-08  4.658e-06
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2
1  0.00134   1.000  0.001
2  0.00134   0.001  1.000


Overlay alternate fit result

In [9]:
f.plotOn(frame, LineStyle="--", LineColor="r")

Out[9]:
<cppyy.gbl.RooPlot object at 0x8ee24b0>

Draw the plot on a canvas

In [10]:
c = ROOT.TCanvas("rf609_xychi2fit", "rf609_xychi2fit", 600, 600)
frame.GetYaxis().SetTitleOffset(1.4)
frame.Draw()

c.SaveAs("rf609_xychi2fit.png")

Info in <TCanvas::Print>: png file rf609_xychi2fit.png has been created


Draw all canvases

In [11]:
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()