# GetEntries function for TH2F

**URL:** <https://root-forum.cern.ch/t/getentries-function-for-th2f/45677>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [July 4, 2021, 7:46pm UTC](https://root-forum.cern.ch/t/getentries-function-for-th2f/45677 "2021-07-04T19:46:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aman\_Upadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aman_upadhyay/32/23958_2.png) [@Aman\_Upadhyay](https://root-forum.cern.ch/u/Aman_Upadhyay)\
**Post date:** [July 4, 2021, 7:46pm UTC](https://root-forum.cern.ch/t/getentries-function-for-th2f/45677/1 "2021-07-04T19:46:28Z")

</div>

I need to get multiple TH1F histograms from a TH2F. One TH1F for each Y bin. I searched for GetEntries() function for TH2F to run a loop over bins but that did not help. Then I decided to run the loop over the variable itself by finding the first and last bin with binwidth.

* * *

```auto
	double firstbin = h2_s->GetYaxis()->FindBin(ymin);
	double lastbin = h2_s->GetYaxis()->FindBin(ymax);
	double binwidth = h2_s->??
 	for(int y=ymin; y<ymax; y=y+binwidth){
 		Int_t biny = h2-s->GetYaxis()->FindBin(y);
		TH1D *h1_s = h2_s->ProjectionX("",biny,biny);}

```

* * *

As you can see I am not sure how to get the firstbin, lastbin or binwidth. I don’t have ymin and ymax values either. Is there a better way to do this? Or if this is ok how can I get the required values?

* * *

ROOT Version:6.24/00  
Platform:(NAME=“HefftorLinux”,ID\_LIKE=“arch”,VERSION\_ID=v2021-02-05)  
Compiler: gcc 11.1.0

* * *

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 4, 2021, 8:02pm UTC](https://root-forum.cern.ch/t/getentries-function-for-th2f/45677/2 "2021-07-04T20:02:09Z")

</div>

Maybe something like this:

```auto
   int firstbin = h2_s->GetYaxis()->GetFirst();
   int lastbin = h2_s->GetYaxis()->GetLast();
   for (int biny=firstbin; biny<lastbin; ++biny) {
      TH1D *h1_s = h2_s->ProjectionX("",biny,biny);
   }

```

---

<div class="post-metadata">

**Author:** ![Aman\_Upadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aman_upadhyay/32/23958_2.png) [@Aman\_Upadhyay](https://root-forum.cern.ch/u/Aman_Upadhyay)\
**Post date:** [July 5, 2021, 5:33pm UTC](https://root-forum.cern.ch/t/getentries-function-for-th2f/45677/3 "2021-07-05T17:33:11Z")

</div>

Thanks that worked!
