TF1 user made arithmetic operation

Dear Rooters

I checked the forum and I found only a reference to the next question, and even it is marked as SOLVED I didn’t see the solution. So I ask again.

I created a function, I called labangle, and it is called 6 times with different parameters, and then I plot them. The macro works as expected. But then I’d like to sum two of them creating a new function (ratio1), unsuscesfully, with the following error.

Error in TFormula::Compile: Empty String
Error in TF1::TF1: function: rat/(protonangle5 + protonangle2) has 0 parameters instead of 1

I checked every TF1 object and the title is empty; well, the output of protonangleX->GeTitle() is always labangle (the function created in other place of the macro).

	Double_t low = 0.03;
	Double_t upp = 0.08;

	TF1 *protonangle1 = new TF1("protonangle1", labangle, low, upp, 2);
	protonangle1->SetParameters(-0.01, .000511);//fixed value of t

	TF1 *protonangle2 = new TF1("protonangle2", labangle, low, upp, 2);
	protonangle2->SetParameters(-0.02, 0.000511);//fixed value of t
	
	TF1 *protonangle3 = new TF1("protonangle3", labangle, low, upp, 2);
	protonangle3->SetParameters(-0.03, 0.000511);//fixed value of t

	Double_t mulow = 0.044;

	TF1 *protonangle4 = new TF1("protonangle4", labangle, mulow, upp, 2);
	protonangle4->SetParameters(-0.01, .105);//fixed value of t
	
	TF1 *protonangle5 = new TF1("protonangle5", labangle, mulow, upp, 2);
	protonangle5->SetParameters(-0.02, 0.105);//fixed value of t
	
	TF1 *protonangle6 = new TF1("protonangle6", labangle, mulow, upp, 2);
	protonangle6->SetParameters(-0.03, 0.105);//fixed value of t


	Double_t ralow = 0.064;
	Double_t raupp = 0.078;

	TF1 *ratio1 = new TF1("rat", "protonangle5 + protonangle2", ralow, raupp);

I think this is similar to https://root.cern.ch/root/html/TF1.html#F3.

Any advice/help/solution? At least, where is my mistake? I think it is trivial, but I didn’t find any hint.

Thank you in advance

Carlos

Hi,
If the TF1 is made is made from a C++ function (a functor or a free function) you cannot use it inside a TFormula expressions (using a string).
however you can combined using C++, for example using a lambda (possible with C++11):

   auto function1 = [] (double *x, double *) { return sin(x[0]); };
   TF1 f1("f1",function1, 0, 5, 0); 

   auto function2 = [] (double *x, double *) { return cos(x[0]); };
   TF1 f2("f2",function2, 0, 5, 0);


   TF1 fcomb("fcomb",[&](double *x, double *) { return f1(x)+f2(x); }, 0, 5, 0);

   fcomb.Draw(); 

Lorenzo