CMS 3D CMS Logo

GBRForestD.h
Go to the documentation of this file.
1 
2 #ifndef EGAMMAOBJECTS_GBRForestD
3 #define EGAMMAOBJECTS_GBRForestD
4 
6 // //
7 // GBRForestD //
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 the output of GBRLikelihood, //
13 // but could also be generalized to otherwise-trained trees //
14 // classification, or other boosting methods in the future //
15 // //
16 // Josh Bendavid - CERN //
18 
20 
21 #include "GBRTreeD.h"
22 
23 #include <vector>
24 
25 class GBRForestD {
26 public:
27  typedef GBRTreeD TreeT;
28 
30  template <typename InputForestT>
31  GBRForestD(const InputForestT &forest);
32 
33  double GetResponse(const float *vector) const;
34 
35  double InitialResponse() const { return fInitialResponse; }
36  void SetInitialResponse(double response) { fInitialResponse = response; }
37 
38  std::vector<GBRTreeD> &Trees() { return fTrees; }
39  const std::vector<GBRTreeD> &Trees() const { return fTrees; }
40 
41 protected:
42  double fInitialResponse = 0.0;
43  std::vector<GBRTreeD> fTrees;
44 
46 };
47 
48 //_______________________________________________________________________
49 inline double GBRForestD::GetResponse(const float *vector) const {
50  double response = fInitialResponse;
51  for (std::vector<GBRTreeD>::const_iterator it = fTrees.begin(); it != fTrees.end(); ++it) {
52  int termidx = it->TerminalIndex(vector);
53  response += it->GetResponse(termidx);
54  }
55  return response;
56 }
57 
58 //_______________________________________________________________________
59 template <typename InputForestT>
60 GBRForestD::GBRForestD(const InputForestT &forest) : fInitialResponse(forest.InitialResponse()) {
61  //templated constructor to allow construction from Forest classes in GBRLikelihood
62  //without creating an explicit dependency
63 
64  for (typename std::vector<typename InputForestT::TreeT>::const_iterator treeit = forest.Trees().begin();
65  treeit != forest.Trees().end();
66  ++treeit) {
67  fTrees.push_back(GBRTreeD(*treeit));
68  }
69 }
70 
71 #endif
std::vector< GBRTreeD > & Trees()
Definition: GBRForestD.h:38
const std::vector< GBRTreeD > & Trees() const
Definition: GBRForestD.h:39
std::vector< GBRTreeD > fTrees
Definition: GBRForestD.h:43
void SetInitialResponse(double response)
Definition: GBRForestD.h:36
GBRTreeD TreeT
Definition: GBRForestD.h:27
double GetResponse(const float *vector) const
Definition: GBRForestD.h:49
#define COND_SERIALIZABLE
Definition: Serializable.h:39
double InitialResponse() const
Definition: GBRForestD.h:35
double fInitialResponse
Definition: GBRForestD.h:42