Hi,
Problems
I’m trying to fit a dataset using ROOT::Fit::Fitter interface in a C++ project. So far, I have been using TF1 class to setup the fitting function.
In general, TF1 has two kinds of interfaces:
-
Using string to define the function.
If i’m not wrong, it’s launching Cling to interpret the string and parse it to a callable function during the runtime. For me, this is an unnecessary performance overhead and I’m trying to avoid this method.
-
Using lambda.
The function is defined during the compile time. However, it must have this signature:
auto [](const double* variables, const double* parameters) -> double;This API is pretty awful as it has basically no way to do bound checking inside the lambda on variables or parameters, unless I’m using a global variable to indicate the sizes. It’s very error-prone.
Thus, I would like to know whether there is any fast and safe API for setting up the fitting function of the Fitter class.
Suggestions on TF1 constructor interface
Adding size information during the runtime
It would be nice if the TF1 allows the lambda to have sizes as its input arguments:
auto [](const double* variables, std::size_t variable_size,
const double* parameters, std::size_t parameter_size) -> double;
Or even better using std::span
auto [](std::span<const double> variables, std::span<const double> parameters) -> double;
Using std::array
Most of the time, the fitting function is fixed already during the compile time, so it makes sense to have something like:
auto [](const std::array<double, 2>& variables,
const std::array<double, 3>& parameters) -> double
{
const auto& [x, y] = variables;
const auto& [a, b, c] = parameters;
return a * x + b * y + c;
};
This looks very nice and clear, but it requires a redesign of TF1, as TF1 would have to be a class template, with the template arguments to be the number of variables and parameters. Or it at least needs two template specialization, one for dynamic sizes (old API, sizes are determined at the runtime) and another for fixed compile-time sizes.
Thanks for your attention
ROOT Version: 6.36.04
Platform: Fedora 42
Compiler: Clang 21