Rf 6 0 7_Fitresult¶

Likelihood and minimization: demonstration of options of the RooFitResult class

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, January 17, 2022 at 10:08 AM.

In :
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooChebychev.h"
#include "RooFitResult.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include "TFile.h"
#include "TStyle.h"
#include "TH2.h"
#include "TMatrixDSym.h"
In :
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;

Create pdf, data¶

Declare observable x

In :
RooRealVar x("x", "x", 0, 10);
RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University

Create two gaussian pdfs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters

In :
RooRealVar mean("mean", "mean of gaussians", 5, -10, 10);
RooRealVar sigma1("sigma1", "width of gaussians", 0.5, 0.1, 10);
RooRealVar sigma2("sigma2", "width of gaussians", 1, 0.1, 10);

RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);

Build chebychev polynomial pdf

In :
RooRealVar a0("a0", "a0", 0.5, 0., 1.);
RooRealVar a1("a1", "a1", -0.2);
RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));

Sum the signal components into a composite signal pdf

In :
RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);

Sum the composite signal and background

In :
RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);

Generate 1000 events

In :
RooDataSet *data = model.generate(x, 1000);
[#0] WARNING:Eval -- Evaluating RooAddPdf without a defined normalization set. This can lead to ambiguos coefficients definition and incorrect results. Use RooAddPdf::fixCoefNormalization(nset) to provide a normalization set for defining uniquely RooAddPdf coefficients!

Fit pdf to data, save fitresult¶

Perform fit and save result

In :
RooFitResult *r = model.fitTo(*data, Save());
input_line_59:2:33: error: reference to 'data' is ambiguous
RooFitResult *r = model.fitTo(*data, Save());
^
input_line_58:2:14: note: candidate found by name lookup is '__cling_N526::data'
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
^

Summary printing: basic info plus final values of floating fit parameters

In :
r->Print();
input_line_61:2:3: error: use of undeclared identifier 'r'
(r->Print())
^
Error in <HandleInterpreterException>: Error evaluating expression (r->Print())
Execution of your code was aborted.

Verbose printing: basic info, values of constant parameters, initial and final values of floating parameters, global correlations

In :
r->Print("v");
input_line_63:2:3: error: use of undeclared identifier 'r'
(r->Print("v"))
^
Error in <HandleInterpreterException>: Error evaluating expression (r->Print("v"))
Execution of your code was aborted.

Visualize correlation matrix¶

Construct 2d color plot of correlation matrix

In :
gStyle->SetOptStat(0);
TH2 *hcorr = r->correlationHist();
input_line_65:2:3: error: use of undeclared identifier 'r'
(r->correlationHist())
^
Error in <HandleInterpreterException>: Error evaluating expression (r->correlationHist())
Execution of your code was aborted.

Visualize ellipse corresponding to single correlation matrix element

In :
RooPlot *frame = new RooPlot(sigma1, sig1frac, 0.45, 0.60, 0.65, 0.90);
frame->SetTitle("Covariance between sigma1 and sig1frac");
r->plotOn(frame, sigma1, sig1frac, "ME12ABHV");
input_line_67:2:3: error: use of undeclared identifier 'r'
(r->plotOn(((*(class RooPlot **)0x7f0cc9edff08)), ((*(class RooRealVar*)0x7f0cac0903d8)), ((*(class RooRealVar*)0x7f0cc9edf400)), "ME12ABHV"))
^
Error in <HandleInterpreterException>: Error evaluating expression (r->plotOn(((*(class RooPlot **)0x7f0cc9edff08)), ((*(class RooRealVar*)0x7f0cac0903d8)), ((*(class RooRealVar*)0x7f0cc9edf400)), "ME12ABHV"))
Execution of your code was aborted.

Access fit result information¶

Access basic information

In :
cout << "EDM = " << r->edm() << endl;
cout << "-log(L) at minimum = " << r->minNll() << endl;
input_line_69:2:53: error: use of undeclared identifier 'r'
(((*(std::ostream*)0x7f0cc78f7480)) << "EDM = " << r->edm() << endl)
^
Error in <HandleInterpreterException>: Error evaluating expression (((*(std::ostream*)0x7f0cc78f7480)) << "EDM = " << r->edm() << endl)
Execution of your code was aborted.

Access list of final fit parameter values

In :
cout << "final value of floating parameters" << endl;
r->floatParsFinal().Print("s");
final value of floating parameters
input_line_71:2:3: error: use of undeclared identifier 'r'
(r->floatParsFinal().Print("s"))
^
Error in <HandleInterpreterException>: Error evaluating expression (r->floatParsFinal().Print("s"))
Execution of your code was aborted.

Access correlation matrix elements

In :
cout << "correlation between sig1frac and a0 is  " << r->correlation(sig1frac, a0) << endl;
cout << "correlation between bkgfrac and mean is " << r->correlation("bkgfrac", "mean") << endl;
input_line_73:2:87: error: use of undeclared identifier 'r'
(((*(std::ostream*)0x7f0cc78f7480)) << "correlation between sig1frac and a0 is  " << r->correlation(((*(class RooRealVar*)0x7f0cc9edf400)), ((*(class RooRealVar*)0x7f0cac032000))) << endl)
^
Error in <HandleInterpreterException>: Error evaluating expression (((*(std::ostream*)0x7f0cc78f7480)) << "correlation between sig1frac and a0 is  " << r->correlation(((*(class RooRealVar*)0x7f0cc9edf400)), ((*(class RooRealVar*)0x7f0cac032000))) << endl)
Execution of your code was aborted.

Extract covariance and correlation matrix as tmatrixdsym

In :
const TMatrixDSym &cor = r->correlationMatrix();
const TMatrixDSym &cov = r->covarianceMatrix();
In module 'Cling_Runtime' imported from input_line_2:1:
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc/cling/Interpreter/DynamicLookupRuntimeUniverse.h:59:14: error: reference to type 'const TMatrixTSym<double>' requires an initializer
return T();
^
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc/cling/Interpreter/DynamicLookupRuntimeUniverse.h:52:14: note: in instantiation of function template specialization 'cling::runtime::internal::EvaluateT<const TMatrixTSym<double> &>' requested here
inline T EvaluateT(DynamicExprInfo* ExprInfo, clang::DeclContext* DC ) {
^
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc/cling/Interpreter/RuntimeUniverse.h:53:7: note: in instantiation of member function 'cling::runtime::internal::EvaluateT' requested here
int InterpreterGeneratedCodeDiagnosticsMaybeIncorrect;
^
In module 'Cling_Runtime_Extra':
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc//cling/Interpreter/Value.h:132:18: error: C-style cast from rvalue to reference type 'const TMatrixTSym<double> &'
return (T) V.getAs<long long>();
^~~~~~~~~~~~~~~~~~~~~~~~
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc//cling/Interpreter/Value.h:273:26: note: in instantiation of member function 'cling::Value::CastFwd<const TMatrixTSym<double> &>::cast' requested here
return CastFwd<T>::cast(*this);
^
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc/cling/Interpreter/DynamicLookupRuntimeUniverse.h:58:23: note: in instantiation of function template specialization 'cling::Value::simplisticCastAs<const TMatrixTSym<double> &>' requested here
return result.simplisticCastAs<T>();
^
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc/cling/Interpreter/DynamicLookupRuntimeUniverse.h:52:14: note: in instantiation of function template specialization 'cling::runtime::internal::EvaluateT<const TMatrixTSym<double> &>' requested here
inline T EvaluateT(DynamicExprInfo* ExprInfo, clang::DeclContext* DC ) {
^
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc/cling/Interpreter/RuntimeUniverse.h:53:7: note: in instantiation of member function 'cling::runtime::internal::EvaluateT' requested here
int InterpreterGeneratedCodeDiagnosticsMaybeIncorrect;
^
In module 'Cling_Runtime_Extra':
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc//cling/Interpreter/Value.h:134:18: error: C-style cast from rvalue to reference type 'const TMatrixTSym<double> &'
return (T) V.getAs<unsigned long long>();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc//cling/Interpreter/Value.h:136:18: error: C-style cast from rvalue to reference type 'const TMatrixTSym<double> &'
return (T) V.getAs<double>();
^~~~~~~~~~~~~~~~~~~~~
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc//cling/Interpreter/Value.h:138:18: error: C-style cast from rvalue to reference type 'const TMatrixTSym<double> &'
return (T) V.getAs<float>();
^~~~~~~~~~~~~~~~~~~~
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc//cling/Interpreter/Value.h:140:18: error: C-style cast from rvalue to reference type 'const TMatrixTSym<double> &'
return (T) V.getAs<long double>();
^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc//cling/Interpreter/Value.h:143:18: error: C-style cast from rvalue to reference type 'const TMatrixTSym<double> &'
return (T) (uintptr_t) V.getAs<void*>();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/etc//cling/Interpreter/Value.h:147:16: error: reference to type 'const TMatrixTSym<double>' requires an initializer
return T();
^

Print correlation, covariance matrix

In :
cout << "correlation matrix" << endl;
cor.Print();
cout << "covariance matrix" << endl;
cov.Print();
correlation matrix
input_line_76:2:3: error: use of undeclared identifier 'cor'
(cor.Print())
^
Error in <HandleInterpreterException>: Error evaluating expression (cor.Print())
Execution of your code was aborted.

Persist fit result in root file¶

Open new root file save save result

In :
TFile f("rf607_fitresult.root", "RECREATE");
r->Write("rf607");
f.Close();
input_line_78:2:3: error: use of undeclared identifier 'r'
(r->Write("rf607"))
^
Error in <HandleInterpreterException>: Error evaluating expression (r->Write("rf607"))
Execution of your code was aborted.

In a clean root session retrieve the persisted fit result as follows: RooFitResult* r = gDirectory->Get("rf607") ;

In :
TCanvas *c = new TCanvas("rf607_fitresult", "rf607_fitresult", 800, 400);
c->Divide(2);
c->cd(1);
hcorr->GetYaxis()->SetTitleOffset(1.4);
hcorr->Draw("colz");
c->cd(2); 