ROOT   Reference Guide
Searching...
No Matches
rf203_ranges.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Addition and convolution: fitting and plotting in sub ranges
5##
6## \macro_image
7## \macro_code
8## \macro_output
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# Construct observables x
20x = ROOT.RooRealVar("x", "x", -10, 10)
21
22# Construct gaussx(x,mx,1)
23mx = ROOT.RooRealVar("mx", "mx", 0, -10, 10)
24gx = ROOT.RooGaussian("gx", "gx", x, mx, 1.0)
25
26# px = 1 (flat in x)
27px = ROOT.RooPolynomial("px", "px", x)
28
29# model = f*gx + (1-f)px
30f = ROOT.RooRealVar("f", "f", 0.0, 1.0)
31model = ROOT.RooAddPdf("model", "model", [gx, px], [f])
32
33# Generated 10000 events in (x,y) from pdf model
34modelData = model.generate({x}, 10000)
35
36# Fit full range
37# ---------------------------
38
39# Fit pdf to all data
40r_full = model.fitTo(modelData, Save=True, PrintLevel=-1)
41
42# Fit partial range
43# ----------------------------------
44
45# Define "signal" range in x as [-3,3]
46x.setRange("signal", -3, 3)
47
48# Fit pdf only to data in "signal" range
49r_sig = model.fitTo(modelData, Save=True, Range="signal", PrintLevel=-1)
50
51# Plot/print results
52# ---------------------------------------
53
54# Make plot frame in x and add data and fitted model
55frame = x.frame(Title="Fitting a sub range")
56modelData.plotOn(frame)
57model.plotOn(frame, Range="Full", LineColor="r", LineStyle="--") # Add shape in full ranged dashed
58model.plotOn(frame) # By default only fitted range is shown
59
60# Print fit results
61print("result of fit on all data ")
62r_full.Print()
63print("result of fit in in signal region (note increased error on signal fraction)")
64r_sig.Print()
65
66# Draw frame on canvas
67c = ROOT.TCanvas("rf203_ranges", "rf203_ranges", 600, 600)