13 if (fullString.length() >= ending.length()) {
14 return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
22 FILE*
f = fopen(weightFile.c_str(),
"r");
24 throw cms::Exception(
"InvalidFileState") <<
"Failed to open MVA file = " << weightFile <<
" !!\n";
28 fread(&magic, 4, 1, f);
29 fseek(f, -4, SEEK_END);
30 fread(&size, 4, 1, f);
34 gzFile
file = gzopen(weightFile.c_str(),
"r");
37 char*
buffer = (
char*)malloc(size);
38 bytes_read = gzread(file, buffer, size - 1);
39 buffer[bytes_read] =
'\0';
42 const char* error_string;
43 error_string = gzerror(file, &err);
46 throw cms::Exception(
"InvalidFileState") <<
"Error while reading gzipped file = " 47 << weightFile <<
" !!\n" << error_string;
58 TMVA::IMethod* ptr =
nullptr;
62 std::cout <<
"Booking TMVA Reader with " << method <<
" and weight file: " << weightFile << std::endl;
66 std::cout <<
"Weight file is pure xml." << std::endl;
68 ptr = reader->BookMVA(method, weightFile);
71 std::cout <<
"Unzipping file." << std::endl;
77 char tmpFilename[] =
"/tmp/tmva.XXXXXX";
78 int fdToUselessFile = mkstemp(tmpFilename);
80 weight_file_name +=
".xml";
81 FILE* theActualFile = fopen(weight_file_name.c_str(),
"w");
82 if (theActualFile !=
nullptr) {
84 fputs(c, theActualFile);
85 fputs(
"\n", theActualFile);
86 fclose(theActualFile);
87 close(fdToUselessFile);
89 throw cms::Exception(
"CannotWriteFile") <<
"Error while writing file = " << weight_file_name <<
" !!\n";
93 ptr = reader->BookMVA(method, weight_file_name);
96 remove(weight_file_name.c_str());
102 std::cout <<
"Reader booked" << std::endl;
107 <<
"I don't understand the extension on the filename: " << weightFile
108 <<
", it should be .xml, .gz, or .gzip" << std::endl;
bool hasEnding(std::string const &fullString, std::string const &ending)
char * readGzipFile(const std::string &weightFile)
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)