Skip to content

Commit 987e2dc

Browse files
committed
[hist] Add test file for TProfiles, testing Chi2Test().
1 parent d3d1505 commit 987e2dc

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

hist/hist/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ROOT_ADD_GTEST(testTGraphSorting test_TGraph_sorting.cxx LIBRARIES Hist)
2929
ROOT_ADD_GTEST(testSpline test_spline.cxx LIBRARIES Hist)
3030
ROOT_ADD_GTEST(testTF1Simple test_tf1_simple.cxx LIBRARIES Hist RIO)
3131
ROOT_ADD_GTEST(testTF1DrawCopy test_tf1_drawcopy.cxx LIBRARIES Hist)
32+
ROOT_ADD_GTEST(test_TProfile test_TProfile.cxx LIBRARIES Hist)
3233

3334
if(fftw3)
3435
ROOT_ADD_GTEST(testTF1 test_tf1.cxx LIBRARIES Hist)

hist/hist/test/test_TProfile.cxx

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "gtest/gtest.h"
2+
3+
#include "TProfile.h"
4+
#include "TProfile2D.h"
5+
#include "TProfile3D.h"
6+
#include "TRandom.h"
7+
8+
#include "ROOT/TestSupport.hxx"
9+
10+
template <typename T>
11+
void runTest(T const &reference, T const &sameDistr, T const &differentDistr)
12+
{
13+
EXPECT_EQ(reference.Chi2Test(&sameDistr), reference.Chi2Test(&sameDistr, "WW"));
14+
EXPECT_EQ(reference.Chi2Test(&sameDistr),
15+
reference.Chi2Test(&sameDistr, "P WW UW")); // Need more than just the default option
16+
EXPECT_EQ(reference.Chi2Test(&sameDistr), reference.Chi2Test(&sameDistr, "UW"));
17+
EXPECT_EQ(reference.Chi2Test(&sameDistr), reference.Chi2Test(&sameDistr, "UU"));
18+
EXPECT_EQ(reference.Chi2Test(&sameDistr), reference.Chi2Test(&sameDistr, "P UU"));
19+
20+
const double probSuccess = reference.Chi2Test(&sameDistr, "P");
21+
EXPECT_GT(probSuccess, 0.1);
22+
EXPECT_LE(probSuccess, 1.);
23+
const double probFail = reference.Chi2Test(&differentDistr, "P");
24+
EXPECT_LT(probFail, 0.05);
25+
}
26+
27+
TEST(TProfile, Chi2Test)
28+
{
29+
TProfile reference("reference", "reference", 10, 0, 10);
30+
TProfile sameDistr("sameDistr", "sameDistr", 10, 0, 10);
31+
TProfile differentDistr("differentDistr", "differentDistr", 10, 0, 10);
32+
33+
for (unsigned int i = 0; i < 100000; i++) {
34+
const double x = gRandom->Uniform(10.);
35+
reference.Fill(x, gRandom->Gaus(5 + x / 2, 5.));
36+
sameDistr.Fill(x, gRandom->Gaus(5 + x / 2, 5.));
37+
differentDistr.Fill(x, gRandom->Gaus(20, 1.));
38+
}
39+
40+
runTest(reference, sameDistr, differentDistr);
41+
}
42+
43+
TEST(TProfile, Chi2TestWithWrongErrors)
44+
{
45+
TProfile reference("reference", "reference", 10, 0, 10);
46+
reference.Fill(1, 2);
47+
reference.Fill(1, 3);
48+
49+
for (auto err : {"s", "i", "g"}) {
50+
ROOT::TestSupport::CheckDiagsRAII checkDiag(kWarning, "TProfile::Chi2Test", "if the error on the mean is used",
51+
false);
52+
53+
TProfile sameDistr("sameDistr", "sameDistr", 10, 0, 10, err);
54+
sameDistr.Fill(1, 2);
55+
sameDistr.Fill(1, 3);
56+
57+
reference.Chi2Test(&sameDistr);
58+
}
59+
}
60+
61+
TEST(TProfile2D, Chi2Test)
62+
{
63+
TProfile2D reference("reference", "reference", 10, 0, 10, 10, 0, 10);
64+
TProfile2D sameDistr("sameDistr", "sameDistr", 10, 0, 10, 10, 0, 10);
65+
TProfile2D differentDistr("differentDistr", "differentDistr", 10, 0, 10, 10, 0, 10);
66+
67+
for (unsigned int i = 0; i < 50000; i++) {
68+
const double x = gRandom->Uniform(10.);
69+
reference.Fill(x, x + 1., gRandom->Gaus(5 + x / 2, 5.));
70+
sameDistr.Fill(x, x + 1., gRandom->Gaus(5 + x / 2, 5.));
71+
differentDistr.Fill(x, x + 1., gRandom->Gaus(20, 1.));
72+
}
73+
74+
runTest(reference, sameDistr, differentDistr);
75+
}
76+
77+
TEST(TProfile3D, Chi2Test)
78+
{
79+
TProfile3D reference("reference", "reference", 10, 0, 10, 11, 0, 11, 12, 0, 12);
80+
TProfile3D sameDistr("sameDistr", "sameDistr", 10, 0, 10, 11, 0, 11, 12, 0, 12);
81+
TProfile3D differentDistr("differentDistr", "differentDistr", 10, 0, 10, 11, 0, 11, 12, 0, 12);
82+
83+
for (unsigned int i = 0; i < 50000; i++) {
84+
const double x = gRandom->Uniform(10.);
85+
reference.Fill(x, x + 1., x + 2., gRandom->Gaus(5 + x / 2, 5.));
86+
sameDistr.Fill(x, x + 1., x + 2., gRandom->Gaus(5 + x / 2, 5.));
87+
differentDistr.Fill(x, x + 1., x + 2., gRandom->Gaus(20, 1.));
88+
}
89+
90+
runTest(reference, sameDistr, differentDistr);
91+
}

0 commit comments

Comments
 (0)