CMS 3D CMS Logo

GBRForestTools.cc
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include <fstream>
5 
6 namespace {
7 
8  // Will return position of n-th occurence of a char in a string.
9  int strpos(const std::string &haystack, char needle, unsigned int nth)
10  {
11  int found = 0;
12  for (unsigned int i=0 ; i<nth ; ++i) {
13  std::size_t pos = haystack.find(needle, found);
14  if (pos == std::string::npos) return -1;
15  else found = pos+1;
16  }
17  return found;
18  }
19 
20  // To get the substring between the n1th and n2th quotation mark in a string
21  std::string get_quoted_substring(const std::string &str, int n1, int n2)
22  {
23  int pos = strpos(str, '"', n1);
24  int count = strpos(str, '"', n2) - pos;
25  return str.substr(pos, count - 1);
26  }
27 
28 };
29 
30 std::unique_ptr<const GBRForest> GBRForestTools::createGBRForest(const std::string &weightFile){
31  edm::FileInPath weightFileEdm(weightFile);
32  return GBRForestTools::createGBRForest(weightFileEdm);
33 }
34 
35 // Creates a pointer to new GBRForest corresponding to a TMVA weights file
36 std::unique_ptr<const GBRForest> GBRForestTools::createGBRForest(const edm::FileInPath &weightFile){
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)
constexpr char const * varNames[]
static std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightFile)
double f[11][100]
char * readGzipFile(const std::string &weightFile)
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
std::string fullPath() const
Definition: FileInPath.cc:184