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