How to draw a TF2 function with a compiled function by user?


ROOT Version (root_v6.13.02):
MacOS high Sierra 10.13.3; Xcode Version 8.3.3 (8E3004b)


I’m still a ROOT beginner. I’ve learned how to Draw a user-defined function TF1 function. But I failed to do so for a 2D plot after I tried the same approach as that of TF1.
I resorted to the TF2 Class in cern root, but I couldn’t quite understanding how to use this line:
TF2 (const char *name, Double_t(*fcn)(Double_t *, Double_t *), Double_t xmin=0, Double_t xmax=1, Double_t ymin=0, Double_t ymax=1, Int_t npar=0, Int_t ndim=2)

The following is my attempt:

Double_t myfunction(Double_t *xx, Double_t *yy, Double_t *par)
{    double x=xx[0];double y=yy[0];
    double f=sin(x)/x*sin(y)/y;
    return f;
}
void myfunc()
{
    TF2 *f1 = new TF2("myfunc",myfunction,-6,6,-6,6,0,2);//no paramater, 2-dimensional.
    f1->Draw("Surf1");
}

With the error report:

/myfunc.C:8:19: error: call to constructor of 'TF2' is ambiguous
    TF2 *f1 = new TF2("myfunc",myfunction,-6,6,-6,6,0,2);
                  ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/root_v6.13.02/etc/../include/TF2.h:53:4: note: candidate constructor [with PtrObj = double (double *, double *, double *),
      MemFn = int]
   TF2(const char *name, const  PtrObj& p, MemFn memFn, Double_t xmin, D...
   ^
/Applications/root_v6.13.02/etc/../include/TF2.h:71:4: note: candidate constructor [with Func = double (*)(double *, double *, double *)]
   TF2(const char *name, Func f, Double_t xmin, Double_t xmax, Double_t ...

Hope someone could help me out and I think this topic will also be useful to somebody else.

Sincerely

Double_t myfunction(const Double_t *xx, const Double_t * /*par*/)
{    double x=xx[0]; double y=xx[1];
1 Like
Double_t myfunction(Double_t *xx, Double_t *par)
{
   double x=xx[0];
   double y=xx[1];
   double f=sin(x)/x*sin(y)/y;
   return f;
}
void myfunc()
{
    TF2 *f1 = new TF2("myfunc",myfunction,-6.,6.,-6.,6.);//no paramater, 2-dimensional.
    f1->Draw("Surf1");
}
1 Like

Ah… Spent hours to find the error in my original long code.
Thank you, sir. Save me a lot of trouble.
:slight_smile:

Aw… Never expected such a reason. Thank you very much sir. (You can see I actually chopped my original long program to this.).
Sign with relief, finally.

BTW.:

  double f =
    ((std::abs(x) > 1.e-11) ? (std::sin(x) / x) : 1.) *
    ((std::abs(y) > 1.e-11) ? (std::sin(y) / y) : 1.);

I have improved the TF2 doc in the reference guide. It will appear tomorrow online.
Thanks to have pointed this lack of documentation.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.