# Using TF1 in compiled code with my own function -- what am I doing wrong?

**URL:** <https://root-forum.cern.ch/t/using-tf1-in-compiled-code-with-my-own-function-what-am-i-doing-wrong/55518>\
**Category:** ROOT\
**Tags:** root\
**Created:** [July 2, 2023, 8:21pm UTC](https://root-forum.cern.ch/t/using-tf1-in-compiled-code-with-my-own-function-what-am-i-doing-wrong/55518 "2023-07-02T20:21:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfpate](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@sfpate](https://root-forum.cern.ch/u/sfpate)\
**Post date:** [July 2, 2023, 8:21pm UTC](https://root-forum.cern.ch/t/using-tf1-in-compiled-code-with-my-own-function-what-am-i-doing-wrong/55518/1 "2023-07-02T20:21:34Z")

</div>

ROOT Version: 6.26/06  
Platform: MacOS 12.6.7  
Compiler: g++

I am trying to make a graph of a function. This simple implementation below does not work. The function chi2\_p2bm clearly exists, because I can calculate a value and print it out via the cout. But the TF1 does not recognize it. I am very confused because this looks exactly like many of the examples in the TF1 documentation.

Here is the compiler line:  
% g++ -std=c++11 testgraph.cpp -o testgraph `root-config --cflags --glibs`

Here is the output:  
% ./testgraph  
0.0225  
0.0256  
0.0289  
0.0324  
input\_line\_8:2:65: error: use of undeclared identifier ‘chi2\_p2bm’  
Double\_t TFormula\_\_\_\_id4664759779193443662(Double\_t \*x){ return chi2\_p2bm(x[0]) ; }  
^  
input\_line\_9:2:65: error: use of undeclared identifier ‘chi2\_p2bm’  
Double\_t TFormula\_\_\_\_id4664759779193443662(Double\_t _x){ return chi2\_p2bm(x[0]) ; }  
^  
Error in : Can’t compile function TFormula\_\_\_\_id4664759779193443662 prototype with arguments Double\_t_  
Error in \< TFormula::InputFormulaIntoCling \>: Error compiling formula expression in Cling  
Error in \< TFormula::ProcessFormula \>: Formula “chi2\_p2bm(x)” is invalid !  
%

Here is the code:

#include \<TObject.h\>  
#include \<TROOT.h\>  
#include \<TClass.h\>  
#include \<TError.h\>  
#include \<TMath.h\>  
#include \<TH1.h\>  
#include \<TH2.h\>  
#include \<TH3.h\>  
#include \<TF1.h\>  
#include \<TGraphErrors.h\>  
#include \<TCanvas.h\>  
#include \<TMinuit.h\>  
#include \<TStyle.h\>  
#include \<TPaveText.h\>  
#include \<TPaveLabel.h\>  
#include \<TLegend.h\>  
#include \<TMatrixD.h\>  
#include \<TFormula.h\>

using namespace std;

double chi2\_p2bm(double x) {  
return pow(x,2);  
}

int main()  
{

```
cout << chi2_p2bm(.15) << endl;
cout << chi2_p2bm(.16) << endl;
cout << chi2_p2bm(.17) << endl;
cout << chi2_p2bm(.18) << endl;

TCanvas *c1 = new TCanvas("c1", "",1900,1100);

```

// c1-\>Divide(5,1);  
// c1-\>cd(1);

```
TF1 *fp2bm = new TF1("fp2bm","chi2_p2bm(x)",0.1,0.3);

```

// fp2bm-\>Draw();

// c1-\>Print(“chi2.pdf”);  
}

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [July 3, 2023, 4:53am UTC](https://root-forum.cern.ch/t/using-tf1-in-compiled-code-with-my-own-function-what-am-i-doing-wrong/55518/2 "2023-07-03T04:53:08Z")

</div>

I think these functions cannot be compiled, see the [TF1 doc](https://root.cern/doc/master/classTF1.html). You can use the types 4-6 on that page, e.g.

```auto
/*
  g++ testgraph.cpp -o testgraph `root-config --cflags --glibs`
*/

#include <TObject.h>
#include <TROOT.h>
#include <TClass.h>
#include <TError.h>
#include <TMath.h>
#include <TH1.h>
#include <TH2.h>
#include <TH3.h>
#include <TF1.h>
#include <TGraphErrors.h>
#include <TCanvas.h>
#include <TMinuit.h>
#include <TStyle.h>
#include <TPaveText.h>
#include <TPaveLabel.h>
#include <TLegend.h>
#include <TMatrixD.h>
#include <TFormula.h>

using namespace std;

Double_t chi2_p2bm(Double_t *x, Double_t *par) {
   return pow(x[0],2);
}

int main() {
/*
  double myx[1]={.15};
  cout << chi2_p2bm(myx,myx) << endl;
*/
  TCanvas *c1 = new TCanvas("c1", "",1900,1100);
  c1->Divide(2,1);

  c1->cd(1);
  TF1 *fp2bm = new TF1("fp2bm", chi2_p2bm, 0.1, 0.3, 0);
  fp2bm->Draw();

  // or with Lambda:
  c1->cd(2);
  TF1 *fp2bmL = new TF1("fp2bmL",[&](double*x, double *p){ return pow(x[0],2); }, 0.1, 0.3, 0);
  fp2bmL->Draw();

  c1->SaveAs("chi2.pdf");
}

```

---

<div class="post-metadata">

**Author:** ![Ako\_b](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/9f8e36/32.png) [@Ako\_b](https://root-forum.cern.ch/u/Ako_b)\
**Post date:** [July 3, 2023, 9:29am UTC](https://root-forum.cern.ch/t/using-tf1-in-compiled-code-with-my-own-function-what-am-i-doing-wrong/55518/3 "2023-07-03T09:29:48Z")

</div>

Helllo @sfpate !  
I guess you should use a different `TF1` constructor, [for example](https://root.cern/doc/master/classTF1.html#F4)

```auto
double chi2_p2bm(double *x, double *par)
{
    return pow(x[0]);
}
...
TF1 *fp2bm = new TF1("fp2bm", chi2_p2bm, 0.1, 0.3, 0);

```

Here the last argument `0` to the constructor is number of parameters of function, which is actually zero.

Edit: I just realized that the post above gives a more detailed explanation, so mine is just a duplicate. I apologize for my haste and inattention.

Also, it might be useful to take a look at this post about the use of `new` statement in modern C++:  
[https://root-forum.cern.ch/t/how-to-properly-delete-ttree-and-tgraph-and-renew-them/55351/2?u=ako\_b](https://root-forum.cern.ch/t/how-to-properly-delete-ttree-and-tgraph-and-renew-them/55351/2)

---

<div class="post-metadata">

**Author:** ![sfpate](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@sfpate](https://root-forum.cern.ch/u/sfpate)\
**Post date:** [July 3, 2023, 1:27pm UTC](https://root-forum.cern.ch/t/using-tf1-in-compiled-code-with-my-own-function-what-am-i-doing-wrong/55518/4 "2023-07-03T13:27:43Z")

</div>

Dear @dastudillo and @Ako_b

Thank you for your quick responses. I chose to use the parametrized function with no parameters.

Steve

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 17, 2023, 1:28pm UTC](https://root-forum.cern.ch/t/using-tf1-in-compiled-code-with-my-own-function-what-am-i-doing-wrong/55518/5 "2023-07-17T13:28:19Z")

</div>

Closed on July 17, 2023, 1:28pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
