# Effficiency: TF1 vs directly evaluating a polynomial?

**URL:** <https://root-forum.cern.ch/t/effficiency-tf1-vs-directly-evaluating-a-polynomial/58420>\
**Category:** ROOT\
**Tags:** root\
**Created:** [March 7, 2024, 6:17am UTC](https://root-forum.cern.ch/t/effficiency-tf1-vs-directly-evaluating-a-polynomial/58420 "2024-03-07T06:17:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 7, 2024, 6:17am UTC](https://root-forum.cern.ch/t/effficiency-tf1-vs-directly-evaluating-a-polynomial/58420/1 "2024-03-07T06:17:07Z")

</div>

Hi everyone,

Use case:  
1.) Reading entries (double) from a TTRee Reader, xi.  
2.) Transforming the entries as  
-Using TF1

```auto
TF1* f = new TF1("pol2");
f->SetParameters(a0, b0, c0);
double yi = f->Eval(xi);

```

-directly evaluating the polynmial

```auto
yi = a0 + b0*xi + c0*xi*xi;

```

I have a trivial query for a ‘large’ number of operations, is there any significant difference in computational times for the two cases - using TF1 vs direct evaluation?  
Further, is any one is aware of more ‘efficient’ ways of doing this, please share them, it will be highly helpful.  
Thanks a lot !

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [March 7, 2024, 7:16am UTC](https://root-forum.cern.ch/t/effficiency-tf1-vs-directly-evaluating-a-polynomial/58420/2 "2024-03-07T07:16:26Z")

</div>

Hi,

The evaluation of the TF1 does not have a significant overhead wrt the evaluation of the parabola directly in C++, provided that you set the value of the parameters and not re-create an instance per entry.

Something which can be more efficient, but perhaps not so useful depending on the context, is to [extract the columns from the datasets with RDataFrame](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#aa571f08b8e4d9300cacdea039a379a22) and combine them in a vectorised fashion with VecOps (see [this tutorial](https://root.cern.ch/doc/master/vo002__VectorCalculations_8C.html)).

Best,  
D

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 7, 2024, 8:31am UTC](https://root-forum.cern.ch/t/effficiency-tf1-vs-directly-evaluating-a-polynomial/58420/3 "2024-03-07T08:31:03Z")

</div>

Hi,  
Thanks for the information.

I’ll try understanding VecOps, thank you for the relevant link.

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [March 7, 2024, 3:37pm UTC](https://root-forum.cern.ch/t/effficiency-tf1-vs-directly-evaluating-a-polynomial/58420/4 "2024-03-07T15:37:41Z")

</div>

Hi @Danilo ,  
I followed the tutorial and think RVecOps() may be a good substitute for the case at hand,  
Currently,  
1.)Reading a TTree having different branches (energy, time, hit\_info etc) using

```auto
TTreeReader ReaderName()

```

2.) Each TTree entry corresponds to one “event.”

3.) Based on certain conditions(multiplicity, detector id, etc) on the branches, I am trying extracting data(energy) from other branches and transforming the energy using a pol2.

I am thinking of using RVecOps here however I am not sure how to substitute a TTreeReader with RVecOps?  
Most probably this is a naive query but I am a complete beginner to RVecOps and vector operations in general, so any assistance will be highly valued. ?

PS: I understand that I am digressing from the original title of the thread and just put this up here for continuity.  
Thanks a lot for the help!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [March 7, 2024, 3:49pm UTC](https://root-forum.cern.ch/t/effficiency-tf1-vs-directly-evaluating-a-polynomial/58420/5 "2024-03-07T15:49:36Z")

</div>

Hi,

I think the best way, if you want to go for the vectorized approach, is to use RDataFrame and not TTreeReader, as mentioned above.

Best,  
Danilo

---

<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:** [March 21, 2024, 3:49pm UTC](https://root-forum.cern.ch/t/effficiency-tf1-vs-directly-evaluating-a-polynomial/58420/6 "2024-03-21T15:49:40Z")

</div>

Closed on March 21, 2024, 3:49pm UTC

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