TF1 pointing to a struct member function

Hi,
I need help to fit several histograms with a variation of the same function.
The way I found to create these functions is as a member function of a struct containing the parameters that modify the function. The problem is that then I cannot plug this function into the TF1 constructor, as they are member functions.

This is a simplified example of my code:

struct FuncStruct {
  double myXe;
  double myN2;
  int k;
  double myFunction(double *x, double *par)
  {
    double A=par[0];//common 
    double B=par[1];//common
    double P1=par[2+k];
    double TA=A*myN2 + B*myXe;
    return P1*TMath::Exp(-x[0]/TA);
  }
};
  FuncStruct F1[2];
  F1[0]=FuncStruct({5.0,2.0,0});
  F1[1]=FuncStruct({1.0,3.0,1});

  TF1 *f[N];
  for(int i=0; i<2; i++) f[i]= new TF1(Form("f%i",i),&F1[i].myFunction,0,100);

Then I get the error:

error: cannot create a non-constant pointer to member function
  for(int i=0; i<2; i++) f[i]= new TF1(Form("f%i",i),&F1[i].myFunction,0,100);

Is there a way to point to the member function, or should I find another way around?
I’ve trying with std::bind (), but I didn’t manage.

Defining the TF1 as a parametrized TString does work, but it is not a solution as the function is not always writable as a string…

Thanks and best regards,
J.

Hi,

You should pass a pointer to the struct and a member function pointer, as shown in

f[i]= new TF1(Form("f%i",i),&F1[i],&FuncStruct::myFunction,0,100);

Lorenzo

Thanks for replying, @moneta. :slight_smile:

Thanks!
I just had to add the number of parameters after the pointer to the member function:

f[i]= new TF1(Form("f%i",i),&F1[i],&FuncStruct::myFunction,3,0,100);

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