Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
13 import ROOT
14 
15 # Set up model
16 # ---------------------
17 
18 # Declare variables x,mean, with associated name, title, value and allowed
19 # range
20 x = ROOT.RooRealVar("x", "x", -10, 10)
21 mean = ROOT.RooRealVar("mean", "mean of gaussian", 1, -10, 10)
22 sigma = ROOT.RooRealVar("sigma", "width of gaussian", 1, 0.1, 10)
23 
24 # Build gaussian p.d.f in terms of x, and sigma
25 gauss = 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
31 dgdx = gauss.derivative(x, 1)
32 
33 # Second and third derivative of normalized gauss(x) w.r.t. observable x
34 d2gdx2 = gauss.derivative(x, 2)
35 d3gdx3 = gauss.derivative(x, 3)
36 
37 # Construct plot frame in 'x'
38 xframe = x.frame(ROOT.RooFit.Title("d(Gauss)/dx"))
39 
40 # Plot gauss in frame (i.e. in x)
41 gauss.plotOn(xframe)
42 
43 # Plot derivatives in same frame
44 dgdx.plotOn(xframe, ROOT.RooFit.LineColor(ROOT.kMagenta))
45 d2gdx2.plotOn(xframe, ROOT.RooFit.LineColor(ROOT.kRed))
46 d3gdx3.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
52 dgds = gauss.derivative(sigma, 1)
53 
54 # Second and third derivative of normalized gauss(x) w.r.t. parameter sigma
55 d2gds2 = gauss.derivative(sigma, 2)
56 d3gds3 = gauss.derivative(sigma, 3)
57 
58 # Construct plot frame in 'sigma'
59 sframe = 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)
63 gauss.plotOn(sframe)
64 
65 # Plot derivatives in same frame
66 dgds.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kMagenta))
67 d2gds2.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kRed))
68 d3gds3.plotOn(sframe, ROOT.RooFit.LineColor(ROOT.kOrange))
69 
70 # Draw all frames on a canvas
71 c = ROOT.TCanvas("rf111_derivatives", "rf111_derivatives", 800, 400)
72 c.Divide(2)
73 c.cd(1)
74 ROOT.gPad.SetLeftMargin(0.15)
75 xframe.GetYaxis().SetTitleOffset(1.6)
76 xframe.Draw()
77 c.cd(2)
78 ROOT.gPad.SetLeftMargin(0.15)
79 sframe.GetYaxis().SetTitleOffset(1.6)
80 sframe.Draw()
81 
82 c.SaveAs("rf111_derivatives.png")