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