ROOT
v6-34
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
9
The TF1 class has several additions for its use from Python, which are also
10
available in its subclasses TF2 and TF3.
11
12
First, TF1 instance can be initialized with user-defined Python functions. Given a generic Python callable,
13
the following can performed:
14
15
\code{.py}
16
def func(x: numpy.ndarray, pars: numpy.ndarray) -> float:
17
return pars[0] * x[0] * x[0] + x[1] * pars[0]
18
19
my_func = ROOT.TF1("my_func", func, -10, 10, npar=2, ndim=2)
20
\endcode
21
22
Second, 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
25
The following example shows how we can create a TF1 instance with a Python function and evaluate it on a dataset:
26
27
\code{.py}
28
import ROOT
29
import math
30
import numpy as np
31
32
def pyf_tf1_coulomb(x, p):
33
return p[1] * x[0] * x[1] / (p[0]**2) * math.exp(-p[2] / p[0])
34
35
rtf1_coulomb = ROOT.TF1("my_func", pyf_tf1_coulomb, -10, 10, ndims = 2, npars = 3)
36
37
# x dataset: 5 pairs of particle charges
38
x = 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
46
params = 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
53
res = rtf1_coulomb.EvalPar(x[:, ::2], params)
54
55
\endcode
56
57
\htmlonly
58
</div>
59
\endhtmlonly
60
*/
v634_TMP
pyzdoc
_tf1.pyzdoc
ROOT v6-34 - Reference Guide Generated on Sun Dec 15 2024 03:24:28 (GVA Time) using Doxygen 1.9.8