Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
gr010_approx_smooth.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_graphs
3## \notebook -js
4## \preview Create a TGraphSmooth and show the usage of the interpolation function Approx.
5##
6## See the [TGraphSmooth documentation](https://root.cern/doc/master/classTGraphSmooth.html)
7##
8## \macro_image
9## \macro_code
10## \author Christian Stratowa, Jamie Gooding
11
12from ctypes import c_double
13
14import numpy as np
15import ROOT
16
17# vC1 = ROOT.TCanvas()
18# # grxy = ROOT.TGraph()
19# grin = ROOT.TGraph()
20# grout = ROOT.TGraph()
21
22
23def DrawSmooth(pad, title, xt, yt):
24 vC1.cd(pad)
25 vFrame = ROOT.gPad.DrawFrame(0, 0, 15, 150)
26 vFrame.SetTitle(title)
33 grxy.Draw("P")
37 grin.Draw("P")
38 grout.DrawClone("LP")
39
40
41# Test data (square)
42n = 11
43x = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 6.0, 6.0, 8.0, 9.0, 10.0])
44y = np.array([1.0, 4.0, 9.0, 16.0, 25.0, 25.0, 36.0, 49.0, 64.0, 81.0, 100.0])
45
46grxy = ROOT.TGraph(n, x, y)
47
48# X values, for which y values should be interpolated
49nout = 14
50xout = np.array([1.2, 1.7, 2.5, 3.2, 4.4, 5.2, 5.7, 6.5, 7.6, 8.3, 9.7, 10.4, 11.3, 13])
51
52# Create Canvas
53vC1 = ROOT.TCanvas("vC1", "square", 200, 10, 700, 700)
54vC1.Divide(2, 2)
55
56# Initialize graph with data
57grin = ROOT.TGraph(n, x, y)
58# Interpolate at equidistant points (use mean for tied x-values)
59gs = ROOT.TGraphSmooth("normal")
60grout = gs.Approx(grin, "linear")
61DrawSmooth(1, "Approx: ties = mean", "X-axis", "Y-axis")
62
63# Re-initialize graph with data
64# (since graph points were set to unique vales)
65grin = ROOT.TGraph(n, x, y)
66# Interpolate at given points xout
67grout = gs.Approx(grin, "linear", 14, xout, 0, 130)
68DrawSmooth(2, "Approx: ties = mean", "", "")
69
70# Print output variables for given values xout
71vNout = grout.GetN()
72vXout = c_double()
73vYout = c_double()
74for k in range(vNout):
75 grout.GetPoint(k, vXout, vYout)
76 print(f"k= {k} vXout[k]= {vXout.value} vYout[k]= {vYout.value}")
77
78# Re-initialize graph with data
79grin = ROOT.TGraph(n, x, y)
80# Interpolate at equidistant points (use min for tied x-values)
81# _grout = gs.Approx(grin,"linear", 50, 0, 0, 0, 1, 0, "min")_
82grout = gs.Approx(grin, "constant", 50, 0, 0, 0, 1, 0.5, "min")
83DrawSmooth(3, "Approx: ties = min", "", "")
84
85# Re-initialize graph with data
86grin = ROOT.TGraph(n, x, y)
87# Interpolate at equidistant points (use max for tied x-values)
88grout = gs.Approx(grin, "linear", 14, xout, 0, 0, 2, 0, "max")
89DrawSmooth(4, "Approx: ties = max", "", "")
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.