At the very least this loop is weird/wrong:[code] int nentries = hnew.GetEntries();
for (int v=0; v<nentries;v++){
if (v%minEventsPerBin==0){
BinCenterObs.push_back(hnew.GetBinCenter(v));
BinContentObs.push_back(hnew.GetBinContent(v));
}
}[/code]as GetBinCenter does not expect an ‘entry index’ but a bin number …
So, could you possibly indicate what the loop should be like in order work as I need it ?? I know, that it starts to be annoying with all continuous posts from my side (also similar topic How to make custom binning ), but my “ultimate” goal as I already said, is to be able to bin a histo with custom#events/bin and maybe pass this to a TAxis for further processing / next plots of the same variable…and as the days pass by and I cannot get into this, I am starting to be really frustrated… So, if by any change you know to implement this (or even correct my existing scripts), in order to have this I would be more obliged
I am probably not understand your goal, but it seems to me that you can not create this histogram only from the histogrammed version of the data (because there is missing information). You might vaguely approximate the result by calculating (pseudo code): double entries_per_bins = h->GetEntries() / h->GetNbins();
Double_t *xq = new Double_t[nbins+1];
Double_t *content = new Double_t[nbins+1];
xq[0] = h->GetXaxis()->GetXmin();
content[0] = h->GetBinContent(0); // underflow
xq[nbins] = h->GetXaxis()->GetXmax();
content[nbins] = h->GetBinContent(nbins); // overflow
double entries_so_far = 0;
int new_i = 1;
for(int i = 1; i < nbins; ++i) {
entries_so_far += h->GetBinContent(1);
if (entries_so_far >= entries_per_bins || (i==(nbins-1)));
xq[ new_i ] = h->GetXaxis()->GetBinUpEdge( i );
content[new_i] = entries_so_far;
entries_so_far = 0;
}
}
etc ...
as I said
a) it’s a very tricky problem,
b) I will post the code once I wrote it - probably tomorrow.
In the meantime I have learned how to solve it (n^2 in memory, n^3 in CPU time with n the number of bins of the original histogram - if anyone can find a better algorithm let me know!)
here you go! As I said: it’s complex And thanks to Jakob Blomer for coming up with this algorithm! Run as
.x mergebins.C+
The function mergebins() creates an example histogram and then lets the algorithm do its job. The result is better for more bins (but the CPU time goes like nbins^3!) and the fewer target bins / the more target bin content you require. Please read the NOTE at the top of the file!
Thanks a lot for this --it is really helpful…Nevertheless, can you directly point me what (or where I need to modify it in order to preserve the integral (ie making a binning according to a given #events/bin instead of #bins in the final histo)
Yes, on a second thought you are completely right… Also, I managed to write a script in order to have a certain binning according to #events per bin, ie the final plot should be a flat distrubution… Nevertheless, I am doing that while filling the histo (from the “original” data )and not from an already created TH1 object
Yes, on a second thought you are completely right… Also, I managed to write a script in order to have a certain binning according to #events per bin, ie the final plot should be a flat distrubution… Nevertheless, I am doing that while filling the histo (from the “original” data )and not from an already created TH1 object
All best[/quote]
Dear Alkass!
Could you please post your modified script that does the optimization during filling?