34TEST_CASE(
"HistogramPool Functions",
"[Framework][functionality]") {
35 const char* test_file =
"/tmp/test_histogram_pool.root";
37 SECTION(
"exception on request") {
40 REQUIRE_THROWS_WITH(test_pool.create(
"dne",
"foo", 10, 0.0, 1.0),
44 SECTION(
"creation on request") {
45 TFile* histogram_file{
nullptr};
46 auto open_on_request = [&histogram_file, &test_file]() -> TDirectory* {
47 if (histogram_file ==
nullptr) {
48 histogram_file = TFile::Open(test_file,
"recreate");
50 return histogram_file->mkdir(
"histo_directory");
54 REQUIRE(histogram_file ==
nullptr);
55 test_pool.create(
"h",
"bla", 10, 0, 1);
57 REQUIRE(histogram_file !=
nullptr);
58 CHECK(histogram_file->Get(
"histo_directory") !=
nullptr);
59 histogram_file->Write();
61 CHECK(histogram_file->Get(
"histo_directory/h") !=
nullptr);
64 SECTION(
"separate pools") {
65 TFile histogram_file{test_file,
"recreate"};
67 static TDirectory* d{histogram_file.mkdir(
"p1")};
71 static TDirectory* d{histogram_file.mkdir(
"p2")};
74 CHECK(histogram_file.Get(
"p1") ==
nullptr);
75 CHECK(histogram_file.Get(
"p2") ==
nullptr);
76 test_pool_1.create(
"h",
"bar", 10, 0, 1);
77 CHECK(histogram_file.Get(
"p1") !=
nullptr);
78 CHECK(histogram_file.Get(
"p2") ==
nullptr);
79 test_pool_2.create(
"h",
"buz", 10, 0, 10);
80 CHECK(histogram_file.Get(
"p1") !=
nullptr);
81 CHECK(histogram_file.Get(
"p2") !=
nullptr);
82 histogram_file.Write();
86 auto h1 =
dynamic_cast<TH1F*
>(histogram_file.Get(
"p1/h"));
87 auto h2 =
dynamic_cast<TH1F*
>(histogram_file.Get(
"p2/h"));
88 REQUIRE(h1 !=
nullptr);
89 REQUIRE(h2 !=
nullptr);
90 CHECK(h1->GetNbinsX() == 10);
91 CHECK(h2->GetNbinsX() == 10);
92 CHECK(h1->GetBinLowEdge(10) == 0.9);
93 CHECK(h2->GetBinLowEdge(10) == 9);
97 SECTION(
"different types of histograms") {
98 TFile histogram_file{test_file,
"recreate"};
100 static TDirectory* d{histogram_file.mkdir(
"p")};
103 std::vector<std::string> cats{
"one",
"two",
"three"};
105 test_pool.
create(
"h1_1",
"foo", 10, 0, 1);
106 test_pool.create(
"h1_2",
"bar", {0.0, 0.5, 0.8, 1.0},
true);
107 test_pool.create(
"h1_3",
"", cats);
109 test_pool.create(
"h2_1",
"baz", 5, -5, 5,
"foo", 10, -5, 5);
110 test_pool.create(
"h2_2",
"baz", {-5.0, -1.0, 0.0, 1.0, 5.0},
"foo",
112 test_pool.create(
"h2_3",
"", cats,
"", cats);
114 test_pool.setWeight(0.75);
115 test_pool.fill(
"h1_2", 0.75);
116 test_pool.fillw(
"h1_2", 0.1, 0.5);
117 test_pool.fill(
"h1_2", 0.75);
118 test_pool.setWeight(1);
120 test_pool.fill(
"h1_3",
"one");
122 test_pool.fill(
"h1_3", 0);
124 test_pool.fill(
"h1_3", 1);
126 test_pool.fill(
"h1_3",
"three");
131 test_pool.fill(
"h2_3",
"one",
"two");
133 histogram_file.Write();
135 auto h1_1 =
dynamic_cast<TH1F*
>(histogram_file.Get(
"p/h1_1"));
136 REQUIRE(h1_1 !=
nullptr);
137 CHECK(h1_1->GetNbinsX() == 10);
138 CHECK(h1_1->GetBinLowEdge(1) == 0.0);
139 CHECK(h1_1->GetBinLowEdge(11) == 1.0);
140 REQUIRE(h1_1->GetSumw2() !=
nullptr);
141 CHECK(h1_1->GetSumw2()->fN == 0);
143 auto h1_2 =
dynamic_cast<TH1F*
>(histogram_file.Get(
"p/h1_2"));
144 REQUIRE(h1_2 !=
nullptr);
145 CHECK(h1_2->GetNbinsX() == 3);
146 CHECK(h1_2->GetBinLowEdge(1) == 0.0);
147 CHECK(h1_2->GetBinLowEdge(2) == 0.5);
148 CHECK(h1_2->GetBinLowEdge(3) == 0.8);
149 CHECK(h1_2->GetBinLowEdge(4) == 1.0);
150 CHECK(h1_2->GetBinContent(1) == 0.5);
151 CHECK(h1_2->GetBinContent(2) == 2 * 0.75);
152 REQUIRE(h1_2->GetSumw2() !=
nullptr);
153 CHECK(h1_2->GetSumw2()->fN == 5);
154 CHECK(h1_2->GetSumw2()->At(1) == 0.25);
155 CHECK(h1_2->GetSumw2()->At(2) == 0.75 * 0.75 * 2);
157 auto h1_3 =
dynamic_cast<TH1F*
>(histogram_file.Get(
"p/h1_3"));
158 REQUIRE(h1_3 !=
nullptr);
159 CHECK(h1_3->GetNbinsX() == 3);
161 CHECK(h1_3->GetBinContent(0) == 0);
162 CHECK(h1_3->GetBinContent(1) == 2);
163 CHECK(h1_3->GetBinContent(2) == 1);
164 CHECK(h1_3->GetBinContent(3) == 1);
165 CHECK(h1_3->GetBinContent(4) == 0);
167 auto h2_1 =
dynamic_cast<TH2F*
>(histogram_file.Get(
"p/h2_1"));
168 REQUIRE(h2_1 !=
nullptr);
169 CHECK(h2_1->GetNbinsX() == 5);
170 CHECK(h2_1->GetNbinsY() == 10);
172 auto h2_2 =
dynamic_cast<TH2F*
>(histogram_file.Get(
"p/h2_2"));
173 REQUIRE(h2_2 !=
nullptr);
174 CHECK(h2_2->GetNbinsX() == 4);
175 CHECK(h2_2->GetNbinsY() == 2);
177 auto h2_3 =
dynamic_cast<TH2F*
>(histogram_file.Get(
"p/h2_3"));
178 REQUIRE(h2_3 !=
nullptr);
179 CHECK(h2_3->GetBinContent(1, 2) == 1);