Date-timestamp

Dear rooters,

I have a file with some columns separated with a withespace. the data in the 1 column they are a date, for example: 2015.10.31
the 2 column are the time, for example: 13:53:31
and the other columns contain a normal numbers.

If I read a file in TGraph, specifying the 3 and 4 columns, with the command :
TGraph *gr1 = new TGraph (“Data.csv” , "%*lg %*lg %lg %lg)
and I plot the TGraph, I obtain a error
Error in TGraphPainter::PaintGraph: illegal number of points (0)

Instead if I read the files, without specificy a columns, so default take the 1 and 2 columns, with the command:

TGraph *gr1 = new TGraph (“Data.csv” )

in this case when I plot I obtain: in the x-axis I have a 1st part of the data, for example 2015.10, like a number, and in y-axis I have a second part of date, for example 0.31

The problem is that only when I have a dates and times in the file, also with a csv format or txt format. I have also tryed to create a TNtuple I obtain a error:

Error in : only whitespace and new line can follow the last number on the line

So there is an method to read a file containing the date and time?

Thank you a lot for the help :wink:

See the last example here to see how to use TGraph with Time axis:

root.cern.ch/how/how-create-axis-time-units

Thanks for the reply.
But at the moment to set the timeaxis it isn’t the problem, the problem is that I can’t specify the columns in the file that I would like plot if in the file there a date and time.

Try:
TGraph *gr1 = new TGraph(“Data.csv”, “%*s %*s %lg %lg”);

As I have write in the 1st post, I have already done this:

If I read a file in TGraph, specifying the 3 and 4 columns, with the command :
TGraph *gr1 = new TGraph (“Data.csv” , "%*lg %*lg %lg %lg)
and I plot the TGraph, I obtain a error
Error in TGraphPainter::PaintGraph: illegal number of points (0)

See my post again. Compare precisely (i.e. character by character) what I wrote with your version.

Oh yes sorry, thank you a lot!!! :slight_smile:
Now I have the problem to plot a data-time and a values. An example of raw that I have on my file is:

2015.11.03 22.54:35 65.7
2015.11.03 22.54:40 70.1
etc.

There is a method to do this directly from the files?

If it not possbile plot directly from file I could put the data in the NTuple, but also in this case I have some problems. In fact:

  1. If the data are sotored in txt file and not contain the date or time the process it is ok
    But If the data contains the date and times I obtain this error:

" only whitespace and new line can follow the last number on the line "

  1. Another anomaly it is that if the data are stored in the csv file I obtain this error, like ROOT don’t see the white space:

" delimiter expected "

But if I put the same data, not in NTuple, but in TGraph, I don’t have no problem!!

Someone can help me please?

Thank a lot

Attach your data file here.

This is an example:

2016.03.10 16:43:55 492
2016.03.10 16:48:55 984
2016.03.10 16:53:55 1475
2016.03.10 16:58:55 1967
2016.03.10 17:03:55 2458
2016.03.10 17:08:55 2950
2016.03.10 17:13:55 3441.4

[code]#include “TString.h”
#include “TDatime.h”
#include “TGraph.h”
#include “TAxis.h”
#include “TCanvas.h”

#include
#include

void data(const char *fin = “data.txt”) {
if (!fin && !fin[0]) return; // just a precaution
std::ifstream ifs(fin, std::ifstream::in);
if (!ifs.good()) return; // just a precaution

TString ymd, hms;
Double_t v;

TGraph *g = new TGraph();

while(ifs >> ymd >> hms >> v)
{
ymd.ReplaceAll(".", “-”); // proper SQL date compatible format
// std::cout << ymd << " " << hms << " " << v << std::endl;
TDatime t(ymd + " " + hms); // "yyyy-mm-dd hh:mm:ss"
g->SetPoint(g->GetN(), t.Convert(), v);
}

if (g->GetN() > 1) {
g->GetXaxis()->SetTimeDisplay(1);
g->GetXaxis()->SetTimeFormat("#splitline{%y-%m-%d}{%H:%M:%S}%F1970-01-01 00:00:00");
g->GetXaxis()->SetLabelOffset(0.025);

TCanvas *c = new TCanvas("c", "c");
g->Draw("A*");
c->Modified(); c->Update();

}
}[/code]

Thank you a lot Pepe Le Pew!!! This is a perfect macro for to plot the data, thanks!!

But my first problem it is create a Ntuple, I need a Ntuple, so question remains, how can I create a Ntuple when the data included a date and hours? And I obtain the error:

" only whitespace and new line can follow the last number on the line "

And the second question is why when the file is in .csv ,ROOT din’t see the column and give me the error:
" delimiter expected "

Thank you very much

Try: { TTree *t = new TTree("t", "t"); t->ReadFile("data.txt", "ymd/C:hms/C:v/D"); t->Print(); t->Scan(); } Unfortunately, it seems that, if the data filename ends with “.csv” or “.CSV”, then the columns’ delimiter cannot be a “space” (by default it is ‘,’).

Perfect!! Now work!! however it is strange that with TTree work and with TNtuple no, can you tell me why?
And for the csv files do you think it is possible do something?

Thank you a lot again :wink:

TNtuple

Thank a lot!!!