Logo 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## pdf = gauss(x,m,s)
7##
8## \macro_code
9##
10## \date February 2018
11## \author Clemens Lange, Wouter Verkerke (C++ version)
12
13import ROOT
14
15# Set up model
16# ---------------------
17
18# Declare variables x,mean, with associated name, title, value and allowed
19# range
20x = ROOT.RooRealVar("x", "x", -10, 10)
21mean = ROOT.RooRealVar("mean", "mean of gaussian", 1, -10, 10)
22sigma = ROOT.RooRealVar("sigma", "width of gaussian", 1, 0.1, 10)
23
24# Build gaussian p.d.f in terms of x, and sigma
25gauss = ROOT.RooGaussian("gauss", "gaussian PDF", x, mean, sigma)
26
27# Create and plot derivatives w.r.t. x
28# ----------------------------------------------------------------------
29
30# Derivative of normalized gauss(x) w.r.t. observable x
31dgdx = gauss.derivative(x, 1)
32
33# Second and third derivative of normalized gauss(x) w.r.t. observable x
34d2gdx2 = gauss.derivative(x, 2)
35d3gdx3 = gauss.derivative(x, 3)
36
37# Construct plot frame in 'x'
38xframe = x.frame(ROOT.RooFit.Title("d(Gauss)/dx"))
39
40# Plot gauss in frame (i.e. in x)
41gauss.plotOn(xframe)
42
43# Plot derivatives in same frame
44dgdx.plotOn(xframe, ROOT.RooFit.LineColor(ROOT.kMagenta))
45d2gdx2.plotOn(xframe, ROOT.RooFit.LineColor(ROOT.kRed))
46d3gdx3.plotOn(xframe, ROOT.RooFit.LineColor(ROOT.kOrange))
47
48# Create and plot derivatives w.r.t. sigma
49# ------------------------------------------------------------------------------
50
51# Derivative of normalized gauss(x) w.r.t. parameter sigma
52dgds = gauss.derivative(sigma, 1)
53
54# Second and third derivative of normalized gauss(x) w.r.t. parameter sigma
55d2gds2 = gauss.derivative(sigma, 2)
56d3gds3 = gauss.derivative(sigma, 3)
57
58# Construct plot frame in 'sigma'
59sframe = sigma.frame(ROOT.RooFit.Title(
60 "d(Gauss)/d(sigma)"), ROOT.RooFit.Range(0., 2.))
61
62# Plot gauss in frame (i.e. in x)
63gauss.plotOn(sframe)
64
65# Plot derivatives in same frame
66dgds.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kMagenta))
67d2gds2.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kRed))
68d3gds3.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kOrange))
69
70# Draw all frames on a canvas
71c = ROOT.TCanvas("rf111_derivatives", "rf111_derivatives", 800, 400)
72c.Divide(2)
73c.cd(1)
74ROOT.gPad.SetLeftMargin(0.15)
75xframe.GetYaxis().SetTitleOffset(1.6)
76xframe.Draw()
77c.cd(2)
78ROOT.gPad.SetLeftMargin(0.15)
79sframe.GetYaxis().SetTitleOffset(1.6)
80sframe.Draw()
81
82c.SaveAs("rf111_derivatives.png")