9 #include <mva/utility/DataDriven.h>
10 #include <mva/interface/Interface.h>
11 #include <framework/utilities/FileSystem.h>
12 #include <framework/utilities/TestHelpers.h>
14 #include <gtest/gtest.h>
33 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};
36 [[nodiscard]]
unsigned int getNumberOfFeatures()
const override {
return 1; }
37 [[nodiscard]]
unsigned int getNumberOfSpectators()
const override {
return 0; }
38 [[nodiscard]]
unsigned int getNumberOfEvents()
const override {
return 20; }
39 void loadEvent(
unsigned int iEvent)
override { m_input[0] = m_a[iEvent]; m_target = iEvent % 2; m_isSignal = m_target == 1; };
40 float getSignalFraction()
override {
return 0.5; };
41 std::vector<float> getFeature(
unsigned int)
override {
return m_a; }
43 std::vector<float> m_a;
47 TEST(SPlotTest, SPlotDataset)
51 general_options.m_variables = {
"A"};
52 TestDataset dataset(general_options);
54 std::vector<float> weights(40);
55 std::iota(weights.begin(), weights.end(), 0.0);
58 EXPECT_EQ(splot_dataset.getNumberOfFeatures(), 1);
59 EXPECT_EQ(splot_dataset.getNumberOfEvents(), 40);
60 EXPECT_EQ(splot_dataset.getSignalFraction(), 0.5);
62 auto feature = dataset.getFeature(0);
63 for (
unsigned int i = 0; i < 40; ++i) {
64 splot_dataset.loadEvent(i);
65 EXPECT_FLOAT_EQ(splot_dataset.m_input[0], feature[i / 2]);
66 EXPECT_FLOAT_EQ(splot_dataset.m_weight, 1.0 * i);
67 EXPECT_EQ(splot_dataset.m_isSignal, (i % 2) == 0);
72 TEST(ReweightingTest, ReweightingDataset)
76 general_options.m_variables = {
"A"};
77 TestDataset dataset(general_options);
79 std::vector<float> weights(20);
80 std::iota(weights.begin(), weights.end(), 0.0);
83 EXPECT_EQ(reweighting_dataset.getNumberOfFeatures(), 1);
84 EXPECT_EQ(reweighting_dataset.getNumberOfEvents(), 20);
86 auto feature = dataset.getFeature(0);
87 for (
unsigned int i = 0; i < 20; ++i) {
88 reweighting_dataset.loadEvent(i);
89 EXPECT_FLOAT_EQ(reweighting_dataset.m_input[0], feature[i]);
90 EXPECT_FLOAT_EQ(reweighting_dataset.m_weight, 1.0 * i);
91 EXPECT_EQ(reweighting_dataset.m_isSignal, (i % 2) == 1);
96 TEST(SPlotTest, GetSPlotWeights)
100 general_options.m_variables = {
"A",
"D"};
101 TestDataset dataset(general_options);
127 auto splot_weights = MVA::getSPlotWeights(dataset, binning);
130 for (
auto& s : splot_weights)
132 EXPECT_FLOAT_EQ(sum, 20.0);
134 EXPECT_EQ(splot_weights.size(), 40);
135 for (
unsigned int i = 0; i < 10; i += 2) {
136 EXPECT_FLOAT_EQ(splot_weights[i], -2.0);
137 EXPECT_FLOAT_EQ(splot_weights[i + 1], 3.0);
139 for (
unsigned int i = 10; i < 20; i += 2) {
140 EXPECT_FLOAT_EQ(splot_weights[i], 3.0);
141 EXPECT_FLOAT_EQ(splot_weights[i + 1], -2.0);
143 for (
unsigned int i = 20; i < 28; i += 2) {
144 EXPECT_FLOAT_EQ(splot_weights[i], 3.0);
145 EXPECT_FLOAT_EQ(splot_weights[i + 1], -2.0);
147 EXPECT_FLOAT_EQ(splot_weights[28], -2.0);
148 EXPECT_FLOAT_EQ(splot_weights[29], 3.0);
149 EXPECT_FLOAT_EQ(splot_weights[30], 3.0);
150 EXPECT_FLOAT_EQ(splot_weights[31], -2.0);
151 for (
unsigned int i = 32; i < 40; i += 2) {
152 EXPECT_FLOAT_EQ(splot_weights[i], -2.0);
153 EXPECT_FLOAT_EQ(splot_weights[i + 1], 3.0);
158 TEST(SPlotTest, GetBoostWeights)
162 general_options.m_variables = {
"A"};
163 TestDataset dataset(general_options);
167 auto boost_weights = MVA::getBoostWeights(dataset, binning);
169 EXPECT_EQ(boost_weights.size(), 40);
170 for (
unsigned int i = 0; i < 10; i += 2) {
171 EXPECT_FLOAT_EQ(boost_weights[i], 0.2 / 0.3 / 4.0);
172 EXPECT_FLOAT_EQ(boost_weights[i + 1], 0.8 / 0.3 / 4.0);
174 for (
unsigned int i = 10; i < 20; i += 2) {
175 EXPECT_FLOAT_EQ(boost_weights[i], 0.5 / 0.2 / 4.0);
176 EXPECT_FLOAT_EQ(boost_weights[i + 1], 0.5 / 0.2 / 4.0);
178 for (
unsigned int i = 20; i < 28; i += 2) {
179 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);
187 EXPECT_FLOAT_EQ(boost_weights[31], 0.2 / 0.2 / 4.0);
188 for (
unsigned int i = 32; i < 40; i += 2) {
189 EXPECT_FLOAT_EQ(boost_weights[i], 1.0 / 0.3 / 4.0);
190 EXPECT_FLOAT_EQ(boost_weights[i + 1], 0.0 / 0.3 / 4.0);
195 TEST(SPlotTest, GetAPlotWeights)
199 general_options.m_variables = {
"A"};
200 TestDataset dataset(general_options);
204 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,
205 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.9, 0.995, 1.0
207 auto aplot_weights = MVA::getAPlotWeights(dataset, binning, boost_prediction);
210 boost_prediction[0] = 0.005;
211 boost_prediction[19] = 0.995;
213 auto splot_weights = MVA::getSPlotWeights(dataset, binning);
215 EXPECT_EQ(aplot_weights.size(), 40);
216 for (
unsigned int i = 0; i < 10; i += 2) {
217 double aplot = 0.1 / boost_prediction[i / 2] + 0.4 / (1 - boost_prediction[i / 2]);
218 EXPECT_FLOAT_EQ(aplot_weights[i], aplot * splot_weights[i]);
219 EXPECT_FLOAT_EQ(aplot_weights[i + 1], aplot * splot_weights[i + 1]);
221 for (
unsigned int i = 10; i < 20; i += 2) {
222 double aplot = 0.25 / boost_prediction[i / 2] + 0.25 / (1 - boost_prediction[i / 2]);
223 EXPECT_FLOAT_EQ(aplot_weights[i], aplot * splot_weights[i]);
224 EXPECT_FLOAT_EQ(aplot_weights[i + 1], aplot * splot_weights[i + 1]);
226 for (
unsigned int i = 20; i < 28; i += 2) {
227 double aplot = 0.4 / boost_prediction[i / 2] + 0.1 / (1 - boost_prediction[i / 2]);
228 EXPECT_FLOAT_EQ(aplot_weights[i], aplot * splot_weights[i]);
229 EXPECT_FLOAT_EQ(aplot_weights[i + 1], aplot * splot_weights[i + 1]);
232 double aplot = 0.5 / boost_prediction[14];
233 EXPECT_FLOAT_EQ(aplot_weights[28], aplot * splot_weights[28]);
234 EXPECT_FLOAT_EQ(aplot_weights[29], aplot * splot_weights[29]);
235 aplot = 0.4 / boost_prediction[15] + 0.1 / (1 - boost_prediction[15]);
236 EXPECT_FLOAT_EQ(aplot_weights[30], aplot * splot_weights[30]);
237 EXPECT_FLOAT_EQ(aplot_weights[31], aplot * splot_weights[31]);
239 for (
unsigned int i = 32; i < 40; i += 2) {
240 double aplot = 0.5 / boost_prediction[i / 2];
241 EXPECT_FLOAT_EQ(aplot_weights[i], aplot * splot_weights[i]);
242 EXPECT_FLOAT_EQ(aplot_weights[i + 1], aplot * splot_weights[i + 1]);
Binning of a data distribution Provides PDF and CDF values of the distribution per bin.
std::vector< float > m_bckgrd_pdf
Background pdf of data distribution per bin.
std::vector< float > m_signal_pdf
Signal pdf of data distribution per bin.
std::vector< float > m_boundaries
Boundaries of data distribution, including minimum and maximum value as first and last boundary.
static Binning CreateEquidistant(const std::vector< float > &data, const std::vector< float > &weights, const std::vector< bool > &isSignal, unsigned int nBins)
Create an equidistant binning.
double m_bckgrd_yield
Background yield in data distribution.
double m_signal_yield
Signal yield in data distribution.
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
General options which are shared by all MVA trainings.
Dataset for Reweighting Wraps a dataset and provides each data-point with a new weight.
Dataset for sPlot Wraps a dataset and provides each data-point twice, once as signal and once as back...
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Abstract base class for different kinds of events.