# TFormula from user-defined TF1

**URL:** <https://root-forum.cern.ch/t/tformula-from-user-defined-tf1/37414>\
**Category:** Newbie\
**Created:** [January 3, 2020, 10:49am UTC](https://root-forum.cern.ch/t/tformula-from-user-defined-tf1/37414 "2020-01-03T10:49:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsfeith](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@tsfeith](https://root-forum.cern.ch/u/tsfeith)\
**Post date:** [January 3, 2020, 10:49am UTC](https://root-forum.cern.ch/t/tformula-from-user-defined-tf1/37414/1 "2020-01-03T10:49:04Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [January 6, 2020, 9:34am UTC](https://root-forum.cern.ch/t/tformula-from-user-defined-tf1/37414/2 "2020-01-06T09:34:49Z")

</div>

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:

```auto
#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

```auto
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.)
