Skip to content

[hist] Allow TF1 lambdas to receive ndim and npar as arguments - Issue #22071#22509

Open
medeirosdev wants to merge 4 commits into
root-project:masterfrom
medeirosdev:tf1-lambda-with-sizes
Open

[hist] Allow TF1 lambdas to receive ndim and npar as arguments - Issue #22071#22509
medeirosdev wants to merge 4 commits into
root-project:masterfrom
medeirosdev:tf1-lambda-with-sizes

Conversation

@medeirosdev

Copy link
Copy Markdown

Users constructing TF1 with a lambda had no safe way to do bound checking on variables and parameters arrays, since the current API only exposes raw pointers with no size information.

This adds support for a 4-argument lambda signature:

[](const double *x, std::size_t ndim,
const double *p, std::size_t npar) -> double

TF1Builder now detects this signature at compile time via std::is_invocable_r_v and wraps it in an adapter that passes fNdim and fNpar captured from the TF1 object. The existing 2-argument API is fully preserved.

Closes #22071

This Pull request:

Changes or fixes:

Checklist:

  • tested changes locally
  • updated the docs (if necessary)

This PR fixes #

Users constructing TF1 with a lambda had no safe way to do bound
checking on variables and parameters arrays, since the current API
only exposes raw pointers with no size information.

This adds support for a 4-argument lambda signature:

  [](const double *x, std::size_t ndim,
     const double *p, std::size_t npar) -> double

TF1Builder now detects this signature at compile time via
std::is_invocable_r_v and wraps it in an adapter that passes
fNdim and fNpar captured from the TF1 object. The existing
2-argument API is fully preserved.

Closes root-project#22071
@YanzhaoW

YanzhaoW commented Jun 8, 2026

Copy link
Copy Markdown

Hi,

thanks for the PR. Would it be possible to have std::span as one of options? std::span has much cleaner API.

Comment thread hist/hist/inc/TF1.h Outdated
Comment thread hist/hist/inc/TF1.h Outdated
Comment thread hist/hist/inc/TF1.h Outdated
medeirosdev and others added 3 commits June 8, 2026 16:26
Yes! nice one

Co-authored-by: Yanzhao Wang (王彦昭) <yannzhaow@gmail.com>
nice suggestions

Co-authored-by: Yanzhao Wang (王彦昭) <yannzhaow@gmail.com>
Both branches set fType to kTemplScalar, so move it before the
if constexpr to avoid the duplication. Also fix extra parenthesis
from applied suggestion and sync the Func* specialization to use
init-captures and make_unique consistently.
@medeirosdev

Copy link
Copy Markdown
Author

Hi,

thanks for the PR. Would it be possible to have std::span as one of options? std::span has much cleaner API.

std::span would definitely be a cleaner API, but t challenge is that it's C++20, while ROOT currently targets C++17 as the minimum. one option would be to support it as an additional signature, guarded by #ifdef __cpp_lib_span, so users on C++20 can use span while C++17 users keep the size_t version. Would that be the right direction, or would you prefer to keep this PR focused on the size_t approach and handle span in a follow-up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants