I am trying to create TGraphs out of TTrees with its Draw method. So far so good, but I have problems with getting TDatimes after retrieving the TGraph
In my tree, I have a branch with three Double_ts and another one with a TDatime.
This is what I am doing:
// I plot a histogram out of a tree
TCanvas* c = new TCanvas();
myTree->Draw("example.mean:time", "", "*L");
// then i retrieve my graph
if (auto graph = dynamic_cast<TGraph*>(c->GetPrimitive("Graph"))) {
graph->SetName(plot.name.c_str());
graph->SetTitle(plot.title.c_str());
...
// when I save the canvas, the dates are preserved
c->SaveAs("graph-canvas.root");
// when I save just the graph, i get only incremental numbers on x axis
graph->SaveAs("graph-direct.root");
...
}
I want to work only on the graph, not the canvas, but then I lose the dates when I retrieve it. Would you know what I might be doing wrong? I am attaching the tree, and the graph-direct saved by that piece of code.
Thanks for the help!
As you can see the TCanvas does not contain “just only” the TGraph. The TH2F htemp draws the axis. Thats why it is fine when you save the whole canvas.
I have one more question concerning this topic.
I am continuing to experiment with plotting graphs against time with TTree::Draw. The problem is, that with large number of data points, the dates become very small and congested on the x axis. I tried to fix that by doing:
Unfortunately, it has no effect if I save the canvas and observe the file with TBrowser. On the other hand, if I open it on https://root.cern/js/latest/ , the new dates format seems to be applied, but the dates are wrong (1995-01-01).
Would you know what I might be still doing wrong?
This is the mentioned graph. graph.root (9.5 KB)
I do not understand how you can get that may labels asking for 5 primary divisions. Can you post a small running macro reproducing that plot . It is still using trend-tree.root ?
You might want to try reproduce it on a tree with larger amount of entries. Attaching a new one: trend-tree-more-entries.root (6.7 KB)
This is how I obtain graph.root:
auto c = new TCanvas();
auto f = new TFile("trend-tree-more-entries.root");
ExampleTrend->Draw("example.mean:time", "", "*L");
if (auto histo = dynamic_cast<TH1*>(c->GetPrimitive("htemp"))) {
histo->GetXaxis()->SetTimeDisplay(1);
histo->GetXaxis()->SetNdivisions(505);
histo->GetXaxis()->SetTimeFormat("%Y-%m-%d %H:%M");
}
c->SaveAs("graph.root");
I think I am missing your point here. Now I know that axes are drawn first by using a histogram. The “time” branch consists of TDatime values, so I expected that they would be supported, given the fact that I can add a branch with a TDatime variable.
I am not sure what is preventing me from correctly redrawing the X axis with less dates/alphanumberic labels, so it looks clearer.
I have to check more closely but it seems, in that case, the X axis is not a time axis but an axis with character strings (alphanumeric), therefore all the labels found in the tree are plotted.
To make sure, this is how I declare and fill a branch with time:
mTrend = std::make_unique<TTree>();
// mTime is of type TDatime
mTrend->Branch("time", &mTime);
...
// later in the loop
while(...) {
mTime = TDatime();
// then updating other variables
...
// when all are updated, I generate a new row
mTrend->Fill();
}
So I should have used another type for storing the time, so it is not interpreted as a string?
I have just tried using TTimeStamp instead and with these corrections it shows time correctly:
Initially I have avoided TTimeStamp because of the time_t overflow and there is a little risk it may concern this piece of code. Is there any extended version of TTimeStamp in ROOT available?
As you can see for example in this example the axis limits for tie axis are defined with TDatime::Convert(); which returns an int. Then the axis is set as a time one using SetTimeDisplay(1);. But the axis limits are int not char.