Retrieving y values in TGraph objects

Hi
Double quick questions…
I Have a TGraph object that I have loaded with N number of points.
Q1. Do those points get sorted at all, or they are always fixed as in the same way as in the data array used to build the TGraph? My guess is that they follow alwasys the relation SetPoint(point number, x,y), right?

Is there an straight forward way to retrieve the y value based on an x value? I was trying to get access to the TGraph::GetHistogram() method but I guess that method is used to build the x axis units, right? Do I have to iterate through the xarray and yarray to find my y value based on my x value? Would there be an inconvenient if I fed my TGraph array with a random unsorted data pairs?

{
  gROOT->Reset();
   TCanvas *c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);
   Double_t x[100], y[100];
   Int_t n = 20;
   for (Int_t i=0;i<n;i++) {
     x[i] = i*0.1;
     y[i] = 10*sin(x[i]);
   }
   x[19]=0.5;
   y[19]=11;
   gr = new TGraph(n,x,y);
   gr->Draw("AC*");

   //Double_t ix   = gr->GetXaxis()->FindBin(1.57);


   TH1* h = gr->GetHistogram();
   
   Double_t ix   = h->GetXaxis()->FindBin(0.5);  //I get 26. My TGraph size is 20(???)
   Int_t nbin = h->GetXaxis()->GetNbins();  //I get 100... not 20.  However gr->GetN() returns 20 as expected.


///If I need to find the y value for x=0.5... what would be the proper way to do it? 

  

   return c1;
}

Hi,

The points in the TGraph are not sorted, they are stored with th eorder given by the user.

To evaluate a y value given x, you can use TGraph::Eval(x)
Unfortunately, there is a bug in 5.26 in this function, but it is fixed in the current trunk or the patch 5.26 versions (5.26a or 5.26b)

Best Regards

Lorenzo

1 Like

in a TGraph the points may be in any order (except in some cases where you want eg to compute the integral). See your code modified below and note the comment.

Rene

[code]{
gROOT->Reset();
TCanvas c1 = new TCanvas(“c1”,“A Simple Graph Example”,200,10,700,500);
Double_t x[100], y[100];
Int_t n = 20;
for (Int_t i=0;i<n;i++) {
x[i] = i
0.1;
y[i] = 10sin(x[i]);
}
x[19]=0.5;
y[19]=11;
gr = new TGraph(n,x,y);
gr->Draw("AC
");
gr->Print(“all”);

//search points (may be more than 1)
Int_t j = 0;
for (j=0;j<n;j++) {
if (TMath::AreEqualRel(0.5,gr->GetX()[j],1e-6)) {
printf(“found point %d with x=%g, y=%g\n”,j,gr->GetX()[j],gr->GetY()[j]);
}
}
return c1;
}
[/code]

In the case I want to compute an integral, then I will need to sort the points first?
Related to the TGraph::Eval() method,

I can totally assume that in the case of extracting Y for a requested value of X, ROOT will have the sorted data pairs? In the case that the function is not one-to-one (two or more Y values for a same X value), how would Eval() behave?

Thank you,

[quote=“moneta”]Hi,

The points in the TGraph are not sorted, they are stored with th eorder given by the user.

To evaluate a y value given x, you can use TGraph::Eval(x)
Unfortunately, there is a bug in 5.26 in this function, but it is fixed in the current trunk or the patch 5.26 versions (5.26a or 5.26b)

Best Regards

Lorenzo[/quote]

Lorenzo,
The bug is only on that release or in any older release. i have 5.25 but I am having problems getting a value from this function since it always returns zero.

I should probably update my ROOT…

see comments in TGraph::Eval source code for an explanation

Rene