Logo ROOT  
Reference Guide
rf111_derivatives.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 ///
5 /// Basic functionality: numerical 1st,2nd and 3rd order derivatives w.r.t. observables and parameters
6 ///
7 /// ```
8 /// pdf = gauss(x,m,s)
9 /// ```
10 ///
11 /// \macro_image
12 /// \macro_output
13 /// \macro_code
14 ///
15 /// \date 07/2008
16 /// \author Wouter Verkerke
17 
18 #include "RooRealVar.h"
19 #include "RooDataSet.h"
20 #include "RooGaussian.h"
21 #include "TCanvas.h"
22 #include "TAxis.h"
23 #include "RooPlot.h"
24 using namespace RooFit;
25 
26 void rf111_derivatives()
27 {
28  // S e t u p m o d e l
29  // ---------------------
30 
31  // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
32  RooRealVar x("x", "x", -10, 10);
33  RooRealVar mean("mean", "mean of gaussian", 1, -10, 10);
34  RooRealVar sigma("sigma", "width of gaussian", 1, 0.1, 10);
35 
36  // Build gaussian p.d.f in terms of x,mean and sigma
37  RooGaussian gauss("gauss", "gaussian PDF", x, mean, sigma);
38 
39  // 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
40  // ----------------------------------------------------------------------
41 
42  // Derivative of normalized gauss(x) w.r.t. observable x
43  RooAbsReal *dgdx = gauss.derivative(x, 1);
44 
45  // Second and third derivative of normalized gauss(x) w.r.t. observable x
46  RooAbsReal *d2gdx2 = gauss.derivative(x, 2);
47  RooAbsReal *d3gdx3 = gauss.derivative(x, 3);
48 
49  // Construct plot frame in 'x'
50  RooPlot *xframe = x.frame(Title("d(Gauss)/dx"));
51 
52  // Plot gauss in frame (i.e. in x)
53  gauss.plotOn(xframe);
54 
55  // Plot derivatives in same frame
56  dgdx->plotOn(xframe, LineColor(kMagenta));
57  d2gdx2->plotOn(xframe, LineColor(kRed));
58  d3gdx3->plotOn(xframe, LineColor(kOrange));
59 
60  // 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
61  // ------------------------------------------------------------------------------
62 
63  // Derivative of normalized gauss(x) w.r.t. parameter sigma
64  RooAbsReal *dgds = gauss.derivative(sigma, 1);
65 
66  // Second and third derivative of normalized gauss(x) w.r.t. parameter sigma
67  RooAbsReal *d2gds2 = gauss.derivative(sigma, 2);
68  RooAbsReal *d3gds3 = gauss.derivative(sigma, 3);
69 
70  // Construct plot frame in 'sigma'
71  RooPlot *sframe = sigma.frame(Title("d(Gauss)/d(sigma)"), Range(0., 2.));
72 
73  // Plot gauss in frame (i.e. in x)
74  gauss.plotOn(sframe);
75 
76  // Plot derivatives in same frame
77  dgds->plotOn(sframe, LineColor(kMagenta));
78  d2gds2->plotOn(sframe, LineColor(kRed));
79  d3gds3->plotOn(sframe, LineColor(kOrange));
80 
81  // Draw all frames on a canvas
82  TCanvas *c = new TCanvas("rf111_derivatives", "rf111_derivatives", 800, 400);
83  c->Divide(2);
84  c->cd(1);
85  gPad->SetLeftMargin(0.15);
86  xframe->GetYaxis()->SetTitleOffset(1.6);
87  xframe->Draw();
88  c->cd(2);
89  gPad->SetLeftMargin(0.15);
90  sframe->GetYaxis()->SetTitleOffset(1.6);
91  sframe->Draw();
92 }
c
#define c(i)
Definition: RSha256.hxx:119
RooPlot::Draw
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
RooGaussian.h
kOrange
@ kOrange
Definition: Rtypes.h:67
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
RooAbsReal
Definition: RooAbsReal.h:61
TCanvas.h
RooDataSet.h
kMagenta
@ kMagenta
Definition: Rtypes.h:66
TGeant4Unit::gauss
static constexpr double gauss
Definition: TGeant4SystemOfUnits.h:269
RooFit
Definition: RooCFunction1Binding.h:29
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1256
RooPlot
Definition: RooPlot.h:44
RooRealVar.h
kRed
@ kRed
Definition: Rtypes.h:66
RooAbsReal::plotOn
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg(), const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg(), const RooCmdArg &arg9=RooCmdArg(), const RooCmdArg &arg10=RooCmdArg()) const
Plot (project) PDF on specified frame.
Definition: RooAbsReal.cxx:1714
sigma
const Double_t sigma
Definition: h1analysisProxy.h:11
RooFit::LineColor
RooCmdArg LineColor(Color_t color)
Definition: RooGlobalFunc.cxx:56
TCanvas
Definition: TCanvas.h:23
TAxis.h
RooFit::Range
RooCmdArg Range(const char *rangeName, Bool_t adjustNorm=kTRUE)
Definition: RooGlobalFunc.cxx:52
gPad
#define gPad
Definition: TVirtualPad.h:287
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
Definition: RooRealVar.h:35
rf111_derivatives
Definition: rf111_derivatives.py:1
RooFit::Title
RooCmdArg Title(const char *name)
Definition: RooGlobalFunc.cxx:173