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