# How to rebin if there is a prime number of bins?

_ROOT Version: 6.19
_Platform: OS 10.14.3

I have a histogram with 521 bins, I tried using Rebin(x) but turns out that it only works if the number of bins is divisible by x, which means that if you have a prime number of bins, there can’t be any x that works.

How do I solve this?, is there a way to eliminate a few bins from a histogram?, just eliminating one would solve my problems

Hi,
also asking other ROOT devs, I think your best, safest option is to create a new histogram with the desired content and copy the bins into it.

You might be able to convince the histogram that it has one bin less, but I’m not 100% this would not have weird side-effects:

``````TH1F h("h", "h", 251, 0., 251.);
auto ax = h.GetXaxis()
ax->Set(ax->GetNbins() - 1, ax->GetXmin(), ax->GetBinUpEdge(ax->GetNbins() - 1))
``````

Cheers,
Enrico

I think you can rebin your histogram. You will get a warning about “not an exact divider of nbins” and the last (up to “ngroup - 1”) bins will be neglected (the x-axis maximum will be adjusted accordingly).

``````TH1F h("h", "h", 251, 0., 251.);
std::cout << h.GetXaxis()->GetXmin() << " " << h.GetXaxis()->GetXmax() << std::endl;
h.Rebin(10); // ngroup = 10
std::cout << h.GetXaxis()->GetXmin() << " " << h.GetXaxis()->GetXmax() << std::endl;
``````
1 Like

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