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## ```
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(ROOT.RooFit.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, ROOT.RooFit.LineColor(ROOT.kMagenta))
47d2gdx2.plotOn(xframe, ROOT.RooFit.LineColor(ROOT.kRed))
48d3gdx3.plotOn(xframe, ROOT.RooFit.LineColor(ROOT.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(ROOT.RooFit.Title(
62 "d(Gauss)/d(sigma)"), ROOT.RooFit.Range(0., 2.))
63
64# Plot gauss in frame (i.e. in x)
65gauss.plotOn(sframe)
66
67# Plot derivatives in same frame
68dgds.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kMagenta))
69d2gds2.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kRed))
70d3gds3.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kOrange))
71
72# Draw all frames on a canvas
73c = ROOT.TCanvas("rf111_derivatives", "rf111_derivatives", 800, 400)
74c.Divide(2)
75c.cd(1)
76ROOT.gPad.SetLeftMargin(0.15)
77xframe.GetYaxis().SetTitleOffset(1.6)
78xframe.Draw()
79c.cd(2)
80ROOT.gPad.SetLeftMargin(0.15)
81sframe.GetYaxis().SetTitleOffset(1.6)
82sframe.Draw()
83
84c.SaveAs("rf111_derivatives.png")