Belle II Software development
FBDTClassifier< Ndims > Class Template Reference

FastBDT as RelationsObject to make it storable and accessible on/via the DataStore. More...

#include <FBDTClassifier.h>

Public Member Functions

 FBDTClassifier (const FBDTForest &forest, const std::vector< FastBDT::FeatureBinning< double > > &fB, const Belle2::DecorrelationMatrix< 9 > &dM)
 constructor from three main parts.
 
 ~FBDTClassifier ()
 TODO destructor.
 
double analyze (const std::array< double, Ndims > &hits) const
 calculate the output of the FastBDT.
 
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 moment, look in FastBDT/src/main.cxx how to solve this in all generality
 
void readFromStream (std::istream &is)
 read all the necessary data from stream and fill the Forest and the FeatureBinnings NOTE: uses FastBDTs IO stuff.
 
void writeToStream (std::ostream &os) const
 write out the data from the Forest and the FeatureBinnings to a stream NOTE: uses FastBDTs IO stuff.
 
FBDTForest getForest () const
 get the forest
 
const std::vector< FastBDT::FeatureBinning< double > > & getFeatureBinnings () const
 get the feature binnings
 
Belle2::DecorrelationMatrix< 9 > getDecorrelationMatrix () const
 get the decorrelation matrix
 

Private Attributes

FBDTForest m_forest {}
 the forest used for classification
 
std::vector< FastBDT::FeatureBinning< double > > m_featBins {}
 the feature binnings corresponding to the BDT
 
Belle2::DecorrelationMatrix< Ndims > m_decorrMat {}
 the decorrelation matrix used in this classifier
 

Detailed Description

template<size_t Ndims = 9>
class Belle2::FBDTClassifier< Ndims >

FastBDT as RelationsObject to make it storable and accessible on/via the DataStore.

Ndims: number of inputs (dimension of input vector)

TODO-List:

  • Longterm: template this properly

Definition at line 37 of file FBDTClassifier.h.

Constructor & Destructor Documentation

◆ FBDTClassifier() [1/2]

template<size_t Ndims = 9>
FBDTClassifier ( )
inline

Definition at line 41 of file FBDTClassifier.h.

41{ ; }// = default; /**< default constructor */

◆ FBDTClassifier() [2/2]

template<size_t Ndims = 9>
FBDTClassifier ( const FBDTForest & forest,
const std::vector< FastBDT::FeatureBinning< double > > & fB,
const Belle2::DecorrelationMatrix< 9 > & dM )
inline

constructor from three main parts.

(for testing purposes mainly)

Definition at line 44 of file FBDTClassifier.h.

45 : m_forest(forest), m_featBins(fB), m_decorrMat(dM) { ; }

◆ ~FBDTClassifier()

template<size_t Ndims = 9>
~FBDTClassifier ( )
inline

TODO destructor.

Definition at line 47 of file FBDTClassifier.h.

Member Function Documentation

◆ getDecorrelationMatrix()

template<size_t Ndims = 9>
Belle2::DecorrelationMatrix< 9 > getDecorrelationMatrix ( ) const
inline

get the decorrelation matrix

Definition at line 76 of file FBDTClassifier.h.

76{ return m_decorrMat; }

◆ getFeatureBinnings()

template<size_t Ndims = 9>
const std::vector< FastBDT::FeatureBinning< double > > & getFeatureBinnings ( ) const
inline

get the feature binnings

Definition at line 73 of file FBDTClassifier.h.

73{ return m_featBins; }

◆ getForest()

template<size_t Ndims = 9>
FBDTForest getForest ( ) const
inline

get the forest

Definition at line 70 of file FBDTClassifier.h.

70{ return m_forest; }

◆ readFromStream()

template<size_t Ndims>
void readFromStream ( std::istream & is)

read all the necessary data from stream and fill the Forest and the FeatureBinnings NOTE: uses FastBDTs IO stuff.

Not compatible with the .xml files that TMVA produces

Definition at line 17 of file FBDTClassifier.cc.

