Re: function pointer problem in TF1

Rene Brun (Rene.Brun@cern.ch)
Mon, 05 Jan 1998 13:15:01 +0100


Vlatko Balic wrote:

> Hi -- I declare 2 TF1 functions (as function pointers) and try to use them
> to define a third function. This does not seem to work. Here's the
> example:
>
> > double fun1(double *xVec, double *Param)
> > {
> > double x, fVal;
> > x=xVec[0];
> > fVal=Param[0]*x*x + Param[1]*x + Param[0];
> > return fVal;
> >}
> >
> >double fun2(double *xVec, double *Param)
> >{
> > double x, fVal;
> > x=xVec[0];
> > fVal=Param[0]*x*x + Param[1]*x + Param[0];
> > return fVal;
> >}
> >
> >TROOT root("TF1 test","TF1 test");
> >
> >void main()
> >{
> > double Params[3]={1.0, 1.0, 1.0};
> >
> > TF1 *f1 = new TF1("fun1", fun1, -10, 10, 3);
> > f1->SetParameters(Params);
> > TF1 *f2 = new TF1("fun2", fun2, -10, 10, 3);
> > f2->SetParameters(Params);
> > printf("\n f1(2)=%f", f1->Eval(2));
> > printf("\n f2(2)=%f", f2->Eval(2));
> >
> > // the following statement produces an error "ERROR 4 : Empty String"
> > TF1 *f3 = new TF1("fun3", "fun1 * fun2");
> >}
>
> This code generates the following output:
> >
> > f1(2)=7.000000
> > f2(2)=7.000000
> >*ERROR 4 :
> > Empty String
>
> So, there's a problem when defining f3. Any comments? Thanks.
>

The possibility to specify combinations of already existing functions
is only implemented for functions created with basic expressions,
not for precompiled or interpreted functions.
You can modify your example to create a 3rd function like
in the macro below:

Rene Brun

double fun1(double *xVec, double *Param)
{
double x, fVal;
x=xVec[0];
fVal=Param[0]*x*x + Param[1]*x + Param[0];
return fVal;
}

double fun2(double *xVec, double *Param)
{
double x, fVal;
x=xVec[0];
fVal=Param[0]*x*x + Param[1]*x + Param[0];
return fVal;
}

double fun3(double *xVec, double *Param)
{
return fun1(xVec, Param) + fun2(xVec, &Param[3]);
}

void balic()
{
double Params[6]={1, 1, 1, 1, 1, 1};

TF1 *f1 = new TF1("fun1", fun1, -10, 10, 3);
f1->SetParameters(Params);
TF1 *f2 = new TF1("fun2", fun2, -10, 10, 3);
f2->SetParameters(Params);
printf(" f1(2)=%f\n", f1->Eval(2));
printf(" f2(2)=%f\n", f2->Eval(2));

// the following statement produces an error "ERROR 4 : Empty String"
TF1 *f3 = new TF1("fun3", fun3, -10, 10, 6);
f3->SetParameters(Params);
printf(" f3(2)=%f\n", f3->Eval(2));
}