TFormula from user-defined TF1

Hi,

I’ve been trying for a while to create a TFormula out of a user defined TF1 and so far I’ve come up empty, so I was wondering if there was some way to do it. I’ve managed to create the TF1, both via a lambda expression and via a functor. The two ways are copied below

TF1 *F1 = new TF1(“f”, [&](double* x, double* p){return -potencial.Derivative(x[0]);},xmin,xmax,1);

double Evaluate(double *x, double <em>p){
Spline3Interpolator potencial(ngrid,grid,pot_el);
return -potencial.Derivative(x[0]);
}
PIC Myfunc = this;
TF1 *F1 = new TF1(“f”,Myfunc,&PIC::Evaluate,xmin,xmax,0,“PIC”,“Evaluate”);

Above, PIC is a custom class, potential is a class object and Derivative is a class method.
Both the expressions above, when I evaluate, for example, F1->Eval(1), return the correct value.
How can I proceed to convert this TF1 into a TFormula? Everything I tried resulted in either “Segmentation Violation” or “Error in TFormula::Eval: Formula is invalid and not ready to execute”.
Thank you in advance

Hi,

PIC Myfunc = this looks wrong to me. You are assigning the this pointer of the current object to an instance of a PIC. That probably doesn’t work.

Concerning TFormula, you can create a TFormula from any function that the interpreter has seen. A macro like this should work:

#include ...
double myFunc() {
  return 3.; 
}

double test() {
  TFormula formula("formula", "myFunc()");
  return formula.Eval(0.);
}

TFormula is not meant, though, to interact with TF1. TF1 will use a TFormula if you pass a C+±like expression, like

TF1 myTF1("myTF1", "sin(x)+3");

If you already wrote C++ code to do the math, you should be able to run without TFormula, and only do things with TF1s.

To rephrase:
Why do you need a TF1 to go into a TFormula? (The information should flow in the opposite direction.)