TAxis::Set Question

Hello All,

Sometimes, when making variable width histograms, I by accident create a histogram with an axis like this -

TH2D* h2 = new TH2D("h2","h2", 1, 0, 1, 5, 0, 5)
double x[4] = {1.2, 1.8, 2.1, 4};
h2->GetXaxis()->Set( 4, x );

Then I go fill, and draw. The axis ranges look fine, and even the bin divisions look fine so at first glance it is hard to see the problem. However, memory has not been allocated for 3x5 bins, but only for 1x5 (excluding under and overflow) and as a result, there is some strange behaviour. The bins are not filled correctly, and this leads to some memory problem when trying to delete the histogram.

For example I have gotten, free(): invalid next size and double free or corruption memory problems.

Looking at the source of the function there does not seem to be a check. Only reason nBins is needed is to loop over the array correctly and set fNbins (which is dangerous!). Would it be useful to have this in there to avoid the problem later? It is just sometimes hard to see until you have stumbled over it a couple times.

-Yakov

Hi,

If you change the binning you should use TH1::SetBins and not TAxis::Set which will not allocate memory for the histogram contents

Lorenzo

Hello Lorenzo,

Thank you for the tip. I actually stumbled into this when working with THnSparse and there is no TH1 like method for setting bins like that. Still, do you think adding a warning in Set would help?

-Yakov

I’m curious if this is related to TH1::SetBins Missing Candidates

Hi,
Not really, the two topics are different. Here we discuss the using of TAxis::Set and not TH1::SetBins.

The problem is that one cannot produce a warning, because there is no way to detect if TAxis::Set is called directly from the user or from TH1::SetBins.
A possible solution would be to make TAxis::Set a private method and make it callable only from TH1

Cheers

Lorenzo

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.