GetEntries function for TH2F

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.


	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


Maybe something like this:

   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);
   }

Thanks that worked!

1 Like