# TAxis::Set Question

**URL:** <https://root-forum.cern.ch/t/taxis-set-question/24337>\
**Category:** ROOT\
**Created:** [April 5, 2017, 11:38pm UTC](https://root-forum.cern.ch/t/taxis-set-question/24337 "2017-04-05T23:38:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ykulinich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/96bed5/32.png) [@ykulinich](https://root-forum.cern.ch/u/ykulinich)\
**Post date:** [April 5, 2017, 11:38pm UTC](https://root-forum.cern.ch/t/taxis-set-question/24337/1 "2017-04-05T23:38:40Z")

</div>

Hello All,

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

```auto
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

---

<div class="post-metadata">

**Author:** ![moneta2](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/97f17d/32.png) [@moneta2](https://root-forum.cern.ch/u/moneta2)\
**Post date:** [April 10, 2017, 1:41pm UTC](https://root-forum.cern.ch/t/taxis-set-question/24337/2 "2017-04-10T13:41:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ykulinich](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/96bed5/32.png) [@ykulinich](https://root-forum.cern.ch/u/ykulinich)\
**Post date:** [April 10, 2017, 10:38pm UTC](https://root-forum.cern.ch/t/taxis-set-question/24337/3 "2017-04-10T22:38:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 10, 2017, 11:14pm UTC](https://root-forum.cern.ch/t/taxis-set-question/24337/4 "2017-04-10T23:14:04Z")

</div>

I’m curious if this is related to [TH1::SetBins Missing Candidates](https://root-forum.cern.ch/t/th1-setbins-missing-candidates/24318)

---

<div class="post-metadata">

**Author:** ![moneta2](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/97f17d/32.png) [@moneta2](https://root-forum.cern.ch/u/moneta2)\
**Post date:** [April 11, 2017, 9:22am UTC](https://root-forum.cern.ch/t/taxis-set-question/24337/5 "2017-04-11T09:22:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [April 25, 2017, 9:31am UTC](https://root-forum.cern.ch/t/taxis-set-question/24337/6 "2017-04-25T09:31:40Z")

</div>

Closed on April 25, 2017, 9:31am UTC

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