Logo ROOT   6.12/07
Reference Guide
rf111_derivatives.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// 'BASIC FUNCTIONALITY' RooFit tutorial macro #111
5 ///
6 /// Numerical 1st,2nd and 3rd order derivatives w.r.t. observables and parameters
7 ///
8 /// pdf = gauss(x,m,s)
9 ///
10 /// \macro_image
11 /// \macro_output
12 /// \macro_code
13 /// \author 07/2008 - Wouter Verkerke
14 
15 
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooGaussian.h"
19 #include "TCanvas.h"
20 #include "TAxis.h"
21 #include "RooPlot.h"
22 using namespace RooFit ;
23 
24 
25 void rf111_derivatives()
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 p.d.f in terms of x,mean and sigma
36  RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ;
37 
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 
61  // 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
62  // ------------------------------------------------------------------------------
63 
64  // Derivative of normalized gauss(x) w.r.t. parameter sigma
65  RooAbsReal* dgds = gauss.derivative(sigma,1) ;
66 
67  // Second and third derivative of normalized gauss(x) w.r.t. parameter sigma
68  RooAbsReal* d2gds2 = gauss.derivative(sigma,2) ;
69  RooAbsReal* d3gds3 = gauss.derivative(sigma,3) ;
70 
71  // Construct plot frame in 'sigma'
72  RooPlot* sframe = sigma.frame(Title("d(Gauss)/d(sigma)"),Range(0.,2.)) ;
73 
74  // Plot gauss in frame (i.e. in x)
75  gauss.plotOn(sframe) ;
76 
77  // Plot derivatives in same frame
78  dgds->plotOn(sframe,LineColor(kMagenta)) ;
79  d2gds2->plotOn(sframe,LineColor(kRed)) ;
80  d3gds3->plotOn(sframe,LineColor(kOrange)) ;
81 
82 
83 
84  // Draw all frames on a canvas
85  TCanvas* c = new TCanvas("rf111_derivatives","rf111_derivatives",800,400) ;
86  c->Divide(2) ;
87  c->cd(1) ; gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.6) ; xframe->Draw() ;
88  c->cd(2) ; gPad->SetLeftMargin(0.15) ; sframe->GetYaxis()->SetTitleOffset(1.6) ; sframe->Draw() ;
89 
90 
91 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:294
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.
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1118
RooCmdArg LineColor(Color_t color)
Definition: Rtypes.h:59
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
RooCmdArg Title(const char *name)
RooCmdArg Range(const char *rangeName, Bool_t adjustNorm=kTRUE)
Double_t x[n]
Definition: legend1.C:17
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
const Double_t sigma
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
The Canvas class.
Definition: TCanvas.h:31
static constexpr double gauss
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1153
#define gPad
Definition: TVirtualPad.h:285
Definition: Rtypes.h:60
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559