Empty histogram from a reduced TH2F

Dear experts,
I want to read a set of files and then
1)create a 2d histogram. Form this i extract a 1d histogram, say X (See function getX). But if
2) I reduce the size the same 2d histogram in #1 and try to get the same X as in #1, it results to an empty histo. What did i miss?


TH1F *readDataFile(char* fname, int nch) {
  int ch;
  float fc;
  TH1F *h = new TH1F("h1","",nch,-0.5,nch-0.5);
  h->Sumw2();
  FILE* fp=fopen(fname,"r");
  if (fp==NULL) {
    printf("Could not open file %s\n",fname);
    return h;
  }
  for (int j=0; j<nch; j++) {  
      fscanf(fp,"%d %f",&ch,&fc);     
      h->SetBinContent(j+1,fc);
  }
  fclose(fp);  
  return h;
}

// Creates a 2D histogram from a set of files (fn is the format of the file name)
TH2F* createScan(char* fn, int mi, int ma, int step, int nch) {    
  char fname[1000];
  TH1F *hdum;int z=5;
  TH2F *h2 = new TH2F("h2","",nch,-0.5,nch-0.5,((ma-mi)/step)+1,mi-(Double_t)step/2.,ma+(Double_t)step/2.);
  h2->SetStats(kFALSE);
  for (int i=mi; i<ma+1; i=i+step) {
    sprintf(fname,fn,i);    
    hdum=readDataFile(fname, nch);
    for (int j=0; j<nch; j++)
      h2->Fill(j,i,hdum->GetBinContent(j+1));
    delete hdum;    
  }
  return h2;
 
}


TH2F*  resize(TH2F* h2,Double_t  irowmin,Double_t  irowmax,Double_t  icolmin,Double_t  icolmax) {
  
	Int_t ix,iy;
	Int_t nbinsx, nbinsy;
	Double_t ymin,ymax, xmin,xmax, xstep, ystep;

 	xmin=h2->GetXaxis()->GetXmin();
	xmax=h2->GetXaxis()->GetXmax();
	ymin=h2->GetYaxis()->GetXmin();
  	ymax=h2->GetYaxis()->GetXmax();
  	nbinsx=h2->GetNbinsX();
  	nbinsy=h2->GetNbinsY();
  	xstep=(xmax-xmin)/nbinsx;
  	ystep=(ymax-ymin)/nbinsy;
  	nbinsx=(irowmax-irowmin)/xstep+1;
  	nbinsy=(icolmax-icolmin)/ystep+1;

  	TH2F *hresized=new TH2F("hsmall",h2->GetTitle(),nbinsx, irowmin,irowmax,nbinsy, icolmin, icolmax);

  	for (ix=0; ix<nbinsx; ix++) {
   	for (iy=0; iy<nbinsy; iy++) {
      	hresized->SetBinContent(ix+1, iy+1, h2->GetBinContent(ix+1+(irowmin-xmin)/xstep,iy+1+(icolmin-ymin)/ystep ));
    	}
  	}
  	return hresized;
}


TH1F* getX(TH2F* h2, int chan) { //createScan(..) for the first argument  is OK. But if resize(...) is used in the first argument, 1D histo is empty.
  int nstep=h2->GetYaxis()->GetNbins();
  Double_t mi=h2->GetYaxis()->GetXmin(); 
  Double_t ma=h2->GetYaxis()->GetXmax(); 
       
 TH1F* h1=new TH1F("h1", "",nstep,mi,ma);
 for (int i=1; i<nstep+1; i++) {    	
    h1->SetBinContent(i, h2->GetBinContent(chan+1,i));    		
 }  

  return h1;
} 

Thanks!

In the “getX” routine, check that “1 <= chan <= h2->GetNbinsX()” and that “nstep” (e.g. “nstep” > 1), “mi” and “ma” (“mi” < “ma”) are reasonable.
in the “resize” routine, check that:
“xmin” <= “irowmin” < “irowmax” <= “xmax”
“ymin” <= “icolmin” < “icolmax” <= "ymax"
and make sure that the newly calculated “nbinsx” (e.g. “nbinsx” > 1) and “nbinsy” (e.g. “nbinsy” > 1) are reasonable.