Loading [MathJax]/extensions/tex2jax.js
Logo ROOT   6.16/01
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
rf111_derivatives.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## 'BASIC FUNCTIONALITY' RooFit tutorial macro #111
5## Numerical 1st, and 3rd order derivatives w.r.t. observables and parameters
6## pdf = gauss(x,m,s)
7##
8## \macro_code
9##
10## \date February 2018
11## \author Clemens Lange
12## \author Wouter Verkerke (C version)
13
14import ROOT
15
16# Set up model
17# ---------------------
18
19# Declare variables x,mean, with associated name, title, value and allowed
20# range
21x = ROOT.RooRealVar("x", "x", -10, 10)
22mean = ROOT.RooRealVar("mean", "mean of gaussian", 1, -10, 10)
23sigma = ROOT.RooRealVar("sigma", "width of gaussian", 1, 0.1, 10)
24
25# Build gaussian p.d.f in terms of x, and sigma
26gauss = ROOT.RooGaussian("gauss", "gaussian PDF", x, mean, sigma)
27
28# Create and plot derivatives w.r.t. x
29# ----------------------------------------------------------------------
30
31# Derivative of normalized gauss(x) w.r.t. observable x
32dgdx = gauss.derivative(x, 1)
33
34# Second and third derivative of normalized gauss(x) w.r.t. observable x
35d2gdx2 = gauss.derivative(x, 2)
36d3gdx3 = gauss.derivative(x, 3)
37
38# Construct plot frame in 'x'
39xframe = x.frame(ROOT.RooFit.Title("d(Gauss)/dx"))
40
41# Plot gauss in frame (i.e. in x)
42gauss.plotOn(xframe)
43
44# Plot derivatives in same frame
45dgdx.plotOn(xframe, ROOT.RooFit.LineColor(ROOT.kMagenta))
46d2gdx2.plotOn(xframe, ROOT.RooFit.LineColor(ROOT.kRed))
47d3gdx3.plotOn(xframe, ROOT.RooFit.LineColor(ROOT.kOrange))
48
49# Create and plot derivatives w.r.t. sigma
50# ------------------------------------------------------------------------------
51
52# Derivative of normalized gauss(x) w.r.t. parameter sigma
53dgds = gauss.derivative(sigma, 1)
54
55# Second and third derivative of normalized gauss(x) w.r.t. parameter sigma
56d2gds2 = gauss.derivative(sigma, 2)
57d3gds3 = gauss.derivative(sigma, 3)
58
59# Construct plot frame in 'sigma'
60sframe = sigma.frame(ROOT.RooFit.Title(
61 "d(Gauss)/d(sigma)"), ROOT.RooFit.Range(0., 2.))
62
63# Plot gauss in frame (i.e. in x)
64gauss.plotOn(sframe)
65
66# Plot derivatives in same frame
67dgds.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kMagenta))
68d2gds2.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kRed))
69d3gds3.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kOrange))
70
71# Draw all frames on a canvas
72c = ROOT.TCanvas("rf111_derivatives", "rf111_derivatives", 800, 400)
73c.Divide(2)
74c.cd(1)
75ROOT.gPad.SetLeftMargin(0.15)
76xframe.GetYaxis().SetTitleOffset(1.6)
77xframe.Draw()
78c.cd(2)
79ROOT.gPad.SetLeftMargin(0.15)
80sframe.GetYaxis().SetTitleOffset(1.6)
81sframe.Draw()
82
83c.SaveAs("rf111_derivatives.png")