Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_tf1.pyzdoc
Go to the documentation of this file.
1/**
2\class TF1
3\brief \parblock \endparblock
4\htmlonly
5<div class="pyrootbox">
6\endhtmlonly
7## PyROOT
8
9The TF1 class has several additions for its use from Python, which are also
10available in its subclasses TF2 and TF3.
11
12First, TF1 instance can be initialized with user-defined Python functions. Given a generic Python callable,
13the following can performed:
14
15\code{.py}
16def func(x: numpy.ndarray, pars: numpy.ndarray) -> float:
17 return pars[0] * x[0] * x[0] + x[1] * pars[0]
18
19my_func = ROOT.TF1("my_func", func, -10, 10, npar=2, ndim=2)
20\endcode
21
22Second, after performing the initialisation with a Python functor, the TF1 instance can be evaluated using the Pythonized
23`TF1::EvalPar` function. The pythonization allows passing in 1D(single set of x variables) or 2D(a dataset) NumPy arrays.
24
25The following example shows how we can create a TF1 instance with a Python function and evaluate it on a dataset:
26
27\code{.py}
28import ROOT
29import math
30import numpy as np
31
32def pyf_tf1_coulomb(x, p):
33 return p[1] * x[0] * x[1] / (p[0]**2) * math.exp(-p[2] / p[0])
34
35rtf1_coulomb = ROOT.TF1("my_func", pyf_tf1_coulomb, -10, 10, ndims = 2, npars = 3)
36
37# x dataset: 5 pairs of particle charges
38x = np.array([
39 [1.0, 10, 2.0],
40 [1.5, 10, 2.5],
41 [2.0, 10, 3.0],
42 [2.5, 10, 3.5],
43 [3.0, 10, 4.0]
44])
45
46params = np.array([
47 [1.0], # Distance between charges r
48 [8.99e9], # Coulomb constant k (in N·m²/C²)
49 [0.1] # Additional factor for modulation
50])
51
52# Slice to avoid the dummy column of 10's
53res = rtf1_coulomb.EvalPar(x[:, ::2], params)
54
55\endcode
56
57\htmlonly
58</div>
59\endhtmlonly
60*/