Rf 6 0 9_Xychi 2Fit

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, July 05, 2020 at 08:33 AM.

In [ ]:
import ROOT
import math

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 [ ]:
x = ROOT.RooRealVar("x", "x", -11, 11)
y = ROOT.RooRealVar("y", "y", -10, 200)
dxy = ROOT.RooDataSet("dxy", "dxy", ROOT.RooArgSet(
    x, y), ROOT.RooFit.StoreError(ROOT.RooArgSet(x, y)))

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

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

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

    dxy.add(ROOT.RooArgSet(x, y))

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

Make fit function

In [ ]:
a = ROOT.RooRealVar("a", "a", 0.0, -10, 10)
b = ROOT.RooRealVar("b", "b", 0.0, -100, 100)
f = ROOT.RooPolyVar(
    "f", "f", x, ROOT.RooArgList(
        b, a, ROOT.RooFit.RooConst(1)))

Plot dataset in X-Y interpretation

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

Fit chi^2 using X and Y errors

In [ ]:
f.chi2FitTo(dxy, ROOT.RooFit.YVar(y))

Overlay fitted function

In [ ]:

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

In [ ]:
f.chi2FitTo(dxy, ROOT.RooFit.YVar(y), ROOT.RooFit.Integrate(ROOT.kTRUE))

Overlay alternate fit result

In [ ]:
f.plotOn(frame, ROOT.RooFit.LineStyle(ROOT.kDashed),

Draw the plot on a canvas

In [ ]:
c = ROOT.TCanvas("rf609_xychi2fit", "rf609_xychi2fit", 600, 600)


Draw all canvases

In [ ]:
from ROOT import gROOT