# FindFirstBinAbove for THStack

**URL:** <https://root-forum.cern.ch/t/findfirstbinabove-for-thstack/15589>\
**Category:** ROOT\
**Created:** [February 4, 2013, 6:53pm UTC](https://root-forum.cern.ch/t/findfirstbinabove-for-thstack/15589 "2013-02-04T18:53:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [February 4, 2013, 6:53pm UTC](https://root-forum.cern.ch/t/findfirstbinabove-for-thstack/15589/1 "2013-02-04T18:53:21Z")

</div>

Hi, I quite enjoy making histograms with a fixed bin width where I set the maximum/minimum bins to very large values to avoid overflows. E.g. in PyROOT:

```auto
binwidth = 0.1
minbin,maxbin = -1000,1000
nbins = (maxbin-minbin)/binwidth
h = ROOT.TH1F("h","h",nbins,minbin,maxbin)
```

Then to draw the histogram properly for looking at, I use:

```auto
firstbin = h.FindFirstBinAbove(0)
lastbin = h.FindLastBinAbove(0)
h.GetXaxis().SetRange(firstbin,lastbin)
```

I have now a THStack with multiple histograms in it, and it is tedious to have to create a set of first/last bins and SetRange to the minimum/maximum of those sets. Would it be possible to add a FindFirstBinAbove and FindLastBinAbove method to THStack that returns the first/last bin of any of the histograms contained therein? Or is there instead a better way to properly set the axis of a THStack such that only the range of populated bins are displayed?

Thanks,  
Jean-François

---

<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:** [February 4, 2013, 8:31pm UTC](https://root-forum.cern.ch/t/findfirstbinabove-for-thstack/15589/2 "2013-02-04T20:31:13Z")

</div>

Try to play with this:

```auto
TH1 *h = ((TH1 *)(YourHStack->GetStack()->Last())); // the "SUM"
Int_t firstbin = h->FindFirstBinAbove(0);
Int_t lastbin = h->FindLastBinAbove(0);
TCanvas *c = new TCanvas("c", "THStack SUM");
h->Draw();

```

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [February 5, 2013, 12:07am UTC](https://root-forum.cern.ch/t/findfirstbinabove-for-thstack/15589/3 "2013-02-05T00:07:14Z")

</div>

Thanks, that helps. In my own code so far I was iterating over THStack-\>GetHists() rather than GetStack(). Is there an important difference between these two methods of THStack? In my tests they seem to contain the same collection of histograms.

Also, is there any reason to use THStack-\>GetStack()-\>Last() over THStack-\>GetHists()[0]? It seems like they would all have the same axes in the end, since this is the purpose of the THStack.

Btw, my code is working now from your example, but I am wondering if there are pitfalls from using GetHists(), or using the zeroth histogram instead of the Last() one.

Jean-François

---

<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:** [February 5, 2013, 12:11am UTC](https://root-forum.cern.ch/t/findfirstbinabove-for-thstack/15589/4 "2013-02-05T00:11:31Z")

</div>

See the “fHists” and “fStack” data members of the [THStack](http://root.cern.ch/root/html/THStack.html) class and the [THStack::BuildStack](http://root.cern.ch/root/html/THStack.html#THStack:BuildStack) method description.  
Try (compare the output, e.g. see the “number of entries” and the “total sum” of histograms):  
YourHStack-\>GetHists()-\>Print();  
YourHStack-\>GetStack()-\>Print();
