Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf314_paramfitrange.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Multidimensional models: working with parameterized ranges in a fit.
5## This an example of a fit with an acceptance that changes per-event
6##
7## `pdf = exp(-t/tau)` with `t[tmin,5]`
8##
9## where `t` and `tmin` are both observables in the dataset
10##
11## \macro_image
12## \macro_code
13## \macro_output
14##
15## \date February 2018
16## \authors Clemens Lange, Wouter Verkerke (C++ version)
17
18import ROOT
19
20
21# Define observables and decay pdf
22# ---------------------------------------------------------------
23
24# Declare observables
25t = ROOT.RooRealVar("t", "t", 0, 5)
26tmin = ROOT.RooRealVar("tmin", "tmin", 0, 0, 5)
27
28# Make parameterized range in t : [tmin,5]
29t.setRange(tmin, ROOT.RooFit.RooConst(t.getMax()))
30
31# Make pdf
32tau = ROOT.RooRealVar("tau", "tau", -1.54, -10, -0.1)
33model = ROOT.RooExponential("model", "model", t, tau)
34
35# Create input data
36# ------------------------------------
37
38# Generate complete dataset without acceptance cuts (for reference)
39dall = model.generate({t}, 10000)
40
41# Generate a (fake) prototype dataset for acceptance limit values
42tmp = ROOT.RooGaussian("gmin", "gmin", tmin, 0.0, 0.5).generate({tmin}, 5000)
43
44# Generate dataset with t values that observe (t>tmin)
45dacc = model.generate({t}, ProtoData=tmp)
46
47# Fit pdf to data in acceptance region
48# -----------------------------------------------------------------------
49
50r = model.fitTo(dacc, Save=True, PrintLevel=-1)
51
52# Plot fitted pdf on full and accepted data
53# ---------------------------------------------------------------------------------
54
55# Make plot frame, datasets and overlay model
56frame = t.frame(Title="Fit to data with per-event acceptance")
57dall.plotOn(frame, MarkerColor="r", LineColor="r")
58model.plotOn(frame)
59dacc.plotOn(frame)
60
61# Print fit results to demonstrate absence of bias
62r.Print("v")
63
64c = ROOT.TCanvas("rf314_paramranges", "rf314_paramranges", 600, 600)
65ROOT.gPad.SetLeftMargin(0.15)
66frame.GetYaxis().SetTitleOffset(1.6)
67frame.Draw()
68
69c.SaveAs("rf314_paramranges.png")