Thanks for the simple example. And obviously the behavior is different between TGraph and TGraph2D. The histogram is only created after filling the graph. So this code works:
TGraph2D *graph()
{
TGraph2D *gr = new TGraph2D();
for (int i=0; i < 10; ++i) gr->SetPoint(gr->GetN(),i,i,i);
gr->GetXaxis()->SetTitle("X axis");
gr->GetYaxis()->SetTitle("Y axis");
return gr;
}
Cheers, Bertrand.
I understand that the behavior is different, but I do not understand why. They are very similar classes and I would expect them to behave similarly. Is there a reason that TGraph2D cannot create the histogram when it is requested instead of simply producing an error? Or should TGraph not produce a histogram and report an error as TGraph2D does?
Well, I agree this is weird, but for performance reasons the histogram is not automatically created in TGraph2D (e.g. when it is empty). And for historical reasons, we cannot change it. Hopefully, this should be reviewed when redesigning the histogram package…
[quote=“ksmith”]Again it is not clear to me what the performance difference between TGraph and TGraph2D would be[/quote]Well, creating a 2D histogram may take time and memory, so if it is not absolutely needed…
[quote=“ksmith”]Thanks for the quick response.[/quote]You’re very welcome
I do not understand your answer.
ksmith is reporting a real bug in TGraph2D::GetHistogram.
This bug must be fixed, and this has nothing to do with a redesign of the histogram package :
I understand that the behavior is different, but I do not understand why. They are very similar classes and I would expect them to behave similarly. Is there a reason that TGraph2D cannot create the histogram when it is requested instead of simply producing an error? Or should TGraph not produce a histogram and report an error as TGraph2D does?
Right now the method TH2D *TGraph2D::GetHistogram(Option_t *option) explecitly exits when there is no points in the TGraph2D. It starts with:
I am not sure wether it was a “lazy way” to implement it or if there is real serious reasons to do it like that.
I need to have a closer look to answer.