Logo ROOT   6.14/05
Reference Guide
rf101_basics.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// 'BASIC FUNCTIONALITY' RooFit tutorial macro #101
5 ///
6 /// Fitting, plotting, toy data generation on one-dimensional p.d.f
7 ///
8 /// pdf = gauss(x,m,s)
9 ///
10 /// \macro_image
11 /// \macro_output
12 /// \macro_code
13 /// \author 07/2008 - Wouter Verkerke
14 
15 
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooGaussian.h"
19 #include "TCanvas.h"
20 #include "RooPlot.h"
21 #include "TAxis.h"
22 using namespace RooFit ;
23 
24 
25 void rf101_basics()
26 {
27  // S e t u p m o d e l
28  // ---------------------
29 
30  // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
31  RooRealVar x("x","x",-10,10) ;
32  RooRealVar mean("mean","mean of gaussian",1,-10,10) ;
33  RooRealVar sigma("sigma","width of gaussian",1,0.1,10) ;
34 
35  // Build gaussian p.d.f in terms of x,mean and sigma
36  RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ;
37 
38  // Construct plot frame in 'x'
39  RooPlot* xframe = x.frame(Title("Gaussian p.d.f.")) ;
40 
41 
42  // 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
43  // ---------------------------------------------------------------------------
44 
45  // Plot gauss in frame (i.e. in x)
46  gauss.plotOn(xframe) ;
47 
48  // Change the value of sigma to 3
49  sigma.setVal(3) ;
50 
51  // Plot gauss in frame (i.e. in x) and draw frame on canvas
52  gauss.plotOn(xframe,LineColor(kRed)) ;
53 
54 
55  // G e n e r a t e e v e n t s
56  // -----------------------------
57 
58  // Generate a dataset of 1000 events in x from gauss
59  RooDataSet* data = gauss.generate(x,10000) ;
60 
61  // Make a second plot frame in x and draw both the
62  // data and the p.d.f in the frame
63  RooPlot* xframe2 = x.frame(Title("Gaussian p.d.f. with data")) ;
64  data->plotOn(xframe2) ;
65  gauss.plotOn(xframe2) ;
66 
67 
68  // F i t m o d e l t o d a t a
69  // -----------------------------
70 
71  // Fit pdf to data
72  gauss.fitTo(*data) ;
73 
74  // Print values of mean and sigma (that now reflect fitted values and errors)
75  mean.Print() ;
76  sigma.Print() ;
77 
78  // Draw all frames on a canvas
79  TCanvas* c = new TCanvas("rf101_basics","rf101_basics",800,400) ;
80  c->Divide(2) ;
81  c->cd(1) ; gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.6) ; xframe->Draw() ;
82  c->cd(2) ; gPad->SetLeftMargin(0.15) ; xframe2->GetYaxis()->SetTitleOffset(1.6) ; xframe2->Draw() ;
83 
84 
85 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:294
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1117
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
Plot dataset on specified frame.
Definition: RooAbsData.cxx:568
RooCmdArg LineColor(Color_t color)
Definition: Rtypes.h:59
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
RooCmdArg Title(const char *name)
Double_t x[n]
Definition: legend1.C:17
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
const Double_t sigma
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
The Canvas class.
Definition: TCanvas.h:31
static constexpr double gauss
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1162
#define gPad
Definition: TVirtualPad.h:285
#define c(i)
Definition: RSha256.hxx:101
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:558