# Plotting custom function within TF1

**URL:** <https://root-forum.cern.ch/t/plotting-custom-function-within-tf1/58901>\
**Category:** Newbie\
**Created:** [April 8, 2024, 2:20pm UTC](https://root-forum.cern.ch/t/plotting-custom-function-within-tf1/58901 "2024-04-08T14:20:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MicheleScattola](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/michelescattola/32/27810_2.png) [@MicheleScattola](https://root-forum.cern.ch/u/MicheleScattola)\
**Post date:** [April 8, 2024, 2:20pm UTC](https://root-forum.cern.ch/t/plotting-custom-function-within-tf1/58901/1 "2024-04-08T14:20:30Z")

</div>

Hi, I’m trying to plot a function (Compton scattering) and I am trying to use TF1.  
My function is defined in a header file

```auto
double E_1(double theta);

```

And in my main file I am trying to recall it like this:

```auto
    TApplication app("App",0,0);
    TF1 *g = new TF1("E(theta)","E_1(x)",0.,2*M_PI);
    TCanvas * c1 = new TCanvas("E_gamma","Energia Fotone uscente",600,600);

		c1->cd();

			g->SetTitle("E_gamma;Theta [rad];Energy [KeV]");

			g->Draw();

    app.Run();

```

Since my custom formula (for another case I have to implement) can be very long I would like to keep its definition outside of the TF1 declaration and the recall it there. Is there any way I can do it?

SOLVED:  
The function needs the proper definition to be accepted by TF1:

```auto
double E_1(double *x,double *params){

    double theta = x[0];
    
    return 511/(2-cos(theta));
}

```

And then the recall within TF1:

```auto
TF1 *g = new TF1("E(x)",E_2,0.,M_PI,0);
//where there are 0 additional parameters in my formula

```

_ROOT Version:_ 6.30/04  
_Platform:_ Linux Mint  
_Compiler:_ g++

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 8, 2024, 2:28pm UTC](https://root-forum.cern.ch/t/plotting-custom-function-within-tf1/58901/2 "2024-04-08T14:28:18Z")

</div>

I think you are looking for [something like that](https://root.cern/doc/master/classTF1.html#F4).

---

<div class="post-metadata">

**Author:** ![MicheleScattola](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/michelescattola/32/27810_2.png) [@MicheleScattola](https://root-forum.cern.ch/u/MicheleScattola)\
**Post date:** [April 8, 2024, 2:42pm UTC](https://root-forum.cern.ch/t/plotting-custom-function-within-tf1/58901/3 "2024-04-08T14:42:00Z")

</div>

I understand the example shown but here is what I struggle with:  
My function E\_1 accepts 1 parameter theta so from what I understand I need to adjust

```auto
TF1 *g = new TF1("E(theta)",E_1,0.,2*M_PI,1);
//where 1 is the number of parameters

```

What I don’t understand is why should I fix the parameters with `g->SetParameters(..)` ? (as shown in the example)  
My intention is to let ROOT plot the function from theta=0 to theta=2\*M\_PI

---

<div class="post-metadata">

**Author:** ![MicheleScattola](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/michelescattola/32/27810_2.png) [@MicheleScattola](https://root-forum.cern.ch/u/MicheleScattola)\
**Post date:** [April 8, 2024, 3:06pm UTC](https://root-forum.cern.ch/t/plotting-custom-function-within-tf1/58901/4 "2024-04-08T15:06:24Z")

</div>

SOLVED:  
The function needs the proper definition to be accepted by TF1:

```auto
double E_1(double *x,double *params){

    double theta = x[0];
    
    return 511/(2-cos(theta));
}

```

And then the recall within TF1:

```auto
TF1 *g = new TF1("E(x)",E_2,0.,M_PI,0);
//where there are 0 additional parameters in my formula

```
