CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions
reco::details Namespace Reference

Functions

bool hasEnding (std::string const &fullString, std::string const &ending)
 
TMVA::IMethod * loadTMVAWeights (TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
 
char * readGzipFile (const std::string &weightFile)
 

Function Documentation

bool reco::details::hasEnding ( std::string const &  fullString,
std::string const &  ending 
)

Definition at line 12 of file TMVAZipReader.cc.

Referenced by loadTMVAWeights().

12  {
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 }
TMVA::IMethod * reco::details::loadTMVAWeights ( TMVA::Reader *  reader,
const std::string &  method,
const std::string &  weightFile,
bool  verbose = false 
)

Definition at line 57 of file TMVAZipReader.cc.

References EnergyCorrector::c, gather_cfg::cout, Exception, hasEnding(), readGzipFile(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by GBRForestWriter::analyze(), cms::PileupJPTJetIdAlgo::bookMVAReader(), PileupJetIdAlgo::bookReader(), MVAJetPuId::bookReader(), and TMVAEvaluator::initialize().

58  {
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  // write xml
86  fputs(c, theActualFile);
87  fputs("\n", theActualFile);
88  fclose(theActualFile);
89  close(fdToUselessFile);
90  if (verbose)
91  std::cout << "Booking MvA" << std::endl;
92  ptr = reader->BookMVA(method, weight_file_name);
93  if (verbose)
94  std::cout << "Cleaning up" << std::endl;
95  remove(weight_file_name.c_str());
96  remove(tmpFilename);
97 
98  // Someday this will work.
99  //reader->BookMVA(TMVA::Types::Instance().GetMethodType(TString(method)), c);
100  if (verbose) {
101  std::cout << "Reader booked" << std::endl;
102  }
103  free(c);
104  } else {
105  throw cms::Exception("BadTMVAWeightFilename")
106  << "I don't understand the extension on the filename: "
107  << weightFile << ", it should be .xml, .gz, or .gzip" << std::endl;
108  }
109 
110  return ptr;
111 }
bool hasEnding(std::string const &fullString, std::string const &ending)
char * readGzipFile(const std::string &weightFile)
tuple cout
Definition: gather_cfg.py:121
char * reco::details::readGzipFile ( const std::string &  weightFile)

Definition at line 21 of file TMVAZipReader.cc.

References Exception, f, mergeVDriftHistosByStation::file, NULL, and findQualityFiles::size.

Referenced by loadTMVAWeights().

22 {
23  FILE *f = fopen(weightFile.c_str(), "r");
24  if (f==NULL) {
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 }
#define NULL
Definition: scimark2.h:8
double f[11][100]
tuple size
Write out results.