# Summing a large number of TF1

**URL:** <https://root-forum.cern.ch/t/summing-a-large-number-of-tf1/51249>\
**Category:** Newbie\
**Tags:** root\
**Created:** [August 18, 2022, 9:11pm UTC](https://root-forum.cern.ch/t/summing-a-large-number-of-tf1/51249 "2022-08-18T21:11:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![delia](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@delia](https://root-forum.cern.ch/u/delia)\
**Post date:** [August 18, 2022, 9:11pm UTC](https://root-forum.cern.ch/t/summing-a-large-number-of-tf1/51249/1 "2022-08-18T21:11:37Z")

</div>

Hello,

I am new to the forum. To anyone who might be able to help me: thank you in advance!

My problem is the following:

I have defined my function:  
TF1 \* eFunc = new TF1(“eFunc” , “(TMath::Erf((x-[0])/[1]))\*[2]”, 21, 22);

And for all the three parameters of the function ([0], [1], [2]) I have 100 set of values. I put this values in 3 arrays - one for each parameter. Therefore I have created p0Array[100] with the values for [0], p1Array[100] for [1] and p2Array[100] for [2]. I know all these values and the arrays are filled correctly.

Now, if I want to evaluate the function for a predefined set of parameters (let’s say for p0Array[2], p1Array[2] and p2Array[2]), I am good and I can draw the function:  
eFunc-\>SetParameters(p0Array[2],p1Array[2],p2Array[2]);  
eFunc-\>Draw();  
and it looks like it should.

But I would like to sum all the 100 curves resulting from evaluating the function eFunc for each set of parameters: from eFunc-\>SetParameters(p0Array[0],p1Array[0],p2Array[0]) to eFunc-\>SetParameters(p0Array[99],p1Array[99],p2Array[99]).  
I could run a for, but still I would have 100 different functions/plots and I wouldn’t know how to sum them.

For sure it is a silly question, it should be simple indeed: I just want to sum the same function, evaluated for 100 different sets of the three parameters.

Thanks!  
Delia

---

<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:** [August 19, 2022, 9:05am UTC](https://root-forum.cern.ch/t/summing-a-large-number-of-tf1/51249/2 "2022-08-19T09:05:58Z")

</div>

Hi,  
The best is you create a new TF1 summing all the 100 functions. You can do as following, assuming you have before defined the arrays p0Array, p1Array and p2Array with 100 elements:

```auto
TF1 * eFunc = new TF1("eFunc" , "(TMath::Erf((x-[0])/[1]))*[2]", 21, 22);
auto sumFunc = [&] (double *x, double *p) { 
     double sum = 0; 
     double par[3];
     for (int i = 0; i < 100; i++) {
          par[0] = p0Array[i];
          par[1] = p1Array[i];
          par[2] = p2Array[i];
          sum += eFunc->EvalPar(x, par);
    } 
    return sum;
};
// define TF1 representing sum of 100 functions
// one could it define with 300 parameters. Now the parameters are 
// captured by the lambda and not stored in the TF1 sFunc with is defined with 0 parameters 
TF1 * sFunc = new TF1("sFunc" , sumFunc, 21, 22, 0);
sFunc->Draw();

```

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![delia](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@delia](https://root-forum.cern.ch/u/delia)\
**Post date:** [August 19, 2022, 11:55am UTC](https://root-forum.cern.ch/t/summing-a-large-number-of-tf1/51249/3 "2022-08-19T11:55:29Z")

</div>

Hi Lorenzo,  
Clever idea. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![delia](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@delia](https://root-forum.cern.ch/u/delia)\
**Post date:** [August 19, 2022, 12:59pm UTC](https://root-forum.cern.ch/t/summing-a-large-number-of-tf1/51249/4 "2022-08-19T12:59:15Z")

</div>

I tried with your suggestion,

but unfortunately there is a crash when I run the code:

```auto
terminate called after throwing an instance of 'cling::InvalidDerefException'
what(): Trying to access a pointer that points to an invalid memory address.

```

If I comment the last line:

`//sFunc->Draw();`

there is no crash though.  
The problem is maybe in the `* sFunc` definition?  
But I am not sure, since I am not practical with lambda functions…

Thanks a lot!!

---

<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:** [August 19, 2022, 1:01pm UTC](https://root-forum.cern.ch/t/summing-a-large-number-of-tf1/51249/5 "2022-08-19T13:01:22Z")

</div>

Try with:  
`static double p0Array[100], p1Array[100], p2Array[100]; // static so that they do not disappear`  
and:  
`static TF1 *eFunc = new TF1(...); // static so that it does not disappear`
