ROOT   Reference Guide
rf315_projectpdf.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook
4/// Multidimensional models: marginizalization of multi-dimensional pdfs through integration
5///
6/// \macro_image
7/// \macro_output
8/// \macro_code
9///
10/// \date July 2008
11/// \author Wouter Verkerke
12
13#include "RooRealVar.h"
14#include "RooDataHist.h"
15#include "RooGaussian.h"
16#include "RooProdPdf.h"
17#include "RooPolyVar.h"
18#include "TH1.h"
19#include "TCanvas.h"
20#include "TAxis.h"
21#include "RooPlot.h"
22#include "RooNumIntConfig.h"
23#include "RooConstVar.h"
24using namespace RooFit;
25
27{
28 // C r e a t e p d f m ( x , y ) = g x ( x | y ) * g ( y )
29 // --------------------------------------------------------------
30
31 // Increase default precision of numeric integration
32 // as this exercise has high sensitivity to numeric integration precision
35
36 // Create observables
37 RooRealVar x("x", "x", -5, 5);
38 RooRealVar y("y", "y", -2, 2);
39
40 // Create function f(y) = a0 + a1*y
41 RooRealVar a0("a0", "a0", 0);
42 RooRealVar a1("a1", "a1", -1.5, -3, 1);
43 RooPolyVar fy("fy", "fy", y, RooArgSet(a0, a1));
44
45 // Create gaussx(x,f(y),sx)
46 RooRealVar sigmax("sigmax", "width of gaussian", 0.5);
47 RooGaussian gaussx("gaussx", "Gaussian in x with shifting mean in y", x, fy, sigmax);
48
49 // Create gaussy(y,0,2)
50 RooGaussian gaussy("gaussy", "Gaussian in y", y, RooConst(0), RooConst(2));
51
52 // Create gaussx(x,sx|y) * gaussy(y)
53 RooProdPdf model("model", "gaussx(x|y)*gaussy(y)", gaussy, Conditional(gaussx, x));
54
55 // M a r g i n a l i z e m ( x , y ) t o m ( x )
56 // ----------------------------------------------------
57
58 // modelx(x) = Int model(x,y) dy
59 RooAbsPdf *modelx = model.createProjection(y);
60
61 // U s e m a r g i n a l i z e d p . d . f . a s r e g u l a r 1 - D p . d . f .
62 // ------------------------------------------------------------------------------------------
63
64 // Sample 1000 events from modelx
65 RooAbsData *data = modelx->generateBinned(x, 1000);
66
67 // Fit modelx to toy data
68 modelx->fitTo(*data, Verbose());
69
70 // Plot modelx over data
71 RooPlot *frame = x.frame(40);
72 data->plotOn(frame);
73 modelx->plotOn(frame);
74
75 // Make 2D histogram of model(x,y)
76 TH1 *hh = model.createHistogram("x,y");
78
79 TCanvas *c = new TCanvas("rf315_projectpdf", "rf315_projectpdf", 800, 400);
80 c->Divide(2);
81 c->cd(1);
83 frame->GetYaxis()->SetTitleOffset(1.4);
84 frame->Draw();
85 c->cd(2);
87 hh->GetZaxis()->SetTitleOffset(2.5);
88 hh->Draw("surf");
89}
#define c(i)
Definition: RSha256.hxx:101
#define e(i)
Definition: RSha256.hxx:103
@ kBlue
Definition: Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:62
virtual RooFitResult * fitTo(RooAbsData &data, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Fit PDF to given dataset.
Definition: RooAbsPdf.cxx:1501
virtual RooDataHist * generateBinned(const RooArgSet &whatVars, double nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none()) const
As RooAbsPdf::generateBinned(const RooArgSet&, const RooCmdArg&,const RooCmdArg&, const RooCmdArg&,...
Definition: RooAbsPdf.h:113
virtual RooAbsPdf * createProjection(const RooArgSet &iset)
Return a p.d.f that represent a projection of this p.d.f integrated over given observables.
Definition: RooAbsPdf.cxx:3291
RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none(), const RooCmdArg &arg9=RooCmdArg::none(), const RooCmdArg &arg10=RooCmdArg::none()) const override
Definition: RooAbsPdf.h:127
static RooNumIntConfig * defaultIntegratorConfig()
Returns the default numeric integration configuration for all RooAbsReals.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
void setEpsRel(double newEpsRel)
Set relative convergence criteria (convergence if abs(Err)/abs(Int)<newEpsRel)
void setEpsAbs(double newEpsAbs)
Set absolute convergence criteria (convergence if abs(Err)<newEpsAbs)
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:43
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
Class RooPolyVar is a RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficie...
Definition: RooPolyVar.h:28
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:33
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:301
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:58
TAxis * GetZaxis()
Definition: TH1.h:324
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition: TH1.cxx:3060
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, bool depsAreCond=false)
RooConstVar & RooConst(double val)
RooCmdArg Verbose(bool flag=true)
Double_t y[n]
Definition: legend1.C:17
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: Common.h:18