# rf109_chi2residpull¶

'BASIC FUNCTIONALITY' RooFit tutorial macro #109 Calculating chi^2 from histograms and curves in ROOT.RooPlots, making histogram of residual and pull distributions

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 Wednesday, November 30, 2022 at 11:22 AM.

In [1]:
from __future__ import print_function
import ROOT

Welcome to JupyROOT 6.27/01


## Set up model¶

Create observables

In [2]:
x = ROOT.RooRealVar("x", "x", -10, 10)


Create Gaussian

In [3]:
sigma = ROOT.RooRealVar("sigma", "sigma", 3, 0.1, 10)
mean = ROOT.RooRealVar("mean", "mean", 0, -10, 10)
gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma)


Generate a sample of 1000 events with sigma=3

In [4]:
data = gauss.generate({x}, 10000)


Change sigma to 3.15

In [5]:
sigma.setVal(3.15)


## Plot data and slightly distorted model¶

Overlay projection of gauss with sigma=3.15 on data with sigma=3.0

In [6]:
frame1 = x.frame(Title="Data with distorted Gaussian pdf", Bins=40)
data.plotOn(frame1, DataError="SumW2")
gauss.plotOn(frame1)

Out[6]:
<cppyy.gbl.RooPlot object at 0x9f71c00>

## Calculate chi^2¶

Show the chi^2 of the curve w.r.t. the histogram If multiple curves or datasets live in the frame you can specify the name of the relevant curve and/or dataset in chiSquare()

In [7]:
print("chi^2 = ", frame1.chiSquare())

chi^2 =  2.6319779436180424


## Show residual and pull dists¶

Construct a histogram with the residuals of the data w.r.t. the curve

In [8]:
hresid = frame1.residHist()


Construct a histogram with the pulls of the data w.r.t the curve

In [9]:
hpull = frame1.pullHist()


Create a frame to draw the residual distribution and add the distribution to the frame

In [10]:
frame2 = x.frame(Title="Residual Distribution")


Create a frame to draw the pull distribution and add the distribution to the frame

In [11]:
frame3 = x.frame(Title="Pull Distribution")

c = ROOT.TCanvas("rf109_chi2residpull", "rf109_chi2residpull", 900, 300)
c.Divide(3)
c.cd(1)
frame1.GetYaxis().SetTitleOffset(1.6)
frame1.Draw()
c.cd(2)
frame2.GetYaxis().SetTitleOffset(1.6)
frame2.Draw()
c.cd(3)
frame3.GetYaxis().SetTitleOffset(1.6)
frame3.Draw()

c.SaveAs("rf109_chi2residpull.png")

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


Draw all canvases

In [12]:
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}