This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rf101_basics.C
86 lines (64 loc) · 2.38 KB
/
rf101_basics.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//////////////////////////////////////////////////////////////////////////
//
// 'BASIC FUNCTIONALITY' RooFit tutorial macro #101
//
// Fitting, plotting, toy data generation on one-dimensional p.d.f
//
// pdf = gauss(x,m,s)
//
//
// 07/2008 - Wouter Verkerke
//
/////////////////////////////////////////////////////////////////////////
#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "TCanvas.h"
#include "RooPlot.h"
#include "TAxis.h"
using namespace RooFit ;
void rf101_basics()
{
// S e t u p m o d e l
// ---------------------
// Declare variables x,mean,sigma with associated name, title, initial value and allowed range
RooRealVar x("x","x",-10,10) ;
RooRealVar mean("mean","mean of gaussian",1,-10,10) ;
RooRealVar sigma("sigma","width of gaussian",1,0.1,10) ;
// Build gaussian p.d.f in terms of x,mean and sigma
RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ;
// Construct plot frame in 'x'
RooPlot* xframe = x.frame(Title("Gaussian p.d.f.")) ;
// 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
// ---------------------------------------------------------------------------
// Plot gauss in frame (i.e. in x)
gauss.plotOn(xframe) ;
// Change the value of sigma to 3
sigma.setVal(3) ;
// Plot gauss in frame (i.e. in x) and draw frame on canvas
gauss.plotOn(xframe,LineColor(kRed)) ;
// G e n e r a t e e v e n t s
// -----------------------------
// Generate a dataset of 1000 events in x from gauss
RooDataSet* data = gauss.generate(x,10000) ;
// Make a second plot frame in x and draw both the
// data and the p.d.f in the frame
RooPlot* xframe2 = x.frame(Title("Gaussian p.d.f. with data")) ;
data->plotOn(xframe2) ;
gauss.plotOn(xframe2) ;
// F i t m o d e l t o d a t a
// -----------------------------
// Fit pdf to data
gauss.fitTo(*data) ;
// Print values of mean and sigma (that now reflect fitted values and errors)
mean.Print() ;
sigma.Print() ;
// Draw all frames on a canvas
TCanvas* c = new TCanvas("rf101_basics","rf101_basics",800,400) ;
c->Divide(2) ;
c->cd(1) ; gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.6) ; xframe->Draw() ;
c->cd(2) ; gPad->SetLeftMargin(0.15) ; xframe2->GetYaxis()->SetTitleOffset(1.6) ; xframe2->Draw() ;
}