rebin.C

Go to the documentation of this file.
00001 //this tutorial illustrates how to:
00002 //  -create a variable binwidth histogram with a binning such
00003 //   that the population per bin is about the same.
00004 //  -rebin a variable binwidth histogram into another one.
00005 //Author: Rene Brun
00006    
00007 #include "TH1.h"
00008 #include "TCanvas.h"
00009 void rebin() {
00010    //create a fix bin histogram
00011    TH1F *h = new TH1F("h","test rebin",100,-3,3);
00012    Int_t nentries = 1000;
00013    h->FillRandom("gaus",nentries);
00014    Double_t xbins[1000];
00015    Int_t k=0;
00016    TAxis *axis = h->GetXaxis();
00017    for (Int_t i=1;i<=100;i++) {
00018       Int_t y = (Int_t)h->GetBinContent(i);
00019       if (y <=0) continue;
00020       Double_t dx = axis->GetBinWidth(i)/y;
00021       Double_t xmin = axis->GetBinLowEdge(i);
00022       for (Int_t j=0;j<y;j++) {
00023          xbins[k] = xmin +j*dx;
00024          k++;
00025       }
00026    }
00027    //create a variable binwidth histogram out of fix bin histogram
00028    //new rebinned histogram should have about 10 entries per bin
00029    TH1F *hnew = new TH1F("hnew","rebinned",k-1,xbins);
00030    hnew->FillRandom("gaus",10*nentries);
00031    
00032    //rebin hnew keeping only 50% of the bins
00033    Double_t xbins2[501];
00034    Int_t kk=0;
00035    for (Int_t j=0;j<k;j+=2) {
00036       xbins2[kk] = xbins[j];
00037       kk++;
00038    }
00039    xbins2[kk] = xbins[k];
00040    TH1F *hnew2 = (TH1F*)hnew->Rebin(kk-1,"hnew2",xbins2);
00041 
00042    //draw the 3 histograms
00043    TCanvas *c1 = new TCanvas("c1","c1",800,1000);
00044    c1->Divide(1,3);
00045    c1->cd(1);
00046    h->Draw();
00047    c1->cd(2);
00048    hnew->Draw();
00049    c1->cd(3);
00050    hnew2->Draw();
00051 }

Generated on Tue Jul 5 15:44:27 2011 for ROOT_528-00b_version by  doxygen 1.5.1