ROOT logo
//////////////////////////////////////////////////////////////////////////
//
// 'BASIC FUNCTIONALITY' RooFit tutorial macro #111
// 
// Numerical 1st,2nd and 3rd order derivatives w.r.t. observables and parameters
//
// 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 "TAxis.h"
#include "RooPlot.h"
using namespace RooFit ;


void rf111_derivatives()
{
  // 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) ;  


  // 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 
  // ----------------------------------------------------------------------
  
  // Derivative of normalized gauss(x) w.r.t. observable x
  RooAbsReal* dgdx = gauss.derivative(x,1) ;  
  
  // Second and third derivative of normalized gauss(x) w.r.t. observable x
  RooAbsReal* d2gdx2 = gauss.derivative(x,2) ;  
  RooAbsReal* d3gdx3 = gauss.derivative(x,3) ;  

  // Construct plot frame in 'x'
  RooPlot* xframe = x.frame(Title("d(Gauss)/dx")) ;
  
  // Plot gauss in frame (i.e. in x) 
  gauss.plotOn(xframe) ;

  // Plot derivatives in same frame
  dgdx->plotOn(xframe,LineColor(kMagenta)) ;
  d2gdx2->plotOn(xframe,LineColor(kRed)) ;
  d3gdx3->plotOn(xframe,LineColor(kOrange)) ;


  // 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 
  // ------------------------------------------------------------------------------
  
  // Derivative of normalized gauss(x) w.r.t. parameter sigma
  RooAbsReal* dgds = gauss.derivative(sigma,1) ;  
  
  // Second and third derivative of normalized gauss(x) w.r.t. parameter sigma
  RooAbsReal* d2gds2 = gauss.derivative(sigma,2) ;  
  RooAbsReal* d3gds3 = gauss.derivative(sigma,3) ;  

  // Construct plot frame in 'sigma'
  RooPlot* sframe = sigma.frame(Title("d(Gauss)/d(sigma)"),Range(0.,2.)) ;
  
  // Plot gauss in frame (i.e. in x) 
  gauss.plotOn(sframe) ;

  // Plot derivatives in same frame
  dgds->plotOn(sframe,LineColor(kMagenta)) ;
  d2gds2->plotOn(sframe,LineColor(kRed)) ;
  d3gds3->plotOn(sframe,LineColor(kOrange)) ;



  // Draw all frames on a canvas
  TCanvas* c = new TCanvas("rf111_derivatives","rf111_derivatives",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) ; sframe->GetYaxis()->SetTitleOffset(1.6) ; sframe->Draw() ;
  
 
}
 rf111_derivatives.C:1
 rf111_derivatives.C:2
 rf111_derivatives.C:3
 rf111_derivatives.C:4
 rf111_derivatives.C:5
 rf111_derivatives.C:6
 rf111_derivatives.C:7
 rf111_derivatives.C:8
 rf111_derivatives.C:9
 rf111_derivatives.C:10
 rf111_derivatives.C:11
 rf111_derivatives.C:12
 rf111_derivatives.C:13
 rf111_derivatives.C:14
 rf111_derivatives.C:15
 rf111_derivatives.C:16
 rf111_derivatives.C:17
 rf111_derivatives.C:18
 rf111_derivatives.C:19
 rf111_derivatives.C:20
 rf111_derivatives.C:21
 rf111_derivatives.C:22
 rf111_derivatives.C:23
 rf111_derivatives.C:24
 rf111_derivatives.C:25
 rf111_derivatives.C:26
 rf111_derivatives.C:27
 rf111_derivatives.C:28
 rf111_derivatives.C:29
 rf111_derivatives.C:30
 rf111_derivatives.C:31
 rf111_derivatives.C:32
 rf111_derivatives.C:33
 rf111_derivatives.C:34
 rf111_derivatives.C:35
 rf111_derivatives.C:36
 rf111_derivatives.C:37
 rf111_derivatives.C:38
 rf111_derivatives.C:39
 rf111_derivatives.C:40
 rf111_derivatives.C:41
 rf111_derivatives.C:42
 rf111_derivatives.C:43
 rf111_derivatives.C:44
 rf111_derivatives.C:45
 rf111_derivatives.C:46
 rf111_derivatives.C:47
 rf111_derivatives.C:48
 rf111_derivatives.C:49
 rf111_derivatives.C:50
 rf111_derivatives.C:51
 rf111_derivatives.C:52
 rf111_derivatives.C:53
 rf111_derivatives.C:54
 rf111_derivatives.C:55
 rf111_derivatives.C:56
 rf111_derivatives.C:57
 rf111_derivatives.C:58
 rf111_derivatives.C:59
 rf111_derivatives.C:60
 rf111_derivatives.C:61
 rf111_derivatives.C:62
 rf111_derivatives.C:63
 rf111_derivatives.C:64
 rf111_derivatives.C:65
 rf111_derivatives.C:66
 rf111_derivatives.C:67
 rf111_derivatives.C:68
 rf111_derivatives.C:69
 rf111_derivatives.C:70
 rf111_derivatives.C:71
 rf111_derivatives.C:72
 rf111_derivatives.C:73
 rf111_derivatives.C:74
 rf111_derivatives.C:75
 rf111_derivatives.C:76
 rf111_derivatives.C:77
 rf111_derivatives.C:78
 rf111_derivatives.C:79
 rf111_derivatives.C:80
 rf111_derivatives.C:81
 rf111_derivatives.C:82
 rf111_derivatives.C:83
 rf111_derivatives.C:84
 rf111_derivatives.C:85
 rf111_derivatives.C:86
 rf111_derivatives.C:87
 rf111_derivatives.C:88
 rf111_derivatives.C:89
 rf111_derivatives.C:90
 rf111_derivatives.C:91
 rf111_derivatives.C:92
 rf111_derivatives.C:93