Logo ROOT  
Reference Guide
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_code
12 ##
13 ## \date February 2018
14 ## \authors Clemens Lange, Wouter Verkerke (C++ version)
15 
16 import ROOT
17 
18 
19 # Define observables and decay pdf
20 # ---------------------------------------------------------------
21 
22 # Declare observables
23 t = ROOT.RooRealVar("t", "t", 0, 5)
24 tmin = ROOT.RooRealVar("tmin", "tmin", 0, 0, 5)
25 
26 # Make parameterized range in t : [tmin,5]
27 t.setRange(tmin, ROOT.RooFit.RooConst(t.getMax()))
28 
29 # Make pdf
30 tau = ROOT.RooRealVar("tau", "tau", -1.54, -10, -0.1)
31 model = ROOT.RooExponential("model", "model", t, tau)
32 
33 # Create input data
34 # ------------------------------------
35 
36 # Generate complete dataset without acceptance cuts (for reference)
37 dall = model.generate(ROOT.RooArgSet(t), 10000)
38 
39 # Generate a (fake) prototype dataset for acceptance limit values
40 tmp = ROOT.RooGaussian("gmin", "gmin", tmin, ROOT.RooFit.RooConst(
41  0), ROOT.RooFit.RooConst(0.5)).generate(ROOT.RooArgSet(tmin), 5000)
42 
43 # Generate dataset with t values that observe (t>tmin)
44 dacc = model.generate(ROOT.RooArgSet(t), ROOT.RooFit.ProtoData(tmp))
45 
46 # Fit pdf to data in acceptance region
47 # -----------------------------------------------------------------------
48 
49 r = model.fitTo(dacc, ROOT.RooFit.Save())
50 
51 # Plot fitted pdf on full and accepted data
52 # ---------------------------------------------------------------------------------
53 
54 # Make plot frame, datasets and overlay model
55 frame = t.frame(ROOT.RooFit.Title("Fit to data with per-event acceptance"))
56 dall.plotOn(frame, ROOT.RooFit.MarkerColor(ROOT.kRed),
57  ROOT.RooFit.LineColor(ROOT.kRed))
58 model.plotOn(frame)
59 dacc.plotOn(frame)
60 
61 # Print fit results to demonstrate absence of bias
62 r.Print("v")
63 
64 c = ROOT.TCanvas("rf314_paramranges", "rf314_paramranges", 600, 600)
65 ROOT.gPad.SetLeftMargin(0.15)
66 frame.GetYaxis().SetTitleOffset(1.6)
67 frame.Draw()
68 
69 c.SaveAs("rf314_paramranges.png")