FindFirstBinAbove in a given range

Hello, I have a histogram such as that one


I am interested in searching the first bin above a given threashold for a given peak. I am awar about the FindFirstBinAbove function. However, if I use it, I get a value for the whole histogram.

Instead, I would like to get the bin in a given range such as between 2000 and 4000 as shown below:

Do you have any idea how to do it?

TH1::FindFirstBinAbove could be easily modified and submitted as a pull request. Or it could be used as a framework for a custom function. Here is the modified function:

 Int_t TH1::FindFirstBinAbove(Double_t threshold, Int_t axis, Int_t firstBin=1, Int_t lastBin=-1) const
 {
    if (fBuffer) ((TH1*)this)->BufferEmpty();
 
    if (axis != 1) {
       Warning("FindFirstBinAbove","Invalid axis number : %d, axis x assumed\n",axis);
       axis = 1;
    }
    if (lastBin < 0 || lastBin > fXaxis.GetNbins()) {
        lastBin = fXaxis.GetNbins();
    }
    for (Int_t bin=firstBin;bin<=lastBin;bin++) {
       if (RetrieveBinContent(bin) > threshold) return bin;
    }
    return -1;
 }
1 Like

Thanks. Indeed, I used a similar solution. For the pull request, it could be interesting. @couet what do you think about that?

Looks like a good idea. But I think @moneta has his word to say …

I agree with this improvement. Please submit a Pull Request,

Thank you

Lorenzo

@pamputt do you want to do it or would you prefer that I do?

You can do it since you have already almost done the job. Note that the patch should also apply for at least TH1::FindLastBinAbove. We may also consider to do the same for TH2::FindFirstBinAbove and TH3::FindFirstBinAbove.
If you are too busy, I can submit the pull request.

Pull request:

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