I was trying to find a good way to determine if a TF1 is properly defined and thought I found what I need with TF1::IsValid.
Unfortunately either it is bugged, or I don’t understand the purpose of this method. It seems to returns false when using lambda functions even though TF1:SetParameter(s) or TF1:Draw returns perfectly valid results.
A simple example
TF1* myf = TF1("myf", [](double* x, double* p){return p[0]*x[0]+p[1];}, -10, 10, 2);
myf->SetParameters(1, 2);
myf->Draw(); <= This displays the expected result
cout << myf->IsValid() << endl; <= returns false ???
Did someone mistakenly write kFALSE in place of kTRUE for the functor case? The comment above it seems to imply that.
Bool_t TF1::IsValid() const
{
if (fFormula) return fFormula->IsValid();
if (fMethodCall) return fMethodCall->IsValid();
// function built on compiled functors are always valid by definition
// (checked at compiled time)
if (fFunctor && fSave.empty()) return kFALSE;
return kTRUE;
}
The following script indicates that TF1::IsValid returns false because fSave is empty. The question is what is the purpose of checking fSave.
$ root test.C
root [0]
Processing test.C...
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
Compiled based function: myf based on a functor object. Ndim = 1, Npar = 2
Save: 0x0
IsValid: False
root [1]
Trying to force a save with the following led to a segmentation violation: