ROOT   Reference Guide
rf109_chi2residpull.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## 'BASIC FUNCTIONALITY' RooFit tutorial macro #109
5## Calculating chi^2 from histograms and curves in ROOT.RooPlots,
6## making histogram of residual and pull distributions
7##
8## \macro_code
9##
10## \date February 2018
11## \authors Clemens Lange, Wouter Verkerke (C version)
12
13from __future__ import print_function
14import ROOT
15
16# Set up model
17# ---------------------
18
19# Create observables
20x = ROOT.RooRealVar("x", "x", -10, 10)
21
22# Create Gaussian
23sigma = ROOT.RooRealVar("sigma", "sigma", 3, 0.1, 10)
24mean = ROOT.RooRealVar("mean", "mean", 0, -10, 10)
25gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma)
26
27# Generate a sample of 1000 events with sigma=3
28data = gauss.generate({x}, 10000)
29
30# Change sigma to 3.15
31sigma.setVal(3.15)
32
33# Plot data and slightly distorted model
34# ---------------------------------------------------------------------------
35
36# Overlay projection of gauss with sigma=3.15 on data with sigma=3.0
37frame1 = x.frame(Title="Data with distorted Gaussian pdf", Bins=40)
38data.plotOn(frame1, DataError="SumW2")
39gauss.plotOn(frame1)
40
41# Calculate chi^2
42# ------------------------------
43
44# Show the chi^2 of the curve w.r.t. the histogram
45# If multiple curves or datasets live in the frame you can specify
46# the name of the relevant curve and/or dataset in chiSquare()
47print("chi^2 = ", frame1.chiSquare())
48
49# Show residual and pull dists
50# -------------------------------------------------------
51
52# Construct a histogram with the residuals of the data w.r.t. the curve
53hresid = frame1.residHist()
54
55# Construct a histogram with the pulls of the data w.r.t the curve
56hpull = frame1.pullHist()
57
58# Create a frame to draw the residual distribution and add the
59# distribution to the frame
60frame2 = x.frame(Title="Residual Distribution")
62
63# Create a frame to draw the pull distribution and add the distribution to
64# the frame
65frame3 = x.frame(Title="Pull Distribution")
67
68c = ROOT.TCanvas("rf109_chi2residpull", "rf109_chi2residpull", 900, 300)
69c.Divide(3)
70c.cd(1)