Belle II Software development
test_Binning.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <mva/utility/Binning.h>
10#include <framework/utilities/FileSystem.h>
11#include <framework/utilities/TestHelpers.h>
12
13#include <gtest/gtest.h>
14
15using namespace Belle2;
16
17namespace {
18
19 TEST(BinningTest, Binning)
20 {
21 MVA::Binning binning(3);
22
23 EXPECT_EQ(binning.m_signal_pdf.size(), 3);
24 EXPECT_EQ(binning.m_signal_cdf.size(), 3);
25 EXPECT_EQ(binning.m_bckgrd_pdf.size(), 3);
26 EXPECT_EQ(binning.m_bckgrd_cdf.size(), 3);
27 EXPECT_EQ(binning.m_boundaries.size(), 4);
28
29 EXPECT_FLOAT_EQ(binning.m_signal_yield, 0.0);
30 EXPECT_FLOAT_EQ(binning.m_bckgrd_yield, 0.0);
31
32 binning.m_signal_pdf[0] = 1.0;
33 binning.m_signal_pdf[1] = 2.0;
34 binning.m_signal_pdf[2] = 3.0;
35
36 binning.m_bckgrd_pdf[0] = 3.0;
37 binning.m_bckgrd_pdf[1] = 2.0;
38 binning.m_bckgrd_pdf[2] = 1.0;
39
40 binning.m_boundaries = {1.0, 2.0, 3.0, 4.0};
41
42 binning.normalizePDFs();
43
44 EXPECT_FLOAT_EQ(binning.m_signal_pdf[0], 1.0 / 6.0 * 3.0);
45 EXPECT_FLOAT_EQ(binning.m_signal_pdf[1], 2.0 / 6.0 * 3.0);
46 EXPECT_FLOAT_EQ(binning.m_signal_pdf[2], 3.0 / 6.0 * 3.0);
47
48 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[0], 3.0 / 6.0 * 3.0);
49 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[1], 2.0 / 6.0 * 3.0);
50 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[2], 1.0 / 6.0 * 3.0);
51
52 EXPECT_FLOAT_EQ(binning.m_signal_yield, 6.0);
53 EXPECT_FLOAT_EQ(binning.m_bckgrd_yield, 6.0);
54
55 binning.calculateCDFsFromPDFs();
56
57 EXPECT_FLOAT_EQ(binning.m_signal_pdf[0], 1.0 / 6.0 * 3.0);
58 EXPECT_FLOAT_EQ(binning.m_signal_pdf[1], 2.0 / 6.0 * 3.0);
59 EXPECT_FLOAT_EQ(binning.m_signal_pdf[2], 3.0 / 6.0 * 3.0);
60
61 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[0], 3.0 / 6.0 * 3.0);
62 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[1], 2.0 / 6.0 * 3.0);
63 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[2], 1.0 / 6.0 * 3.0);
64
65 EXPECT_FLOAT_EQ(binning.m_signal_yield, 6.0);
66 EXPECT_FLOAT_EQ(binning.m_bckgrd_yield, 6.0);
67
68 EXPECT_FLOAT_EQ(binning.m_signal_cdf[0], 1.0 / 6.0);
69 EXPECT_FLOAT_EQ(binning.m_signal_cdf[1], 3.0 / 6.0);
70 // cppcheck-suppress duplicateExpression
71 EXPECT_FLOAT_EQ(binning.m_signal_cdf[2], 6.0 / 6.0);
72
73 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf[0], 3.0 / 6.0);
74 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf[1], 5.0 / 6.0);
75 // cppcheck-suppress duplicateExpression
76 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf[2], 6.0 / 6.0);
77
78 binning.m_boundaries[0] = 1.0;
79 binning.m_boundaries[1] = 2.0;
80 binning.m_boundaries[2] = 3.0;
81 binning.m_boundaries[3] = 4.0;
82
83 EXPECT_EQ(binning.getBin(0.0), 0);
84 EXPECT_EQ(binning.getBin(1.0), 0);
85 EXPECT_EQ(binning.getBin(1.5), 0);
86 EXPECT_EQ(binning.getBin(2.0), 1);
87 EXPECT_EQ(binning.getBin(2.5), 1);
88 EXPECT_EQ(binning.getBin(3.0), 2);
89 EXPECT_EQ(binning.getBin(3.5), 2);
90 EXPECT_EQ(binning.getBin(4.5), 2);
91
92 }
93
94 TEST(BinningTest, EqualFrequencyBinning)
95 {
96 std::vector<float> data(100);
97 std::vector<float> weights(100);
98 std::vector<bool> isSignal(100);
99
100 for (unsigned int i = 0; i < 100; ++i) {
101 data[i] = i;
102 weights[i] = static_cast<int>(i / 10) + 1;
103 isSignal[i] = (i % 2) == 1;
104 }
105
106 MVA::Binning binning = MVA::Binning::CreateEqualFrequency(data, weights, isSignal, 3);
107
108 EXPECT_FLOAT_EQ(binning.m_boundaries.size(), 4);
109 EXPECT_FLOAT_EQ(binning.m_boundaries[0], 0.0);
110 EXPECT_FLOAT_EQ(binning.m_boundaries[1], 55.0);
111 EXPECT_FLOAT_EQ(binning.m_boundaries[2], 80.0);
112 EXPECT_FLOAT_EQ(binning.m_boundaries[3], 99.0);
113
114 EXPECT_FLOAT_EQ(binning.m_signal_pdf.size(), 3);
115 EXPECT_FLOAT_EQ(binning.m_signal_pdf[0] * binning.m_signal_yield * 55.0 / 99.0, 87.0);
116 EXPECT_FLOAT_EQ(binning.m_signal_pdf[1] * binning.m_signal_yield * 25.0 / 99.0, 93.0);
117 EXPECT_FLOAT_EQ(binning.m_signal_pdf[2] * binning.m_signal_yield * 19.0 / 99.0, 95.0);
118
119 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf.size(), 3);
120 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[0] * binning.m_bckgrd_yield * 55.0 / 99.0, 93.0);
121 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[1] * binning.m_bckgrd_yield * 25.0 / 99.0, 87.0);
122 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[2] * binning.m_bckgrd_yield * 19.0 / 99.0, 95.0);
123
124 EXPECT_FLOAT_EQ(binning.m_signal_cdf.size(), 3);
125 EXPECT_FLOAT_EQ(binning.m_signal_cdf[0] * binning.m_signal_yield, 87.0);
126 EXPECT_FLOAT_EQ(binning.m_signal_cdf[1] * binning.m_signal_yield, 180.0);
127 EXPECT_FLOAT_EQ(binning.m_signal_cdf[2] * binning.m_signal_yield, 275.0);
128
129 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf.size(), 3);
130 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf[0] * binning.m_bckgrd_yield, 93.0);
131 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf[1] * binning.m_bckgrd_yield, 180.0);
132 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf[2] * binning.m_bckgrd_yield, 275.0);
133
134 }
135
136 TEST(BinningTest, EquidistantBinning)
137 {
138 std::vector<float> data(100);
139 std::vector<float> weights(100);
140 std::vector<bool> isSignal(100);
141
142 for (unsigned int i = 0; i < 100; ++i) {
143 data[i] = i;
144 weights[i] = static_cast<int>(i / 10) + 1;
145 isSignal[i] = (i % 2) == 1;
146 }
147
148 MVA::Binning binning = MVA::Binning::CreateEquidistant(data, weights, isSignal, 3);
149
150 EXPECT_FLOAT_EQ(binning.m_boundaries.size(), 4);
151 EXPECT_FLOAT_EQ(binning.m_boundaries[0], 0.0);
152 EXPECT_FLOAT_EQ(binning.m_boundaries[1], 33.0);
153 EXPECT_FLOAT_EQ(binning.m_boundaries[2], 66.0);
154 EXPECT_FLOAT_EQ(binning.m_boundaries[3], 99.0);
155
156 EXPECT_FLOAT_EQ(binning.m_signal_pdf.size(), 3);
157 EXPECT_FLOAT_EQ(binning.m_signal_pdf[0] * binning.m_signal_yield / 3.0, 34.0);
158 EXPECT_FLOAT_EQ(binning.m_signal_pdf[1] * binning.m_signal_yield / 3.0, 92.0);
159 EXPECT_FLOAT_EQ(binning.m_signal_pdf[2] * binning.m_signal_yield / 3.0, 149.0);
160
161 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf.size(), 3);
162 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[0] * binning.m_bckgrd_yield / 3.0, 38.0);
163 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[1] * binning.m_bckgrd_yield / 3.0, 88.0);
164 EXPECT_FLOAT_EQ(binning.m_bckgrd_pdf[2] * binning.m_bckgrd_yield / 3.0, 149.0);
165
166 EXPECT_FLOAT_EQ(binning.m_signal_cdf.size(), 3);
167 EXPECT_FLOAT_EQ(binning.m_signal_cdf[0] * binning.m_signal_yield, 34.0);
168 EXPECT_FLOAT_EQ(binning.m_signal_cdf[1] * binning.m_signal_yield, 126.0);
169 EXPECT_FLOAT_EQ(binning.m_signal_cdf[2] * binning.m_signal_yield, 275.0);
170
171 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf.size(), 3);
172 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf[0] * binning.m_bckgrd_yield, 38.0);
173 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf[1] * binning.m_bckgrd_yield, 126.0);
174 EXPECT_FLOAT_EQ(binning.m_bckgrd_cdf[2] * binning.m_bckgrd_yield, 275.0);
175
176 }
177
178}
Binning of a data distribution Provides PDF and CDF values of the distribution per bin.
Definition: Binning.h:27
std::vector< float > m_bckgrd_pdf
Background pdf of data distribution per bin.
Definition: Binning.h:58
std::vector< float > m_signal_pdf
Signal pdf of data distribution per bin.
Definition: Binning.h:56
std::vector< float > m_boundaries
Boundaries of data distribution, including minimum and maximum value as first and last boundary.
Definition: Binning.h:61
std::vector< float > m_bckgrd_cdf
Background cdf of data distribution per bin.
Definition: Binning.h:59
double m_bckgrd_yield
Background yield in data distribution.
Definition: Binning.h:54
double m_signal_yield
Signal yield in data distribution.
Definition: Binning.h:53
std::vector< float > m_signal_cdf
Signal cdf of data distribution per bin.
Definition: Binning.h:57
std::vector< std::pair< double, double > > Binning
Bin holder as vector for bin limit pairs: [energy limits, theta limits, phi limits].
Abstract base class for different kinds of events.