Rebin histogram such that each bin has one entry

Hi

I wonder if there exists a simple-ish way to rebin a 1d histogram such that every bin contains one entry?

Thanks

Lily

Hi Lily,

It’s not possible for a general histogram; not easy in the rest of the cases. :smiley: My guess is that you may be more interested in other objects (e.g., TTrees).

If you have bins that already have more than 1 entry, there is no way to rebin that histogram so that it has 1 entry per bin.

If you have a very sparsely populated histogram, it may be possible to come up with a variable-width histogram that does this, but it a) won’t be easy and b) almost certainly won’t do what you want.

What are you trying to do?

Cheers,

Charles

You can get some inspiration from this short example

Rene

void rebin1() { TH1F *h = new TH1F("h","test rebin",100,-3,3); Int_t nentries = 1000; h->FillRandom("gaus",nentries); Double_t *xbins = new Double_t[nentries+1]; Int_t k=0; TAxis *axis = h->GetXaxis(); for (Int_t i=1;i<=100;i++) { Int_t y = (Int_t)h->GetBinContent(i); if (y <=0) continue; Double_t dx = axis->GetBinWidth(i)/y; Double_t xmin = axis->GetBinLowEdge(i); for (Int_t j=0;j<y;j++) { xbins[k] = xmin +j*dx; printf("xbins[%d]= %g\n",k,xbins[k]); k++; } } TH1F *hnew = new TH1F("hnew","rebinned",k-1,xbins); //new rebinned histogram should have about 10 entries per bin hnew->FillRandom("gaus",10*nentries); hnew->Draw(); }

Hi Rene,

[quote=“brun”]You can get some inspiration from this short example
[/quote]

This is a nice (and useful) example of how to rebin based on one given distribution so that a similar distribution will be flat(ish), but doesn’t meet the OP (unrealistic) goal of exactly 1 entry per bin. :smiley:

Charles

Charles,

By construction, my example shows how to rebin with one entry per bin in the newly constructed histogram with variable bin sizes. In this case you get exactly one entry per bin, assuming that the histogram has been filled with weights =1.
Of course, the algorithm assumes that the Nj entries in bin j are now distributed in j equidistant bins.

Rene

Hi Rene,

[quote=“brun”]
Of course, the algorithm assumes that the Nj entries in bin j are now distributed in j equidistant bins.[/quote]

That’s my point. That’s almost certainly not a good assumption. :smiley:

Cheers,
Charles

[quote]That’s my point. That’s almost certainly not a good assumption. Very Happy
[/quote]
well, you do not destroy anything of the original assumption that all entries in one bin are uniformly distributed within one bin.

Rene

This is actually exactly what I need, thankyou.

What I want is to have approximately the same statistics in each bin, so this is fine.

Thanks again

Lily