Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
gr011_graph2d_errorsfit.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_graphs
3## \notebook
4## \preview Create, draw and fit a TGraph2DErrors. See the [TGraph2DErrors documentation](https://root.cern/doc/master/classTGraph2DErrors.html)
5##
6## \macro_image
7## \macro_code
8## \author Olivier Couet, Jamie Gooding
9
10from ctypes import c_double
11
12import ROOT
13
14c1 = ROOT.TCanvas("c1")
15
16e = 0.3
17nd = 500
18
19# To generate some random data to put into the graph
20r = ROOT.TRandom()
21f2 = ROOT.TF2("f2", "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200", -6, 6, -6, 6)
23
24dte = ROOT.TGraph2DErrors(nd)
25
26# Fill the 2D graph. It was created only specifying the number of points, so all
27# elements are empty. We now "fill" the values and errors with SetPoint and SetPointError.
28# Note that the first point has index zero
29x = c_double()
30y = c_double()
31zmax = 0
32for i in range(nd):
33 f2.GetRandom2(x, y)
34 rnd = r.Uniform(-e, e) # Generate a random number in [-e,e]
35 z = f2.Eval(x, y) * (1 + rnd)
36 if z > zmax:
37 zmax = z
38 dte.SetPoint(i, x, y, z)
39 ex = 0.05 * r.Rndm()
40 ey = 0.05 * r.Rndm()
41 ez = abs(z * rnd)
42 dte.SetPointError(i, ex, ey, ez)
43
44# If the fit is not needed, just draw dte here and skip the lines below
45# dte.Draw("A p0")
46
47# To do the fit we use a function, in this example the same f2 from above
48f2.SetParameters(0.5, 1.5)
49dte.Fit(f2)
50fit2 = dte.FindObject("f2")
51fit2.SetTitle("Minuit fit result on the Graph2DErrors points")
56fit2.Draw("surf1")
57dte.Draw("same p0")
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.