CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GBRTree2D.h
Go to the documentation of this file.
1 
2 #ifndef EGAMMAOBJECTS_GBRTree2D
3 #define EGAMMAOBJECTS_GBRTree2D
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 - MIT //
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 
29 class GBRTree2D {
30 public:
31  GBRTree2D() {}
32 
33  void GetResponse(const float *vector, double &x, double &y) const;
34  int TerminalIndex(const float *vector) const;
35 
36  std::vector<float> &ResponsesX() { return fResponsesX; }
37  const std::vector<float> &ResponsesX() const { return fResponsesX; }
38 
39  std::vector<float> &ResponsesY() { return fResponsesY; }
40  const std::vector<float> &ResponsesY() const { return fResponsesY; }
41 
42  std::vector<unsigned short> &CutIndices() { return fCutIndices; }
43  const std::vector<unsigned short> &CutIndices() const { return fCutIndices; }
44 
45  std::vector<float> &CutVals() { return fCutVals; }
46  const std::vector<float> &CutVals() const { return fCutVals; }
47 
48  std::vector<int> &LeftIndices() { return fLeftIndices; }
49  const std::vector<int> &LeftIndices() const { return fLeftIndices; }
50 
51  std::vector<int> &RightIndices() { return fRightIndices; }
52  const std::vector<int> &RightIndices() const { return fRightIndices; }
53 
54 protected:
55  std::vector<unsigned short> fCutIndices;
56  std::vector<float> fCutVals;
57  std::vector<int> fLeftIndices;
58  std::vector<int> fRightIndices;
59  std::vector<float> fResponsesX;
60  std::vector<float> fResponsesY;
61 
63 };
64 
65 //_______________________________________________________________________
66 inline void GBRTree2D::GetResponse(const float *vector, double &x, double &y) const {
67  int index = 0;
68 
69  unsigned short cutindex = fCutIndices[0];
70  float cutval = fCutVals[0];
71 
72  while (true) {
73  if (vector[cutindex] > cutval) {
74  index = fRightIndices[index];
75  } else {
76  index = fLeftIndices[index];
77  }
78 
79  if (index > 0) {
80  cutindex = fCutIndices[index];
81  cutval = fCutVals[index];
82  } else {
83  x = fResponsesX[-index];
84  y = fResponsesY[-index];
85  return;
86  }
87  }
88 }
89 
90 //_______________________________________________________________________
91 inline int GBRTree2D::TerminalIndex(const float *vector) const {
92  int index = 0;
93 
94  unsigned short cutindex = fCutIndices[0];
95  float cutval = fCutVals[0];
96 
97  while (true) {
98  if (vector[cutindex] > cutval) {
99  index = fRightIndices[index];
100  } else {
101  index = fLeftIndices[index];
102  }
103 
104  if (index > 0) {
105  cutindex = fCutIndices[index];
106  cutval = fCutVals[index];
107  } else {
108  return (-index);
109  }
110  }
111 }
112 
113 #endif
std::vector< unsigned short > & CutIndices()
Definition: GBRTree2D.h:42
const std::vector< int > & RightIndices() const
Definition: GBRTree2D.h:52
std::vector< int > & LeftIndices()
Definition: GBRTree2D.h:48
const std::vector< int > & LeftIndices() const
Definition: GBRTree2D.h:49
int TerminalIndex(const float *vector) const
Definition: GBRTree2D.h:91
std::vector< float > fResponsesY
Definition: GBRTree2D.h:60
std::vector< float > & ResponsesY()
Definition: GBRTree2D.h:39
std::vector< int > & RightIndices()
Definition: GBRTree2D.h:51
std::vector< float > & CutVals()
Definition: GBRTree2D.h:45
const std::vector< float > & CutVals() const
Definition: GBRTree2D.h:46
void GetResponse(const float *vector, double &x, double &y) const
Definition: GBRTree2D.h:66
std::vector< int > fLeftIndices
Definition: GBRTree2D.h:57
std::vector< int > fRightIndices
Definition: GBRTree2D.h:58
GBRTree2D()
Definition: GBRTree2D.h:31
const std::vector< float > & ResponsesY() const
Definition: GBRTree2D.h:40
std::vector< float > fResponsesX
Definition: GBRTree2D.h:59
const std::vector< float > & ResponsesX() const
Definition: GBRTree2D.h:37
#define COND_SERIALIZABLE
Definition: Serializable.h:39
std::vector< float > & ResponsesX()
Definition: GBRTree2D.h:36
float x
std::vector< float > fCutVals
Definition: GBRTree2D.h:56
std::vector< unsigned short > fCutIndices
Definition: GBRTree2D.h:55
const std::vector< unsigned short > & CutIndices() const
Definition: GBRTree2D.h:43