Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions hist/hist/inc/TF1.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
//////////////////////////////////////////////////////////////////////////

#include "RConfigure.h"
#include <functional>
#include <cassert>
#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include <vector>
Expand Down Expand Up @@ -709,7 +710,14 @@ namespace ROOT {
#endif
{
f->fType = TF1::EFType::kTemplScalar;
f->fFunctor.reset(new TF1::TF1FunctorPointerImpl(ROOT::Math::ParamFunctorTempl<double>(func)));
if constexpr (std::is_invocable_r_v<double, Func, const double *, std::size_t, const double *, std::size_t>) {
auto wrapper = [func, npar = f->fNpar, ndim = f->fNdim](const double *x, const double *p) -> double {
return func(x, static_cast<std::size_t>(ndim), p, static_cast<std::size_t>(npar));
};
f->fFunctor = std::make_unique<TF1::TF1FunctorPointerImpl<double>>(ROOT::Math::ParamFunctorTempl<double>(wrapper));
} else {
f->fFunctor.reset(new TF1::TF1FunctorPointerImpl(ROOT::Math::ParamFunctorTempl<double>(func)));
}
}

f->fParams = std::make_unique<TF1Parameters>(f->fNpar);
Expand All @@ -727,7 +735,14 @@ namespace ROOT {
#endif
{
f->fType = TF1::EFType::kTemplScalar;
f->fFunctor.reset(new TF1::TF1FunctorPointerImpl(ROOT::Math::ParamFunctorTempl<double>(func)));
if constexpr (std::is_invocable_r_v<double, Func, const double *, std::size_t, const double *, std::size_t>) {
auto wrapper = [func, npar = f->fNpar, ndim = f->fNdim](const double *x, const double *p) -> double {
return (*func)(x, static_cast<std::size_t>(ndim), p, static_cast<std::size_t>(npar));
};
f->fFunctor = std::make_unique<TF1::TF1FunctorPointerImpl<double>>(ROOT::Math::ParamFunctorTempl<double>(wrapper));
} else {
f->fFunctor.reset(new TF1::TF1FunctorPointerImpl(ROOT::Math::ParamFunctorTempl<double>(func)));
}
}

f->fParams = std::make_unique<TF1Parameters>(f->fNpar);
Expand Down
24 changes: 24 additions & 0 deletions hist/hist/test/test_tf1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "ROOT/TestSupport.hxx"

#include <cmath>
#include <cstddef>
#include <iostream>

class MyClass {
Expand Down Expand Up @@ -240,6 +241,29 @@ TEST(TF1, Constructors)
EXPECT_EQ(tf1(&x, &p), 2);
}

TEST(TF1, LambdaWithSizes)
{
// lambda with explicit ndim and npar -- allows safe bound checking inside
auto f4arg = [](const double *x, std::size_t ndim, const double *p, std::size_t npar) -> double {
EXPECT_EQ(ndim, std::size_t{1});
EXPECT_EQ(npar, std::size_t{2});
return p[0] * x[0] + p[1];
};

TF1 f("f4arg", f4arg, 0, 10, 2);
f.SetParameters(3.0, 1.0);

EXPECT_NEAR(f.Eval(2.0), 7.0, 1e-10);
EXPECT_NEAR(f.Eval(5.0), 16.0, 1e-10);

// same but passing via pointer
TF1 fptr("f4arg_ptr", &f4arg, 0, 10, 2);
fptr.SetParameters(3.0, 1.0);

EXPECT_NEAR(fptr.Eval(2.0), 7.0, 1e-10);
EXPECT_NEAR(fptr.Eval(5.0), 16.0, 1e-10);
}

TEST(TF1, Save)
{
TF1 linear("linear", "x", -10., 10.);
Expand Down