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->writeOneIOV(*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 
categoryEntryType(const edm::ParameterSet &cfg)
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:35
std::string moduleLabel_
GBRForestWriter(const edm::ParameterSet &)
std::vector< categoryEntryType * > categories_
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
~GBRForestWriter() override
jobEntryType(const edm::ParameterSet &cfg)
void analyze(const edm::Event &, const edm::EventSetup &) override
std::vector< jobEntryType * > jobs_
bool isAvailable() const
Definition: Service.h:40
const std::string & fullPath() const
Definition: FileInPath.cc:144
std::vector< std::string > vstring
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)