CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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);
67  } else if (reco::details::hasEnding(weightFile, ".gz") || reco::details::hasEnding(weightFile, ".gzip")) {
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 }
const edm::EventSetup & c
bool hasEnding(std::string const &fullString, std::string const &ending)
static constexpr int verbose
tuple reader
Definition: DQM.py:105
char * readGzipFile(const std::string &weightFile)
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
tuple cout
Definition: gather_cfg.py:144
tuple size
Write out results.