# Bins with equal number of entries

**URL:** <https://root-forum.cern.ch/t/bins-with-equal-number-of-entries/41806>\
**Category:** Newbie\
**Created:** [October 10, 2020, 4:30pm UTC](https://root-forum.cern.ch/t/bins-with-equal-number-of-entries/41806 "2020-10-10T16:30:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [October 10, 2020, 4:30pm UTC](https://root-forum.cern.ch/t/bins-with-equal-number-of-entries/41806/1 "2020-10-10T16:30:04Z")

</div>

Dear,

I want to have a TH1 binning with every bin having the same number of entries, however, I’m also plotting another histogram on the same canvas, and I want this second TH1 to have the same binning as the first one. It is important because I need a ratio plot of the two. The first part of my question was already asked, but I can’t find a solution. I’m sure this method is intensively used, is there a function or an efficient way to implement it?

Many thanks!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 12, 2020, 1:00pm UTC](https://root-forum.cern.ch/t/bins-with-equal-number-of-entries/41806/2 "2020-10-12T13:00:29Z")

</div>

may be @moneta can help. But if each bin have the same number of entries and if each entry has the same weight you will get a flat histogram ?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [October 12, 2020, 1:41pm UTC](https://root-forum.cern.ch/t/bins-with-equal-number-of-entries/41806/3 "2020-10-12T13:41:41Z")

</div>

Hi,

If you have the data points before , this can be done with the TKDTreeBinning class, which optimally divide the space (find your bin histograms) to have same content in each bin.  
An example (in 2D) is here

> **[ROOT: tutorials/math/kdTreeBinning.C File Reference](https://root.cern.ch/doc/master/kdTreeBinning_8C.html)**

The histogram will be flat, but since the bin volumes are different, one is interested in displaying the data density, i.e. number of events in bin divided by the bin volume

Lorenzo

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [October 12, 2020, 2:35pm UTC](https://root-forum.cern.ch/t/bins-with-equal-number-of-entries/41806/4 "2020-10-12T14:35:33Z")

</div>

Hi Couet and Lorenzo,

Many thanks for the solution, indeed I meant bins with the same number of entries but different bin widths, since the data is not uniformly distributed. Basically, I want to reduce the statistical errors in the tails of the distributions, so I’m not sure if this is the best way to do it. If you have any other suggestions, I would be very grateful to hear them!
