# Using TF1 functions

**URL:** <https://root-forum.cern.ch/t/using-tf1-functions/54272>\
**Category:** Newbie\
**Created:** [March 28, 2023, 3:55pm UTC](https://root-forum.cern.ch/t/using-tf1-functions/54272 "2023-03-28T15:55:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonardi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/f0a364/32.png) [@leonardi](https://root-forum.cern.ch/u/leonardi)\
**Post date:** [March 28, 2023, 3:55pm UTC](https://root-forum.cern.ch/t/using-tf1-functions/54272/1 "2023-03-28T15:55:30Z")

</div>

* * *

_ROOT Version:_ 6.08/02  
_Platform:_ Linux CentOS 7  
_Compiler:_ Root macro

* * *

In a ROOT macro I am defining a TF1 function to be used in histogram fitting. The function depends on some variables defined in the main program.

After looking to the documentation and to some examples, I came up with the following code:

```auto
  auto fTemplateFunction = new TF1("tempfunc",[&](double* x, double* p){
      Double_t xx = x[0]-fTemplateCFShift;
      if (xx < 0.) return 0.;
      Double_t xint, xdec;
      xdec = std::modf(xx,&xint);
      Int_t xi = (Int_t)xint;
      printf("%f %f %f %f %d %f %f\n",p[0],x[0],fTemplateCFShift,xx,xi,xdec,p[0]*(fTemplate[xi]+(fTemplate[xi+1]-fTemplate[xi])*xdec));
      return p[0]*(fTemplate[xi]+(fTemplate[xi+1]-fTemplate[xi])*xdec);
    },0.,1024.,1);

```

I understood that the [&] definition allows the function to access variables from the main program, in this case fTemplateCFShift and fTemplate.

When I use this function to fit an histogram, I can see that the external variables are indeed correctly accessed:

```auto
h->Fit("tempfunc","","",200.,994.);
37128.852670 200.500000 177.458362 23.041638 23 0.041638 153.575151
37128.852670 201.500000 177.458362 24.041638 24 0.041638 157.093462
37128.852670 202.500000 177.458362 25.041638 25 0.041638 160.193099
etc.etc.

```

but if I try to use the function-\>Draw() command, the external variables are all zero:

```auto
fTemplateFunction->Draw();
37129.474010 200.500000 0.000000 200.500000 200 0.500000 0.000000
37129.474010 201.500000 0.000000 201.500000 201 0.500000 0.000000
37129.474010 202.500000 0.000000 202.500000 202 0.500000 0.000000
etc.etc.

```

What is the correct way to get the same behavior in the two cases?  
Thank you

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 28, 2023, 4:53pm UTC](https://root-forum.cern.ch/t/using-tf1-functions/54272/2 "2023-03-28T16:53:29Z")

</div>

Try to define fTemplateCFShift and fTemplate as “`static`” variables.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 28, 2023, 5:24pm UTC](https://root-forum.cern.ch/t/using-tf1-functions/54272/3 "2023-03-28T17:24:26Z")

</div>

Hi,  
You need to make sure that the function TF1 is used within the scope of the variables passed to the lambda. Otherwise you need to make them global available.

Lorenzo

---

<div class="post-metadata">

**Author:** ![leonardi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/f0a364/32.png) [@leonardi](https://root-forum.cern.ch/u/leonardi)\
**Post date:** [March 29, 2023, 10:02am UTC](https://root-forum.cern.ch/t/using-tf1-functions/54272/4 "2023-03-29T10:02:33Z")

</div>

Declaring fTemplateCFShift and fTemplate as “static” solved the issue. Thank you.

Still I do not understand why the variables are in scope during the fitting section of the h-\>Fit(…) call but out of scope in the drawing section of the same method…

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 29, 2023, 10:06am UTC](https://root-forum.cern.ch/t/using-tf1-functions/54272/5 "2023-03-29T10:06:05Z")

</div>

Hi,  
The problem is that the drawing is not actually happening when calling `h->Draw()` but when calling `h->Paint()` that is done automatically when exiting the macro. You can force the drawing before by calling `gPad->Update()` after `h->Draw()`

Lorenzo
