Likelihood and minimization: interactive minimization with MINUIT
Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:16 PM.
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooProdPdf.h"
#include "RooAddPdf.h"
#include "RooMinimizer.h"
#include "RooFitResult.h"
#include "RooPlot.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TH1.h"
using namespace RooFit;
Observable
RooRealVar x("x", "x", -20, 20);
Model (intentional strong correlations)
RooRealVar mean("mean", "mean of g1 and g2", 0);
RooRealVar sigma_g1("sigma_g1", "width of g1", 3);
RooGaussian g1("g1", "g1", x, mean, sigma_g1);
RooRealVar sigma_g2("sigma_g2", "width of g2", 4, 3.0, 6.0);
RooGaussian g2("g2", "g2", x, mean, sigma_g2);
RooRealVar frac("frac", "frac", 0.5, 0.0, 1.0);
RooAddPdf model("model", "model", RooArgList(g1, g2), frac);
[#0] WARNING:InputArguments -- The parameter 'sigma_g1' with range [-inf, inf] of the RooGaussian 'g1' exceeds the safe range of (0, inf). Advise to limit its range.
Generate 1000 events
std::unique_ptr<RooDataSet> data{model.generate(x, 1000)};
input_line_50:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration std::unique_ptr<RooDataSet> data{model.generate(x, 1000)}; ^
Construct unbinned likelihood of model w.r.t. data
std::unique_ptr<RooAbsReal> nll{model.createNLL(*data)};
input_line_51:2:51: error: reference to 'data' is ambiguous std::unique_ptr<RooAbsReal> nll{model.createNLL(*data)}; ^ input_line_50:2:30: note: candidate found by name lookup is 'data' std::unique_ptr<RooDataSet> data{model.generate(x, 1000)}; ^ /usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data' data(initializer_list<_Tp> __il) noexcept ^ /usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data' data(_Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data' data(const _Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data' data(_Tp (&__array)[_Nm]) noexcept ^
Create MINUIT interface object
RooMinimizer m(*nll);
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv, _ZN12__cling_N5241mE, $.cling-module-309.__inits.0, __cxx_global_var_initcling_module_309_.2, _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb, __orc_init_func.cling-module-309, _ZN12__cling_N52416__cling_Un1Qu323EPv, _ZN12__cling_N52424__dynamic__cling_Un1Qu30E, __cxx_global_var_initcling_module_309_, _GLOBAL__sub_I_cling_module_309, _ZN5cling7runtime8internal15DynamicExprInfoC2EPKcPPvb }) } IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerC1EPNS1_15DynamicExprInfoEPN5clang11DeclContextEPKcPNS_11InterpreterE' unresolved while linking [cling interface function]! You are probably missing the definition of cling::runtime::internal::LifetimeHandler::LifetimeHandler(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*, char const*, cling::Interpreter*) Maybe you need to load the corresponding shared library? IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerD1Ev' unresolved while linking [cling interface function]! You are probably missing the definition of cling::runtime::internal::LifetimeHandler::~LifetimeHandler() Maybe you need to load the corresponding shared library?
Activate verbose logging of MINUIT parameter space stepping
m.setVerbose(true);
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-309 }) } cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5241mE }) }
Call MIGRAD to minimize the likelihood
m.migrad();
cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5241mE }) }
Print values of all parameters, that reflect values (and error estimates) that are back propagated from MINUIT
std::unique_ptr<RooArgSet>{model.getParameters(x)}->Print("s");
1) RooRealVar:: frac = 0.5 2) RooRealVar:: mean = 0 3) RooRealVar:: sigma_g1 = 3 4) RooRealVar:: sigma_g2 = 4
Disable verbose logging
m.setVerbose(false);
cling JIT session error: Failed to materialize symbols: { (main, { _ZN12RooMinimizer10setVerboseEb }) }
Run HESSE to calculate errors from d2L/dp2
m.hesse();
cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5241mE }) }
Print value (and error) of sigma_g2 parameter, that reflects value and error back propagated from MINUIT
sigma_g2.Print();
RooRealVar::sigma_g2 = 4 L(3 - 6)
Run MINOS on sigma_g2 parameter only
m.minos(sigma_g2);
cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5241mE }) }
Print value (and error) of sigma_g2 parameter, that reflects value and error back propagated from MINUIT
sigma_g2.Print();
RooRealVar::sigma_g2 = 4 L(3 - 6)
Save a snapshot of the fit result. This object contains the initial fit parameters, the final fit parameters, the complete correlation matrix, the EDM, the minimized FCN , the last MINUIT status code and the number of times the RooFit function object has indicated evaluation problems (e.g. zero probabilities during likelihood evaluation)
std::unique_ptr<RooFitResult> fitResult{m.save()};
cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5241mE }) } [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZNSt5tupleIJP12RooFitResultSt14default_deleteIS0_EEEC2IS1_S3_Lb1EEEv, _ZSt12__get_helperILm0EP12RooFitResultJSt14default_deleteIS0_EEERT0_RSt11_Tuple_implIXT_EJS4_DpT1_EE, _ZNSt10_Head_baseILm1ESt14default_deleteI12RooFitResultELb1EEC2Ev, _ZN12__cling_N53316__cling_Un1Qu332EPv, _ZNSt10unique_ptrI12RooFitResultSt14default_deleteIS0_EEC1IS2_vEEPS0_, _ZNSt15__uniq_ptr_implI12RooFitResultSt14default_deleteIS0_EE10_M_deleterEv, _ZNSt11_Tuple_implILm1EJSt14default_deleteI12RooFitResultEEEC2Ev, _ZNSt10unique_ptrI12RooFitResultSt14default_deleteIS0_EED1Ev, _ZNKSt14default_deleteI12RooFitResultEclEPS0_, _ZNSt11_Tuple_implILm0EJP12RooFitResultSt14default_deleteIS0_EEEC2Ev, _ZSt12__get_helperILm1ESt14default_deleteI12RooFitResultEJEERT0_RSt11_Tuple_implIXT_EJS3_DpT1_EE, _ZNSt15__uniq_ptr_implI12RooFitResultSt14default_deleteIS0_EE6_M_ptrEv, _ZNSt10_Head_baseILm0EP12RooFitResultLb0EE7_M_headERS2_, _ZNSt15__uniq_ptr_implI12RooFitResultSt14default_deleteIS0_EEC2EPS0_, _ZNSt5tupleIJP12RooFitResultSt14default_deleteIS0_EEEC1IS1_S3_Lb1EEEv, _ZNSt11_Tuple_implILm1EJSt14default_deleteI12RooFitResultEEE7_M_headERS3_, _ZNSt10unique_ptrI12RooFitResultSt14default_deleteIS0_EEC2IS2_vEEPS0_, $.cling-module-318.__inits.0, _ZSt3getILm0EJP12RooFitResultSt14default_deleteIS0_EEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERS8_, _ZNSt10_Head_baseILm0EP12RooFitResultLb0EEC2Ev, __orc_init_func.cling-module-318, _ZN12__cling_N5339fitResultE, _ZNSt10unique_ptrI12RooFitResultSt14default_deleteIS0_EE11get_deleterEv, _ZNSt15__uniq_ptr_implI12RooFitResultSt14default_deleteIS0_EEC1EPS0_, _ZNSt10_Head_baseILm1ESt14default_deleteI12RooFitResultELb1EE7_M_headERS3_, _ZNSt10unique_ptrI12RooFitResultSt14default_deleteIS0_EED2Ev, _GLOBAL__sub_I_cling_module_318, _ZNSt11_Tuple_implILm0EJP12RooFitResultSt14default_deleteIS0_EEE7_M_headERS4_, __cxx_global_var_initcling_module_318_, _ZSt3getILm1EJP12RooFitResultSt14default_deleteIS0_EEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERS8_ }) }
Make contour plot of mx vs sx at 1,2,3 sigma
RooPlot *frame = m.contour(frac, sigma_g2, 1, 2, 3);
frame->SetTitle("Minuit contour plot");
cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5241mE }) } [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN12__cling_N53416__cling_Un1Qu333EPv, $.cling-module-319.__inits.0, __orc_init_func.cling-module-319, _GLOBAL__sub_I_cling_module_319, __cxx_global_var_initcling_module_319_, _ZN12__cling_N5345frameE }) }
Print the fit result snapshot
fitResult->Print("v");
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-318 }) } cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5339fitResultE }) }
At any moment you can manually change the value of a (constant) parameter
mean = 0.3;
Rerun MIGRAD,HESSE
m.migrad();
m.hesse();
frac.Print();
cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5241mE }) }
Now fix sigma_g2
sigma_g2.setConstant(true);
Rerun MIGRAD,HESSE
m.migrad();
m.hesse();
frac.Print();
new TCanvas("rf601_intminuit", "rf601_intminuit", 600, 600);
gPad->SetLeftMargin(0.15);
frame->GetYaxis()->SetTitleOffset(1.4);
frame->Draw();
cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5241mE }) }
Draw all canvases
%jsroot on
gROOT->GetListOfCanvases()->Draw()