Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:11

0001 
0002 #ifndef EGAMMAOBJECTS_GBRForestD
0003 #define EGAMMAOBJECTS_GBRForestD
0004 
0005 //////////////////////////////////////////////////////////////////////////
0006 //                                                                      //
0007 // GBRForestD                                                           //
0008 //                                                                      //
0009 // A fast minimal implementation of Gradient-Boosted Regression Trees   //
0010 // which has been especially optimized for size on disk and in memory.  //
0011 //                                                                      //
0012 // Designed to be built from the output of GBRLikelihood,               //
0013 // but could also be  generalized to otherwise-trained trees            //
0014 // classification, or other boosting methods in the future              //
0015 //                                                                      //
0016 //  Josh Bendavid - CERN                                                //
0017 //////////////////////////////////////////////////////////////////////////
0018 
0019 #include "CondFormats/Serialization/interface/Serializable.h"
0020 
0021 #include "GBRTreeD.h"
0022 
0023 #include <vector>
0024 
0025 class GBRForestD {
0026 public:
0027   typedef GBRTreeD TreeT;
0028 
0029   GBRForestD() {}
0030   template <typename InputForestT>
0031   GBRForestD(const InputForestT &forest);
0032 
0033   double GetResponse(const float *vector) const;
0034 
0035   double InitialResponse() const { return fInitialResponse; }
0036   void SetInitialResponse(double response) { fInitialResponse = response; }
0037 
0038   std::vector<GBRTreeD> &Trees() { return fTrees; }
0039   const std::vector<GBRTreeD> &Trees() const { return fTrees; }
0040 
0041 protected:
0042   double fInitialResponse = 0.0;
0043   std::vector<GBRTreeD> fTrees;
0044 
0045   COND_SERIALIZABLE;
0046 };
0047 
0048 //_______________________________________________________________________
0049 inline double GBRForestD::GetResponse(const float *vector) const {
0050   double response = fInitialResponse;
0051   for (std::vector<GBRTreeD>::const_iterator it = fTrees.begin(); it != fTrees.end(); ++it) {
0052     int termidx = it->TerminalIndex(vector);
0053     response += it->GetResponse(termidx);
0054   }
0055   return response;
0056 }
0057 
0058 //_______________________________________________________________________
0059 template <typename InputForestT>
0060 GBRForestD::GBRForestD(const InputForestT &forest) : fInitialResponse(forest.InitialResponse()) {
0061   //templated constructor to allow construction from Forest classes in GBRLikelihood
0062   //without creating an explicit dependency
0063 
0064   for (typename std::vector<typename InputForestT::TreeT>::const_iterator treeit = forest.Trees().begin();
0065        treeit != forest.Trees().end();
0066        ++treeit) {
0067     fTrees.push_back(GBRTreeD(*treeit));
0068   }
0069 }
0070 
0071 #endif