# How can I find all TH2 bins that contain a specific range of values without using for loops?

**URL:** <https://root-forum.cern.ch/t/how-can-i-find-all-th2-bins-that-contain-a-specific-range-of-values-without-using-for-loops/35671>\
**Category:** Newbie\
**Created:** [August 16, 2019, 7:26pm UTC](https://root-forum.cern.ch/t/how-can-i-find-all-th2-bins-that-contain-a-specific-range-of-values-without-using-for-loops/35671 "2019-08-16T19:26:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggsAndCoffee](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/ecb155/32.png) [@eggsAndCoffee](https://root-forum.cern.ch/u/eggsAndCoffee)\
**Post date:** [August 16, 2019, 7:26pm UTC](https://root-forum.cern.ch/t/how-can-i-find-all-th2-bins-that-contain-a-specific-range-of-values-without-using-for-loops/35671/1 "2019-08-16T19:26:52Z")

</div>

Hi all. I’m trying to avoid using for loops and manually querying all the bins in a TH2 that I have, and I’m wondering if ROOT has some functionality that lets me get back a list of all bin indices whose bins have a certain range of values? Something like “FindBinsWithThisValue” as a function in TH2 would be great, does something like that exist?

I’m using ROOT version 6.10. Thanks!

---

<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:** [August 16, 2019, 7:28pm UTC](https://root-forum.cern.ch/t/how-can-i-find-all-th2-bins-that-contain-a-specific-range-of-values-without-using-for-loops/35671/2 "2019-08-16T19:28:34Z")

</div>

[TH2 Class Reference](https://root.cern.ch/doc/master/classTH2.html)

---

<div class="post-metadata">

**Author:** ![eggsAndCoffee](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/ecb155/32.png) [@eggsAndCoffee](https://root-forum.cern.ch/u/eggsAndCoffee)\
**Post date:** [August 16, 2019, 7:47pm UTC](https://root-forum.cern.ch/t/how-can-i-find-all-th2-bins-that-contain-a-specific-range-of-values-without-using-for-loops/35671/3 "2019-08-16T19:47:28Z")

</div>

Thanks for the answer, @Wile_E_Coyote. The best that I could find in the documentation was GetBinWithContent2, which only returns a single set of bin indices. That means that I’d still need to work this function into a loop, and make sure that the loop is adjusting where it’s directing GetBinWithContent2 to look, which is what I wanted to avoid. Is there any way around this, something like “GetAllBinsWithContent”?

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [August 18, 2019, 11:42am UTC](https://root-forum.cern.ch/t/how-can-i-find-all-th2-bins-that-contain-a-specific-range-of-values-without-using-for-loops/35671/4 "2019-08-18T11:42:12Z")

</div>

I guess that it cannot be done without a loop.
