Cloning TF1


A weird thing happens when one wants to clone an entitled TF1, the formula of which contains a TMath functions :

TF1 *f1 = new TF1 ("f1name","TMath::Exp(x)",0.,10.) ; f1->SetTitle("f1title") ; TF1 *f2 = (TF1*) f1->Clone("f2name") ;

Here’s the error message I get : ‘Error in TF1::Compile: Bad numerical expression : “f1title”’.
This does not seem so odd when one reads the TF1::Compile preamble (the formula is stored in fTitle)

Nevertheless, everything’s fine if one of the following actions is undertaken :

  • the formula does not contain a TMath function (an equivalent cmath function is fine : “exp(x)” in this case. This is unfortunately not always possible, e.g. TMath::Landau(x) doesn’t have any cmath equivalent)
  • the title of the function looks like a formula e.g. “sin(x)”,
  • the SetTitle() method is not used.

Of course using other TF1 mechanisms (like the C one “C - A general C function with parameters” ) can fix this :

Double_t myfunction (Double_t *x, Double_t *par) { return (TMath::Landau(x[0])) ; } void myfunc() { TF1 *f1 = new TF1 ("f1name",myfunction,0.,10.,0) ; f1->SetTitle("f1title") ; TF1 *f2 = (TF1*) f1->Clone("f2name") ; }

but still that might be a bug…

Cheers, Z


The title is the authoritative location of the formula (as least for now). The fact that Clone appear to work in some cases when you change the title is just an happy accident.