Re: Rebinning a TH2

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Jan 11 2000 - 17:12:22 MET


Mike Miller wrote:
> 
> Does anyone have a macro for rebinning two dimensional
> histograms?
> 

Hi Mike,
Here is a macro to do this job.

Rene Brun

void rebin2(TH1 *h, Int_t ngx, Int_t ngy)
{
   //Rebin 2-d histogram h, grouping ngx bins together along X
   //and ngy bins together along Y
   //NB: this macro ignores histogram errors if defined
   
   //make a clone of h
   TH1 *hold = (TH1*)h->Clone();
   hold->SetDirectory(0);

   Int_t  nbinsx = hold->GetXaxis()->GetNbins();
   Int_t  nbinsy = hold->GetYaxis()->GetNbins();
   Float_t xmin  = hold->GetXaxis()->GetXmin();
   Float_t xmax  = hold->GetXaxis()->GetXmax();
   Float_t ymin  = hold->GetYaxis()->GetXmin();
   Float_t ymax  = hold->GetYaxis()->GetXmax();
   Int_t nx = nbinsx/ngx;
   Int_t ny = nbinsy/ngy;
   h->SetBins (nx,xmin,xmax,ny,ymin,ymax);

   //loop on all bins to reset contents and errors
   Double_t cu;
   Float_t bx,by;
   Int_t ix,iy,ibin,bin,binx,biny;
   for (biny=1;biny<=nbinsy;biny++) {
      for (binx=1;binx<=nbinsx;binx++) {
         ibin = h->GetBin(binx,biny);
         h->SetBinContent(ibin,0);
      }
   }
   //loop on all bins and refill
   for (biny=1;biny<=nbinsy;biny++) {
      by  = hold->GetYaxis()->GetBinCenter(biny);
      iy  = h->GetYaxis()->FindBin(by);
      for (binx=1;binx<=nbinsx;binx++) {
         bx = hold->GetXaxis()->GetBinCenter(binx);
         ix  = h->GetXaxis()->FindBin(bx);
         bin = hold->GetBin(binx,biny);
         ibin= h->GetBin(ix,iy);
         cu  = hold->GetBinContent(bin);
         h->AddBinContent(ibin,cu);
      }
   }
   delete hold;          
}



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:16 MET