ROOT   Reference Guide
Searching...
No Matches
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_code
8/// \macro_output
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"
23using namespace RooFit;
24
26{
27 // C r e a t e p d f m ( x , y ) = g x ( x | y ) * g ( y )
28 // --------------------------------------------------------------
29
30 // Increase default precision of numeric integration
31 // as this exercise has high sensitivity to numeric integration precision
34
35 // Create observables
36 RooRealVar x("x", "x", -5, 5);
37 RooRealVar y("y", "y", -2, 2);
38
39 // Create function f(y) = a0 + a1*y
40 RooRealVar a0("a0", "a0", 0);
41 RooRealVar a1("a1", "a1", -1.5, -3, 1);
42 RooPolyVar fy("fy", "fy", y, RooArgSet(a0, a1));
43
44 // Create gaussx(x,f(y),sx)
45 RooRealVar sigmax("sigmax", "width of gaussian", 0.5);
46 RooGaussian gaussx("gaussx", "Gaussian in x with shifting mean in y", x, fy, sigmax);
47
48 // Create gaussy(y,0,2)
49 RooGaussian gaussy("gaussy", "Gaussian in y", y, 0.0, 2.0);
50
51 // Create gaussx(x,sx|y) * gaussy(y)
52 RooProdPdf model("model", "gaussx(x|y)*gaussy(y)", gaussy, Conditional(gaussx, x));
53
54 // M a r g i n a l i z e m ( x , y ) t o m ( x )
55 // ----------------------------------------------------
56
57 // modelx(x) = Int model(x,y) dy
58 RooAbsPdf *modelx = model.createProjection(y);
59
60 // 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 .
61 // ------------------------------------------------------------------------------------------
62
63 // Sample 1000 events from modelx
64 std::unique_ptr<RooAbsData> data{modelx->generateBinned(x, 1000)};
65
66 // Fit modelx to toy data
67 modelx->fitTo(*data, Verbose(), PrintLevel(-1));
68
69 // Plot modelx over data
70 RooPlot *frame = x.frame(40);
71 data->plotOn(frame);
72 modelx->plotOn(frame);
73
74 // Make 2D histogram of model(x,y)
75 TH1 *hh = model.createHistogram("x,y");
77
78 TCanvas *c = new TCanvas("rf315_projectpdf", "rf315_projectpdf", 800, 400);
79 c->Divide(2);
80 c->cd(1);
82 frame->GetYaxis()->SetTitleOffset(1.4);
83 frame->Draw();
84 c->cd(2);
86 hh->GetZaxis()->SetTitleOffset(2.5);
87 hh->Draw("surf");
88}
#define c(i)
Definition RSha256.hxx:101
#define e(i)
Definition RSha256.hxx:103
double fy() const
@ kBlue
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}, const RooCmdArg &arg9={}, const RooCmdArg &arg10={}) const override
Definition RooAbsPdf.h:124
RooFit::OwningPtr< RooFitResult > fitTo(RooAbsData &data, CmdArgs_t const &... cmdArgs)
Fit PDF to given dataset.
Definition RooAbsPdf.h:157
virtual RooFit::OwningPtr< RooDataHist > generateBinned(const RooArgSet &whatVars, double nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}) const
As RooAbsPdf::generateBinned(const RooArgSet&, const RooCmdArg&,const RooCmdArg&, const RooCmdArg&,...
Definition RooAbsPdf.h:110
virtual RooAbsPdf * createProjection(const RooArgSet &iset)
Return a p.d.f that represent a projection of this p.d.f integrated over given observables.
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:55
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
void setEpsRel(double newEpsRel)
Set relative convergence criteria (convergence if std::abs(Err)/abs(Int)<newEpsRel)
void setEpsAbs(double newEpsAbs)
Set absolute convergence criteria (convergence if std::abs(Err)<newEpsAbs)
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:237
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
A RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficients.
Definition RooPolyVar.h:25
Efficient implementation of a product of PDFs of the form.
Definition RooProdPdf.h:33
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 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
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, bool depsAreCond=false)
RooCmdArg PrintLevel(Int_t code)
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 JSONIO.h:26