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

◆ hasEnding()

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

Definition at line 11 of file TMVAZipReader.cc.

Referenced by loadTMVAWeights().

11  {
12  if (fullString.length() >= ending.length()) {
13  return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
14  } else {
15  return false;
16  }
17 }

◆ loadTMVAWeights()

TMVA::IMethod * reco::details::loadTMVAWeights ( TMVA::Reader *  reader,
const std::string &  method,
const std::string &  weightFile,
bool  verbose = false 
)

Definition at line 52 of file TMVAZipReader.cc.

References HltBtagPostValidation_cff::c, gather_cfg::cout, Exception, free(), hasEnding(), AlcaSiPixelAliHarvester0T_cff::method, DQM::reader, readGzipFile(), AlCaHLTBitMon_QueryRunRegistry::string, verbose, and HLT_2024v13_cff::weightFile.

Referenced by BaseMVAValueMapProducer< pat::Muon >::BaseMVAValueMapProducer(), cms::PileupJPTJetIdAlgo::bookMVAReader(), MVAJetPuId::bookReader(), l1t::HGC3DClusterTMVASelector::HGC3DClusterTMVASelector(), TMVAEvaluator::initialize(), and l1tpf::HGC3DClusterEgID::prepareTMVA().

55  {
56  TMVA::IMethod* ptr = nullptr;
57 
58  verbose = false;
59  if (verbose)
60  std::cout << "Booking TMVA Reader with " << method << " and weight file: " << weightFile << std::endl;
61 
62  if (reco::details::hasEnding(weightFile, ".xml")) {
63  if (verbose)
64  std::cout << "Weight file is pure xml." << std::endl;
65  // Let TMVA read the file
66  ptr = reader->BookMVA(method, weightFile);
68  if (verbose)
69  std::cout << "Unzipping file." << std::endl;
70  char* c = readGzipFile(weightFile);
71 
72  // We can't use tmpnam, gcc emits a warning about security.
73  // This is also technically insecure in the same way, since we append
74  // a suffix and then open another file.
75  char tmpFilename[] = "/tmp/tmva.XXXXXX";
76  int fdToUselessFile = mkstemp(tmpFilename);
77  std::string weight_file_name(tmpFilename);
78  weight_file_name += ".xml";
79  FILE* theActualFile = fopen(weight_file_name.c_str(), "w");
80  if (theActualFile != nullptr) {
81  // write xml
82  fputs(c, theActualFile);
83  fputs("\n", theActualFile);
84  fclose(theActualFile);
85  close(fdToUselessFile);
86  } else {
87  throw cms::Exception("CannotWriteFile") << "Error while writing file = " << weight_file_name << " !!\n";
88  }
89  if (verbose)
90  std::cout << "Booking MvA" << std::endl;
91  ptr = reader->BookMVA(method, weight_file_name);
92  if (verbose)
93  std::cout << "Cleaning up" << std::endl;
94  remove(weight_file_name.c_str());
95  remove(tmpFilename);
96 
97  // Someday this will work.
98  // reader->BookMVA(TMVA::Types::Instance().GetMethodType(TString(method)), c);
99  if (verbose) {
100  std::cout << "Reader booked" << std::endl;
101  }
102  free(c);
103  } else {
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;
106  }
107 
108  return ptr;
109 }
bool hasEnding(std::string const &fullString, std::string const &ending)
bool verbose
reader
Definition: DQM.py:105
void free(void *ptr) noexcept
char * readGzipFile(const std::string &weightFile)

◆ readGzipFile()

char * reco::details::readGzipFile ( const std::string &  weightFile)

Definition at line 19 of file TMVAZipReader.cc.

References edmScanValgrind::buffer, submitPVResolutionJobs::err, Exception, f, geometryDiff::file, free(), malloc(), and HLT_2024v13_cff::weightFile.

Referenced by loadTMVAWeights().

19  {
20  FILE* f = fopen(weightFile.c_str(), "r");
21  if (f == nullptr) {
22  throw cms::Exception("InvalidFileState") << "Failed to open MVA file = " << weightFile << " !!\n";
23  }
24  int magic;
25  int size;
26  fread(&magic, 4, 1, f);
27  fseek(f, -4, SEEK_END);
28  fread(&size, 4, 1, f);
29  fclose(f);
30  // printf("%x, %i\n", magic, size);
31 
32  gzFile file = gzopen(weightFile.c_str(), "r");
33 
34  int bytes_read;
35  char* buffer = (char*)malloc(size);
36  bytes_read = gzread(file, buffer, size - 1);
37  buffer[bytes_read] = '\0';
38  if (!gzeof(file)) {
39  int err;
40  const char* error_string;
41  error_string = gzerror(file, &err);
42  if (err) {
43  free(buffer);
44  throw cms::Exception("InvalidFileState") << "Error while reading gzipped file = " << weightFile << " !!\n"
45  << error_string;
46  }
47  }
48  gzclose(file);
49  return buffer;
50 }
size
Write out results.
void free(void *ptr) noexcept
double f[11][100]
void * malloc(size_t size) noexcept