ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rebin.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// Rebin a variable bin-width histogram.
4 ///
5 /// This tutorial illustrates how to:
6 /// - create a variable bin-width histogram with a binning such
7 /// that the population per bin is about the same.
8 /// - rebin a variable bin-width histogram into another one.
9 ///
10 /// \macro_image
11 /// \macro_code
12 ///
13 /// \author Rene Brun
14 
15 #include "TH1.h"
16 #include "TCanvas.h"
17 void rebin() {
18  //create a fix bin histogram
19  TH1F *h = new TH1F("h","test rebin",100,-3,3);
20  Int_t nentries = 1000;
21  h->FillRandom("gaus",nentries);
22  Double_t xbins[1001];
23  Int_t k=0;
24  TAxis *axis = h->GetXaxis();
25  for (Int_t i=1;i<=100;i++) {
26  Int_t y = (Int_t)h->GetBinContent(i);
27  if (y <=0) continue;
28  Double_t dx = axis->GetBinWidth(i)/y;
29  Double_t xmin = axis->GetBinLowEdge(i);
30  for (Int_t j=0;j<y;j++) {
31  xbins[k] = xmin +j*dx;
32  k++;
33  }
34  }
35  xbins[k] = axis->GetXmax();
36  //create a variable bin-width histogram out of fix bin histogram
37  //new rebinned histogram should have about 10 entries per bin
38  TH1F *hnew = new TH1F("hnew","rebinned",k,xbins);
39  hnew->FillRandom("gaus",10*nentries);
40 
41  //rebin hnew keeping only 50% of the bins
42  Double_t xbins2[501];
43  Int_t kk=0;
44  for (Int_t j=0;j<k;j+=2) {
45  xbins2[kk] = xbins[j];
46  kk++;
47  }
48  xbins2[kk] = xbins[k];
49  TH1F *hnew2 = (TH1F*)hnew->Rebin(kk,"hnew2",xbins2);
50 
51  //draw the 3 histograms
52  TCanvas *c1 = new TCanvas("c1","c1",800,1000);
53  c1->Divide(1,3);
54  c1->cd(1);
55  h->Draw();
56  c1->cd(2);
57  hnew->Draw();
58  c1->cd(3);
59  hnew2->Draw();
60 }
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4629
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition: TAxis.cxx:489
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
Definition: TAxis.cxx:511
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3330
Class to manage histogram axis.
Definition: TAxis.h:36
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual TH1 * Rebin(Int_t ngroup=2, const char *newname="", const Double_t *xbins=0)
Rebin this histogram.
Definition: TH1.cxx:5859
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
Double_t GetXmax() const
Definition: TAxis.h:138
Double_t y[n]
Definition: legend1.C:17
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1073
TAxis * GetXaxis()
Definition: TH1.h:319