Home | News | Documentation | Download

Problems with a fit function that uses TGraph->Eval (i. e. requires initialization)

Hi,

I’m having trouble with a fit function that uses TGraph->Eval;(). It is almost a direct copy from the example at https://root.cern.ch/doc/master/classTF1.html with the subheading " Using a lambda function as a general C++ functor object"

Essentially, I want to fit my histogram to a linear combination of two TGraphs. My code reads:

TF1 *fitfu = new TF1("fitfu",[&](double*x double*p){return p[0]*tWSgraph->Eval(x[0])+(1-p[0])*tBDgraph->Eval(x[0]);},0,.075,1);

fitfu->SetParLimits(0,0.,1.);
coherentscaled->Fit("fitfu"); 

If I comment everything out but the first line, I get no errors, but, of course, also no fit. If I
uncomment either the second or their line, then I get a bunch of errors that I don’t understand:

/opt/local/libexec/root6/include/root/Math/ParamFunctor.h:104:17: error: no matching function for call to object of type
      '(lambda at /Users/sklein/twopifit/./fitter.C:98:31)'
         return f(x,p);
                ^
/opt/local/libexec/root6/include/root/Math/ParamFunctor.h:88:35: note: in instantiation of member function
      'ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctor, (lambda at
      /Users/sklein/twopifit/./fitter.C:98:31)>::FuncEvaluator<(lambda at /Users/sklein/twopifit/./fitter.C:98:31)>::Eval'
      requested here
      return FuncEvaluator<Func>::Eval(fFunc,x,p);
                                  ^
/opt/local/libexec/root6/include/root/Math/ParamFunctor.h:70:4: note: in instantiation of member function
      'ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctor, (lambda at
      /Users/sklein/twopifit/./fitter.C:98:31)>::operator()' requested here
   ParamFunctorHandler(const Func & fun) : fFunc(fun) {}
   ^
/opt/local/libexec/root6/include/root/Math/ParamFunctor.h:238:17: note: in instantiation of member function
      'ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctor, (lambda at
      /Users/sklein/twopifit/./fitter.C:98:31)>::ParamFunctorHandler' requested here
      fImpl(new ParamFunctorHandler<ParamFunctor,Func>(f) )
                ^
/opt/local/libexec/root6/include/root/TF1.h:515:24: note: in instantiation of function template specialization
      'ROOT::Math::ParamFunctor::ParamFunctor<(lambda at /Users/sklein/twopifit/./fitter.C:98:31)>' requested here
         f->fFunctor = ROOT::Math::ParamFunctor(func);
                       ^
/opt/local/libexec/root6/include/root/TF1.h:505:38: note: in instantiation of member function
      'ROOT::Internal::TF1Builder<(lambda at /Users/sklein/twopifit/./fitter.C:98:31)>::Build' requested here
   ROOT::Internal::TF1Builder<Func>::Build(this,f); 
                                     ^
/Users/sklein/twopifit/./fitter.C:98:19: note: in instantiation of function template specialization 'TF1::TF1<(lambda at
      /Users/sklein/twopifit/./fitter.C:98:31)>' requested here
 TF1 *fitfu = new TF1("fitfu",[&](double*x double*p){return p[0]*tWSgraph->Eval(x[0])+(1-p[0])*tBDgraph->Eval(x[0])...
                  ^
/Users/sklein/twopifit/./fitter.C:98:31: note: candidate function not viable: requires single argument 'x', but 2 arguments
      were provided
 TF1 *fitfu = new TF1("fitfu",[&](double*x double*p){return p[0]*tWSgraph->Eval(x[0])+(1-p[0])*tBDgraph->Eval(x[0])...

I am confused because this seems to be very nearly a copy of the example. I have checked that the TGraphs and histogram look the way that I expect them to. Since this happens even with the SetParLimits command, I suspect a syntax problem of some sort.

Any hints would be appreciated? Does that example still work? If not, I would really appreciate a clear (fully fleshed out) example of how to do a fit to a function like this (or, more generally, a function that requires significant initialization before being called - the graphs are effectively read in from disk; I can’t do that for each call.

Thanks.

Spencer


_ROOT Version:6.06/08
_Platform:Mac/High Sierra 10.13.6
Compiler: Not Provided


Hello Spencer,

I quickly added three ` around the code, so the forum displays it properly.

Please check the lambda. You seem to be missing a comma in

[&](double*x double*p)

I hope that solves it.

Cheers,
Stephan

Stephan:

That fixed it. Thank you very much.

Spencer