CMS 3D CMS Logo

GBRTreeD.h
Go to the documentation of this file.
1 
2 #ifndef EGAMMAOBJECTS_GBRTreeD
3 #define EGAMMAOBJECTS_GBRTreeD
4 
6 // //
7 // GBRForest //
8 // //
9 // A fast minimal implementation of Gradient-Boosted Regression Trees //
10 // which has been especially optimized for size on disk and in memory. //
11 // //
12 // Designed to be built from TMVA-trained trees, but could also be //
13 // generalized to otherwise-trained trees, classification, //
14 // or other boosting methods in the future //
15 // //
16 // Josh Bendavid - CERN //
18 
19 // The decision tree is implemented here as a set of two arrays, one for
20 // intermediate nodes, containing the variable index and cut value, as well
21 // as the indices of the 'left' and 'right' daughter nodes. Positive indices
22 // indicate further intermediate nodes, whereas negative indices indicate
23 // terminal nodes, which are stored simply as a vector of regression responses
24 
26 
27 #include <vector>
28 #include <map>
29 #include <stdio.h>
30 #include <cmath>
31 #include "Rtypes.h"
32 
33 class GBRTreeD {
34 
35  public:
36 
37  GBRTreeD() {}
38  template<typename InputTreeT> GBRTreeD(const InputTreeT &tree);
39  virtual ~GBRTreeD();
40 
41  //double GetResponse(const float* vector) const;
42  double GetResponse(int termidx) const { return fResponses[termidx]; }
43  int TerminalIndex(const float *vector) const;
44 
45  std::vector<double> &Responses() { return fResponses; }
46  const std::vector<double> &Responses() const { return fResponses; }
47 
48  std::vector<unsigned short> &CutIndices() { return fCutIndices; }
49  const std::vector<unsigned short> &CutIndices() const { return fCutIndices; }
50 
51  std::vector<float> &CutVals() { return fCutVals; }
52  const std::vector<float> &CutVals() const { return fCutVals; }
53 
54  std::vector<int> &LeftIndices() { return fLeftIndices; }
55  const std::vector<int> &LeftIndices() const { return fLeftIndices; }
56 
57  std::vector<int> &RightIndices() { return fRightIndices; }
58  const std::vector<int> &RightIndices() const { return fRightIndices; }
59 
60  protected:
61  std::vector<unsigned short> fCutIndices;
62  std::vector<float> fCutVals;
63  std::vector<int> fLeftIndices;
64  std::vector<int> fRightIndices;
65  std::vector<double> fResponses;
66 
68 };
69 
70 
71 //_______________________________________________________________________
72 inline int GBRTreeD::TerminalIndex(const float* vector) const {
73 
74  int index = 0;
75 
76  unsigned short cutindex = fCutIndices[0];
77  float cutval = fCutVals[0];
78 
79  while (true) {
80  if (vector[cutindex] > cutval) {
81  index = fRightIndices[index];
82  }
83  else {
84  index = fLeftIndices[index];
85  }
86 
87  if (index>0) {
88  cutindex = fCutIndices[index];
89  cutval = fCutVals[index];
90  }
91  else {
92  return (-index);
93  }
94 
95  }
96 
97 }
98 
99 //_______________________________________________________________________
100 template<typename InputTreeT> GBRTreeD::GBRTreeD(const InputTreeT &tree) :
101  fCutIndices(tree.CutIndices()),
102  fCutVals(tree.CutVals()),
103  fLeftIndices(tree.LeftIndices()),
104  fRightIndices(tree.RightIndices()),
105  fResponses(tree.Responses()) {
106 
107 }
108 
109 #endif
std::vector< unsigned short > & CutIndices()
Definition: GBRTreeD.h:48
const std::vector< float > & CutVals() const
Definition: GBRTreeD.h:52
std::vector< int > fRightIndices
Definition: GBRTreeD.h:64
const std::vector< int > & RightIndices() const
Definition: GBRTreeD.h:58
const std::vector< unsigned short > & CutIndices() const
Definition: GBRTreeD.h:49
std::vector< int > & LeftIndices()
Definition: GBRTreeD.h:54
std::vector< float > & CutVals()
Definition: GBRTreeD.h:51
std::vector< double > fResponses
Definition: GBRTreeD.h:65
std::vector< float > fCutVals
Definition: GBRTreeD.h:62
const std::vector< int > & LeftIndices() const
Definition: GBRTreeD.h:55
std::vector< int > fLeftIndices
Definition: GBRTreeD.h:63
virtual ~GBRTreeD()
Definition: GBRTreeD.cc:4
std::vector< double > & Responses()
Definition: GBRTreeD.h:45
const std::vector< double > & Responses() const
Definition: GBRTreeD.h:46
GBRTreeD()
Definition: GBRTreeD.h:37
double GetResponse(int termidx) const
Definition: GBRTreeD.h:42
std::vector< unsigned short > fCutIndices
Definition: GBRTreeD.h:61
#define COND_SERIALIZABLE
Definition: Serializable.h:38
Definition: tree.py:1
std::vector< int > & RightIndices()
Definition: GBRTreeD.h:57
int TerminalIndex(const float *vector) const
Definition: GBRTreeD.h:72