Lambda expression to TF1

Hi,

Is there a way to cast a lambda expression to TF1 for plotting on a canvas?

For that matter, it will be sufficient if someone could guide me to how one can plot a c++ std::function/lambda expression directly to a TCanvas.

Thanks a lot,
Nikhil

[quote=“njoshi3”]Hi,

Is there a way to cast a lambda expression to TF1 for plotting on a canvas?

For that matter, it will be sufficient if someone could guide me to how one can plot a c++ std::function/lambda expression directly to a TCanvas.

Thanks a lot,
Nikhil[/quote]

Hmmmm…

Have a look at TF1 ctors, how do you think, which one you can use with C++ lambdas?

   TF1();
   TF1(const char *name, const char *formula, Double_t xmin=0, Double_t xmax=1);
   TF1(const char *name, Double_t xmin, Double_t xmax, Int_t npar);
#ifndef __CINT__
   TF1(const char *name, Double_t (*fcn)(Double_t *, Double_t *), Double_t xmin=0, Double_t xmax=1, Int_t npar=0);
   TF1(const char *name, Double_t (*fcn)(const Double_t *, const Double_t *), Double_t xmin=0, Double_t xmax=1, Int_t npar=0);
#endif

   // Constructors using functors (compiled mode only)
   TF1(const char *name, ROOT::Math::ParamFunctor f, Double_t xmin = 0, Double_t xmax = 1, Int_t npar = 0);

   // Template constructors from any  C++ callable object,  defining  the operator() (double * , double *)
   // and returning a double.
   // The class name is not needed when using compile code, while it is required when using
   // interpreted code via the specialized constructor with void *.
   // An instance of the C++ function class or its pointer can both be used. The former is reccomended when using
   // C++ compiled code, but if CINT compatibility is needed, then a pointer to the function class must be used.
   // xmin and xmax specify the plotting range,  npar is the number of parameters.
   // See the tutorial math/exampleFunctor.C for an example of using this constructor
   template <typename Func>
   TF1(const char *name, Func f, Double_t xmin, Double_t xmax, Int_t npar, const char * = 0  );

// Template constructors from a pointer to any C++ class of type PtrObj with a specific member function of type
   // MemFn.
   // The member function must have the signature of  (double * , double *) and returning a double.
   // The class name and the method name are not needed when using compile code
   // (the member function pointer is used in this case), while they are required when using interpreted
   // code via the specialized constructor with void *.
   // xmin and xmax specify the plotting range,  npar is the number of parameters.
   // See the tutorial math/exampleFunctor.C for an example of using this constructor
   template <class PtrObj, typename MemFn>
   TF1(const char *name, const  PtrObj& p, MemFn memFn, Double_t xmin, Double_t xmax, Int_t npar, const char * = 0, const char * = 0) ;
  TF1(const TF1 &f1);

Is this a quiz?

At least a capture-less lambda can decay to an ordinary
function pointer from which a TF1 can be constructed. This requires the lambda
to take two pointers, one to an array of function arguments and one to an
array of parameters.

auto l = [](double* xs, double ps*) { return xs[0]*xs[0]; };
TF1 f("f", l, 0, 100, 0);

If he wants to use C++ lambdas, I assume he knows about possible conversions. I gave him the constructor declarations. So no, it’s not a quiz, it’s a hint.

Thanks honk. It worked nicely :slight_smile: