ROOT   Reference Guide
Searching...
No Matches
rf111_derivatives.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Basic functionality: numerical 1st,2nd and 3rd order derivatives w.r.t. observables and parameters
5///
6/// 
7/// pdf = gauss(x,m,s)
8/// 
9///
10/// \macro_image
11/// \macro_code
12/// \macro_output
13///
14/// \date July 2008
15/// \author Wouter Verkerke
16
17#include "RooRealVar.h"
18#include "RooDataSet.h"
19#include "RooGaussian.h"
20#include "TCanvas.h"
21#include "TAxis.h"
22#include "RooPlot.h"
23using namespace RooFit;
24
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 pdf in terms of x,mean and sigma
36 RooGaussian gauss("gauss", "gaussian PDF", x, mean, sigma);
37
38 // C r e a t e a n d p l o t d e r i v a t i v e s w . r . t . x
39 // ----------------------------------------------------------------------
40
41 // Derivative of normalized gauss(x) w.r.t. observable x
42 RooAbsReal *dgdx = gauss.derivative(x, 1);
43
44 // Second and third derivative of normalized gauss(x) w.r.t. observable x
45 RooAbsReal *d2gdx2 = gauss.derivative(x, 2);
46 RooAbsReal *d3gdx3 = gauss.derivative(x, 3);
47
48 // Construct plot frame in 'x'
49 RooPlot *xframe = x.frame(Title("d(Gauss)/dx"));
50
51 // Plot gauss in frame (i.e. in x)
52 gauss.plotOn(xframe);
53
54 // Plot derivatives in same frame
55 dgdx->plotOn(xframe, LineColor(kMagenta));
56 d2gdx2->plotOn(xframe, LineColor(kRed));
57 d3gdx3->plotOn(xframe, LineColor(kOrange));
58
59 // C r e a t e a n d p l o t d e r i v a t i v e s w . r . t . s i g m a
60 // ------------------------------------------------------------------------------
61
62 // Derivative of normalized gauss(x) w.r.t. parameter sigma
63 RooAbsReal *dgds = gauss.derivative(sigma, 1);
64
65 // Second and third derivative of normalized gauss(x) w.r.t. parameter sigma
66 RooAbsReal *d2gds2 = gauss.derivative(sigma, 2);
67 RooAbsReal *d3gds3 = gauss.derivative(sigma, 3);
68
69 // Construct plot frame in 'sigma'
70 RooPlot *sframe = sigma.frame(Title("d(Gauss)/d(sigma)"), Range(0., 2.));
71
72 // Plot gauss in frame (i.e. in x)
73 gauss.plotOn(sframe);
74
75 // Plot derivatives in same frame
76 dgds->plotOn(sframe, LineColor(kMagenta));
77 d2gds2->plotOn(sframe, LineColor(kRed));
78 d3gds3->plotOn(sframe, LineColor(kOrange));
79
80 // Draw all frames on a canvas
81 TCanvas *c = new TCanvas("rf111_derivatives", "rf111_derivatives", 800, 400);
82 c->Divide(2);
83 c->cd(1);
85 xframe->GetYaxis()->SetTitleOffset(1.6);
86 xframe->Draw();
87 c->cd(2);
89 sframe->GetYaxis()->SetTitleOffset(1.6);
90 sframe->Draw();
91}
#define c(i)
Definition RSha256.hxx:101
@ kRed
Definition Rtypes.h:66
@ kOrange
Definition Rtypes.h:67
@ kMagenta
Definition Rtypes.h:66
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
RooDerivative * derivative(RooRealVar &obs, Int_t order=1, double eps=0.001)
Return function representing first, second or third order derivative of this function.
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}, const RooCmdArg &arg9={}, const RooCmdArg &arg10={}) const
Plot (project) PDF on specified frame.
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
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 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
Ta Range(0, 0, 1, 1)