TGraph from cvs file

Dear All,

Probably,This is a simple question.
I want to draw a TGraph which data is provided from “file.txt”.
file.txt is …
[ul]
011/3/24,1:00,2780,2630
2011/3/24,2:00,2690,2560
2011/3/24,3:00,2630,2500
2011/3/24,4:00,2610,2500
2011/3/24,5:00,2750,2620

[/ul]
Format of “file.txt” is “date,time,value1,value2”.
For example “2011/3/24,1:00,2780,2630”,
“2011/3/24” is date,
“1:00” is time,
“2780” is value1,
“2630” is value2

How to draw TGraph “time vs value1” from “file.txt”?
Simply do

 TGraph *gr = new TGraph("file.cvs","%*lf,%lf,%lf,%*lf");
 gr->Draw("ap");

??

Please give me advice.

Best Regard,

Kimiaki

To build the TGraph from the a file you need to use this constructor:
root.cern.ch/root/html/TGraph.ht … :TGraph%10
You got that right.You need to read two columns as explained…

BUT…

The tricky part is the date. If you read it like it is written, then you should consider it as a character string because it has “/” in it. This will not work because TGraph doesn’t not support alphanumeric labels (at least not directly).

So the best would be to make a time axis for a TGprah as shown is the following example. You will have to convert the date s using TDatime. So it looks like the TGraph constructor using a file is not really appropriate in your case because you have to do some conversion before adding a point to a TGraph. You should read the file and then add the points one by one.

{
   TDatime da1(2008,02,28,15,52,00);
   TDatime da2(2008,02,28,15,53,00);

   double x[2],y[2];

   y[0] = 1.;
   y[1] = 2.;
   x[0] = da1.Convert();
   x[1] = da2.Convert();

   TGraph mgr(2,x,y);
   mgr.SetMarkerStyle(20);

   mgr.Draw("ap");

   mgr.GetXaxis()->SetTimeDisplay(1);
   mgr.GetXaxis()->SetNdivisions(-503);
   mgr.GetXaxis()->SetTimeFormat("%H:%M:%S");
   mgr.GetXaxis()->SetTimeOffset(0,"gmt");
}

time.pdf (12.5 KB)

Great !!! Thanks!!