Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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_code
8/// \macro_output
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
22using namespace RooFit;
23
25{
26 // ----------------------------------------------------
27 // G e n e r i c i n t e r p r e t e d p . d . f .
28 // ====================================================
29
30 // Declare observable x
31 RooRealVar x("x", "x", -20, 20);
32
33 // 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
34 // -------------------------------------------------------------------------------------------------
35
36 // To construct a proper pdf, the formula expression is explicitly normalized internally by dividing
37 // it by a numeric integral of the expression over x in the range [-20,20]
38 //
39 RooRealVar alpha("alpha", "alpha", 5, 0.1, 10);
40 RooGenericPdf genpdf("genpdf", "genpdf", "(1+0.1*abs(x)+sin(sqrt(abs(x*alpha+0.1))))", RooArgSet(x, alpha));
41
42 // 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
43 // ---------------------------------------------------------------
44
45 // Generate a toy dataset from the interpreted pdf
46 std::unique_ptr<RooDataSet> data{genpdf.generate(x, 10000)};
47
48 // Fit the interpreted pdf to the generated data
49 genpdf.fitTo(*data, PrintLevel(-1));
50
51 // Make a plot of the data and the pdf overlaid
52 RooPlot *xframe = x.frame(Title("Interpreted expression pdf"));
53 data->plotOn(xframe);
54 genpdf.plotOn(xframe);
55
56 // -----------------------------------------------------------------------------------------------------------
57 // 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
58 // ==========================================================================================================
59 // Make a gauss(x,sqrt(mean2),sigma) from a standard RooGaussian
60
61 // 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
62 // ------------------------------------------------------------------------------------------------------------
63
64 // Construct parameter mean2 and sigma
65 RooRealVar mean2("mean2", "mean^2", 10, 0, 200);
66 RooRealVar sigma("sigma", "sigma", 3, 0.1, 10);
67
68 // Construct interpreted function mean = sqrt(mean^2)
69 RooFormulaVar mean("mean", "mean", "sqrt(mean2)", mean2);
70
71 // Construct a gaussian g2(x,sqrt(mean2),sigma) ;
72 RooGaussian g2("g2", "h2", x, mean, sigma);
73
74 // G e n e r a t e t o y d a t a
75 // ---------------------------------
76
77 // Construct a separate gaussian g1(x,10,3) to generate a toy Gaussian dataset with mean 10 and width 3
78 RooGaussian g1("g1", "g1", x, 10.0, 3.0);
79 std::unique_ptr<RooDataSet> data2{g1.generate(x, 1000)};
80
81 // 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
82 // -------------------------------------------------------------------
83
84 // Fit g2 to data from g1
85 std::unique_ptr<RooFitResult> fitResult{g2.fitTo(*data2, Save(), PrintLevel(-1))};
86 fitResult->Print();
87
88 // Plot data on frame and overlay projection of g2
89 RooPlot *xframe2 = x.frame(Title("Tailored Gaussian pdf"));
90 data2->plotOn(xframe2);
91 g2.plotOn(xframe2);
92
93 // Draw all frames on a canvas
94 TCanvas *c = new TCanvas("rf103_interprfuncs", "rf103_interprfuncs", 800, 400);
95 c->Divide(2);
96 c->cd(1);
97 gPad->SetLeftMargin(0.15);
98 xframe->GetYaxis()->SetTitleOffset(1.4);
99 xframe->Draw();
100 c->cd(2);
101 gPad->SetLeftMargin(0.15);
102 xframe2->GetYaxis()->SetTitleOffset(1.4);
103 xframe2->Draw();
104}
#define c(i)
Definition RSha256.hxx:101
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gPad
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Implementation of a probability density function that takes a RooArgList of servers and a C++ express...
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
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:237
TAxis * GetYaxis() const
Definition RooPlot.cxx:1276
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:649
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 Save(bool flag=true)
RooCmdArg PrintLevel(Int_t code)
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