Logo ROOT  
Reference Guide
rf606_nllerrorhandling.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4##
5## 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #606
6##
7## Understanding and customizing error handling in likelihood evaluations
8##
9## \macro_code
10##
11## \date February 2018
12## \author Clemens Lange
13## \author Wouter Verkerke (C version)
14
15
16import ROOT
17
18
19# Create model and dataset
20# ----------------------------------------------
21
22# Observable
23m = ROOT.RooRealVar("m", "m", 5.20, 5.30)
24
25# Parameters
26m0 = ROOT.RooRealVar("m0", "m0", 5.291, 5.20, 5.30)
27k = ROOT.RooRealVar("k", "k", -30, -50, -10)
28
29# Pdf
30argus = ROOT.RooArgusBG("argus", "argus", m, m0, k)
31
32# Sample 1000 events in m from argus
33data = argus.generate({m}, 1000)
34
35# Plot model and data
36# --------------------------------------
37
38frame1 = m.frame(Bins=40, Title="Argus model and data")
39data.plotOn(frame1)
40argus.plotOn(frame1)
41
42# Fit model to data
43# ---------------------------------
44
45# The ARGUS background shape has a sharp kinematic cutoff at m=m0
46# and is prone to evaluation errors if the cutoff parameter m0
47# is floated: when the pdf cutoff value is lower than that in data
48# events with m>m0 will have zero probability
49
50# Perform unbinned ML fit. Print detailed error messages for up to
51# 10 events per likelihood evaluation. The default error handling strategy
52# is to return a very high value of the likelihood to MINUIT if errors occur,
53# which will force MINUIT to retreat from the problematic area
54
55argus.fitTo(data, PrintEvalErrors=10)
56
57# Peform another fit. In self configuration only the number of errors per
58# likelihood evaluation is shown, it is greater than zero. The
59# EvalErrorWall(kFALSE) arguments disables the default error handling strategy
60# and will cause the actual (problematic) value of the likelihood to be passed
61# to MINUIT.
62#
63# NB: Use of self option is NOT recommended as default strategt as broken -log(L) values
64# can often be lower than 'good' ones because offending events are removed.
65# ROOT.This may effectively create a False minimum in problem areas. ROOT.This is clearly
66# illustrated in the second plot
67
68m0.setError(0.1)
69argus.fitTo(data, PrintEvalErrors=0, EvalErrorWall=False)
70
71# Plot likelihood as function of m0
72# ------------------------------------------------------------------
73
74# Construct likelihood function of model and data
75nll = ROOT.RooNLLVar("nll", "nll", argus, data)
76
77# Plot likelihood in m0 in range that includes problematic values
78# In self configuration no messages are printed for likelihood evaluation errors,
79# but if an likelihood value evaluates with error, corresponding value
80# on the curve will be set to the value given in EvalErrorValue().
81
82frame2 = m0.frame(Range=(5.288, 5.293), Title="-log(L) scan vs m0, regions masked")
83nll.plotOn(frame2, ShiftToZero=True, PrintEvalErrors=-1, EvalErrorValue=(nll.getVal() + 10), LineColor="r")
84frame2.SetMaximum(15)
85frame2.SetMinimum(0)
86
87c = ROOT.TCanvas("rf606_nllerrorhandling", "rf606_nllerrorhandling", 1200, 400)
88c.Divide(2)
89c.cd(1)
90ROOT.gPad.SetLeftMargin(0.15)
91frame1.GetYaxis().SetTitleOffset(1.4)
92frame1.Draw()
93c.cd(2)
94ROOT.gPad.SetLeftMargin(0.15)
95frame2.GetYaxis().SetTitleOffset(1.4)
96frame2.Draw()
97
98c.SaveAs("rf606_nllerrorhandling.png")