CMS 3D CMS Logo

GBRForestWriter.cc
Go to the documentation of this file.
1 
19 
20 #include <TFile.h>
21 #include <string>
22 #include <vector>
23 
25 public:
27  ~GBRForestWriter() override;
28 
29 private:
30  void analyze(const edm::Event&, const edm::EventSetup&) override;
31 
33 
34  bool hasRun_;
35 
36  typedef std::vector<std::string> vstring;
37 
40  if (cfg.existsAs<edm::FileInPath>("inputFileName")) {
41  edm::FileInPath inputFileName_fip = cfg.getParameter<edm::FileInPath>("inputFileName");
42  inputFileName_ = inputFileName_fip.fullPath();
43  } else if (cfg.existsAs<std::string>("inputFileName")) {
44  inputFileName_ = cfg.getParameter<std::string>("inputFileName");
45  } else
46  throw cms::Exception("GBRForestWriter") << " Undefined Configuration Parameter 'inputFileName !!\n";
47  std::string inputFileType_string = cfg.getParameter<std::string>("inputFileType");
48  if (inputFileType_string == "XML")
50  else if (inputFileType_string == "GBRForest")
52  else
53  throw cms::Exception("GBRForestWriter")
54  << " Invalid Configuration Parameter 'inputFileType' = " << inputFileType_string << " !!\n";
55  if (inputFileType_ == kXML) {
56  inputVariables_ = cfg.getParameter<vstring>("inputVariables");
57  spectatorVariables_ = cfg.getParameter<vstring>("spectatorVariables");
58  methodName_ = cfg.getParameter<std::string>("methodName");
60  (cfg.existsAs<std::string>("gbrForestName") ? cfg.getParameter<std::string>("gbrForestName") : methodName_);
61  } else {
62  gbrForestName_ = cfg.getParameter<std::string>("gbrForestName");
63  }
64  }
67  enum { kXML, kGBRForest };
73  };
74  struct jobEntryType {
76  if (cfg.exists("categories")) {
77  edm::VParameterSet cfgCategories = cfg.getParameter<edm::VParameterSet>("categories");
78  for (edm::VParameterSet::const_iterator cfgCategory = cfgCategories.begin(); cfgCategory != cfgCategories.end();
79  ++cfgCategory) {
80  categoryEntryType* category = new categoryEntryType(*cfgCategory);
81  categories_.push_back(category);
82  }
83  } else {
85  categories_.push_back(category);
86  }
87  std::string outputFileType_string = cfg.getParameter<std::string>("outputFileType");
88  if (outputFileType_string == "GBRForest")
90  else if (outputFileType_string == "SQLLite")
92  else
93  throw cms::Exception("GBRForestWriter")
94  << " Invalid Configuration Parameter 'outputFileType' = " << outputFileType_string << " !!\n";
95  if (outputFileType_ == kGBRForest) {
96  outputFileName_ = cfg.getParameter<std::string>("outputFileName");
97  }
98  if (outputFileType_ == kSQLLite) {
99  outputRecord_ = cfg.getParameter<std::string>("outputRecord");
100  }
101  }
103  for (std::vector<categoryEntryType*>::iterator it = categories_.begin(); it != categories_.end(); ++it) {
104  delete (*it);
105  }
106  }
107  std::vector<categoryEntryType*> categories_;
108  enum { kGBRForest, kSQLLite };
112  };
113  std::vector<jobEntryType*> jobs_;
114 };
115 
117  : moduleLabel_(cfg.getParameter<std::string>("@module_label")) {
118  edm::VParameterSet cfgJobs = cfg.getParameter<edm::VParameterSet>("jobs");
119  for (edm::VParameterSet::const_iterator cfgJob = cfgJobs.begin(); cfgJob != cfgJobs.end(); ++cfgJob) {
120  jobEntryType* job = new jobEntryType(*cfgJob);
121  jobs_.push_back(job);
122  }
123 }
124 
126  for (std::vector<jobEntryType*>::iterator it = jobs_.begin(); it != jobs_.end(); ++it) {
127  delete (*it);
128  }
129 }
130 
132  for (std::vector<jobEntryType*>::iterator job = jobs_.begin(); job != jobs_.end(); ++job) {
133  std::map<std::string, const GBRForest*> gbrForests; // key = name
134  for (std::vector<categoryEntryType*>::iterator category = (*job)->categories_.begin();
135  category != (*job)->categories_.end();
136  ++category) {
137  const GBRForest* gbrForest = nullptr;
138  if ((*category)->inputFileType_ == categoryEntryType::kXML) {
139  gbrForest = createGBRForest((*category)->inputFileName_).release();
140  } else if ((*category)->inputFileType_ == categoryEntryType::kGBRForest) {
141  TFile* inputFile = new TFile((*category)->inputFileName_.data());
142  // gbrForest = dynamic_cast<GBRForest*>(inputFile->Get((*category)->gbrForestName_.data())); // CV:
143  // dynamic_cast<GBRForest*> fails for some reason ?!
144  gbrForest = (GBRForest*)inputFile->Get((*category)->gbrForestName_.data());
145  delete inputFile;
146  }
147  if (!gbrForest)
148  throw cms::Exception("GBRForestWriter") << " Failed to load GBRForest = " << (*category)->gbrForestName_.data()
149  << " from file = " << (*category)->inputFileName_ << " !!\n";
150  gbrForests[(*category)->gbrForestName_] = gbrForest;
151  }
152  if ((*job)->outputFileType_ == jobEntryType::kGBRForest) {
153  TFile* outputFile = new TFile((*job)->outputFileName_.data(), "RECREATE");
154 
155  for (std::map<std::string, const GBRForest*>::iterator gbrForest = gbrForests.begin();
156  gbrForest != gbrForests.end();
157  ++gbrForest) {
158  outputFile->WriteObject(gbrForest->second, gbrForest->first.data());
159  }
160  delete outputFile;
161  } else if ((*job)->outputFileType_ == jobEntryType::kSQLLite) {
163  if (!dbService.isAvailable())
164  throw cms::Exception("GBRForestWriter") << " Failed to access PoolDBOutputService !!\n";
165 
166  for (std::map<std::string, const GBRForest*>::iterator gbrForest = gbrForests.begin();
167  gbrForest != gbrForests.end();
168  ++gbrForest) {
169  std::string outputRecord = (*job)->outputRecord_;
170  if (gbrForests.size() > 1)
171  outputRecord.append("_").append(gbrForest->first);
172  dbService->writeOne(gbrForest->second, dbService->beginOfTime(), outputRecord);
173  }
174  }
175 
176  // gbrforest deletion
177  for (std::map<std::string, const GBRForest*>::iterator gbrForest = gbrForests.begin();
178  gbrForest != gbrForests.end();
179  ++gbrForest) {
180  delete gbrForest->second;
181  }
182  }
183 }
184 
GBRForestWriter::categoryEntryType::categoryEntryType
categoryEntryType(const edm::ParameterSet &cfg)
Definition: GBRForestWriter.cc:39
taus_updatedMVAIds_cff.category
category
Definition: taus_updatedMVAIds_cff.py:30
GBRForestWriter::categoryEntryType::inputFileName_
std::string inputFileName_
Definition: GBRForestWriter.cc:66
GBRForestWriter::jobEntryType
Definition: GBRForestWriter.cc:74
GBRForestWriter::jobEntryType::jobEntryType
jobEntryType(const edm::ParameterSet &cfg)
Definition: GBRForestWriter.cc:75
GBRForestWriter::jobEntryType::outputRecord_
std::string outputRecord_
Definition: GBRForestWriter.cc:111
cond::service::PoolDBOutputService::beginOfTime
cond::Time_t beginOfTime() const
Definition: PoolDBOutputService.cc:177
GBRForestWriter::jobEntryType::kSQLLite
Definition: GBRForestWriter.cc:108
GBRForestTools.h
GBRForest
Definition: GBRForest.h:25
GBRForestWriter::jobEntryType::outputFileName_
std::string outputFileName_
Definition: GBRForestWriter.cc:110
edm::VParameterSet
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
GBRForestWriter::jobEntryType::~jobEntryType
~jobEntryType()
Definition: GBRForestWriter.cc:102
createGBRForest
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
Definition: GBRForestTools.cc:244
GBRForestWriter::moduleLabel_
std::string moduleLabel_
Definition: GBRForestWriter.cc:32
EDAnalyzer.h
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
GBRForestWriter::categoryEntryType::gbrForestName_
std::string gbrForestName_
Definition: GBRForestWriter.cc:71
GBRForestWriter::~GBRForestWriter
~GBRForestWriter() override
Definition: GBRForestWriter.cc:125
edm::EDAnalyzer
Definition: EDAnalyzer.h:29
edm::FileInPath
Definition: FileInPath.h:64
MakerMacros.h
download_sqlite_cfg.outputFile
outputFile
Definition: download_sqlite_cfg.py:5
PoolDBOutputService.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Service.h
GBRForestWriter::jobEntryType::outputFileType_
int outputFileType_
Definition: GBRForestWriter.cc:109
GBRForestWriter::categoryEntryType::kXML
Definition: GBRForestWriter.cc:67
GBRForestWriter::jobEntryType::kGBRForest
Definition: GBRForestWriter.cc:108
GBRForestWriter::jobEntryType::categories_
std::vector< categoryEntryType * > categories_
Definition: GBRForestWriter.cc:107
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
GBRForestWriter::GBRForestWriter
GBRForestWriter(const edm::ParameterSet &)
Definition: GBRForestWriter.cc:116
GBRForestWriter::categoryEntryType::inputFileType_
int inputFileType_
Definition: GBRForestWriter.cc:68
edm::ParameterSet
Definition: ParameterSet.h:36
GBRForestWriter::categoryEntryType::methodName_
std::string methodName_
Definition: GBRForestWriter.cc:72
GBRForestWriter
Definition: GBRForestWriter.cc:24
Event.h
GBRForestWriter::vstring
std::vector< std::string > vstring
Definition: GBRForestWriter.cc:36
GBRForestWriter::categoryEntryType::~categoryEntryType
~categoryEntryType()
Definition: GBRForestWriter.cc:65
edm::Service< cond::service::PoolDBOutputService >
dtResolutionTest_cfi.inputFile
inputFile
Definition: dtResolutionTest_cfi.py:14
GBRForestWriter::categoryEntryType::spectatorVariables_
vstring spectatorVariables_
Definition: GBRForestWriter.cc:70
edm::EventSetup
Definition: EventSetup.h:57
GBRForestWriter::jobs_
std::vector< jobEntryType * > jobs_
Definition: GBRForestWriter.cc:113
GBRForestWriter::hasRun_
bool hasRun_
Definition: GBRForestWriter.cc:34
looper.cfg
cfg
Definition: looper.py:297
GBRForestWriter::categoryEntryType
Definition: GBRForestWriter.cc:38
std
Definition: JetResolutionObject.h:76
GBRForestWriter::categoryEntryType::inputVariables_
vstring inputVariables_
Definition: GBRForestWriter.cc:69
Frameworkfwd.h
GBRForestWriter::categoryEntryType::kGBRForest
Definition: GBRForestWriter.cc:67
Exception
Definition: hltDiff.cc:246
cond::service::PoolDBOutputService::writeOne
Hash writeOne(const T *payload, Time_t time, const std::string &recordName)
Definition: PoolDBOutputService.h:56
Exception.h
cms::Exception
Definition: Exception.h:70
GBRForestWriter::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: GBRForestWriter.cc:131
ParameterSet.h
edm::Event
Definition: Event.h:73
edm::FileInPath::fullPath
std::string fullPath() const
Definition: FileInPath.cc:163