Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist017_TH1_smooth.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/// \date July 2016
10/// \author Rene Brun
11
12int ipad = 1;
13TCanvas *c1 = 0;
14
15void smooth_hist(const char *fname, double xmin, double xmax, int n1, int n2)
16{
17
18 std::cout << "smoothing a " << fname << " histogram" << std::endl;
19
20 TH1D *h1 = new TH1D("h1", "h1", 100, xmin, xmax);
21 TH1D *h2 = new TH1D("h2", "h2", 100, xmin, xmax);
23
24 TH1D *h1_s = new TH1D(*h1);
25 h1_s->SetName("h1_s");
26 h1_s->Smooth();
27
28 h2->FillRandom(fname, n2);
29
30 double p1 = h1->Chi2Test(h2, "");
31 double p2 = h1_s->Chi2Test(h2, "UU");
32 if (p2 < p1)
33 Error("testSmooth", "TH1::Smooth is not working correctly - a worst chi2 is obtained");
34
35 std::cout << " chi2 test non-smoothed histo " << p1 << std::endl;
36 std::cout << " chi2 test smoothed histo " << p2 << std::endl;
37
38 double a1 = h1->AndersonDarlingTest(h2);
39 double a2 = h1_s->AndersonDarlingTest(h2);
40
41 std::cout << " AD test non-smoothed histo " << a1 << std::endl;
42 std::cout << " AD test smoothed histo " << a2 << std::endl;
43
44 double k1 = h1->KolmogorovTest(h2);
45 double k2 = h1_s->KolmogorovTest(h2);
46
47 std::cout << " KS test non-smoothed histo " << k1 << std::endl;
48 std::cout << " KS test smoothed histo " << k2 << std::endl;
49
50 c1->cd(ipad++);
51 h1->Draw("E");
52 h1_s->SetLineColor(kRed);
53 h1_s->Draw("same");
54 h2->Scale(double(n1) / n2);
55 h2->SetLineColor(kGreen);
56 h2->Draw("same");
57}
58
59void hist017_TH1_smooth(int n1 = 1000, int n2 = 1000000)
60{
61
62 TH1::AddDirectory(false);
63
64 c1 = new TCanvas();
65 c1->Divide(1, 3);
66
67 smooth_hist("gaus", -5, 5, n1, n2);
68 smooth_hist("landau", -5, 15, n1, n2);
69 smooth_hist("expo", -5, 0, n1, n2);
70}
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
float xmin
float xmax
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:693
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3499
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Definition TH1.cxx:1263
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3037
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:8064
virtual Double_t Chi2Test(const TH1 *h2, Option_t *option="UU", Double_t *res=nullptr) const
test for comparing weighted and unweighted histograms.
Definition TH1.cxx:1979
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:8180
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5