Logo ROOT   6.08/07
Reference Guide
testSmooth.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// \notebook
4 /// Histogram smoothing.
5 ///
6 /// \macro_image
7 /// \macro_code
8 ///
9 /// \author Rene Brun
10 
11 int ipad = 1;
12 TCanvas * c1 = 0;
13 
14 void smooth_hist(const char * fname, double xmin, double xmax, int n1, int n2) {
15 
16  std::cout << "smoothing a " << fname << " histogram" << std::endl;
17 
18  TH1D * h1 = new TH1D("h1","h1",100,xmin,xmax);
19  TH1D * h2 = new TH1D("h2","h2",100,xmin,xmax);
20  h1->FillRandom(fname,n1);
21 
22  TH1D * h1_s = new TH1D(*h1);
23  h1_s->SetName("h1_s");
24  h1_s->Smooth();
25 
26  h2->FillRandom(fname,n2);
27 
28  double p1 = h1->Chi2Test(h2,"");
29  double p2 = h1_s->Chi2Test(h2,"UU");
30  if (p2 < p1) Error("testSmooth","TH1::Smooth is not working correctly - a worst chi2 is obtained");
31 
32  std::cout << " chi2 test non-smoothed histo " << p1 << std::endl;
33  std::cout << " chi2 test smoothed histo " << p2 << std::endl;
34 
35  double a1 = h1->AndersonDarlingTest(h2);
36  double a2 = h1_s->AndersonDarlingTest(h2);
37 
38  std::cout << " AD test non-smoothed histo " << a1 << std::endl;
39  std::cout << " AD test smoothed histo " << a2 << std::endl;
40 
41  double k1 = h1->KolmogorovTest(h2);
42  double k2 = h1_s->KolmogorovTest(h2);
43 
44  std::cout << " KS test non-smoothed histo " << k1 << std::endl;
45  std::cout << " KS test smoothed histo " << k2 << std::endl;
46 
47  c1->cd(ipad++);
48  h1->Draw("E");
49  h1_s->SetLineColor(kRed);
50  h1_s->Draw("same");
51  h2->Scale(double(n1)/n2);
52  h2->SetLineColor(kGreen);
53  h2->Draw("same");
54 }
55 
56 void testSmooth(int n1 = 1000, int n2 = 1000000) {
57 
58  TH1::AddDirectory(false);
59 
60  c1 = new TCanvas();
61  c1->Divide(1,3);
62 
63 
64  smooth_hist("gaus",-5,5,n1,n2);
65  smooth_hist("landau",-5,15,n1,n2);
66  smooth_hist("expo",-5,0,n1,n2);
67 
68 }
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:5936
float xmin
Definition: THbookFile.cxx:93
return c1
Definition: legend1.C:41
Definition: Rtypes.h:61
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual void Smooth(Int_t ntimes=1, Option_t *option="")
Smooth bin contents of this histogram.
Definition: TH1.cxx:6194
Definition: Rtypes.h:61
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Definition: TH1.cxx:1218
virtual Double_t Chi2Test(const TH1 *h2, Option_t *option="UU", Double_t *res=0) const
test for comparing weighted and unweighted histograms
Definition: TH1.cxx:1830
static double p2(double t, double a, double b, double c)
TH1F * h1
Definition: legend1.C:5
void Error(const char *location, const char *msgfmt,...)
virtual Double_t KolmogorovTest(const TH1 *h2, Option_t *option="") const
Statistical test of compatibility in shape between this histogram and h2, using Kolmogorov test...
Definition: TH1.cxx:7359
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:46
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3293
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual Double_t AndersonDarlingTest(const TH1 *h2, Option_t *option="") const
Statistical test of compatibility in shape between this histogram and h2, using the Anderson-Darling ...
Definition: TH1.cxx:7244
static double p1(double t, double a, double b)
float xmax
Definition: THbookFile.cxx:93
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:618
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8071
The Canvas class.
Definition: TCanvas.h:41
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:1089
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:301