# Rf 3 1 5_Projectpdf¶

Multidimensional models: marginizalization of multi-dimensional pdfs through integration

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:00 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataHist.h"
#include "RooGaussian.h"
#include "RooProdPdf.h"
#include "RooPolyVar.h"
#include "TH1.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include "RooNumIntConfig.h"
#include "RooConstVar.h"

In [2]:
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;


## Create pdf m(x,y) = gx(x|y)*g(y)¶

Increase default precision of numeric integration as this exercise has high sensitivity to numeric integration precision

In [3]:
RooAbsPdf::defaultIntegratorConfig()->setEpsRel(1e-8);
RooAbsPdf::defaultIntegratorConfig()->setEpsAbs(1e-8);

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University



Create observables

In [4]:
RooRealVar x("x", "x", -5, 5);
RooRealVar y("y", "y", -2, 2);


Create function f(y) = a0 + a1*y

In [5]:
RooRealVar a0("a0", "a0", 0);
RooRealVar a1("a1", "a1", -1.5, -3, 1);
RooPolyVar fy("fy", "fy", y, RooArgSet(a0, a1));


Create gaussx(x,f(y),sx)

In [6]:
RooRealVar sigmax("sigmax", "width of gaussian", 0.5);
RooGaussian gaussx("gaussx", "Gaussian in x with shifting mean in y", x, fy, sigmax);

[#0] WARNING:InputArguments -- The parameter 'sigmax' with range [-1e+30, 1e+30] of the RooGaussian 'gaussx' exceeds the safe range of (0, inf). Advise to limit its range.


Create gaussy(y,0,2)

In [7]:
RooGaussian gaussy("gaussy", "Gaussian in y", y, RooConst(0), RooConst(2));


Create gaussx(x,sx|y) * gaussy(y)

In [8]:
RooProdPdf model("model", "gaussx(x|y)*gaussy(y)", gaussy, Conditional(gaussx, x));


## Marginalize m(x,y) to m(x)¶

Modelx(x) = int model(x,y) dy

In [9]:
RooAbsPdf *modelx = model.createProjection(y);


## Use marginalized p.d.f. as regular 1-d p.d.f.¶

Sample 1000 events from modelx

In [10]:
RooAbsData *data = modelx->generateBinned(x, 1000);

[#1] INFO:NumericIntegration -- RooRealIntegral::init([gaussy_NORM[y]_X_gaussx_NORM[x]]_Int[y]) using numeric integrator RooIntegrator1D to calculate Int(y)


Fit modelx to toy data

In [11]:
modelx->fitTo(*data, Verbose());

input_line_64:2:17: error: reference to 'data' is ambiguous
modelx->fitTo(*data, Verbose());
^
input_line_60:2:14: note: candidate found by name lookup is '__cling_N528::data'
RooAbsData *data = modelx->generateBinned(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
^


Plot modelx over data

In [12]:
RooPlot *frame = x.frame(40);
data->plotOn(frame);
modelx->plotOn(frame);

input_line_65:3:1: error: reference to 'data' is ambiguous
data->plotOn(frame);
^
input_line_60:2:14: note: candidate found by name lookup is '__cling_N528::data'
RooAbsData *data = modelx->generateBinned(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
^


Make 2d histogram of model(x,y)

In [13]:
TH1 *hh = model.createHistogram("x,y");
hh->SetLineColor(kBlue);

TCanvas *c = new TCanvas("rf315_projectpdf", "rf315_projectpdf", 800, 400);
c->Divide(2);
c->cd(1);
frame->GetYaxis()->SetTitleOffset(1.4);
frame->Draw();
c->cd(2);
hh->GetZaxis()->SetTitleOffset(2.5);
hh->Draw("surf");

input_line_67:2:3: error: use of undeclared identifier 'frame'
(frame->GetYaxis()->SetTitleOffset(1.3999999999999999))
^
Error in <HandleInterpreterException>: Error evaluating expression (frame->GetYaxis()->SetTitleOffset(1.3999999999999999))
Execution of your code was aborted.


Draw all canvases

In [14]:
gROOT->GetListOfCanvases()->Draw()