Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 /// pdf = gauss(x,m,s)
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 /// \author 07/2008 - 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 using namespace RooFit;
20 
21 void rf111_derivatives()
22 {
23  // S e t u p m o d e l
24  // ---------------------
25 
26  // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
27  RooRealVar x("x", "x", -10, 10);
28  RooRealVar mean("mean", "mean of gaussian", 1, -10, 10);
29  RooRealVar sigma("sigma", "width of gaussian", 1, 0.1, 10);
30 
31  // Build gaussian p.d.f in terms of x,mean and sigma
32  RooGaussian gauss("gauss", "gaussian PDF", x, mean, sigma);
33 
34  // 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
35  // ----------------------------------------------------------------------
36 
37  // Derivative of normalized gauss(x) w.r.t. observable x
38  RooAbsReal *dgdx = gauss.derivative(x, 1);
39 
40  // Second and third derivative of normalized gauss(x) w.r.t. observable x
41  RooAbsReal *d2gdx2 = gauss.derivative(x, 2);
42  RooAbsReal *d3gdx3 = gauss.derivative(x, 3);
43 
44  // Construct plot frame in 'x'
45  RooPlot *xframe = x.frame(Title("d(Gauss)/dx"));
46 
47  // Plot gauss in frame (i.e. in x)
48  gauss.plotOn(xframe);
49 
50  // Plot derivatives in same frame
51  dgdx->plotOn(xframe, LineColor(kMagenta));
52  d2gdx2->plotOn(xframe, LineColor(kRed));
53  d3gdx3->plotOn(xframe, LineColor(kOrange));
54 
55  // 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
56  // ------------------------------------------------------------------------------
57 
58  // Derivative of normalized gauss(x) w.r.t. parameter sigma
59  RooAbsReal *dgds = gauss.derivative(sigma, 1);
60 
61  // Second and third derivative of normalized gauss(x) w.r.t. parameter sigma
62  RooAbsReal *d2gds2 = gauss.derivative(sigma, 2);
63  RooAbsReal *d3gds3 = gauss.derivative(sigma, 3);
64 
65  // Construct plot frame in 'sigma'
66  RooPlot *sframe = sigma.frame(Title("d(Gauss)/d(sigma)"), Range(0., 2.));
67 
68  // Plot gauss in frame (i.e. in x)
69  gauss.plotOn(sframe);
70 
71  // Plot derivatives in same frame
72  dgds->plotOn(sframe, LineColor(kMagenta));
73  d2gds2->plotOn(sframe, LineColor(kRed));
74  d3gds3->plotOn(sframe, LineColor(kOrange));
75 
76  // Draw all frames on a canvas
77  TCanvas *c = new TCanvas("rf111_derivatives", "rf111_derivatives", 800, 400);
78  c->Divide(2);
79  c->cd(1);
80  gPad->SetLeftMargin(0.15);
81  xframe->GetYaxis()->SetTitleOffset(1.6);
82  xframe->Draw();
83  c->cd(2);
84  gPad->SetLeftMargin(0.15);
85  sframe->GetYaxis()->SetTitleOffset(1.6);
86  sframe->Draw();
87 }