Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf601_intminuit.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4##
5## 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #601
6##
7## Interactive minimization with MINUIT
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# Setup pdf and likelihood
21# -----------------------------------------------
22
23# Observable
24x = ROOT.RooRealVar("x", "x", -20, 20)
25
26# Model (intentional strong correlations)
27mean = ROOT.RooRealVar("mean", "mean of g1 and g2", 0)
28sigma_g1 = ROOT.RooRealVar("sigma_g1", "width of g1", 3)
29g1 = ROOT.RooGaussian("g1", "g1", x, mean, sigma_g1)
30
31sigma_g2 = ROOT.RooRealVar("sigma_g2", "width of g2", 4, 3.0, 6.0)
32g2 = ROOT.RooGaussian("g2", "g2", x, mean, sigma_g2)
33
34frac = ROOT.RooRealVar("frac", "frac", 0.5, 0.0, 1.0)
35model = ROOT.RooAddPdf("model", "model", [g1, g2], [frac])
36
37# Generate 1000 events
38data = model.generate({x}, 1000)
39
40# Construct unbinned likelihood of model w.r.t. data
41nll = model.createNLL(data)
42
43# Interactive minimization, error analysis
44# -------------------------------------------------------------------------------
45
46# Create MINUIT interface object
47m = ROOT.RooMinimizer(nll)
48
49# Activate verbose logging of MINUIT parameter space stepping
50m.setVerbose(True)
51
52# Call MIGRAD to minimize the likelihood
53m.migrad()
54
55# Print values of all parameters, reflect values (and error estimates)
56# that are back propagated from MINUIT
57model.getParameters({x}).Print("s")
58
59# Disable verbose logging
60m.setVerbose(False)
61
62# Run HESSE to calculate errors from d2L/dp2
63m.hesse()
64
65# Print value (and error) of sigma_g2 parameter, reflects
66# value and error back propagated from MINUIT
67sigma_g2.Print()
68
69# Run MINOS on sigma_g2 parameter only
70m.minos({sigma_g2})
71
72# Print value (and error) of sigma_g2 parameter, reflects
73# value and error back propagated from MINUIT
74sigma_g2.Print()
75
76# Saving results, contour plots
77# ---------------------------------------------------------
78
79# Save a snapshot of the fit result. ROOT.This object contains the initial
80# fit parameters, final fit parameters, complete correlation
81# matrix, EDM, minimized FCN , last MINUIT status code and
82# the number of times the ROOT.RooFit function object has indicated evaluation
83# problems (e.g. zero probabilities during likelihood evaluation)
84r = m.save()
85
86# Make contour plot of mx vs sx at 1,2, sigma
87frame = m.contour(frac, sigma_g2, 1, 2, 3)
88frame.SetTitle("Contour plot")
89
90# Print the fit result snapshot
91r.Print("v")
92
93# Change parameter values, plotting
94# -----------------------------------------------------------------
95
96# At any moment you can manually change the value of a (constant)
97# parameter
98mean.setVal(0.3)
99
100# Rerun MIGRAD,HESSE
101m.migrad()
102m.hesse()
103frac.Print()
104
105# Now fix sigma_g2
106sigma_g2.setConstant(True)
107
108# Rerun MIGRAD,HESSE
109m.migrad()
110m.hesse()
111frac.Print()
112
113c = ROOT.TCanvas("rf601_intminuit", "rf601_intminuit", 600, 600)
114ROOT.gPad.SetLeftMargin(0.15)
115frame.GetYaxis().SetTitleOffset(1.4)
116frame.Draw()
117
118c.SaveAs("rf601_intminuit.png")
void Print(GNN_Data &d, std::string txt="")