Belle II Software  release-05-01-25
test_Options.cc
1 /* BASF2 (Belle Analysis Framework 2) *
2  * Copyright(C) 2016 - Belle II Collaboration *
3  * *
4  * Author: The Belle II Collaboration *
5  * Contributors: Thomas Keck *
6  * *
7  * This software is provided "as is" without any warranty. *
8  **************************************************************************/
9 
10 #include <mva/interface/Options.h>
11 
12 #include <boost/property_tree/ptree.hpp>
13 #include <gtest/gtest.h>
14 
15 using namespace Belle2;
16 
17 namespace {
18 
19  TEST(OptionsTest, GeneralOptions)
20  {
21 
22  MVA::GeneralOptions general_options;
23 
24  EXPECT_EQ(general_options.m_method, "");
25  EXPECT_EQ(general_options.m_identifier, "");
26  EXPECT_EQ(general_options.m_datafiles.size(), 0);
27  EXPECT_EQ(general_options.m_treename, "ntuple");
28  EXPECT_EQ(general_options.m_variables.size(), 0);
29  EXPECT_EQ(general_options.m_spectators.size(), 0);
30  EXPECT_EQ(general_options.m_signal_class, 1);
31  EXPECT_EQ(general_options.m_target_variable, "isSignal");
32  EXPECT_EQ(general_options.m_weight_variable, "__weight__");
33  EXPECT_EQ(general_options.m_max_events, 0u);
34 
35  general_options.m_method = "Method";
36  general_options.m_identifier = "Weightfile";
37  general_options.m_datafiles = {"Datafile"};
38  general_options.m_treename = "Tree";
39  general_options.m_variables = {"v", "a", "r", "s"};
40  general_options.m_spectators = {"x", "M"};
41  general_options.m_signal_class = 2;
42  general_options.m_max_events = 100;
43  general_options.m_target_variable = "Target";
44  general_options.m_weight_variable = "Weight";
45 
46  boost::property_tree::ptree pt;
47  general_options.save(pt);
48  EXPECT_EQ(pt.get<std::string>("method"), "Method");
49  EXPECT_EQ(pt.get<std::string>("weightfile"), "Weightfile");
50  EXPECT_EQ(pt.get<unsigned int>("number_data_files"), 1);
51  EXPECT_EQ(pt.get<std::string>("datafile0"), "Datafile");
52  EXPECT_EQ(pt.get<std::string>("treename"), "Tree");
53  EXPECT_EQ(pt.get<std::string>("target_variable"), "Target");
54  EXPECT_EQ(pt.get<std::string>("weight_variable"), "Weight");
55  EXPECT_EQ(pt.get<int>("signal_class"), 2);
56  EXPECT_EQ(pt.get<unsigned int>("max_events"), 100u);
57  EXPECT_EQ(pt.get<unsigned int>("number_feature_variables"), 4);
58  EXPECT_EQ(pt.get<std::string>("variable0"), "v");
59  EXPECT_EQ(pt.get<std::string>("variable1"), "a");
60  EXPECT_EQ(pt.get<std::string>("variable2"), "r");
61  EXPECT_EQ(pt.get<std::string>("variable3"), "s");
62  EXPECT_EQ(pt.get<unsigned int>("number_spectator_variables"), 2);
63  EXPECT_EQ(pt.get<std::string>("spectator0"), "x");
64  EXPECT_EQ(pt.get<std::string>("spectator1"), "M");
65 
66  MVA::GeneralOptions general_options2;
67  general_options2.load(pt);
68 
69  EXPECT_EQ(general_options2.m_method, "Method");
70  EXPECT_EQ(general_options2.m_identifier, "Weightfile");
71  EXPECT_EQ(general_options2.m_datafiles.size(), 1);
72  EXPECT_EQ(general_options2.m_datafiles[0], "Datafile");
73  EXPECT_EQ(general_options2.m_treename, "Tree");
74  EXPECT_EQ(general_options2.m_variables.size(), 4);
75  EXPECT_EQ(general_options2.m_variables[0], "v");
76  EXPECT_EQ(general_options2.m_variables[1], "a");
77  EXPECT_EQ(general_options2.m_variables[2], "r");
78  EXPECT_EQ(general_options2.m_variables[3], "s");
79  EXPECT_EQ(general_options2.m_spectators.size(), 2);
80  EXPECT_EQ(general_options2.m_spectators[0], "x");
81  EXPECT_EQ(general_options2.m_spectators[1], "M");
82  EXPECT_EQ(general_options2.m_signal_class, 2);
83  EXPECT_EQ(general_options2.m_max_events, 100u);
84  EXPECT_EQ(general_options2.m_target_variable, "Target");
85  EXPECT_EQ(general_options2.m_weight_variable, "Weight");
86 
87  // Test if po::options_description is created without crashing
88  auto description = general_options.getDescription();
89  EXPECT_EQ(description.options().size(), 11);
90  }
91 
92  TEST(OptionsTest, MetaOptions)
93  {
94  MVA::MetaOptions meta_options;
95  EXPECT_EQ(meta_options.m_use_splot, false);
96  EXPECT_EQ(meta_options.m_splot_variable, "M");
97  EXPECT_EQ(meta_options.m_splot_mc_files.size(), 0);
98  EXPECT_EQ(meta_options.m_splot_combined, false);
99  EXPECT_EQ(meta_options.m_splot_boosted, false);
100  EXPECT_EQ(meta_options.m_use_sideband_substraction, false);
101  EXPECT_EQ(meta_options.m_sideband_variable, "");
102  EXPECT_EQ(meta_options.m_sideband_mc_files.size(), 0u);
103  EXPECT_EQ(meta_options.m_use_reweighting, false);
104  EXPECT_EQ(meta_options.m_reweighting_identifier, "");
105  EXPECT_EQ(meta_options.m_reweighting_variable, "");
106  EXPECT_EQ(meta_options.m_reweighting_data_files.size(), 0u);
107  EXPECT_EQ(meta_options.m_reweighting_mc_files.size(), 0u);
108 
109  meta_options.m_use_reweighting = true;
110  meta_options.m_reweighting_identifier = "test";
111  meta_options.m_reweighting_variable = "A";
112  meta_options.m_reweighting_mc_files = {"reweighting_mc.root"};
113  meta_options.m_reweighting_data_files = {"reweighting_data.root"};
114  meta_options.m_use_sideband_substraction = true;
115  meta_options.m_sideband_variable = "B";
116  meta_options.m_sideband_mc_files = {"sideband_mc.root"};
117  meta_options.m_use_splot = true;
118  meta_options.m_splot_variable = "Q";
119  meta_options.m_splot_mc_files = {"splot_mc.root"};
120  meta_options.m_splot_combined = true;
121  meta_options.m_splot_boosted = true;
122 
123  boost::property_tree::ptree pt;
124  meta_options.save(pt);
125  EXPECT_EQ(pt.get<bool>("use_splot"), true);
126  EXPECT_EQ(pt.get<bool>("splot_combined"), true);
127  EXPECT_EQ(pt.get<bool>("splot_boosted"), true);
128  EXPECT_EQ(pt.get<unsigned int>("splot_number_of_mc_files"), 1);
129  EXPECT_EQ(pt.get<std::string>("splot_mc_file0"), "splot_mc.root");
130  EXPECT_EQ(pt.get<std::string>("splot_variable"), "Q");
131  EXPECT_EQ(pt.get<bool>("use_sideband_substraction"), true);
132  EXPECT_EQ(pt.get<std::string>("sideband_variable"), "B");
133  EXPECT_EQ(pt.get<bool>("use_reweighting"), true);
134  EXPECT_EQ(pt.get<std::string>("reweighting_identifier"), "test");
135  EXPECT_EQ(pt.get<std::string>("reweighting_variable"), "A");
136  EXPECT_EQ(pt.get<unsigned int>("reweighting_number_of_mc_files"), 1);
137  EXPECT_EQ(pt.get<std::string>("reweighting_mc_file0"), "reweighting_mc.root");
138  EXPECT_EQ(pt.get<unsigned int>("reweighting_number_of_data_files"), 1);
139  EXPECT_EQ(pt.get<std::string>("reweighting_data_file0"), "reweighting_data.root");
140  EXPECT_EQ(pt.get<unsigned int>("sideband_number_of_mc_files"), 1);
141  EXPECT_EQ(pt.get<std::string>("sideband_mc_file0"), "sideband_mc.root");
142 
143  MVA::MetaOptions meta_options2;
144  meta_options2.load(pt);
145 
146  EXPECT_EQ(meta_options2.m_use_splot, true);
147  EXPECT_EQ(meta_options2.m_splot_variable, "Q");
148  EXPECT_EQ(meta_options2.m_splot_mc_files.size(), 1);
149  EXPECT_EQ(meta_options2.m_splot_mc_files[0], "splot_mc.root");
150  EXPECT_EQ(meta_options2.m_splot_combined, true);
151  EXPECT_EQ(meta_options2.m_splot_boosted, true);
152  EXPECT_EQ(meta_options2.m_use_sideband_substraction, true);
153  EXPECT_EQ(meta_options2.m_sideband_variable, "B");
154  EXPECT_EQ(meta_options2.m_sideband_mc_files.size(), 1);
155  EXPECT_EQ(meta_options2.m_sideband_mc_files[0], "sideband_mc.root");
156  EXPECT_EQ(meta_options2.m_use_reweighting, true);
157  EXPECT_EQ(meta_options2.m_reweighting_identifier, "test");
158  EXPECT_EQ(meta_options2.m_reweighting_variable, "A");
159  EXPECT_EQ(meta_options2.m_reweighting_mc_files.size(), 1);
160  EXPECT_EQ(meta_options2.m_reweighting_mc_files[0], "reweighting_mc.root");
161  EXPECT_EQ(meta_options2.m_reweighting_data_files.size(), 1);
162  EXPECT_EQ(meta_options2.m_reweighting_data_files[0], "reweighting_data.root");
163 
164  // Test if po::options_description is created without crashing
165  auto description = meta_options.getDescription();
166  EXPECT_EQ(description.options().size(), 13);
167 
168  }
169 
170 }
Belle2::MVA::GeneralOptions::m_identifier
std::string m_identifier
Identifier containing the finished training.
Definition: Options.h:85
Belle2::MVA::MetaOptions::m_use_sideband_substraction
bool m_use_sideband_substraction
Use sideband substraction.
Definition: Options.h:138
Belle2::MVA::MetaOptions::m_reweighting_data_files
std::vector< std::string > m_reweighting_data_files
Data files for the pretraining.
Definition: Options.h:147
Belle2::MVA::MetaOptions::m_splot_boosted
bool m_splot_boosted
Use boosted sPlot training (aPlot)
Definition: Options.h:136
Belle2::MVA::MetaOptions::load
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism (used by Weightfile) to load Options from a xml tree.
Definition: Options.cc:134
Belle2::MVA::GeneralOptions::m_max_events
unsigned int m_max_events
Maximum number of events to process, 0 means all.
Definition: Options.h:93
Belle2::MVA::MetaOptions::m_reweighting_mc_files
std::vector< std::string > m_reweighting_mc_files
MC files for the pretraining.
Definition: Options.h:148
Belle2::MVA::MetaOptions::m_sideband_mc_files
std::vector< std::string > m_sideband_mc_files
used to estimate the number of events in the different regions
Definition: Options.h:139
Belle2::MVA::MetaOptions::m_reweighting_variable
std::string m_reweighting_variable
Variable defining for which events the reweighting should be used (1) or not used (0).
Definition: Options.h:145
Belle2::MVA::GeneralOptions::load
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism (used by Weightfile) to load Options from a xml tree.
Definition: Options.cc:52
Belle2::MVA::GeneralOptions::m_weight_variable
std::string m_weight_variable
Weight variable (branch name) defining the weights.
Definition: Options.h:92
Belle2::MVA::MetaOptions::m_splot_variable
std::string m_splot_variable
Discriminating variable.
Definition: Options.h:133
Belle2::MVA::MetaOptions::m_splot_mc_files
std::vector< std::string > m_splot_mc_files
Monte carlo files used for the distribution of the discriminating variable.
Definition: Options.h:134
Belle2::MVA::MetaOptions::m_reweighting_identifier
std::string m_reweighting_identifier
Identifier used to save the reweighting expert.
Definition: Options.h:144
Belle2::MVA::GeneralOptions::m_treename
std::string m_treename
Name of the TTree inside the datafile containing the training data.
Definition: Options.h:87
Belle2::MVA::GeneralOptions::m_spectators
std::vector< std::string > m_spectators
Vector of all spectators (branch names) used in the training.
Definition: Options.h:89
Belle2::MVA::GeneralOptions::m_method
std::string m_method
Name of the MVA method to use.
Definition: Options.h:84
Belle2::MVA::MetaOptions::m_use_reweighting
bool m_use_reweighting
Use a pretraining of data against mc and weight the mc afterwards.
Definition: Options.h:143
Belle2::MVA::MetaOptions::m_use_splot
bool m_use_splot
Use splot training.
Definition: Options.h:132
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MVA::GeneralOptions::m_target_variable
std::string m_target_variable
Target variable (branch name) defining the target.
Definition: Options.h:91
Belle2::MVA::GeneralOptions::m_signal_class
int m_signal_class
Signal class which is used as signal in a classification problem.
Definition: Options.h:90
Belle2::MVA::GeneralOptions::m_variables
std::vector< std::string > m_variables
Vector of all variables (branch names) used in the training.
Definition: Options.h:88
Belle2::MVA::GeneralOptions
General options which are shared by all MVA trainings.
Definition: Options.h:64
Belle2::TEST
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Definition: utilityFunctions.cc:18
Belle2::MVA::GeneralOptions::m_datafiles
std::vector< std::string > m_datafiles
Name of the datafiles containing the training data.
Definition: Options.h:86
Belle2::MVA::MetaOptions::m_splot_combined
bool m_splot_combined
Combine sPlot training with PDF classifier for discriminating variable.
Definition: Options.h:135
Belle2::MVA::MetaOptions::m_sideband_variable
std::string m_sideband_variable
Variable defining the signal region (1) background region (2) negative signal region (3) or unused (o...
Definition: Options.h:140
Belle2::MVA::MetaOptions
Meta Options which modify the underlying training by doing sPlot, Multiclass and HyperparameterSearch...
Definition: Options.h:112