9#include <mva/methods/PDF.h>
10#include <framework/logging/Logger.h>
21 int version = pt.get<
int>(
"PDF_version");
23 B2ERROR(
"Unknown weightfile version " << std::to_string(version));
24 throw std::runtime_error(
"Unknown weightfile version " + std::to_string(version));
26 m_binning = pt.get<std::string>(
"PDF_binning");
27 m_mode = pt.get<std::string>(
"PDF_mode");
28 m_nBins = pt.get<
unsigned int>(
"PDF_nBins");
34 pt.put(
"PDF_version", 1);
36 pt.put(
"PDF_mode",
m_mode);
42 po::options_description description(
"PDF options");
43 description.add_options()
44 (
"binning", po::value<std::string>(&
m_binning),
"Binning used to bin the data (frequency|equidistant)")
45 (
"nBins", po::value<unsigned int>(&
m_nBins),
"Number of bins used to bin the data")
46 (
"mode", po::value<std::string>(&
m_mode),
"Mode used to calculate return value of expert");
52 m_specific_options(specific_options) { }
57 unsigned int numberOfFeatures = training_data.getNumberOfFeatures();
58 if (numberOfFeatures != 1) {
59 B2ERROR(
"PDF method only supports exactly one feature!");
60 throw std::runtime_error(
"PDF method only supports exactly one feature!");
62 unsigned int numberOfEvents = training_data.getNumberOfEvents();
64 std::vector<float> data(numberOfEvents);
65 std::vector<float> weights(numberOfEvents);
66 std::vector<bool> isSignal(numberOfEvents);
67 for (
unsigned int iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
68 training_data.loadEvent(iEvent);
69 data[iEvent] = training_data.m_input[0];
70 weights[iEvent] = training_data.m_weight;
71 isSignal[iEvent] = training_data.m_isSignal;
78 binning = Binning::CreateEqualFrequency(data, weights, isSignal, nBins);
80 binning = Binning::CreateEquidistant(data, weights, isSignal, nBins);
88 std::fstream file(custom_weightfile, std::ios_base::out | std::ios_base::trunc);
90 std::vector<double> value(nBins, 0);
92 for (
unsigned int iBin = 0; iBin < nBins; ++iBin) {
101 file << nBins << std::endl;
102 for (
unsigned int iBin = 0; iBin < nBins; ++iBin) {
103 file << value[iBin] <<
" " << binning.
m_boundaries[iBin] << std::endl;
111 weightfile.
addFile(
"PDF_Weightfile", custom_weightfile);
122 weightfile.
getFile(
"PDF_Weightfile", custom_weightfile);
123 std::fstream file(custom_weightfile, std::ios_base::in);
125 unsigned int nBins = 0;
129 std::vector<float> boundaries(nBins + 1, 0);
131 for (
unsigned int iBin = 0; iBin < nBins; ++iBin) {
132 file >>
m_value[iBin] >> boundaries[iBin];
134 file >> boundaries[nBins];
145 std::vector<float> probabilities(test_data.getNumberOfEvents(), 0);
146 for (
unsigned int iEvent = 0; iEvent < test_data.getNumberOfEvents(); ++iEvent) {
147 test_data.loadEvent(iEvent);
149 probabilities[iEvent] =
m_value[bin];
151 return probabilities;
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.
unsigned int getBin(float datapoint) const
Gets the bin corresponding to the given datapoint.
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.
Binning m_binning
used binning
virtual std::vector< float > apply(Dataset &test_data) const override
Apply PDF expert onto a dataset.
PDFOptions m_specific_options
Specific options of the PDF method.
std::vector< double > m_value
value returned by expert for each bin
virtual void load(Weightfile &weightfile) override
Load the PDF expert from a Weightfile.
Options for the PDF MVA method.
unsigned int m_nBins
number of bins used to bin the data
std::string m_binning
which type of binning is performed e.g.
virtual po::options_description getDescription() override
Returns a program options description for all available options.
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism (used by Weightfile) to load Options from a xml tree.
virtual void save(boost::property_tree::ptree &pt) const override
Save mechanism (used by Weightfile) to store Options in a xml tree.
std::string m_mode
mode which defines the final output e.g.
PDFOptions m_specific_options
Specific options of the PDF method.
PDFTeacher(const GeneralOptions &general_options, const PDFOptions &specific_options)
Constructs a new teacher using the GeneralOptions and PDFoptions for this training.
virtual Weightfile train(Dataset &training_data) const override
Train PDF method using the given dataset returning a Weightfile.
Abstract base class of all Teachers Each MVA library has its own implementation of this class,...
GeneralOptions m_general_options
GeneralOptions containing all shared options.
The Weightfile class serializes all information about a training into an xml tree.
void addFile(const std::string &identifier, const std::string &custom_weightfile)
Add a file (mostly a weightfile from a MVA library) to our Weightfile.
void addOptions(const Options &options)
Add an Option object to the xml tree.
void getOptions(Options &options) const
Fills an Option object from the xml tree.
void addSignalFraction(float signal_fraction)
Saves the signal fraction in the xml tree.
std::string generateFileName(const std::string &suffix="")
Returns a temporary filename with the given suffix.
void getFile(const std::string &identifier, const std::string &custom_weightfile)
Creates a file from our weightfile (mostly this will be a weightfile of an MVA library)
Abstract base class for different kinds of events.