Logo ROOT  
Reference Guide
rf101_basics.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Basic functionality: fitting, plotting, toy data generation on one-dimensional PDFs.
5///
6/// pdf = gauss(x,m,s)
7///
8/// \macro_image
9/// \macro_output
10/// \macro_code
11///
12/// \date July 2008
13/// \author Wouter Verkerke
14
15#include "RooRealVar.h"
16#include "RooDataSet.h"
17#include "RooGaussian.h"
18#include "TCanvas.h"
19#include "RooPlot.h"
20#include "TAxis.h"
21using namespace RooFit;
22
23void rf101_basics()
24{
25 // S e t u p m o d e l
26 // ---------------------
27
28 // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
29 RooRealVar x("x", "x", -10, 10);
30 RooRealVar mean("mean", "mean of gaussian", 1, -10, 10);
31 RooRealVar sigma("sigma", "width of gaussian", 1, 0.1, 10);
32
33 // Build gaussian pdf in terms of x,mean and sigma
34 RooGaussian gauss("gauss", "gaussian PDF", x, mean, sigma);
35
36 // Construct plot frame in 'x'
37 RooPlot *xframe = x.frame(Title("Gaussian pdf."));
38
39 // P l o t m o d e l a n d c h a n g e p a r a m e t e r v a l u e s
40 // ---------------------------------------------------------------------------
41
42 // Plot gauss in frame (i.e. in x)
43 gauss.plotOn(xframe);
44
45 // Change the value of sigma to 3
46 sigma.setVal(3);
47
48 // Plot gauss in frame (i.e. in x) and draw frame on canvas
49 gauss.plotOn(xframe, LineColor(kRed));
50
51 // G e n e r a t e e v e n t s
52 // -----------------------------
53
54 // Generate a dataset of 1000 events in x from gauss
55 RooDataSet *data = gauss.generate(x, 10000);
56
57 // Make a second plot frame in x and draw both the
58 // data and the pdf in the frame
59 RooPlot *xframe2 = x.frame(Title("Gaussian pdf with data"));
60 data->plotOn(xframe2);
61 gauss.plotOn(xframe2);
62
63 // F i t m o d e l t o d a t a
64 // -----------------------------
65
66 // Fit pdf to data
67 gauss.fitTo(*data);
68
69 // Print values of mean and sigma (that now reflect fitted values and errors)
70 mean.Print();
71 sigma.Print();
72
73 // Draw all frames on a canvas
74 TCanvas *c = new TCanvas("rf101_basics", "rf101_basics", 800, 400);
75 c->Divide(2);
76 c->cd(1);
77 gPad->SetLeftMargin(0.15);
78 xframe->GetYaxis()->SetTitleOffset(1.6);
79 xframe->Draw();
80 c->cd(2);
81 gPad->SetLeftMargin(0.15);
82 xframe2->GetYaxis()->SetTitleOffset(1.6);
83 xframe2->Draw();
84}
#define c(i)
Definition: RSha256.hxx:101
@ kRed
Definition: Rtypes.h:66
#define gPad
Definition: TVirtualPad.h:287
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
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
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:1263
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:39
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
The Canvas class.
Definition: TCanvas.h:23
RooCmdArg LineColor(Color_t color)
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...
static constexpr double gauss
const char * Title
Definition: TXMLSetup.cxx:68