CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/RecoTauTag/RecoTau/src/TMVAZipReader.cc

Go to the documentation of this file.
00001 #include "RecoTauTag/RecoTau/interface/TMVAZipReader.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <cstdio>
00006 #include <cstdlib>
00007 #include "zlib.h"
00008 
00009 using namespace std;
00010 
00011 // From http://stackoverflow.com/questions/874134/find-if-string-endswith-another-string-in-c
00012 bool reco::details::hasEnding(std::string const &fullString, std::string const &ending) {
00013   if (fullString.length() >= ending.length()) {
00014     return (0 == fullString.compare(
00015           fullString.length() - ending.length(), ending.length(), ending));
00016   } else {
00017     return false;
00018   }
00019 }
00020 
00021 char* reco::details::readGzipFile(const std::string& weightFile)
00022 {
00023   FILE *f = fopen(weightFile.c_str(), "r");
00024   int magic;
00025   int size;
00026   fread(&magic, 4, 1, f);
00027   fseek(f, -4, SEEK_END);
00028   fread(&size, 4, 1, f);
00029   fclose(f);
00030   //printf("%x, %i\n", magic, size);
00031 
00032   gzFile  file = gzopen (weightFile.c_str(), "r");
00033 
00034   int bytes_read;
00035   char *buffer = (char*)malloc(size);
00036   bytes_read = gzread (file, buffer, size - 1);
00037   buffer[bytes_read] = '\0';
00038   if (!gzeof (file)) {
00039     int err;
00040     const char * error_string;
00041     error_string = gzerror (file, & err);
00042     if (err) {
00043       cout<<"Error while reading gzipped file:  "<<error_string;
00044     }
00045   }
00046   gzclose (file);
00047   return buffer;
00048 }
00049 
00050 void reco::details::loadTMVAWeights(TMVA::Reader* reader, const std::string& method,
00051     const std::string& weightFile, bool verbose) {
00052   verbose = false;
00053   if (verbose)
00054     std::cout << "Booking TMVA Reader with " << method << " and weight file: " << weightFile
00055       << std::endl;
00056 
00057   if (reco::details::hasEnding(weightFile, ".xml")) {
00058     if (verbose)
00059       std::cout << "Weight file is pure xml." << std::endl;
00060     // Let TMVA read the file
00061     reader->BookMVA(method, weightFile);
00062   } else if (reco::details::hasEnding(weightFile, ".gz") || reco::details::hasEnding(weightFile, ".gzip")) {
00063     if (verbose)
00064       std::cout << "Unzipping file." << std::endl;
00065     char* c = readGzipFile(weightFile);
00066 
00067     // We can't use tmpnam, gcc emits a warning about security.
00068     // This is also technically insecure in the same way, since we append
00069     // a suffix and then open another file.
00070     char tmpFilename[] = "/tmp/tmva.XXXXXX";
00071     int fdToUselessFile = mkstemp(tmpFilename);
00072     std::string weight_file_name(tmpFilename);
00073     weight_file_name += ".xml";
00074     FILE *theActualFile = fopen(weight_file_name.c_str(), "w");
00075     // write xml
00076     fputs(c, theActualFile);
00077     fputs("\n", theActualFile);
00078     fclose(theActualFile);
00079     close(fdToUselessFile);
00080     if (verbose)
00081       std::cout << "Booking MvA" << std::endl;
00082     reader->BookMVA(method, weight_file_name);
00083     if (verbose)
00084       std::cout << "Cleaning up" << std::endl;
00085     remove(weight_file_name.c_str());
00086     remove(tmpFilename);
00087 
00088     // Someday this will work.
00089     //reader->BookMVA(TMVA::Types::Instance().GetMethodType(TString(method)), c);
00090     if (verbose) {
00091       std::cout << "Reader booked" << std::endl;
00092     }
00093     delete c;
00094   } else {
00095     throw cms::Exception("BadTMVAWeightFilename")
00096       << "I don't understand the extension on the filename: "
00097       << weightFile << ", it should be .xml, .gz, or .gzip" << std::endl;
00098   }
00099 }