Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf607_fitresult.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook
4/// Likelihood and minimization: demonstration of options of the RooFitResult class
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 "RooAddPdf.h"
17#include "RooChebychev.h"
18#include "RooFitResult.h"
19#include "TCanvas.h"
20#include "TAxis.h"
21#include "RooPlot.h"
22#include "TFile.h"
23#include "TStyle.h"
24#include "TH2.h"
25#include "TMatrixDSym.h"
26
27using namespace RooFit;
28
29void rf607_fitresult()
30{
31 // C r e a t e p d f , d a t a
32 // --------------------------------
33
34 // Declare observable x
35 RooRealVar x("x", "x", 0, 10);
36
37 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
38 RooRealVar mean("mean", "mean of gaussians", 5, -10, 10);
39 RooRealVar sigma1("sigma1", "width of gaussians", 0.5, 0.1, 10);
40 RooRealVar sigma2("sigma2", "width of gaussians", 1, 0.1, 10);
41
42 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
43 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
44
45 // Build Chebychev polynomial pdf
46 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
47 RooRealVar a1("a1", "a1", -0.2);
48 RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
49
50 // Sum the signal components into a composite signal pdf
51 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
52 RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
53
54 // Sum the composite signal and background
55 RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
56 RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
57
58 // Generate 1000 events
59 std::unique_ptr<RooDataSet> data{model.generate(x, 1000)};
60
61 // F i t p d f t o d a t a , s a v e f i t r e s u l t
62 // -------------------------------------------------------------
63
64 // Perform fit and save result
65 std::unique_ptr<RooFitResult> r{model.fitTo(*data, Save(), PrintLevel(-1))};
66
67 // P r i n t f i t r e s u l t s
68 // ---------------------------------
69
70 // Summary printing: Basic info plus final values of floating fit parameters
71 r->Print();
72
73 // Verbose printing: Basic info, values of constant parameters, initial and
74 // final values of floating parameters, global correlations
75 r->Print("v");
76
77 // V i s u a l i z e c o r r e l a t i o n m a t r i x
78 // -------------------------------------------------------
79
80 // Construct 2D color plot of correlation matrix
82 TH2 *hcorr = r->correlationHist();
83
84 // Visualize ellipse corresponding to single correlation matrix element
85 RooPlot *frame = new RooPlot(sigma1, sig1frac, 0.45, 0.60, 0.65, 0.90);
86 frame->SetTitle("Covariance between sigma1 and sig1frac");
87 r->plotOn(frame, sigma1, sig1frac, "ME12ABHV");
88
89 // A c c e s s f i t r e s u l t i n f o r m a t i o n
90 // ---------------------------------------------------------
91
92 // Access basic information
93 cout << "EDM = " << r->edm() << endl;
94 cout << "-log(L) at minimum = " << r->minNll() << endl;
95
96 // Access list of final fit parameter values
97 cout << "final value of floating parameters" << endl;
98 r->floatParsFinal().Print("s");
99
100 // Access correlation matrix elements
101 cout << "correlation between sig1frac and a0 is " << r->correlation(sig1frac, a0) << endl;
102 cout << "correlation between bkgfrac and mean is " << r->correlation("bkgfrac", "mean") << endl;
103
104 // Extract covariance and correlation matrix as TMatrixDSym
105 const TMatrixDSym &cor = r->correlationMatrix();
106 const TMatrixDSym &cov = r->covarianceMatrix();
107
108 // Print correlation, covariance matrix
109 cout << "correlation matrix" << endl;
110 cor.Print();
111 cout << "covariance matrix" << endl;
112 cov.Print();
113
114 // P e r s i s t f i t r e s u l t i n r o o t f i l e
115 // -------------------------------------------------------------
116
117 // Open new ROOT file save save result
118 TFile f("rf607_fitresult.root", "RECREATE");
119 r->Write("rf607");
120 f.Close();
121
122 // In a clean ROOT session retrieve the persisted fit result as follows:
123 // RooFitResult* r = gDirectory->Get("rf607") ;
124
125 TCanvas *c = new TCanvas("rf607_fitresult", "rf607_fitresult", 800, 400);
126 c->Divide(2);
127 c->cd(1);
128 gPad->SetLeftMargin(0.15);
129 hcorr->GetYaxis()->SetTitleOffset(1.4);
130 hcorr->Draw("colz");
131 c->cd(2);
132 gPad->SetLeftMargin(0.15);
133 frame->GetYaxis()->SetTitleOffset(1.6);
134 frame->Draw();
135}
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
Efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:33
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Chebychev polynomial p.d.f.
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
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
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
TAxis * GetYaxis()
Definition TH1.h:325
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
Service class for 2-D histogram classes.
Definition TH2.h:30
void Print(Option_t *name="") const override
Print the matrix as a table of elements.
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1640
RooCmdArg Save(bool flag=true)
RooCmdArg PrintLevel(Int_t code)
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26