CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TMVAZipReader.cc
Go to the documentation of this file.
3 #include <stdio.h>
4 #include <stdlib.h>
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 
21 char* reco::details::readGzipFile(const std::string& weightFile)
22 {
23  FILE *f = fopen(weightFile.c_str(), "r");
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  cout<<"Error while reading gzipped file: "<<error_string;
44  }
45  }
46  gzclose (file);
47  return buffer;
48 }
49 
50 void reco::details::loadTMVAWeights(TMVA::Reader* reader, const std::string& method,
51  const std::string& weightFile, bool verbose) {
52  verbose = false;
53  if (verbose)
54  std::cout << "Booking TMVA Reader with " << method << " and weight file: " << weightFile
55  << std::endl;
56 
57  if (reco::details::hasEnding(weightFile, ".xml")) {
58  if (verbose)
59  std::cout << "Weight file is pure xml." << std::endl;
60  // Let TMVA read the file
61  reader->BookMVA(method, weightFile);
62  } else if (reco::details::hasEnding(weightFile, ".gz") || reco::details::hasEnding(weightFile, ".gzip")) {
63  if (verbose)
64  std::cout << "Unzipping file." << std::endl;
65  char* c = readGzipFile(weightFile);
66 
67  // We can't use tmpnam, gcc emits a warning about security.
68  // This is also technically insecure in the same way, since we append
69  // a suffix and then open another file.
70  char tmpFilename[] = "/tmp/tmva.XXXXXX";
71  int fdToUselessFile = mkstemp(tmpFilename);
72  std::string weight_file_name(tmpFilename);
73  weight_file_name += ".xml";
74  FILE *theActualFile = fopen(weight_file_name.c_str(), "w");
75  // write xml
76  fputs(c, theActualFile);
77  fputs("\n", theActualFile);
78  fclose(theActualFile);
79  close(fdToUselessFile);
80  if (verbose)
81  std::cout << "Booking MvA" << std::endl;
82  reader->BookMVA(method, weight_file_name);
83  if (verbose)
84  std::cout << "Cleaning up" << std::endl;
85  remove(weight_file_name.c_str());
86  remove(tmpFilename);
87 
88  // Someday this will work.
89  //reader->BookMVA(TMVA::Types::Instance().GetMethodType(TString(method)), c);
90  if (verbose) {
91  std::cout << "Reader booked" << std::endl;
92  }
93  delete c;
94  } else {
95  throw cms::Exception("BadTMVAWeightFilename")
96  << "I don't understand the extension on the filename: "
97  << weightFile << ", it should be .xml, .gz, or .gzip" << std::endl;
98  }
99 }
bool hasEnding(std::string const &fullString, std::string const &ending)
double f[11][100]
char * readGzipFile(const std::string &weightFile)
tuple cout
Definition: gather_cfg.py:121
tuple size
Write out results.
void loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)