Store Data with time in a Tree

I tried to store my Data with TDatime in the tree, in order to be able to draw TGraphs afterward with the treeviewer. Therefore I need the x axis as time-axis. I read in my Data as Integer with

tree->Branch(“staff”,&staff.year,…) and
sscanf(&line[0],"%d%d%d%d%d%d",&staff.year,…) to convert by
TDatime(staff.year,…).

This is a bit complected, it maybe would be easier to read just two strings for day and time but its possible…
Now I don’t know how to store all 6 data in one Tree.
I couldn’t find anywhere any example containing how to get the usual time format (yyyy-mm-dd hh:mm:ss) as time in a tree and setting the corresponding data in right intervals in a next step in relation to the time axis. Maybe its just because I’m still starting using ROOT but I really spend a lot of time for that problem.

P.S. The links of
root.cern.ch/root/html/TDatime.html
root.cern.ch/root/html/tutorials … ild.C.html
root.cern.ch/drupal/content/how- … time-units
Didn’t really help me but maybe I just oversight something (if, please tell me)

Thanks Wolfgang

Hi,

You should store directly the TDatime object: mytree->Branch("when.",&object); and then you have 2 choices, either use the string representation as the x values:mytree->Draw("when.AsString()");or use the SetTimeDisplay option:mytree->Draw("when.fDatime"); htemp->GetXaxis()->SetTimeDisplay(1); gPad->Modified();

Cheers,
Philippe.

thank you.
I just still have some little Problems:
I declared my Object (1), build a Branch (2), and tried to fill it (3).
TDatime time1; 1
t1.Branch(“time”,&time,“time”); 2
file2.open(“htest2.txt”,ios::in); 3
for ( i=1;i<10;i++)
{
file1 >>day>>time
time1 = TDatime(day,time)…}

