NAN returned from TGraph::Eval()


I have a series of TGraphs that I both draw and evaluate using a TSpline3 to interpolate. Usually this works fine, but one of the graphs (named gFixNegNeg1) can be drawn fine as a smooth curve (using option “C”), but when I evaluate it then NAN is returned.

I’ve attached the ROOT file and provided short code to reproduce the problem. Any idea why this happens and how to fix it?


void test() {

  TFile *f = new TFile("test.root");
  TGraph *gGood = f->Get("gFixPosPos1");
  TGraph *gBad = f->Get("gFixNegNeg1");

  // This graph works fine
  TCanvas c0("c0", "c0", 600, 600);
  cout << gGood->Eval(0.9, 0, "s") << endl;

  // This graph looks fine but returns NAN
  TCanvas c1("c1", "c1", 600, 600);
  cout << gBad->Eval(0.9, 0, "s") << endl;

test.root (418 KB)

Your “gFixNegNeg1” TGraph has two “y” values for the “x” value 0.3 (try “gBad->Print();” and see points 3 and 4) and this creates the problem. Try to “Eval” it without the “S” option (or remove one of these harmful points).

Ah okay, that makes sense – thanks for the quick resolution!