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