CMS 3D CMS Logo

GBRForestWriter.cc
Go to the documentation of this file.
2 
4 
7 
9 
10 #include <TFile.h>
11 
13  : moduleLabel_(cfg.getParameter<std::string>("@module_label"))
14 {
15  edm::VParameterSet cfgJobs = cfg.getParameter<edm::VParameterSet>("jobs");
16  for (edm::VParameterSet::const_iterator cfgJob = cfgJobs.begin(); cfgJob != cfgJobs.end(); ++cfgJob) {
17  jobEntryType* job = new jobEntryType(*cfgJob);
18  jobs_.push_back(job);
19  }
20 }
21 
23 {
24  for (std::vector<jobEntryType*>::iterator it = jobs_.begin(); it != jobs_.end(); ++it) {
25  delete (*it);
26  }
27 }
28 
30 {
31 
32  for (std::vector<jobEntryType*>::iterator job = jobs_.begin(); job != jobs_.end(); ++job) {
33  std::map<std::string, const GBRForest*> gbrForests; // key = name
34  for (std::vector<categoryEntryType*>::iterator category = (*job)->categories_.begin();
35  category != (*job)->categories_.end(); ++category) {
36  const GBRForest* gbrForest = nullptr;
37  if ((*category)->inputFileType_ == categoryEntryType::kXML) {
38  gbrForest = createGBRForest((*category)->inputFileName_).release();
39  } else if ((*category)->inputFileType_ == categoryEntryType::kGBRForest) {
40  TFile* inputFile = new TFile((*category)->inputFileName_.data());
41  // gbrForest = dynamic_cast<GBRForest*>(inputFile->Get((*category)->gbrForestName_.data())); // CV:
42  // dynamic_cast<GBRForest*> fails for some reason ?!
43  gbrForest = (GBRForest*)inputFile->Get((*category)->gbrForestName_.data());
44  delete inputFile;
45  }
46  if (!gbrForest)
47  throw cms::Exception("GBRForestWriter")
48  << " Failed to load GBRForest = " << (*category)->gbrForestName_.data()
49  << " from file = " << (*category)->inputFileName_ << " !!\n";
50  gbrForests[(*category)->gbrForestName_] = gbrForest;
51  }
52  if ((*job)->outputFileType_ == jobEntryType::kGBRForest) {
53  TFile* outputFile = new TFile((*job)->outputFileName_.data(), "RECREATE");
54 
55  for (std::map<std::string, const GBRForest*>::iterator gbrForest = gbrForests.begin();
56  gbrForest != gbrForests.end(); ++gbrForest) {
57  outputFile->WriteObject(gbrForest->second, gbrForest->first.data());
58  }
59  delete outputFile;
60  } else if ((*job)->outputFileType_ == jobEntryType::kSQLLite) {
62  if (!dbService.isAvailable())
63  throw cms::Exception("GBRForestWriter") << " Failed to access PoolDBOutputService !!\n";
64 
65  for (std::map<std::string, const GBRForest*>::iterator gbrForest = gbrForests.begin();
66  gbrForest != gbrForests.end(); ++gbrForest) {
67  std::string outputRecord = (*job)->outputRecord_;
68  if (gbrForests.size() > 1)
69  outputRecord.append("_").append(gbrForest->first);
70  dbService->writeOne(gbrForest->second, dbService->beginOfTime(), outputRecord);
71  }
72  }
73 
74  // gbrforest deletion
75  for (std::map<std::string, const GBRForest*>::iterator gbrForest = gbrForests.begin();
76  gbrForest != gbrForests.end(); ++gbrForest) {
77  delete gbrForest->second;
78  }
79  }
80 }
81 
83 
T getParameter(std::string const &) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
GBRForestWriter(const edm::ParameterSet &)
bool isAvailable() const
Definition: Service.h:46
void writeOne(T *payload, Time_t time, const std::string &recordName, bool withlogging=false)
~GBRForestWriter() override
void analyze(const edm::Event &, const edm::EventSetup &) override
std::vector< jobEntryType * > jobs_
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)