Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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_code
10/// \macro_output
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 std::unique_ptr<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, PrintLevel(-1));
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); c->Divide(2);
75 c->cd(1);
76 gPad->SetLeftMargin(0.15);
77 xframe->GetYaxis()->SetTitleOffset(1.6);
78 xframe->Draw();
79 c->cd(2);
80 gPad->SetLeftMargin(0.15);
81 xframe2->GetYaxis()->SetTitleOffset(1.6);
82 xframe2->Draw();
83}
#define c(i)
Definition RSha256.hxx:101
@ kRed
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gPad
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:45
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:225
TAxis * GetYaxis() const
Definition RooPlot.cxx:1264
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:637
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
The Canvas class.
Definition TCanvas.h:23
RooCmdArg PrintLevel(Int_t code)
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...
Definition JSONIO.h:26
const char * Title
Definition TXMLSetup.cxx:68