CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions
GBRForestTools Class Reference

#include <GBRForestTools.h>

Public Member Functions

 GBRForestTools ()
 

Static Public Member Functions

static std::unique_ptr< const GBRForestcreateGBRForest (const std::string &weightFile)
 
static std::unique_ptr< const GBRForestcreateGBRForest (const edm::FileInPath &weightFile)
 

Detailed Description

Definition at line 25 of file GBRForestTools.h.

Constructor & Destructor Documentation

GBRForestTools::GBRForestTools ( )
inline

Member Function Documentation

std::unique_ptr< const GBRForest > GBRForestTools::createGBRForest ( const std::string &  weightFile)
static
std::unique_ptr< const GBRForest > GBRForestTools::createGBRForest ( const edm::FileInPath weightFile)
static

Definition at line 36 of file GBRForestTools.cc.

References f, edm::FileInPath::fullPath(), reco::details::hasEnding(), mps_fire::i, geometryCSVtoXML::line, reco::details::loadTMVAWeights(), AlcaSiPixelAliHarvester0T_cff::method, reco::details::readGzipFile(), AlCaHLTBitMon_QueryRunRegistry::string, and varNames.

36  {
37 
39 
40  unsigned int NVar = 0;
41  unsigned int NSpec = 0;
42 
43  std::vector<float> dumbVars;
44  std::vector<float> dumbSpecs;
45 
46  std::vector<std::string> varNames;
47  std::vector<std::string> specNames;
48 
50  std::ifstream f;
51  std::string tmpstr;
52 
53  bool gzipped = false;
54 
55  //
56  // Set up the input buffers, for gzipped or raw xml file
57  //
58  if (reco::details::hasEnding(weightFile.fullPath(), ".xml")) {
59  f.open(weightFile.fullPath());
60  tmpstr = "";
61  } else if (reco::details::hasEnding(weightFile.fullPath(), ".gz") || reco::details::hasEnding(weightFile.fullPath(), ".gzip")) {
62  gzipped = true;
63  tmpstr = std::string(reco::details::readGzipFile(weightFile.fullPath()));
64  }
65  std::stringstream is(tmpstr);
66 
67  bool isend;
68 
69  while(true) {
70 
71  if (gzipped) isend = !std::getline(is, line);
72  else isend = !std::getline(f, line);
73 
74  if (isend) break;
75 
76  // Terminate reading of weights file
77  if (line.find("<Weights ") != std::string::npos) break;
78 
79  // Method name
80  else if (line.find("<MethodSetup Method=") != std::string::npos) {
81  method = get_quoted_substring(line, 1, 2);
82  }
83 
84  // Number of variables
85  else if (line.find("<Variables NVar=") != std::string::npos) {
86  NVar = std::atoi(get_quoted_substring(line, 1, 2).c_str());
87  }
88 
89  // Number of spectators
90  else if (line.find("<Spectators NSpec=") != std::string::npos) {
91  NSpec = std::atoi(get_quoted_substring(line, 1, 2).c_str());
92  }
93 
94  // If variable
95  else if (line.find("<Variable ") != std::string::npos) {
96  unsigned int pos = line.find("Expression=");
97  varNames.push_back(get_quoted_substring(line.substr(pos, line.length() - pos), 1, 2));
98  dumbVars.push_back(0);
99  }
100 
101  // If spectator
102  else if (line.find("Spectator ") != std::string::npos) {
103  unsigned int pos = line.find("Expression=");
104  specNames.push_back(get_quoted_substring(line.substr(pos, line.length() - pos), 1, 2));
105  dumbSpecs.push_back(0);
106  }
107  }
108 
109  //
110  // Create the reader
111  //
112  TMVA::Reader* mvaReader = new TMVA::Reader("!Color:Silent:!Error");
113 
114  //
115  // Configure all variables and spectators. Note: the order and names
116  // must match what is found in the xml weights file!
117  //
118  for(size_t i = 0; i < NVar; ++i){
119  mvaReader->AddVariable(varNames[i], &dumbVars[i]);
120  }
121 
122  for(size_t i = 0; i < NSpec; ++i){
123  mvaReader->AddSpectator(specNames[i], &dumbSpecs[i]);
124  }
125 
126  //
127  // Book the method and set up the weights file
128  //
129 
130  reco::details::loadTMVAWeights(mvaReader, method, weightFile.fullPath());
131 
132  TMVA::MethodBDT* bdt = dynamic_cast<TMVA::MethodBDT*>( mvaReader->FindMVA(method) );
133  std::unique_ptr<const GBRForest> gbrForest = std::make_unique<const GBRForest>(GBRForest(bdt));
134  delete mvaReader;
135 
136  return gbrForest;
137 }
bool hasEnding(std::string const &fullString, std::string const &ending)
double f[11][100]
char * readGzipFile(const std::string &weightFile)
char const * varNames[]
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
std::string fullPath() const
Definition: FileInPath.cc:197