TGraphErrors Reading Input from File

According to the documentation for TGraphErrors, there is a constructor that reads input text files. The documentation, http://root.cern.ch/root/html/TGraphErrors.html#TGraphErrors:TGraphErrors@8, states that a format of “%lg %lg %lg” will read the first three columns a as X, Y, and EY. When running the following script, errors are read into EX. As shown in the plot.

TGraphErrors* g= new TGraphErrors("test.csv", "%lg %lg %lg", ","); g->Draw("AP");

Is this a mistake in documentation of implementation.


can you post the csv file ?

Ahh, I knew I forgot something.
test.csv (198 Bytes)

All if fine for me. I did the follwoing test:

{
TGraphErrors* g= new TGraphErrors("test.csv", "%lg %lg %lg", ",");
Double_t *x=g->GetX();
Double_t *y=g->GetY();
Double_t *ex=g->GetEX();

for (int i=0; i<8; i++) printf("%g %g %g\n",x[i],y[i],ex[i]);

}

which gives:

root [0] .x csv.C
240.27 -3.88522 0.126246
266.67 -3.768 0.0526381
319.8 -3.76976 0.0353706
349.54 -3.62117 0.0572677
481.94 -3.7652 0.106624
554.4 -4.08538 0.302589
975.8 -4.69185 0.0815721
1048.1 -4.97544 0.208396

which is exactly your file …

I think that what ksmith meant is that in the documentation, it is written

This is not EY but EX

Ah yes but looking at the code it seems that this feature applies only in the case there is no delimiter defined.

replace all “,” by " "in your csv file (global edit) and it will work as you expect

root [0] .x csv.C
240.27 -3.88522 0.126246
266.67 -3.768 0.0526381
319.8 -3.76976 0.0353706
349.54 -3.62117 0.0572677
481.94 -3.7652 0.106624
554.4 -4.08538 0.302589
975.8 -4.69185 0.0815721
1048.1 -4.97544 0.208396
root [1] 
{
TGraphErrors* g= new TGraphErrors("test.csv", "%lg %lg %lg" );
Double_t *x=g->GetX();
Double_t *y=g->GetY();
Double_t *ey=g->GetEY();

for (int i=0; i<8; i++) printf("%g %g %g\n",x[i],y[i],ey[i]);

}
$ cat test.csv 
240.27 -3.88522 0.126246
266.67 -3.768 0.0526381
319.8 -3.76976 0.0353706
349.54 -3.62117 0.0572677
481.94 -3.7652 0.106624
554.4 -4.08538 0.302589
975.8 -4.69185 0.0815721
1048.1 -4.97544 0.208396

Exactly, the documentation does not make it clear that the functionality of this method changes when a delimiter is specified. Is this a documentation or implementation mistake?

It is a documentation issue. I will try to improve.

1 Like

It would be nice to have this feature for csv files. Is this difficult to implement?

The code is totally different. You can have a look if you want. I am not sure why the author did not implement it that way, but looking at the code it looks more complex.

Any way a global edit or a small “sed” command on your csv file to replace ; by blanck will do the job.

If you come with a working implementation we will be please to include it.