before I saved time as a Char Array(Char_t day[11],time[13]…t1.Branch(“time”,time,“time/C”):wink: so I could go on with that, too.
Anyway there is still some mistake inside, (I’m sorry if its a simple problem, I’m still beginner…)
thanks Wolfgang

t1.Branch("time",&time,"time"); 2Using this syntax (the 3rd argument here is a ‘leaflist’) you are (inadvertently but explicitly) telling the TTree that ‘time’ is a float!

Since TDatime has a dictionary, all you need to do is:t1.Branch("time",&time);

Rather than:time1 = TDatime(day,time)..., you should consider time1.Set(day,time);

Cheers,
Philippe.

Thanks it almost works, I just have my data in format 2010-06-02 18:00:00.00,
As I stored the Data in a Char. Array I would convert them into Int (with atoi()) and use the Set function afterward. I thought it should be possible to use Strings in Set(…) but that didn’t work. Anyway, maybe there is a way more elegant to work with strings…
Thanks

Hi,

What do you mean. We have a TDatime::Set that take a const char*: root.cern.ch/root/html/TDatime.html#TDatime:Set%4

How did it fail for you?

Philippe.

I didn’t fail, thank you very much it works. I just wondered if it’s possible to use the strings without converting them (with atoi). But I think there is no way…
thanks Wolfgang

[quote]. I just wondered if it’s possible to use the strings without converting them (with atoi).[/quote]Humm I am confused … isn’t:mydatime.Set(line); // line is the string with the human readable date/time exactly what you are looking for?

Philippe

if I try :

mytree->Draw(“x:time.fDatime>>ht”);
ht->Get->GetXaxis()->SetTimeDisplay(1);
htemp->GetXaxis()->SetTimeFormat("%d/%m/%y");
gPad->Modified();

it draws the wrong data, although I did
mytree->Scan(“time”)
and it has been all right…

Sorry, I just could saw the massage too late.
I thought so too, but as I tried to put in the strings

file.open…
file>>date>>time (in the file:yyyy-mm-dd hh:mm:ss)
and then
myTDatimeobj.Set(date,time);
it didn’t work.

so I did:
read in day, time in an Char_t array (also in the tree)
convert the parts of the array in Int with atoi y= atoi(&day[0]); m=…
and then give them to myTDatimeobj.Set(y,m,…);

Complected?! But it worked expect that drawing Problem…

[quote]file.open…
file>>date>>time (in the file:yyyy-mm-dd hh:mm:ss)
and then
myTDatimeobj.Set(date,time);
it didn’t work. [/quote]Indeed, set takes only one string. So you need to do:

file>>date>>time; // (in the file:yyyy-mm-dd hh:mm:ss) std::string datetime = date + " " + time; myTDatimeobj.Set(datetime.c_str());

Cheers,.
Philippe.

mytree->Draw("x:time.fDatime>>ht"); ht->Get->GetXaxis()->SetTimeDisplay(1); htemp->GetXaxis()->SetTimeFormat("%d\/%m\/%y"); gPad->Modified();Humm The 3rd lines does not seem correct, shouldn’t it be? :mytree->Draw("x:time.fDatime>>ht"); ht->Get->GetXaxis()->SetTimeDisplay(1); ht->GetXaxis()->SetTimeFormat("%d\/%m\/%y"); gPad->Modified();

Philippe.

Sorry, That was just a mistake by copying it. To put the strings inside worked well, by scanning the tree its all right. But there is still something false by drawing it. (time alone and time on x axis against some values…) I’m sorry for asking so much but especially for that topic I didn’t found any explanation or example.

Hi,

The example I tried seems to work. Could you provide a complete running example showing the issues (also describe or provide a picture of the problem, i.e. what you expected vs what you saw)?

Cheers,
Philippe.

Here is my example:
The Datas:

2010-05-01 00:00:01.123	0.1277965657	0.12816317723
2011-12-02 10:00:02.111	0.1277965657	0.12816317723
2012-10-18 20:00:10.321	0.1277965657	0.12816317723
2013-05-18 10:00:13.99	0.1277965657	0.12816317723
2014-05-18 14:12:00.000	0.1277965657	0.12816317723
2015-08-18 15:01:00.000	0.1277965657	0.12816317723
2010-05-18 16:04:00.000	0.1277965657	0.12816317723
    The main Prog. steps:
 TDatime zeit; 
 string day, time, datetime;
   t1.Branch("ev",&ev,"ev/I");
   t1.Branch("zeit",&zeit);

file1.open("htest1.txt",ios::in);
 for ( i=1;i<10;i++)
   {
     file1 >>day>>time>>px>>py;
     datetime = day +" "+ time;
     zeit.Set(datetime.c_str());
     t1.Fill();
   }
  t1.Write();
  t1->Scan("zeit:px:ev");
             And the commends in another Prog. to draw:
       c2->cd(1);
     t1->Draw("px:zeit.fDatime");
     htemp->GetXaxis()->SetTimeDisplay(1);
     htemp->GetXaxis()->SetTimeFormat("%d\/%m\/%y");
     gPad->Modified();
       c2->cd(2);
     t1->Draw("px:zeit");

In the Result there are not the right Dates on the x axis, otherwise it’s ok.
If you don’t mind I could even try to analyze your example, if you send it.
Cheers Wolfgang

The second, string method has the right dates but there its a Problem if you have a lot of dates all of them are drawn and you can’t decrease it…

Hi,

Indeed, I can reproduce that neither graphs is very readable (either the axis overlaps or the date are not in increasing order). We are taking a look.

Cheers,
Philippe.

On the left plot you use a time axis. The date are in the right order but the labels overlap. To avoid the overlap you have two solutions:

  1. reduce the label size with:
    htemp->GetXaxis()->SetLabelSize();
  2. write the labels on two lines using #splitline:
    axis->SetTimeFormat("#splitline{%Y}{%d/%m}");
    You can combine both solutions of course.

On the right side your X axis has alphanumeric labels. They are just character strings. The concept of date is lost somehow…

You really can Reproduce it right?
I get on my time axis Labels with:
26/31/12
28/31/12
30/31/12
32/31/12
34/31/12
36/31/12
01/31/12
which is not in between 2010 and 2015 and somehow there are 31 months?
I checked out the intervals in between and they should be fine. I tried to add an time offset but that didn’t work, somehow it’s not drawing the right format…

I tried the macro Philippe sent me again, replacing “%y” by “%Y” in the time format to make the year more clear. I see it goes from 2026 to 2036. Is does the match the date shown in the other plot. I guess there is something wrong in your time conversion. See:
root.cern.ch/drupal/content/how- … time-units