Adding several TF1


I need to add two TF1s. The following works fine :

TF1* f = new TF1("ff","x") TF1* g = new TF1("gg","x") TF1* h = new TF1("hh","ff+gg")

However, I need to create f and g from an already defined function, ie :

TF1* a = new TF1("aa","x") TF1* f = new TF1(*a) TF1* g = new TF1(*a)

Then I am not able to add f and g :

f -> SetName("ff") g -> SetName("gg") TF1* h = new TF1("hh","ff+gg")
gives (and it is the same with SetTitle) :

[quote]Error in TFormula::Compile: Bad numerical expression : "ff"
Error in TF1::TF1: function: hh/ff+gg has 0 parameters instead of 1[/quote]

What am I doing wrong ?


Try the following instead:

TF1* a = new TF1("aa","x"); TF1 *f = (TF1*)a->Clone("ff"); TF1 *g = (TF1*)a->Clone("gg"); TF1* h = new TF1("hh","ff+gg");
When calling the copy constructor, there is a problem with teh name of the
source function. Clone is taking care of it.


Thanks, it works fine.

Does this approach to combining TFormulae work for all TF1 types or just the “Type A” ones shown here?


It only works for the “type A”, i.e. the one defined via string. For TFormulae defined via a function, you will need to provide a new function that does the combination.