ROOT   Reference Guide
rf111_derivatives.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Basic functionality: numerical 1st, and 3rd order derivatives w.r.t. observables and parameters
5##
6## 
7## pdf = gauss(x,m,s)
8## 
9##
10## \macro_code
11##
12## \date February 2018
13## \authors Clemens Lange, Wouter Verkerke (C++ version)
14
15import ROOT
16
17# Set up model
18# ---------------------
19
20# Declare variables x,mean, with associated name, title, value and allowed
21# range
22x = ROOT.RooRealVar("x", "x", -10, 10)
23mean = ROOT.RooRealVar("mean", "mean of gaussian", 1, -10, 10)
24sigma = ROOT.RooRealVar("sigma", "width of gaussian", 1, 0.1, 10)
25
26# Build gaussian pdf in terms of x, and sigma
27gauss = ROOT.RooGaussian("gauss", "gaussian PDF", x, mean, sigma)
28
29# Create and plot derivatives w.r.t. x
30# ----------------------------------------------------------------------
31
32# Derivative of normalized gauss(x) w.r.t. observable x
33dgdx = gauss.derivative(x, 1)
34
35# Second and third derivative of normalized gauss(x) w.r.t. observable x
36d2gdx2 = gauss.derivative(x, 2)
37d3gdx3 = gauss.derivative(x, 3)
38
39# Construct plot frame in 'x'
40xframe = x.frame(Title="d(Gauss)/dx")
41
42# Plot gauss in frame (i.e. in x)
43gauss.plotOn(xframe)
44
45# Plot derivatives in same frame
46dgdx.plotOn(xframe, LineColor="m")
47d2gdx2.plotOn(xframe, LineColor="r")
48d3gdx3.plotOn(xframe, LineColor="kOrange")
49
50# Create and plot derivatives w.r.t. sigma
51# ------------------------------------------------------------------------------
52
53# Derivative of normalized gauss(x) w.r.t. parameter sigma
54dgds = gauss.derivative(sigma, 1)
55
56# Second and third derivative of normalized gauss(x) w.r.t. parameter sigma
57d2gds2 = gauss.derivative(sigma, 2)
58d3gds3 = gauss.derivative(sigma, 3)
59
60# Construct plot frame in 'sigma'
61sframe = sigma.frame(Title="d(Gauss)/d(sigma)", Range=(0.0, 2.0))
62
63# Plot gauss in frame (i.e. in x)
64gauss.plotOn(sframe)
65
66# Plot derivatives in same frame
67dgds.plotOn(sframe, LineColor="m")
68d2gds2.plotOn(sframe, LineColor="r")
69d3gds3.plotOn(sframe, LineColor="kOrange")
70
71# Draw all frames on a canvas
72c = ROOT.TCanvas("rf111_derivatives", "rf111_derivatives", 800, 400)
73c.Divide(2)
74c.cd(1)