12 if (fullString.length() >= ending.length()) {
13 return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
20 FILE*
f = fopen(weightFile.c_str(),
"r");
22 throw cms::Exception(
"InvalidFileState") <<
"Failed to open MVA file = " << weightFile <<
" !!\n";
26 fread(&magic, 4, 1, f);
27 fseek(f, -4, SEEK_END);
28 fread(&size, 4, 1, f);
32 gzFile
file = gzopen(weightFile.c_str(),
"r");
35 char*
buffer = (
char*)malloc(size);
36 bytes_read = gzread(file, buffer, size - 1);
37 buffer[bytes_read] =
'\0';
40 const char* error_string;
41 error_string = gzerror(file, &err);
44 throw cms::Exception(
"InvalidFileState") <<
"Error while reading gzipped file = " << weightFile <<
" !!\n" 56 TMVA::IMethod* ptr =
nullptr;
60 std::cout <<
"Booking TMVA Reader with " << method <<
" and weight file: " << weightFile << std::endl;
64 std::cout <<
"Weight file is pure xml." << std::endl;
66 ptr = reader->BookMVA(method, weightFile);
69 std::cout <<
"Unzipping file." << std::endl;
75 char tmpFilename[] =
"/tmp/tmva.XXXXXX";
76 int fdToUselessFile = mkstemp(tmpFilename);
78 weight_file_name +=
".xml";
79 FILE* theActualFile = fopen(weight_file_name.c_str(),
"w");
80 if (theActualFile !=
nullptr) {
82 fputs(c, theActualFile);
83 fputs(
"\n", theActualFile);
84 fclose(theActualFile);
85 close(fdToUselessFile);
87 throw cms::Exception(
"CannotWriteFile") <<
"Error while writing file = " << weight_file_name <<
" !!\n";
91 ptr = reader->BookMVA(method, weight_file_name);
94 remove(weight_file_name.c_str());
100 std::cout <<
"Reader booked" << std::endl;
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;
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)