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