CMS 3D CMS Logo

TMVAZipReader.cc
Go to the documentation of this file.
3 
4 #include <cstdio>
5 #include <cstdlib>
6 #include <zlib.h>
7 
8 using namespace std;
9 
10 // From http://stackoverflow.com/questions/874134/find-if-string-endswith-another-string-in-c
11 bool reco::details::hasEnding(std::string const& fullString, std::string const& ending) {
12  if (fullString.length() >= ending.length()) {
13  return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
14  } else {
15  return false;
16  }
17 }
18 
20  FILE* f = fopen(weightFile.c_str(), "r");
21  if (f == nullptr) {
22  throw cms::Exception("InvalidFileState") << "Failed to open MVA file = " << weightFile << " !!\n";
23  }
24  int magic;
25  int size;
26  fread(&magic, 4, 1, f);
27  fseek(f, -4, SEEK_END);
28  fread(&size, 4, 1, f);
29  fclose(f);
30  // printf("%x, %i\n", magic, size);
31 
32  gzFile file = gzopen(weightFile.c_str(), "r");
33 
34  int bytes_read;
35  char* buffer = (char*)malloc(size);
36  bytes_read = gzread(file, buffer, size - 1);
37  buffer[bytes_read] = '\0';
38  if (!gzeof(file)) {
39  int err;
40  const char* error_string;
41  error_string = gzerror(file, &err);
42  if (err) {
43  free(buffer);
44  throw cms::Exception("InvalidFileState") << "Error while reading gzipped file = " << weightFile << " !!\n"
45  << error_string;
46  }
47  }
48  gzclose(file);
49  return buffer;
50 }
51 
52 TMVA::IMethod* reco::details::loadTMVAWeights(TMVA::Reader* reader,
53  const std::string& method,
54  const std::string& weightFile,
55  bool verbose) {
56  TMVA::IMethod* ptr = nullptr;
57 
58  verbose = false;
59  if (verbose)
60  std::cout << "Booking TMVA Reader with " << method << " and weight file: " << weightFile << std::endl;
61 
62  if (reco::details::hasEnding(weightFile, ".xml")) {
63  if (verbose)
64  std::cout << "Weight file is pure xml." << std::endl;
65  // Let TMVA read the file
66  ptr = reader->BookMVA(method, weightFile);
68  if (verbose)
69  std::cout << "Unzipping file." << std::endl;
70  char* c = readGzipFile(weightFile);
71 
72  // We can't use tmpnam, gcc emits a warning about security.
73  // This is also technically insecure in the same way, since we append
74  // a suffix and then open another file.
75  char tmpFilename[] = "/tmp/tmva.XXXXXX";
76  int fdToUselessFile = mkstemp(tmpFilename);
77  std::string weight_file_name(tmpFilename);
78  weight_file_name += ".xml";
79  FILE* theActualFile = fopen(weight_file_name.c_str(), "w");
80  if (theActualFile != nullptr) {
81  // write xml
82  fputs(c, theActualFile);
83  fputs("\n", theActualFile);
84  fclose(theActualFile);
85  close(fdToUselessFile);
86  } else {
87  throw cms::Exception("CannotWriteFile") << "Error while writing file = " << weight_file_name << " !!\n";
88  }
89  if (verbose)
90  std::cout << "Booking MvA" << std::endl;
91  ptr = reader->BookMVA(method, weight_file_name);
92  if (verbose)
93  std::cout << "Cleaning up" << std::endl;
94  remove(weight_file_name.c_str());
95  remove(tmpFilename);
96 
97  // Someday this will work.
98  // reader->BookMVA(TMVA::Types::Instance().GetMethodType(TString(method)), c);
99  if (verbose) {
100  std::cout << "Reader booked" << std::endl;
101  }
102  free(c);
103  } else {
104  throw cms::Exception("BadTMVAWeightFilename") << "I don't understand the extension on the filename: " << weightFile
105  << ", it should be .xml, .gz, or .gzip" << std::endl;
106  }
107 
108  return ptr;
109 }
reco::details::readGzipFile
char * readGzipFile(const std::string &weightFile)
Definition: TMVAZipReader.cc:19
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
gather_cfg.cout
cout
Definition: gather_cfg.py:144
AlcaSiPixelAliHarvester0T_cff.method
method
Definition: AlcaSiPixelAliHarvester0T_cff.py:41
HLT_FULL_cff.weightFile
weightFile
Definition: HLT_FULL_cff.py:6711
edmScanValgrind.buffer
buffer
Definition: edmScanValgrind.py:171
DQM.reader
reader
Definition: DQM.py:105
verbose
static constexpr int verbose
Definition: HLTExoticaSubAnalysis.cc:25
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
submitPVResolutionJobs.err
err
Definition: submitPVResolutionJobs.py:85
std
Definition: JetResolutionObject.h:76
Exception
Definition: hltDiff.cc:246
reco::details::loadTMVAWeights
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
Definition: TMVAZipReader.cc:52
MatrixUtil.remove
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:219
TMVAZipReader.h
Exception.h
reco::details::hasEnding
bool hasEnding(std::string const &fullString, std::string const &ending)
Definition: TMVAZipReader.cc:11
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443