From a reference or a pointer you can always do a Clone(), so saving your histogram.
Anyhow, your original code returns a valid copies of the histogram.
The problem is the way you use it in your test program. When you do
a.getHisto().GetXaxis();
the first function call (a.getHisto()) creates the context where to evaluate the second (GetAxis()); when this is done the context is deleted, together with the copy of the histogram. So the TAxis pointer you get back points to a non valid object.
You should save the histogram before using it:
TH2F h = a.getHisto();
TAxis* axis = h.GetXaxis();
or do not resolve the intermediate TAxis*, i.e
for (int i = 1; i != a.getHisto().GetXaxis()->GetNbins(); ++i)
{
const double x = a.getHisto().GetXaxis()->GetBinLowEdge(i);
cout << "x: " << x << endl;
}
but this would be super-inefficient, creating a copy of the histogram for each call.
From a reference or a pointer you can always do a Clone(), so saving your histogram.
Anyhow, your original code returns a valid copies of the histogram.
The problem is the way you use it in your test program. When you do
a.getHisto().GetXaxis();
the first function call (a.getHisto()) creates the context where to evaluate the second (GetAxis()); when this is done the context is deleted, together with the copy of the histogram. So the TAxis pointer you get back points to a non valid object.
You should save the histogram before using it:
TH2F h = a.getHisto();
TAxis* axis = h.GetXaxis();
or do not resolve the intermediate TAxis*, i.e
for (int i = 1; i != a.getHisto().GetXaxis()->GetNbins(); ++i)
{
const double x = a.getHisto().GetXaxis()->GetBinLowEdge(i);
cout << "x: " << x << endl;
}
but this would be super-inefficient, creating a copy of the histogram for each call.
G. Ganis[/quote]
I understand, but I don’t understand why my first version didn’t work.