# How to combine two C++ user defined function in one function with some calculation of both?

**URL:** <https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939>\
**Category:** Newbie\
**Created:** [June 26, 2024, 12:39pm UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939 "2024-06-26T12:39:50Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 26, 2024, 12:39pm UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/1 "2024-06-26T12:39:50Z")

</div>

Ref: [https://root.cern.ch/doc/master/classTF1.html](https://root.cern.ch/doc/master/classTF1.html)  
// Macro myfunc.C

[Double\_t](https://root.cern.ch/doc/master/classdouble.html) myfunction([Double\_t](https://root.cern.ch/doc/master/classdouble.html) \*[x](https://root.cern.ch/doc/master/legend1_8C.html#a1cf21b68373bbb5839af0adcd2aba7fa), [Double\_t](https://root.cern.ch/doc/master/classdouble.html) \*par)

{

[Float\_t](https://root.cern.ch/doc/master/RtypesCore_8h.html#aa65ddc5b79a777f9c9cfb5945492e2ac) xx =[x](https://root.cern.ch/doc/master/legend1_8C.html#a1cf21b68373bbb5839af0adcd2aba7fa)[0];

[Double\_t](https://root.cern.ch/doc/master/classdouble.html) [f](https://root.cern.ch/doc/master/RSha256_8hxx.html#a357394e0f6f88c8a57bd893ab28dc8f8) = [TMath::Abs](https://root.cern.ch/doc/master/namespaceTMath.html#af682354cea223351f1306bf98a858dfd)(par[0]\*sin(par[1]\*xx)/xx);

return [f](https://root.cern.ch/doc/master/RSha256_8hxx.html#a357394e0f6f88c8a57bd893ab28dc8f8);

}

void myfunc()

{

auto [f1](https://root.cern.ch/doc/master/legend1_8C.html#abeb628b1c10a75ff3a4579f727637979) = new [TF1](https://root.cern.ch/doc/master/classTF1.html#ab6ab7f7769712f3b81cc48d48a210126)(“myfunc”,myfunction,0,10,2);

[f1](https://root.cern.ch/doc/master/legend1_8C.html#abeb628b1c10a75ff3a4579f727637979) → [SetParameters](https://root.cern.ch/doc/master/classTF1.html#ad3b4503c22e70e5c219031d83e137905)(2,1);

[f1](https://root.cern.ch/doc/master/legend1_8C.html#abeb628b1c10a75ff3a4579f727637979) → [SetParNames](https://root.cern.ch/doc/master/classTF1.html#a5c60948e734b250f66f01caaa86d9c91)(“constant”,“coefficient”);

[f1](https://root.cern.ch/doc/master/legend1_8C.html#abeb628b1c10a75ff3a4579f727637979) → [Draw](https://root.cern.ch/doc/master/classTF1.html#a922c3f3cc1014da7b7850b2d9b0533ca)();

}

void myfit()

{

auto [h1](https://root.cern.ch/doc/master/legend1_8C.html#aaf2545ecc018288fcf442904a5af9e83) = new [TH1F](https://root.cern.ch/doc/master/classTH1F.html)(“h1”,“test”,100,0,10);

[h1](https://root.cern.ch/doc/master/legend1_8C.html#aaf2545ecc018288fcf442904a5af9e83) → [FillRandom](https://root.cern.ch/doc/master/classTH1.html#a6229a437465f46c694734ea6b2fc39cd)(“myfunc”,20000);

[TF1](https://root.cern.ch/doc/master/classTF1.html) \*[f1](https://root.cern.ch/doc/master/legend1_8C.html#abeb628b1c10a75ff3a4579f727637979) = ([TF1](https://root.cern.ch/doc/master/classTF1.html) \*)[gROOT](https://root.cern.ch/doc/master/TROOT_8h.html#a851dd11b3478372a25ba6eff6dcfa5b1)-\>GetFunction(“myfunc”);

[f1](https://root.cern.ch/doc/master/legend1_8C.html#abeb628b1c10a75ff3a4579f727637979) → [SetParameters](https://root.cern.ch/doc/master/classTF1.html#ad3b4503c22e70e5c219031d83e137905)(800,1);

[h1](https://root.cern.ch/doc/master/legend1_8C.html#aaf2545ecc018288fcf442904a5af9e83) → [Fit](https://root.cern.ch/doc/master/classTH1.html#a63eb028df86bc86c8e20c989eb23fb2a)(“myfunc”);

}

**Can I add myfunction2 and then create “auto f1= new TF1(“myfunc”, myfunc + myfunc2, 0, 10, 2)” ?**

Example exists in this version below , but not in c++ user defined functions?

{

auto fcos = new [TF1](https://root.cern.ch/doc/master/classTF1.html#ab6ab7f7769712f3b81cc48d48a210126) (“fcos”, “[0]\*cos(x)”, 0., 10.);

fcos-\>SetParNames( “cos”);

fcos-\>SetParameter( 0, 1.1);

auto fsin = new [TF1](https://root.cern.ch/doc/master/classTF1.html#ab6ab7f7769712f3b81cc48d48a210126) (“fsin”, “[0]\*sin(x)”, 0., 10.);

fsin-\>SetParNames( “sin”);

fsin-\>SetParameter( 0, 2.1);

auto fsincos = new [TF1](https://root.cern.ch/doc/master/classTF1.html#ab6ab7f7769712f3b81cc48d48a210126) (“fsc”, “fcos+fsin”);

auto fs2 = new [TF1](https://root.cern.ch/doc/master/classTF1.html#ab6ab7f7769712f3b81cc48d48a210126) (“fs2”, “fsc+fsc”);

}

---

<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:** [June 26, 2024, 1:13pm UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/2 "2024-06-26T13:13:07Z")

</div>

Hi,

Thanks for the post.  
I think I understand that you would like to build a TF1 wrapping the sum of two arbitrary C++ functions (please correct me if I am wrong).  
If that’s what you want, it’s certainly possible, and very easy if the two functions are known to the interpreter. Below an example:

```auto
TF1 f("f", "cosh(x)+sinh(x)",-1,1);
f.Draw()

```

Results in

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/0/9/09f8de36855561e14e5bd3777927d63697b29b93.png)

I hope this helps.

Best,  
D

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 26, 2024, 2:21pm UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/3 "2024-06-26T14:21:33Z")

</div>

I mean combination of 2 user defined functions with variable and parameters?

@Danilo

I asked if this is possible: **Can I add myfunction2 and then create “auto f1= new TF1(“myfunc”, myfunc + myfunc2, 0, 10, 2)” ?**

I think I found an example.

---

<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:** [June 26, 2024, 9:10pm UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/5 "2024-06-26T21:10:59Z")

</div>

Hi,

Thanks for the clarification. The answer is yes. Minimal snipped to show the functionality:

```auto
TF1 a("f1", "cos(x)", 0., 10.);
TF1 b("f2", "x+1", 0., 10.);
TF1 c("f3", "f1(x)+f2(x)", 0., 10.);
c.Draw();

```

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/0/1/01d1d4a26b34e442697d94e913e6fd96d72ee30a.png)

I hope this gets you started.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 27, 2024, 7:16am UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/6 "2024-06-27T07:16:21Z")

</div>

Thanks. I see my mistake now. Instead of calling the function by name without quotation mark, we need to call it like “f1(x) + f2(x)” .

Let me play around for a bit.

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 27, 2024, 7:37am UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/7 "2024-06-27T07:37:02Z")

</div>

> [@Danilo](#):
>
> ```auto
> TF1 a("f1", "cos(x)", 0., 10.);
> TF1 b("f2", "x+1", 0., 10.);
> TF1 c("f3", "f1(x)+f2(x)", 0., 10.);
> c.Draw();
> 
> ```

When I type like yours, I got empty canvas. However, I got it right if I type like below. Why is that?

TCanvas _c1 = new TCanvas(“c1”, “Combined Functions”, 800, 600);  
c1-\>cd();  
TF1_ f1 = new TF1(“f1”, “cos(x)”, 0., 10.);  
TF1\* f2 = new TF1(“f2”, “x+1”, 0., 10.);  
TF1\* f3 = new TF1(“f3”, “f1(x)+f2(x)”, 0., 10.);  
f3-\>Draw();

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 27, 2024, 8:12am UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/8 "2024-06-27T08:12:29Z")

</div>

> [@spyhunter01](#):
>
> Why is that?

Because if `a`, `b`, and `c` are allocated on the stack, they will be destroyed as soon as these `TF1`’s go out of scope.

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 27, 2024, 10:32am UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/9 "2024-06-27T10:32:37Z")

</div>

Why this code doesn’t scale?

#include \<TF1.h\>  
#include \<TCanvas.h\>  
#include \<TMath.h\>

// Define the standalone function  
double myFunction(double\* x, double\* params) {  
return params[0] \* TMath::Sin(x[0]);  
}

void CombineFunctions4(){ //copyAndScaleFunction() {  
// Create the original TF1 object with the standalone function  
TF1\* originalFunction = new TF1(“originalFunction”, myFunction, 0, 10, 1);  
originalFunction-\>SetParameter(0, 1.); // Set the initial parameter

```
// Copy the TF1 object
TF1* copiedFunction = (TF1*)originalFunction->Clone("copiedFunction");

// Scale the copied function by a factor of 10
copiedFunction->SetParameter(0, 10. * copiedFunction->GetParameter(0));

// Print the original and scaled functions
originalFunction->Print();
copiedFunction->Print();

// Draw the functions
TCanvas* canvas = new TCanvas("canvas", "Function Copy and Scale Example", 800, 600);
originalFunction->SetLineColor(kRed);
originalFunction->Draw();
copiedFunction->SetLineColor(kBlue);
copiedFunction->Draw("SAME");

```

}

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 27, 2024, 10:59am UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/10 "2024-06-27T10:59:56Z")

</div>

@Danilo

I have a user defined c++ function with parameters.

double f2(double \*x,double \*par){  
return x[0]\*par[0]\*par[1]\*30. ;  
}

TF1\* f1 = new TF1(“f1”, f2, 0. , 1000. , 2);  
f1-\>SetNpx(50000.);  
f1-\>FixParameter(0,3. );  
f1-\>FixParameter(1,4. );  
double Scaling = 10. ;

```
TF1*f3 = new TF1("f3", "Scaling* f1(x)", 0. , 1000. );
f3->SetNpx(50000.);

```

f1-Draw();  
f3-\>Draw(“same”);

HOW TO FIX THIS CODE?

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 27, 2024, 11:28am UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/11 "2024-06-27T11:28:18Z")

</div>

> [@spyhunter01](#):
>
> ```auto
> double Scaling = 10. ;
> TF1* f3 = new TF1("f3", "Scaling* f1(x)", 0. , 1000. );
> 
> ```

This can’t work. Try the following instead:

```auto
double Scaling = 10. ;
TF1* f3 = new TF1("f3", "[0]* f1(x)", 0. , 1000. );
f3->SetParameter(0, Scaling);

```

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 27, 2024, 11:44am UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/12 "2024-06-27T11:44:29Z")

</div>

@yus  
What about the number of parameters carried from f1?  
f1 is defined with 2 parameters, but we haven’t stated any parameter for f3 at all.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 27, 2024, 11:56am UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/13 "2024-06-27T11:56:46Z")

</div>

That I don’t know for sure. Maybe `f3` defined this way already “knows” that both parameters of `f1` were already fixed…

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 27, 2024, 11:57am UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/14 "2024-06-27T11:57:50Z")

</div>

@Danilo  
With same logic :  
If STP\_PDTA is a user defined c++ function like :

double STP\_PDTA(double \*x,double \*par){  
…  
}

what is wrong with this bit of the code below:

```
f_STP_Proton = new TF1("f_STP_Proton", STP_PDTA , min_Limit, max_Limit,6); //set up parameters
// *****set up parameters*****************************************************************
double Scaling = 10.;

f_STP_Proton->SetNpx(50000.);
f_STP_Proton->FixParameter(0,RME_proton );
f_STP_Proton->FixParameter(1,z_proton ); // z=1 for , e, positron, p, d, t, z=2 for alpha
f_STP_Proton->FixParameter(2,RME_e );
f_STP_Proton->FixParameter(3,N_medium );
f_STP_Proton->FixParameter(4,Z_medium );
f_STP_Proton->FixParameter(5,MEP_medium );

f_STP_Proton->Draw("");
TF1* f2 = new TF1("f2", "[0]*f_STP_Proton(x)" , min_Limit, max_Limit,1); //HOW TO FIX HERE, THE NEW SCALED FUNCTION????????????
f2->SetParameter(0,Scaling);
f2->SetLineColor(kBlack);
f2->Draw("same");

```

---

<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:** [June 27, 2024, 12:11pm UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/15 "2024-06-27T12:11:23Z")

</div>

Hi,

What is exactly the problem?

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [June 27, 2024, 1:05pm UTC](https://root-forum.cern.ch/t/how-to-combine-two-c-user-defined-function-in-one-function-with-some-calculation-of-both/59939/16 "2024-06-27T13:05:05Z")

</div>

Hi,  
My codes are usually long codes with headers, function definitions and macros are all in separate files, so I don’t wanna post it all to create a mess here.

The problem is : Error in TFormula::Eval: Formula is invalid and not ready to execute

To simply it , I post here a part of it after I juggled with it many times with failure.  
I don’t post the STP\_PDTA function with 6 parameters to avoid the mass. It’s working anyways.

double STP\_PDTA(double \*x,double \*par){ …}

double min\_Limit= 10. ;  
double max\_Limit= 1.e2 ;

```
f_STP_Proton = new TF1("f_STP_Proton", STP_PDTA , min_Limit, max_Limit,6); //set up parameters
// *****set up parameters*****************************************************************
double Scaling = 10.;

f_STP_Proton->SetNpx(50000.);
f_STP_Proton->FixParameter(0,RME_proton );
f_STP_Proton->FixParameter(1,z_proton ); // z=1 for , e, positron, p, d, t, z=2 for alpha
f_STP_Proton->FixParameter(2,RME_e );
f_STP_Proton->FixParameter(3,N_medium );
f_STP_Proton->FixParameter(4,Z_medium );
f_STP_Proton->FixParameter(5,MEP_medium );

f_STP_Proton->Draw("");
TF1* f2 = new TF1("f2", "[0]*f_STP_Proton(x)" , min_Limit, max_Limit,1);
f2->SetNpx(50000.);
f2->SetParameter(0,Scaling);
f2->SetLineColor(kBlack);
f2->Draw("same");

```

I assume the constructor for f2 function is not designed this way. All I want is to scale the “double STP\_PDTA(double \*x,double \*par){ …}”. function I created with parameters.
