How to find x-value for all maxbin in 1D histogram

I was faced with a task "find x- value for all maxbin in 1D histogram ". I know how can i do it for 1 maxBIn

TH1F *hframe = new TH1F("hframe","hframe",100,0,60);
int binmax = hframe->GetMaximumBin();
    float x = hframe->GetXaxis()->GetBinCenter(binmax);
    cout << "max X- " << x<< endl;

I have 2 more peaks for which I need to know the value of x.Any idea?

I think you will have to make the list of maxima yourself, as root only returns one maximum (the first one it finds, see the code here).
You can first use GetMaximumBin to know the value and then loop over all bins using GetBinContent and save/print out the bin centres of all bins with contents equal to the maximum.

  Int_t Nbins = hframe->GetNbinsX();
  for (Int_t i=1; i<=Nbins; ++i) {
    if (hframe->GetBinContent(i)==binmax) cout << "max x = " << hframe->GetXaxis()->GetBinCenter(i) << endl;

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