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)
 
void 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 }
void 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 trackerHits::c, gather_cfg::cout, edm::hlt::Exception, hasEnding(), readGzipFile(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by cms::PileupJPTJetIdAlgo::bookMVAReader(), and PileupJetIdAlgo::bookReader().

58  {
59  verbose = false;
60  if (verbose)
61  std::cout << "Booking TMVA Reader with " << method << " and weight file: " << weightFile
62  << std::endl;
63 
64  if (reco::details::hasEnding(weightFile, ".xml")) {
65  if (verbose)
66  std::cout << "Weight file is pure xml." << std::endl;
67  // Let TMVA read the file
68  reader->BookMVA(method, weightFile);
69  } else if (reco::details::hasEnding(weightFile, ".gz") || reco::details::hasEnding(weightFile, ".gzip")) {
70  if (verbose)
71  std::cout << "Unzipping file." << std::endl;
72  char* c = readGzipFile(weightFile);
73 
74  // We can't use tmpnam, gcc emits a warning about security.
75  // This is also technically insecure in the same way, since we append
76  // a suffix and then open another file.
77  char tmpFilename[] = "/tmp/tmva.XXXXXX";
78  int fdToUselessFile = mkstemp(tmpFilename);
79  std::string weight_file_name(tmpFilename);
80  weight_file_name += ".xml";
81  FILE *theActualFile = fopen(weight_file_name.c_str(), "w");
82  // write xml
83  fputs(c, theActualFile);
84  fputs("\n", theActualFile);
85  fclose(theActualFile);
86  close(fdToUselessFile);
87  if (verbose)
88  std::cout << "Booking MvA" << std::endl;
89  reader->BookMVA(method, weight_file_name);
90  if (verbose)
91  std::cout << "Cleaning up" << std::endl;
92  remove(weight_file_name.c_str());
93  remove(tmpFilename);
94 
95  // Someday this will work.
96  //reader->BookMVA(TMVA::Types::Instance().GetMethodType(TString(method)), c);
97  if (verbose) {
98  std::cout << "Reader booked" << std::endl;
99  }
100  free(c);
101  } else {
102  throw cms::Exception("BadTMVAWeightFilename")
103  << "I don't understand the extension on the filename: "
104  << weightFile << ", it should be .xml, .gz, or .gzip" << std::endl;
105  }
106 }
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 edm::hlt::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.