# GetMinumum value X axis for TH1

**URL:** <https://root-forum.cern.ch/t/getminumum-value-x-axis-for-th1/24424>\
**Category:** ROOT\
**Created:** [April 13, 2017, 8:50pm UTC](https://root-forum.cern.ch/t/getminumum-value-x-axis-for-th1/24424 "2017-04-13T20:50:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sharey](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@Sharey](https://root-forum.cern.ch/u/Sharey)\
**Post date:** [April 13, 2017, 8:50pm UTC](https://root-forum.cern.ch/t/getminumum-value-x-axis-for-th1/24424/1 "2017-04-13T20:50:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 13, 2017, 9:26pm UTC](https://root-forum.cern.ch/t/getminumum-value-x-axis-for-th1/24424/2 "2017-04-13T21:26:53Z")

</div>

EDIT: Wile’s solution is better:

> [@Wile\_E\_Coyote](#):
>
> TH1::FindFirstBinAbove  
> TH1::FindLastBinAbove

How about this?

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 13, 2017, 9:52pm UTC](https://root-forum.cern.ch/t/getminumum-value-x-axis-for-th1/24424/3 "2017-04-13T21:52:43Z")

</div>

[TH1::FindFirstBinAbove](https://root.cern.ch/doc/master/classTH1.html#a7f300b8fdf4f8c01ab11d840768b7595)  
[TH1::FindLastBinAbove](https://root.cern.ch/doc/master/classTH1.html#ad2a5ec067c5718f5c4b36995b19ec421)

---

<div class="post-metadata">

**Author:** ![Sharey](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@Sharey](https://root-forum.cern.ch/u/Sharey)\
**Post date:** [April 26, 2017, 3:45pm UTC](https://root-forum.cern.ch/t/getminumum-value-x-axis-for-th1/24424/4 "2017-04-26T15:45:08Z")

</div>

Thanks !! it worked!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 10, 2017, 3:45pm UTC](https://root-forum.cern.ch/t/getminumum-value-x-axis-for-th1/24424/5 "2017-05-10T15:45:14Z")

</div>

Closed on May 10, 2017, 3:45pm UTC

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