10 #include <mva/utility/DataDriven.h>
11 #include <mva/interface/Interface.h>
12 #include <framework/utilities/FileSystem.h>
13 #include <framework/utilities/TestHelpers.h>
15 #include <gtest/gtest.h>
34 m_a = {1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 3.0, 4.0, 4.0, 4.0, 4.0};
37 [[nodiscard]]
unsigned int getNumberOfFeatures()
const override {
return 1; }
38 [[nodiscard]]
unsigned int getNumberOfSpectators()
const override {
return 0; }
39 [[nodiscard]]
unsigned int getNumberOfEvents()
const override {
return 20; }
40 void loadEvent(
unsigned int iEvent)
override { m_input[0] = m_a[iEvent]; m_target = iEvent % 2; m_isSignal = m_target == 1; };
41 float getSignalFraction()
override {
return 0.5; };
42 std::vector<float> getFeature(
unsigned int)
override {
return m_a; }
44 std::vector<float> m_a;
48 TEST(SPlotTest, SPlotDataset)
52 general_options.m_variables = {
"A"};
53 TestDataset dataset(general_options);
55 std::vector<float> weights(40);
56 std::iota(weights.begin(), weights.end(), 0.0);
59 EXPECT_EQ(splot_dataset.getNumberOfFeatures(), 1);
60 EXPECT_EQ(splot_dataset.getNumberOfEvents(), 40);
61 EXPECT_EQ(splot_dataset.getSignalFraction(), 0.5);
63 auto feature = dataset.getFeature(0);
64 for (
unsigned int i = 0; i < 40; ++i) {
65 splot_dataset.loadEvent(i);
66 EXPECT_FLOAT_EQ(splot_dataset.m_input[0], feature[i / 2]);
67 EXPECT_FLOAT_EQ(splot_dataset.m_weight, 1.0 * i);
68 EXPECT_EQ(splot_dataset.m_isSignal, (i % 2) == 0);
73 TEST(ReweightingTest, ReweightingDataset)
77 general_options.m_variables = {
"A"};
78 TestDataset dataset(general_options);
80 std::vector<float> weights(20);
81 std::iota(weights.begin(), weights.end(), 0.0);
84 EXPECT_EQ(reweighting_dataset.getNumberOfFeatures(), 1);
85 EXPECT_EQ(reweighting_dataset.getNumberOfEvents(), 20);
87 auto feature = dataset.getFeature(0);
88 for (
unsigned int i = 0; i < 20; ++i) {
89 reweighting_dataset.loadEvent(i);
90 EXPECT_FLOAT_EQ(reweighting_dataset.m_input[0], feature[i]);
91 EXPECT_FLOAT_EQ(reweighting_dataset.m_weight, 1.0 * i);
92 EXPECT_EQ(reweighting_dataset.m_isSignal, (i % 2) == 1);
97 TEST(SPlotTest, GetSPlotWeights)
101 general_options.m_variables = {
"A",
"D"};
102 TestDataset dataset(general_options);
128 auto splot_weights = MVA::getSPlotWeights(dataset, binning);
131 for (
auto& s : splot_weights)
133 EXPECT_FLOAT_EQ(sum, 20.0);
135 EXPECT_EQ(splot_weights.size(), 40);
136 for (
unsigned int i = 0; i < 10; i += 2) {
137 EXPECT_FLOAT_EQ(splot_weights[i], -2.0);
138 EXPECT_FLOAT_EQ(splot_weights[i + 1], 3.0);
140 for (
unsigned int i = 10; i < 20; i += 2) {
141 EXPECT_FLOAT_EQ(splot_weights[i], 3.0);
142 EXPECT_FLOAT_EQ(splot_weights[i + 1], -2.0);
144 for (
unsigned int i = 20; i < 28; i += 2) {
145 EXPECT_FLOAT_EQ(splot_weights[i], 3.0);
146 EXPECT_FLOAT_EQ(splot_weights[i + 1], -2.0);
148 EXPECT_FLOAT_EQ(splot_weights[28], -2.0);
149 EXPECT_FLOAT_EQ(splot_weights[29], 3.0);
150 EXPECT_FLOAT_EQ(splot_weights[30], 3.0);
151 EXPECT_FLOAT_EQ(splot_weights[31], -2.0);
152 for (
unsigned int i = 32; i < 40; i += 2) {
153 EXPECT_FLOAT_EQ(splot_weights[i], -2.0);
154 EXPECT_FLOAT_EQ(splot_weights[i + 1], 3.0);
159 TEST(SPlotTest, GetBoostWeights)
163 general_options.m_variables = {
"A"};
164 TestDataset dataset(general_options);
168 auto boost_weights = MVA::getBoostWeights(dataset, binning);
170 EXPECT_EQ(boost_weights.size(), 40);
171 for (
unsigned int i = 0; i < 10; i += 2) {
172 EXPECT_FLOAT_EQ(boost_weights[i], 0.2 / 0.3 / 4.0);
173 EXPECT_FLOAT_EQ(boost_weights[i + 1], 0.8 / 0.3 / 4.0);
175 for (
unsigned int i = 10; i < 20; i += 2) {
176 EXPECT_FLOAT_EQ(boost_weights[i], 0.5 / 0.2 / 4.0);
177 EXPECT_FLOAT_EQ(boost_weights[i + 1], 0.5 / 0.2 / 4.0);
179 for (
unsigned int i = 20; i < 28; i += 2) {
180 EXPECT_FLOAT_EQ(boost_weights[i], 0.8 / 0.2 / 4.0);
181 EXPECT_FLOAT_EQ(boost_weights[i + 1], 0.2 / 0.2 / 4.0);
183 EXPECT_FLOAT_EQ(boost_weights[28], 1.0 / 0.3 / 4.0);
184 EXPECT_FLOAT_EQ(boost_weights[29], 0.0 / 0.3 / 4.0);
185 EXPECT_FLOAT_EQ(boost_weights[30], 0.8 / 0.2 / 4.0);
186 EXPECT_FLOAT_EQ(boost_weights[31], 0.2 / 0.2 / 4.0);
187 for (
unsigned int i = 32; i < 40; i += 2) {
188 EXPECT_FLOAT_EQ(boost_weights[i], 1.0 / 0.3 / 4.0);
189 EXPECT_FLOAT_EQ(boost_weights[i + 1], 0.0 / 0.3 / 4.0);
194 TEST(SPlotTest, GetAPlotWeights)
198 general_options.m_variables = {
"A"};
199 TestDataset dataset(general_options);
203 std::vector<float> boost_prediction = {0.0, 0.005, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45,
204 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.9, 0.995, 1.0
206 auto aplot_weights = MVA::getAPlotWeights(dataset, binning, boost_prediction);
209 boost_prediction[0] = 0.005;
210 boost_prediction[19] = 0.995;
212 auto splot_weights = MVA::getSPlotWeights(dataset, binning);
214 EXPECT_EQ(aplot_weights.size(), 40);
215 for (
unsigned int i = 0; i < 10; i += 2) {
216 double aplot = 0.1 / boost_prediction[i / 2] + 0.4 / (1 - boost_prediction[i / 2]);
217 EXPECT_FLOAT_EQ(aplot_weights[i], aplot * splot_weights[i]);
218 EXPECT_FLOAT_EQ(aplot_weights[i + 1], aplot * splot_weights[i + 1]);
220 for (
unsigned int i = 10; i < 20; i += 2) {
221 double aplot = 0.25 / boost_prediction[i / 2] + 0.25 / (1 - boost_prediction[i / 2]);
222 EXPECT_FLOAT_EQ(aplot_weights[i], aplot * splot_weights[i]);
223 EXPECT_FLOAT_EQ(aplot_weights[i + 1], aplot * splot_weights[i + 1]);
225 for (
unsigned int i = 20; i < 28; i += 2) {
226 double aplot = 0.4 / boost_prediction[i / 2] + 0.1 / (1 - boost_prediction[i / 2]);
227 EXPECT_FLOAT_EQ(aplot_weights[i], aplot * splot_weights[i]);
228 EXPECT_FLOAT_EQ(aplot_weights[i + 1], aplot * splot_weights[i + 1]);
231 double aplot = 0.5 / boost_prediction[14];
232 EXPECT_FLOAT_EQ(aplot_weights[28], aplot * splot_weights[28]);
233 EXPECT_FLOAT_EQ(aplot_weights[29], aplot * splot_weights[29]);
234 aplot = 0.4 / boost_prediction[15] + 0.1 / (1 - boost_prediction[15]);
235 EXPECT_FLOAT_EQ(aplot_weights[30], aplot * splot_weights[30]);
236 EXPECT_FLOAT_EQ(aplot_weights[31], aplot * splot_weights[31]);
238 for (
unsigned int i = 32; i < 40; i += 2) {
239 double aplot = 0.5 / boost_prediction[i / 2];
240 EXPECT_FLOAT_EQ(aplot_weights[i], aplot * splot_weights[i]);
241 EXPECT_FLOAT_EQ(aplot_weights[i + 1], aplot * splot_weights[i + 1]);