Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf608_fitresultaspdf.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook
4/// Likelihood and minimization: representing the parabolic approximation of the fit as a multi-variate Gaussian on the
5/// parameters of the fitted pdf
6///
7/// \macro_image
8/// \macro_code
9/// \macro_output
10///
11/// \date July 2008
12/// \author Wouter Verkerke
13
14#include "RooRealVar.h"
15#include "RooDataSet.h"
16#include "RooGaussian.h"
17#include "RooAddPdf.h"
18#include "RooChebychev.h"
19#include "RooFitResult.h"
20#include "TCanvas.h"
21#include "TAxis.h"
22#include "RooPlot.h"
23#include "TFile.h"
24#include "TStyle.h"
25#include "TH2.h"
26#include "TH3.h"
27
28using namespace RooFit;
29
31{
32 // C r e a t e m o d e l a n d d a t a s e t
33 // -----------------------------------------------
34
35 // Observable
36 RooRealVar x("x", "x", -20, 20);
37
38 // Model (intentional strong correlations)
39 RooRealVar mean("mean", "mean of g1 and g2", 0, -1, 1);
40 RooRealVar sigma_g1("sigma_g1", "width of g1", 2);
41 RooGaussian g1("g1", "g1", x, mean, sigma_g1);
42
43 RooRealVar sigma_g2("sigma_g2", "width of g2", 4, 3.0, 5.0);
44 RooGaussian g2("g2", "g2", x, mean, sigma_g2);
45
46 RooRealVar frac("frac", "frac", 0.5, 0.0, 1.0);
47 RooAddPdf model("model", "model", RooArgList(g1, g2), frac);
48
49 // Generate 1000 events
50 std::unique_ptr<RooDataSet> data{model.generate(x, 1000)};
51
52 // F i t m o d e l t o d a t a
53 // ----------------------------------
54
55 std::unique_ptr<RooFitResult> r{model.fitTo(*data, Save(), PrintLevel(-1))};
56
57 // C r e a t e M V G a u s s i a n p d f o f f i t t e d p a r a m e t e r s
58 // ------------------------------------------------------------------------------------
59
60 RooAbsPdf *parabPdf = r->createHessePdf(RooArgSet(frac, mean, sigma_g2));
61
62 // S o m e e x e c e r c i s e s w i t h t h e p a r a m e t e r p d f
63 // -----------------------------------------------------------------------------
64
65 // Generate 100K points in the parameter space, sampled from the MVGaussian pdf
66 std::unique_ptr<RooDataSet> d{parabPdf->generate({mean, sigma_g2, frac}, 100000)};
67
68 // Sample a 3-D histogram of the pdf to be visualized as an error ellipsoid using the GLISO draw option
69 TH3 *hh_3d = (TH3 *)parabPdf->createHistogram("mean,sigma_g2,frac", 25, 25, 25);
70 hh_3d->SetFillColor(kBlue);
71
72 // Project 3D parameter pdf down to 3 permutations of two-dimensional pdfs
73 // The integrations corresponding to these projections are performed analytically
74 // by the MV Gaussian pdf
75 RooAbsPdf *pdf_sigmag2_frac = parabPdf->createProjection(mean);
76 RooAbsPdf *pdf_mean_frac = parabPdf->createProjection(sigma_g2);
77 RooAbsPdf *pdf_mean_sigmag2 = parabPdf->createProjection(frac);
78
79 // Make 2D plots of the 3 two-dimensional pdf projections
80 TH2 *hh_sigmag2_frac = (TH2 *)pdf_sigmag2_frac->createHistogram("sigma_g2,frac", 50, 50);
81 TH2 *hh_mean_frac = (TH2 *)pdf_mean_frac->createHistogram("mean,frac", 50, 50);
82 TH2 *hh_mean_sigmag2 = (TH2 *)pdf_mean_sigmag2->createHistogram("mean,sigma_g2", 50, 50);
83 hh_mean_frac->SetLineColor(kBlue);
84 hh_sigmag2_frac->SetLineColor(kBlue);
85 hh_mean_sigmag2->SetLineColor(kBlue);
86
87 // Draw the 'sigar'
88 new TCanvas("rf608_fitresultaspdf_1", "rf608_fitresultaspdf_1", 600, 600);
89 hh_3d->Draw("iso");
90
91 // Draw the 2D projections of the 3D pdf
92 TCanvas *c2 = new TCanvas("rf608_fitresultaspdf_2", "rf608_fitresultaspdf_2", 900, 600);
93 c2->Divide(3, 2);
94 c2->cd(1);
95 gPad->SetLeftMargin(0.15);
96 hh_mean_sigmag2->GetZaxis()->SetTitleOffset(1.4);
97 hh_mean_sigmag2->Draw("surf3");
98 c2->cd(2);
99 gPad->SetLeftMargin(0.15);
100 hh_sigmag2_frac->GetZaxis()->SetTitleOffset(1.4);
101 hh_sigmag2_frac->Draw("surf3");
102 c2->cd(3);
103 gPad->SetLeftMargin(0.15);
104 hh_mean_frac->GetZaxis()->SetTitleOffset(1.4);
105 hh_mean_frac->Draw("surf3");
106
107 // Draw the distributions of parameter points sampled from the pdf
108 TH1 *tmp1 = d->createHistogram("mean,sigma_g2", Binning(50), Binning(50));
109 TH1 *tmp2 = d->createHistogram("sigma_g2,frac", Binning(50), Binning(50));
110 TH1 *tmp3 = d->createHistogram("mean,frac", Binning(50), Binning(50));
111
112 c2->cd(4);
113 gPad->SetLeftMargin(0.15);
114 tmp1->GetZaxis()->SetTitleOffset(1.4);
115 tmp1->Draw("lego3");
116 c2->cd(5);
117 gPad->SetLeftMargin(0.15);
118 tmp2->GetZaxis()->SetTitleOffset(1.4);
119 tmp2->Draw("lego3");
120 c2->cd(6);
121 gPad->SetLeftMargin(0.15);
122 tmp3->GetZaxis()->SetTitleOffset(1.4);
123 tmp3->Draw("lego3");
124}
#define d(i)
Definition RSha256.hxx:102
@ kBlue
Definition Rtypes.h:66
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
#define gPad
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooFit::OwningPtr< RooDataSet > generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={})
See RooAbsPdf::generate(const RooArgSet&,const RooCmdArg&,const RooCmdArg&,const RooCmdArg&,...
Definition RooAbsPdf.h:57
virtual RooAbsPdf * createProjection(const RooArgSet &iset)
Return a p.d.f that represent a projection of this p.d.f integrated over given observables.
TH1 * createHistogram(RooStringView varNameList, Int_t xbins=0, Int_t ybins=0, Int_t zbins=0) const
Create and fill a ROOT histogram TH1, TH2 or TH3 with the values of this function for the variables w...
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:24
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
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
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
The Canvas class.
Definition TCanvas.h:23
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
TAxis * GetZaxis()
Definition TH1.h:326
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
The 3-D histogram classes derived from the 1-D histogram classes.
Definition TH3.h:31
RooCmdArg Save(bool flag=true)
RooCmdArg PrintLevel(Int_t code)
RooCmdArg Binning(const RooAbsBinning &binning)
Double_t x[n]
Definition legend1.C:17
return c2
Definition legend2.C:14
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26