GetMinumum value X axis for TH1

Hi rooters,

I am filling TH1 histogram with data, I want to know what is the minimum /maximum values of x that was filled by data. When I use:
h->GetXaxis()->GetXmin(); // it will return to 0 or to the value the I defined as the minimum for h.
But I want the real value of x minimum/maximum (after filling from data points) so I can redefine the limits of x axis.
Could anyone tell me how to do that?

Thanks!

Sharey

EDIT: Wile’s solution is better:

How about this?

int minXbin = 1, maxXbin = hist->GetNbinsX();
for (int i=1; i <= hist->GetNbinsX(); i++) {
   if (hist->GetBinContent(i)) {
      minXbin = i;
      break;
   }
}
for (int i=hist->GetNbinsX(); i >= 1; i--) {
   if (hist->GetBinContent(i)) {
      maxXbin = i;
      break;
   }
}
hist->GetXaxis()->SetRange(minXbin, maxXbin);
1 Like

TH1::FindFirstBinAbove
TH1::FindLastBinAbove

1 Like

Thanks !! it worked!

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