ROOT   Reference Guide
Searching...
No Matches
rf601_intminuit.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Likelihood and minimization: interactive minimization with MINUIT
5///
6/// \macro_image
7/// \macro_code
8/// \macro_output
9///
10/// \date July 2008
11/// \author Wouter Verkerke
12
13#include "RooRealVar.h"
14#include "RooDataSet.h"
15#include "RooGaussian.h"
16#include "RooProdPdf.h"
18#include "RooMinimizer.h"
19#include "RooFitResult.h"
20#include "RooPlot.h"
21#include "TCanvas.h"
22#include "TAxis.h"
23#include "TH1.h"
24using namespace RooFit;
25
26void rf601_intminuit()
27{
28 // S e t u p p d f a n d l i k e l i h o o d
29 // -----------------------------------------------
30
31 // Observable
32 RooRealVar x("x", "x", -20, 20);
33
34 // Model (intentional strong correlations)
35 RooRealVar mean("mean", "mean of g1 and g2", 0);
36 RooRealVar sigma_g1("sigma_g1", "width of g1", 3);
37 RooGaussian g1("g1", "g1", x, mean, sigma_g1);
38
39 RooRealVar sigma_g2("sigma_g2", "width of g2", 4, 3.0, 6.0);
40 RooGaussian g2("g2", "g2", x, mean, sigma_g2);
41
42 RooRealVar frac("frac", "frac", 0.5, 0.0, 1.0);
43 RooAddPdf model("model", "model", RooArgList(g1, g2), frac);
44
45 // Generate 1000 events
46 std::unique_ptr<RooDataSet> data{model.generate(x, 1000)};
47
48 // Construct unbinned likelihood of model w.r.t. data
49 std::unique_ptr<RooAbsReal> nll{model.createNLL(*data)};
50
51 // I n t e r a c t i v e m i n i m i z a t i o n , e r r o r a n a l y s i s
52 // -------------------------------------------------------------------------------
53
54 // Create MINUIT interface object
55 RooMinimizer m(*nll);
56
57 // Activate verbose logging of MINUIT parameter space stepping
58 m.setVerbose(true);
59
60 // Call MIGRAD to minimize the likelihood
62
63 // Print values of all parameters, that reflect values (and error estimates)
64 // that are back propagated from MINUIT
65 std::unique_ptr<RooArgSet>{model.getParameters(x)}->Print("s");
66
67 // Disable verbose logging
68 m.setVerbose(false);
69
70 // Run HESSE to calculate errors from d2L/dp2
71 m.hesse();
72
73 // Print value (and error) of sigma_g2 parameter, that reflects
74 // value and error back propagated from MINUIT
75 sigma_g2.Print();
76
77 // Run MINOS on sigma_g2 parameter only
78 m.minos(sigma_g2);
79
80 // Print value (and error) of sigma_g2 parameter, that reflects
81 // value and error back propagated from MINUIT
82 sigma_g2.Print();
83
84 // S a v i n g r e s u l t s , c o n t o u r p l o t s
85 // ---------------------------------------------------------
86
87 // Save a snapshot of the fit result. This object contains the initial
88 // fit parameters, the final fit parameters, the complete correlation
89 // matrix, the EDM, the minimized FCN , the last MINUIT status code and
90 // the number of times the RooFit function object has indicated evaluation
91 // problems (e.g. zero probabilities during likelihood evaluation)
92 std::unique_ptr<RooFitResult> fitResult{m.save()};
93
94 // Make contour plot of mx vs sx at 1,2,3 sigma
95 RooPlot *frame = m.contour(frac, sigma_g2, 1, 2, 3);
96 frame->SetTitle("Minuit contour plot");
97
98 // Print the fit result snapshot
99 fitResult->Print("v");
100
101 // C h a n g e p a r a m e t e r v a l u e s , f l o a t i n g
102 // -----------------------------------------------------------------
103
104 // At any moment you can manually change the value of a (constant)
105 // parameter
106 mean = 0.3;
107
110 m.hesse();
111 frac.Print();
112
113 // Now fix sigma_g2
114 sigma_g2.setConstant(true);
115
118 m.hesse();
119 frac.Print();
120
121 new TCanvas("rf601_intminuit", "rf601_intminuit", 600, 600);
123 frame->GetYaxis()->SetTitleOffset(1.4);
124 frame->Draw();
125}
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Efficient implementation of a sum of PDFs of the form.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Wrapper class around ROOT::Fit:Fitter that provides a seamless interface between the minimizer functi...
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
void SetTitle(const char *name) override
Set the title of the RooPlot to 'title'.
Definition RooPlot.cxx:1255
TAxis * GetYaxis() const
Definition RooPlot.cxx:1276
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:649
Variable that can be changed from the outside.
Definition RooRealVar.h:37
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
The Canvas class.
Definition TCanvas.h:23
void Print(Option_t *option="") const override
Dump this marker with its attributes.
Definition TMarker.cxx:335
Double_t x[n]
Definition legend1.C:17
double nll(double pdf, double weight, int binnedL, int doBinOffset)
Definition MathFuncs.h:321
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
TMarker m
Definition textangle.C:8