CMS 3D CMS Logo

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 GBRForestTools::createGBRForest(), and 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(), BaseMVAValueMapProducer< pat::Jet >::BaseMVAValueMapProducer(), cms::PileupJPTJetIdAlgo::bookMVAReader(), MVAJetPuId::bookReader(), GBRForestTools::createGBRForest(), PileupJetIdAlgo::AlgoGBRForestsAndConstants::getMVA(), 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);
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  if (theActualFile != nullptr) {
86  // write xml
87  fputs(c, theActualFile);
88  fputs("\n", theActualFile);
89  fclose(theActualFile);
90  close(fdToUselessFile);
91  } else {
92  throw cms::Exception("CannotWriteFile")
93  << "Error while writing file = " << weight_file_name << " !!\n";
94  }
95  if (verbose)
96  std::cout << "Booking MvA" << std::endl;
97  ptr = reader->BookMVA(method, weight_file_name);
98  if (verbose)
99  std::cout << "Cleaning up" << std::endl;
100  remove(weight_file_name.c_str());
101  remove(tmpFilename);
102 
103  // Someday this will work.
104  //reader->BookMVA(TMVA::Types::Instance().GetMethodType(TString(method)), c);
105  if (verbose) {
106  std::cout << "Reader booked" << std::endl;
107  }
108  free(c);
109  } else {
110  throw cms::Exception("BadTMVAWeightFilename")
111  << "I don't understand the extension on the filename: "
112  << weightFile << ", it should be .xml, .gz, or .gzip" << std::endl;
113  }
114 
115  return ptr;
116 }
bool hasEnding(std::string const &fullString, std::string const &ending)
char * readGzipFile(const std::string &weightFile)
char * reco::details::readGzipFile ( const std::string &  weightFile)

Definition at line 21 of file TMVAZipReader.cc.

References edmScanValgrind::buffer, Exception, f, FrontierConditions_GlobalTag_cff::file, and findQualityFiles::size.

Referenced by GBRForestTools::createGBRForest(), and loadTMVAWeights().

22 {
23  FILE *f = fopen(weightFile.c_str(), "r");
24  if (f==nullptr) {
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 }
size
Write out results.
double f[11][100]