Skip to content

Constructing TF1 with the lambda that contains size information for parameters and variables #22071

@YanzhaoW

Description

@YanzhaoW

Explain what you would like to see improved and how.

Original issue: root forum

Issue

The lambda used to construct the TF1 object has the following signature for the current version:

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, which is very error-prone.

Improvements

Runtime size information

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;

Compile-time size information

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.

ROOT version

6.36.04

Installation method

rpm

Operating system

Fedora 42

Additional context

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions