GBRTreeD

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

#ifndef EGAMMAOBJECTS_GBRTreeD
#define EGAMMAOBJECTS_GBRTreeD

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// GBRForest                                                            //
//                                                                      //
// A fast minimal implementation of Gradient-Boosted Regression Trees   //
// which has been especially optimized for size on disk and in memory.  //
//                                                                      //
// Designed to be built from TMVA-trained trees, but could also be      //
// generalized to otherwise-trained trees, classification,              //
//  or other boosting methods in the future                             //
//                                                                      //
//  Josh Bendavid - CERN                                                 //
//////////////////////////////////////////////////////////////////////////

// The decision tree is implemented here as a set of two arrays, one for
// intermediate nodes, containing the variable index and cut value, as well
// as the indices of the 'left' and 'right' daughter nodes.  Positive indices
// indicate further intermediate nodes, whereas negative indices indicate
// terminal nodes, which are stored simply as a vector of regression responses

#include "CondFormats/Serialization/interface/Serializable.h"

#include <vector>

class GBRTreeD {
public:
  GBRTreeD() {}
  template <typename InputTreeT>
  GBRTreeD(const InputTreeT &tree);

  //double GetResponse(const float* vector) const;
  double GetResponse(int termidx) const { return fResponses[termidx]; }
  int TerminalIndex(const float *vector) const;

  std::vector<double> &Responses() { return fResponses; }
  const std::vector<double> &Responses() const { return fResponses; }

  std::vector<unsigned short> &CutIndices() { return fCutIndices; }
  const std::vector<unsigned short> &CutIndices() const { return fCutIndices; }

  std::vector<float> &CutVals() { return fCutVals; }
  const std::vector<float> &CutVals() const { return fCutVals; }

  std::vector<int> &LeftIndices() { return fLeftIndices; }
  const std::vector<int> &LeftIndices() const { return fLeftIndices; }

  std::vector<int> &RightIndices() { return fRightIndices; }
  const std::vector<int> &RightIndices() const { return fRightIndices; }

protected:
  std::vector<unsigned short> fCutIndices;
  std::vector<float> fCutVals;
  std::vector<int> fLeftIndices;
  std::vector<int> fRightIndices;
  std::vector<double> fResponses;

  COND_SERIALIZABLE;
};

//_______________________________________________________________________
inline int GBRTreeD::TerminalIndex(const float *vector) const {
  int index = 0;

  unsigned short cutindex = fCutIndices[0];
  float cutval = fCutVals[0];

  while (true) {
    if (vector[cutindex] > cutval) {
      index = fRightIndices[index];
    } else {
      index = fLeftIndices[index];
    }

    if (index > 0) {
      cutindex = fCutIndices[index];
      cutval = fCutVals[index];
    } else {
      return (-index);
    }
  }
}

//_______________________________________________________________________
template <typename InputTreeT>
GBRTreeD::GBRTreeD(const InputTreeT &tree)
    : fCutIndices(tree.CutIndices()),
      fCutVals(tree.CutVals()),
      fLeftIndices(tree.LeftIndices()),
      fRightIndices(tree.RightIndices()),
      fResponses(tree.Responses()) {}

#endif