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