ROOT   Reference Guide
rf103_interprfuncs.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4///
5/// Basic functionality: interpreted functions and p.d.f.s
6///
7/// \macro_image
8/// \macro_output
9/// \macro_code
10///
11/// \date 07/2008
12/// \author Wouter Verkerke
13
14#include "RooRealVar.h"
15#include "RooDataSet.h"
16#include "RooGaussian.h"
17#include "TCanvas.h"
18#include "TAxis.h"
19#include "RooPlot.h"
20#include "RooFitResult.h"
21#include "RooGenericPdf.h"
22#include "RooConstVar.h"
23
24using namespace RooFit;
25
27{
28 // ----------------------------------------------------
29 // G e n e r i c i n t e r p r e t e d p . d . f .
30 // ====================================================
31
32 // Declare observable x
33 RooRealVar x("x", "x", -20, 20);
34
35 // C o n s t r u c t g e n e r i c p d f f r o m i n t e r p r e t e d e x p r e s s i o n
36 // -------------------------------------------------------------------------------------------------
37
38 // To construct a proper p.d.f, the formula expression is explicitly normalized internally by dividing
39 // it by a numeric integral of the expression over x in the range [-20,20]
40 //
41 RooRealVar alpha("alpha", "alpha", 5, 0.1, 10);
42 RooGenericPdf genpdf("genpdf", "genpdf", "(1+0.1*abs(x)+sin(sqrt(abs(x*alpha+0.1))))", RooArgSet(x, alpha));
43
44 // S a m p l e , f i t a n d p l o t g e n e r i c p d f
45 // ---------------------------------------------------------------
46
47 // Generate a toy dataset from the interpreted p.d.f
48 RooDataSet *data = genpdf.generate(x, 10000);
49
50 // Fit the interpreted p.d.f to the generated data
51 genpdf.fitTo(*data);
52
53 // Make a plot of the data and the p.d.f overlaid
54 RooPlot *xframe = x.frame(Title("Interpreted expression pdf"));
55 data->plotOn(xframe);
56 genpdf.plotOn(xframe);
57
58 // -----------------------------------------------------------------------------------------------------------
59 // S t a n d a r d p . d . f a d j u s t w i t h i n t e r p r e t e d h e l p e r f u n c t i o n
60 // ==========================================================================================================
61 // Make a gauss(x,sqrt(mean2),sigma) from a standard RooGaussian
62
63 // C o n s t r u c t s t a n d a r d p d f w i t h f o r m u l a r e p l a c i n g p a r a m e t e r
64 // ------------------------------------------------------------------------------------------------------------
65
66 // Construct parameter mean2 and sigma
67 RooRealVar mean2("mean2", "mean^2", 10, 0, 200);
68 RooRealVar sigma("sigma", "sigma", 3, 0.1, 10);
69
70 // Construct interpreted function mean = sqrt(mean^2)
71 RooFormulaVar mean("mean", "mean", "sqrt(mean2)", mean2);
72
73 // Construct a gaussian g2(x,sqrt(mean2),sigma) ;
74 RooGaussian g2("g2", "h2", x, mean, sigma);
75
76 // G e n e r a t e t o y d a t a
77 // ---------------------------------
78
79 // Construct a separate gaussian g1(x,10,3) to generate a toy Gaussian dataset with mean 10 and width 3
80 RooGaussian g1("g1", "g1", x, RooConst(10), RooConst(3));
81 RooDataSet *data2 = g1.generate(x, 1000);
82
83 // F i t a n d p l o t t a i l o r e d s t a n d a r d p d f
84 // -------------------------------------------------------------------
85
86 // Fit g2 to data from g1
87 RooFitResult *r = g2.fitTo(*data2, Save());
88 r->Print();
89
90 // Plot data on frame and overlay projection of g2
91 RooPlot *xframe2 = x.frame(Title("Tailored Gaussian pdf"));
92 data2->plotOn(xframe2);
93 g2.plotOn(xframe2);
94
95 // Draw all frames on a canvas
96 TCanvas *c = new TCanvas("rf103_interprfuncs", "rf103_interprfuncs", 800, 400);
97 c->Divide(2);
98 c->cd(1);
100 xframe->GetYaxis()->SetTitleOffset(1.4);
101 xframe->Draw();
102 c->cd(2);
104 xframe2->GetYaxis()->SetTitleOffset(1.4);
105 xframe2->Draw();
106}
ROOT::R::TRInterface & r
Definition: Object.C:4
#define c(i)
Definition: RSha256.hxx:101
virtual 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
Calls RooPlot* plotOn(RooPlot* frame, const RooLinkedList& cmdList) const ;.
Definition: RooAbsData.cxx:546
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Definition: RooFitResult.h:40
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
Definition: RooFormulaVar.h:29
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
RooGenericPdf is a concrete implementation of a probability density function, which takes a RooArgLis...
Definition: RooGenericPdf.h:25
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1277
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:712
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:294
The Canvas class.
Definition: TCanvas.h:27
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:550
RooConstVar & RooConst(Double_t val)
RooCmdArg Save(Bool_t flag=kTRUE)
const Double_t sigma
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
const char * Title
Definition: TXMLSetup.cxx:67