Home | News | Documentation | Download

TGraph2D::GetHistogram


#1

Hi,

I often make empty TGraph objects set the axis titles and then fill them with points. As in this example:

TGraph *gr = new TGraph();
gr->GetXaxis()->SetTitle("X axis");
gr->GetYaxis()->SetTitle("Y axis");
for (int i=0; i < 10; ++i) gr->SetPoint(gr->GetN(),i,i);

Recently, I have tried this with TGraph2D, but the behavior is different and I receive the error

Is there any reason the behavior should be different?


#2

Hi,

Do you have a concrete example where it fails?

Cheers, Bertrand.


#3

Sorry I should have posted it that way, I even wrapped it in as a function for your convenience.

TGraph2D *graph() {
   TGraph2D *gr = new TGraph2D();
   gr->GetXaxis()->SetTitle("X axis");
   gr->GetYaxis()->SetTitle("Y axis");
   for (int i=0; i < 10; ++i) gr->SetPoint(gr->GetN(),i,i);
   return gr;
}

Using a TGrpah instead of a TGraph2D works with no problems.


#4

Hi,

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.


#5

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?


#6

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…

Cheers, Bertrand.


#7

Again it is not clear to me what the performance difference between TGraph and TGraph2D would be. I agree that is should be reviewed.

Thanks for the quick response.


#8

[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


#9

Bertrand,

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 ::slight_smile:

Rene


TMultiGraph->GetXaxis()->SetTitle (version 4.01)
#10
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:

   if (fNpoints <= 0) {
      Error("GetHistogram", "Empty TGraph2D");
      return 0;
   }

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.


#11

This problem is now fixed in ROOT 5.34 and ROOT 6
Thanks to have reported it.