3The
TF1 class has several additions
for its use from Python, which are also
4available in its subclasses
TF2 and
TF3.
6First,
TF1 instance can be initialized with user-defined Python functions. Given
a generic Python callable,
7the following can performed:
10def func(
x: numpy.ndarray, pars: numpy.ndarray) -> float:
11 return pars[0] *
x[0] *
x[0] +
x[1] * pars[0]
13my_func =
ROOT.TF1(
"my_func", func, -10, 10, npar=2, ndim=2)
16Second, after performing the initialisation with
a Python functor, the
TF1 instance can be evaluated
using the Pythonized
26def pyf_tf1_coulomb(x, p):
27 return p[1] * x[0] * x[1] / (p[0]**2) * math.exp(-p[2] / p[0])
29rtf1_coulomb = ROOT.TF1("my_func", pyf_tf1_coulomb, -10, 10, ndims = 2, npars = 3)
31#
x dataset: 5 pairs of particle charges
42 [8.99e9], # Coulomb constant k (in N·m²/C²)
43 [0.1] # Additional factor
for modulation
46#
Slice to avoid the dummy column of 10's
47res = rtf1_coulomb.EvalPar(
x[:, ::2], params)
54#
for illustration,
a sinusoidal
function with six parameters
55myFunction =
ROOT.
TF1(
"myExampleFunction",
"[0] + [1] * x + [2]*sin([3] * x) + [4]*cos([5] * x)", -5, 5)
57# declare the parameters
as a numpy.array or array.array
58myParams = np.array([0.04, 0.4, 3.0, 3.14, 1.5, 3.14])
60# note: the following
line would also work by setting all six parameters equal to 3.0
61# myParams = np.array([3.0] * 6)
static Roo_reg_AGKInteg1D instance
double evaluate() const override
void SetParameters(TFitEditor::FuncParams_t &pars, TF1 *func)
Restore the parameters from pars into the function.
virtual Double_t EvalPar(const Double_t *x, const Double_t *params=nullptr)
pythonization(class_name, ns="::", is_prefix=False)
Decorator that allows to pythonize C++ classes.
T Slice(const T &histo, std::vector< Int_t > &args)
Creates a sliced copy of the given histogram.
AxisAngle::Scalar Distance(const AxisAngle &r1, const R &r2)
Distance between two rotations.
void function(const Char_t *name_, T fun, const Char_t *docstring=0)