TF2 Constructor error

I am running some code from a few years back and I get an error for the TF2 constructor for this line of code:

TF2 *fit = new TF2(“fit”, gauss2d_fit_bb, 1330, 1400, 1330, 1400, 11);

gauss2d_fit_bb is a C function defined as Double_t gauss2d_fit_bb(Double_t *x, Double_t *par).

I have also tried TF2 *fit = new TF2(“fit”, gauss2d_fit_bb, 1330, 1400, 1330, 1400, 11, 2); where I assume that ndim is the number of dimensions, but I get the same error: no matching constructor for initialization of ‘TF2’

It seems to be correct from the documentation and it also ran fine previously. Is there a mistake I am making here?

Hi,

this code on ROOT 6.07 works:

Double_t gauss2d_fit_bb(Double_t *x, Double_t *par)
{
  return 1;
}

void f(){
   auto fit = new TF2("fit", gauss2d_fit_bb, 1330, 1400, 1330, 1400, 11);
};

What error are you getting running what code with which ROOT version?

I am running ROOT 6.04/02 and the error I get is
input_line_44:2:7: error: no matching constructor for initialization of ‘TF2’
(new TF2((“fit”, gauss2d_fit_bb, 1330, 1400, 1330, 1400, 11)))
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I will try a new version of root and report back.

Instead of:
… TF2((“fit”, …
you should have:
… TF2(“fit”, …

I am using just one set of parentheses, but in the error, there seems to be extra ones appended. Here is the exact line copied and pasted from my code.

TF2 *fit = new TF2(“fit”, gauss2d_fit_bb, 1330, 1400, 1330, 1400, 11);

I get the same error using ROOT 6.06/00.

input_line_44:2:7: error: no matching constructor for initialization of ‘TF2’
(new TF2((“fit”, gauss2d_fit_bb, 1330, 1400, 1330, 1400, 11)))

I tried a copy and paste to a new file and used a second browser and I get the same error. I also checked in a hex editor to see if there were any invisible characters and I didn’t find any. My code is below.

{
  gROOT->Reset();
  gROOT->ProcessLine(".L ./matrix_functions.C");

  char root_file[20];
  strcpy(root_file, "gambb.root");
  //cout << "Enter root file name: ";
  //cin >> root_file;
  TFile *f = new TFile(root_file, "update");
  hist->GetXaxis()->SetRangeUser(1330, 1400);
  hist->GetYaxis()->SetRangeUser(1330, 1400);
  hist->Draw("surf2");
  TF2 *fit = new TF2("fit", gauss2d_fit_bb, 1330, 1400, 1330, 1400, 11);
  Double_t params[11] = {1361, 1364, 4, 0.018, 0.004, 2, 5, 80, 5, 5, 5};
  fit->SetParameters(params);

  TFitResultPtr result = hist->Fit("fit", "LL, S, 0");
  fit->Draw("surf, same");
  result->Write();
  f->Write();
}

Perhaps there are other errors there? matrix_functions.C has the definition of gauss2d_fit_bb.

Your macro will work with ROOT 5 only.
With ROOT 6 you meet the problem: Calling custom classes in ROOT 6

Hi,

the issue is that ROOT6 does not interpret the macros line by line as CINT did for ROOT5.
As a result, the variable name you use in the constructor is not available to the interpreter.
There are many solutions which would allow you to solve this issue, also with minimal reformulations of your macro. For example:
o Load the macro with the function before running the macro, e.g. invoking ROOT like this

root -e "gROOT->ProcessLine(\".L ./matrix_functions.C\");" myOtherMacro.C

or like this

root [0] .L ./matrix_functions.C 
root [1] .x myOtherMacro.C

o Include matrix_functions.C in the myOtherMacro.C with an #include statement.

Cheers,
Danilo

Thanks. I tried both of your suggestions and they worked. Thank you.

The macros worked without the #include statement. Is this necessary?

Hi,

Glad it worked.
No, they are not necessary if you adopt the first option and they are if you do not specify the load explicitly when invoking root.

Danilo