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)
 
static std::unique_ptr< const GBRForestcreateGBRForest (const std::string &weightFile, std::vector< std::string > &varNames)
 
static std::unique_ptr< const GBRForestcreateGBRForest (const edm::FileInPath &weightFile, std::vector< std::string > &varNames)
 

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

Definition at line 143 of file GBRForestTools.cc.

References varNames.

Referenced by createGBRForest(), GBRForestTools(), ElectronMVAEstimatorRun2::init(), and PhotonMVAEstimator::PhotonMVAEstimator().

143  {
144  std::vector<std::string> varNames;
146 }
static std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightFile)
char const * varNames[]
std::unique_ptr< const GBRForest > GBRForestTools::createGBRForest ( const edm::FileInPath weightFile)
static

Definition at line 148 of file GBRForestTools.cc.

References createGBRForest(), and varNames.

148  {
149  std::vector<std::string> varNames;
150  return GBRForestTools::createGBRForest(weightFile, varNames);
151 }
static std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightFile)
char const * varNames[]
std::unique_ptr< const GBRForest > GBRForestTools::createGBRForest ( const std::string &  weightFile,
std::vector< std::string > &  varNames 
)
static

Definition at line 30 of file GBRForestTools.cc.

References createGBRForest().

31  {
32  edm::FileInPath weightFileEdm(weightFile);
33  return GBRForestTools::createGBRForest(weightFileEdm, varNames);
34 }
static std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightFile)
char const * varNames[]
std::unique_ptr< const GBRForest > GBRForestTools::createGBRForest ( const edm::FileInPath weightFile,
std::vector< std::string > &  varNames 
)
static

Definition at line 37 of file GBRForestTools.cc.

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

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