9 #include <tracking/trackFindingVXD/filterTools/FBDTClassifier.h>
10 #include <framework/logging/Logger.h>
12 #if FastBDT_VERSION_MAJOR >= 3
13 #include <FastBDT_IO.h>
20 template<
size_t Ndims>
24 B2DEBUG(20,
"Reading the FeatureBinnings");
26 B2DEBUG(20,
"Reading the Forest");
27 #if FastBDT_VERSION_MAJOR >= 3
28 m_forest = FastBDT::readForestFromStream<unsigned int>(is);
30 m_forest = FastBDT::readForestFromStream(is);
32 B2DEBUG(20,
"Reading the DecorrelationMatrix");
33 if (!m_decorrMat.readFromStream(is)) {
34 B2ERROR(
"Reading in the decorrelation matrix did not work! The decorrelation matrix of this classifier will be set to identity!");
39 template<
size_t Ndims>
42 B2DEBUG(20,
"Reading the FeatureBinnings");
43 os << m_featBins << std::endl;
44 B2DEBUG(20,
"Reading the Forest");
45 os << m_forest << std::endl;
46 B2DEBUG(20,
"Reading the DecorrelationMatrix");
47 os << m_decorrMat.print() << std::endl;
50 template<
size_t Ndims>
52 int nTrees,
int depth,
double shrinkage,
double ratio)
54 if (samples.empty()) {
55 B2ERROR(
"No samples passed for training a FBDTClassifier.");
59 unsigned int nBinCuts = 8;
60 size_t nSamples = samples.size();
61 B2DEBUG(20,
"Using for training: nBinCuts: " << nBinCuts <<
", with " << Ndims <<
" features and " << nSamples <<
" samples.");
63 B2DEBUG(20,
"FBDTClassifier::train(): Starting to restructure the data into the format better suited for later use");
64 std::array<std::vector<double>, Ndims> data;
65 for (
const auto& event : samples) {
66 for (
size_t iSP = 0; iSP < Ndims; ++iSP) {
67 data[iSP].push_back(event.hits[iSP]);
71 B2DEBUG(20,
"FBDTClassifier::train(): Calculating the decorrelation transformation.");
72 m_decorrMat.calculateDecorrMatrix(data,
false);
73 B2DEBUG(20,
"FBDTClassifier::train(): Applying decorrelation transformation");
74 data = m_decorrMat.decorrelate(data);
76 B2DEBUG(20,
"FBDTClassifier::train(): Determining the FeatureBinnings");
77 std::vector<unsigned int> nBinningLevels;
79 for (
auto featureVec : data) {
80 #if FastBDT_VERSION_MAJOR >= 3
81 m_featBins.push_back(FastBDT::FeatureBinning<double>(nBinCuts, featureVec));
83 m_featBins.push_back(FastBDT::FeatureBinning<double>(nBinCuts, featureVec.begin(), featureVec.end()));
85 nBinningLevels.push_back(nBinCuts);
89 B2DEBUG(20,
"FBDTClassifier::train(): Creating the EventSamples");
90 #if FastBDT_VERSION_MAJOR >= 5
91 FastBDT::EventSample eventSample(nSamples, Ndims, 0, nBinningLevels);
93 FastBDT::EventSample eventSample(nSamples, Ndims, nBinningLevels);
95 for (
size_t iS = 0; iS < nSamples; ++iS) {
96 std::vector<unsigned> bins(Ndims);
97 for (
size_t iF = 0; iF < Ndims; ++iF) {
98 bins[iF] = m_featBins[iF].ValueToBin(data[iF][iS]);
100 eventSample.AddEvent(bins, 1.0, samples[iS].signal);
103 B2DEBUG(20,
"FBDTClassifier::train(): Training the FastBDT");
104 FastBDT::ForestBuilder fbdt(eventSample, nTrees, shrinkage, ratio, depth);
106 B2DEBUG(20,
"FBDTClassifier::train(): getting FastBDT to internal member");
107 #if FastBDT_VERSION_MAJOR >= 3
108 FBDTForest forest(fbdt.GetF0(), fbdt.GetShrinkage(),
true);
110 FBDTForest forest(fbdt.GetF0(), fbdt.GetShrinkage());
112 for (
const auto& tree : fbdt.GetForest()) {
113 forest.AddTree(tree);
void readFromStream(std::istream &is)
read all the necessary data from stream and fill the Forest and the FeatureBinnings NOTE: uses FastBD...
void writeToStream(std::ostream &os) const
write out the data from the Forest and the FeatureBinnings to a stream NOTE: uses FastBDTs IO stuff.
void train(const std::vector< Belle2::FBDTTrainSample< Ndims > > &samples, int nTree, int depth, double shrinkage=0.15, double ratio=0.5)
train the BDT NOTE overwrites a currently existing classifier internally TODO does not work at the mo...
Abstract base class for different kinds of events.
bundle together the classifier input and the target value into one struct for easier passing around.