CMS 3D CMS Logo

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