18{
19 m_featBins.clear(); // clear possibly present feature Binning
20 B2DEBUG(20, "Reading the FeatureBinnings");
21 is >> m_featBins;
22 B2DEBUG(20, "Reading the Forest");
24 B2DEBUG(20, "Reading the DecorrelationMatrix");
25 if (!m_decorrMat.readFromStream(is)) { // for some reason this does not stop if there is no decor matrix
26 B2ERROR("Reading in the decorrelation matrix did not work! The decorrelation matrix of this classifier will be set to identity!");
28 }
29}
FastBDT as RelationsObject to make it storable and accessible on/via the DataStore.
FBDTForest m_forest
the forest used for classification
std::vector< FastBDT::FeatureBinning< double > > m_featBins
the feature binnings corresponding to the BDT
Belle2::DecorrelationMatrix< Ndims > m_decorrMat
the decorrelation matrix used in this classifier

◆ train()

template<size_t Ndims>
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 moment, look in FastBDT/src/main.cxx how to solve this in all generality

Definition at line 43 of file FBDTClassifier.cc.

45{
46 if (samples.empty()) {
47 B2ERROR("No samples passed for training a FBDTClassifier.");
48 return;
49 }
50
51 unsigned int nBinCuts = 8;
52 size_t nSamples = samples.size();
53 B2DEBUG(20, "Using for training: nBinCuts: " << nBinCuts << ", with " << Ndims << " features and " << nSamples << " samples.");
54
55 B2DEBUG(20, "FBDTClassifier::train(): Starting to restructure the data into the format better suited for later use");
57 for (const auto& event : samples) {
58 for (size_t iSP = 0; iSP < Ndims; ++iSP) {
59 data[iSP].push_back(event.hits[iSP]);
60 }
61 }
62
63 B2DEBUG(20, "FBDTClassifier::train(): Calculating the decorrelation transformation.");
64 m_decorrMat.calculateDecorrMatrix(data, false);
65 B2DEBUG(20, "FBDTClassifier::train(): Applying decorrelation transformation");
66 data = m_decorrMat.decorrelate(data);
67
68 B2DEBUG(20, "FBDTClassifier::train(): Determining the FeatureBinnings");
70 m_featBins.clear(); // clear the feature binnings (if present)
71 for (auto featureVec : data) {
73 nBinningLevels.push_back(nBinCuts);
74 }
75
76 // have to use the decorrelated data for training!!!
77 B2DEBUG(20, "FBDTClassifier::train(): Creating the EventSamples");
79 for (size_t iS = 0; iS < nSamples; ++iS) {
81 for (size_t iF = 0; iF < Ndims; ++iF) {
82 bins[iF] = m_featBins[iF].ValueToBin(data[iF][iS]);
83 }
84 eventSample.AddEvent(bins, 1.0, samples[iS].signal);
85 }
86
87 B2DEBUG(20, "FBDTClassifier::train(): Training the FastBDT");
89
90 B2DEBUG(20, "FBDTClassifier::train(): getting FastBDT to internal member");
91 FBDTForest forest(fbdt.GetF0(), fbdt.GetShrinkage(), true);
92 for (const auto& tree : fbdt.GetForest()) {
93 forest.AddTree(tree);
94 }
95
96 m_forest = forest; // check if this can be done better with move or something similar
97}

◆ writeToStream()

template<size_t Ndims>
void writeToStream ( std::ostream & os) const

write out the data from the Forest and the FeatureBinnings to a stream NOTE: uses FastBDTs IO stuff.

Not compatible with the .xml files that TMVA produces

Definition at line 32 of file FBDTClassifier.cc.

33{
34 B2DEBUG(20, "Reading the FeatureBinnings");
36 B2DEBUG(20, "Reading the Forest");
37 os << m_forest << std::endl;
38 B2DEBUG(20, "Reading the DecorrelationMatrix");
39 os << m_decorrMat.print() << std::endl;
40}

Member Data Documentation

◆ m_decorrMat

template<size_t Ndims = 9>
Belle2::DecorrelationMatrix<Ndims> m_decorrMat {}
private

the decorrelation matrix used in this classifier

Definition at line 84 of file FBDTClassifier.h.

84{};

◆ m_featBins

template<size_t Ndims = 9>
std::vector<FastBDT::FeatureBinning<double> > m_featBins {}
private

the feature binnings corresponding to the BDT

Definition at line 82 of file FBDTClassifier.h.

82{};

◆ m_forest

template<size_t Ndims = 9>
FBDTForest m_forest {}
private

the forest used for classification

Definition at line 80 of file FBDTClassifier.h.

80{};

The documentation for this class was generated from the following files: