File indexing completed on 2022-03-05 02:38:53
0001 #ifndef CommonTools_ConditionDBWriter_ConditionDBWriter_h
0002 #define CommonTools_ConditionDBWriter_ConditionDBWriter_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127 #include <memory>
0128 #include <string>
0129 #include <cstdlib>
0130
0131
0132 #include "FWCore/Framework/interface/Frameworkfwd.h"
0133 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0134 #include "FWCore/Framework/interface/Run.h"
0135 #include "FWCore/Framework/interface/ESHandle.h"
0136 #include "FWCore/ServiceRegistry/interface/Service.h"
0137 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0138 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0139 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0140 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0141
0142 #include "FWCore/Utilities/interface/Exception.h"
0143
0144
0145 #include "FWCore/Framework/interface/Event.h"
0146 #include "DataFormats/Common/interface/Handle.h"
0147
0148 template <class T>
0149 class ConditionDBWriter : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
0150 public:
0151 explicit ConditionDBWriter(const edm::ParameterSet &iConfig)
0152 : minRunRange_(1 << 31),
0153 maxRunRange_(0),
0154 LumiBlockMode_(false),
0155 RunMode_(false),
0156 JobMode_(false),
0157 AlgoDrivenMode_(false),
0158 Time_(0),
0159 setSinceTime_(false),
0160 firstRun_(true) {
0161 edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
0162 SinceAppendMode_ = iConfig.getParameter<bool>("SinceAppendMode");
0163 std::string IOVMode = iConfig.getParameter<std::string>("IOVMode");
0164 if (IOVMode == std::string("Job"))
0165 JobMode_ = true;
0166 else if (IOVMode == std::string("Run"))
0167 RunMode_ = true;
0168 else if (IOVMode == std::string("LumiBlock"))
0169 LumiBlockMode_ = true;
0170 else if (IOVMode == std::string("AlgoDriven"))
0171 AlgoDrivenMode_ = true;
0172 else
0173 edm::LogError(
0174 "ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything "
0175 "on the DB")
0176 << std::endl;
0177 Record_ = iConfig.getParameter<std::string>("Record");
0178 doStore_ = iConfig.getParameter<bool>("doStoreOnDB");
0179 timeFromEndRun_ = iConfig.getUntrackedParameter<bool>("TimeFromEndRun", false);
0180 timeFromStartOfRunRange_ = iConfig.getUntrackedParameter<bool>("TimeFromStartOfRunRange", false);
0181
0182 if (!SinceAppendMode_)
0183 edm::LogError("ConditionDBWriter::endJob(): ERROR - only SinceAppendMode support!!!!");
0184 }
0185
0186 ~ConditionDBWriter() override { edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl; }
0187
0188
0189 static void fillPSetDescription(edm::ParameterSetDescription &desc) {
0190 desc.add<bool>("SinceAppendMode");
0191 desc.add<std::string>("IOVMode");
0192 desc.add<std::string>("Record");
0193 desc.add<bool>("doStoreOnDB");
0194 desc.addUntracked<bool>("TimeFromEndRun", false);
0195 desc.addUntracked<bool>("TimeFromStartOfRunRange", false);
0196 }
0197
0198 private:
0199
0200
0201 virtual std::unique_ptr<T> getNewObject() = 0;
0202
0203
0204
0205
0206 virtual void algoBeginJob(const edm::EventSetup &){};
0207
0208 virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
0209
0210 virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
0211
0212 virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &){};
0213
0214 virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
0215
0216 virtual void algoEndJob(){};
0217
0218 void beginJob() override {}
0219
0220 void beginRun(const edm::Run &run, const edm::EventSetup &es) override {
0221 if (firstRun_) {
0222 edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
0223 if ((JobMode_ || AlgoDrivenMode_) && SinceAppendMode_)
0224 setSinceTime_ = true;
0225 algoBeginJob(es);
0226 firstRun_ = false;
0227 }
0228
0229 if (run.id().run() < minRunRange_)
0230 minRunRange_ = run.id().run();
0231 if (run.id().run() > maxRunRange_)
0232 maxRunRange_ = run.id().run();
0233
0234 edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
0235 if (RunMode_ && SinceAppendMode_)
0236 setSinceTime_ = true;
0237 algoBeginRun(run, es);
0238 }
0239
0240 void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup) override {
0241 edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
0242 if (LumiBlockMode_ && SinceAppendMode_)
0243 setSinceTime_ = true;
0244 algoBeginLuminosityBlock(lumiBlock, iSetup);
0245 }
0246
0247 void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override {
0248 if (setSinceTime_) {
0249 setTime();
0250 setSinceTime_ = false;
0251 }
0252 algoAnalyze(event, iSetup);
0253 }
0254
0255 void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override {
0256 edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
0257 algoEndLuminosityBlock(lumiBlock, es);
0258
0259 if (LumiBlockMode_) {
0260 std::unique_ptr<T> objPointer = getNewObject();
0261
0262 if (objPointer) {
0263 storeOnDb(objPointer);
0264 } else {
0265 edm::LogError(
0266 "ConditionDBWriter::endLuminosityblock(): ERROR - requested to store on DB on a Lumi Block based interval, "
0267 "but received null pointer...will not store anything on the DB")
0268 << std::endl;
0269 }
0270 }
0271 }
0272
0273 virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
0274
0275 void endRun(const edm::Run &run, const edm::EventSetup &es) override {
0276 edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
0277
0278 algoEndRun(run, es);
0279
0280 if (RunMode_) {
0281 std::unique_ptr<T> objPointer = getNewObject();
0282
0283 if (objPointer) {
0284 if (timeFromEndRun_)
0285 Time_ = run.id().run();
0286 storeOnDb(objPointer);
0287 } else {
0288 edm::LogError(
0289 "ConditionDBWriter::endRun(): ERROR - requested to store on DB on a Run based interval, but received null "
0290 "pointer...will not store anything on the DB")
0291 << std::endl;
0292 }
0293 }
0294 }
0295
0296 void endJob() override {
0297 edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
0298
0299 algoEndJob();
0300
0301 if (JobMode_) {
0302 std::unique_ptr<T> objPointer = getNewObject();
0303
0304 if (objPointer) {
0305 storeOnDb(objPointer);
0306 }
0307
0308 else {
0309 edm::LogError(
0310 "ConditionDBWriter::endJob(): ERROR - requested to store on DB on a Job based interval, but received null "
0311 "pointer...will not store anything on the DB")
0312 << std::endl;
0313 }
0314 }
0315 }
0316
0317 void storeOnDb(std::unique_ptr<T> &objPointer) {
0318 edm::LogInfo("ConditionDBWriter::storeOnDb ") << std::endl;
0319
0320 setSinceTime_ = true;
0321
0322 if (!objPointer) {
0323 edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB");
0324 return;
0325 }
0326
0327
0328 if (!doStore_)
0329 return;
0330 edm::Service<cond::service::PoolDBOutputService> mydbservice;
0331 if (!mydbservice.isAvailable()) {
0332 edm::LogError("ConditionDBWriter") << "PoolDBOutputService is unavailable" << std::endl;
0333 return;
0334 }
0335
0336 cond::Time_t since =
0337 (mydbservice->isNewTagRequest(Record_) && !timeFromEndRun_) ? mydbservice->beginOfTime() : Time_;
0338
0339
0340 if (timeFromStartOfRunRange_)
0341 since = minRunRange_;
0342
0343 edm::LogInfo("ConditionDBWriter") << "appending a new object to tag " << Record_ << " in since mode " << std::endl;
0344
0345 mydbservice->writeOneIOV<T>(*objPointer, since, Record_);
0346 }
0347
0348 void setTime() {
0349 edm::Service<cond::service::PoolDBOutputService> mydbservice;
0350
0351 if (mydbservice.isAvailable()) {
0352 Time_ = mydbservice->currentTime();
0353 edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
0354 } else {
0355 edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time")
0356 << std::endl;
0357 }
0358 }
0359
0360 protected:
0361
0362
0363 void storeOnDbNow() {
0364 if (AlgoDrivenMode_) {
0365 setSinceTime_ = true;
0366
0367 std::unique_ptr<T> objPointer = getNewObject();
0368
0369 if (!objPointer) {
0370 edm::LogError(
0371 "ConditionDBWriter::storeOnDbNow: ERROR - requested to store on DB a new object (module configuration is "
0372 "algo driven based IOV), but received NULL pointer...will not store anything on the DB")
0373 << std::endl;
0374 return;
0375 } else {
0376 storeOnDb(objPointer);
0377 }
0378
0379 } else {
0380 edm::LogError(
0381 "ConditionDBWriter::storeOnDbNow(): ERROR - received a direct request from concrete algorithm to store on DB "
0382 "a new object, but module configuration is not to store on DB on an algo driven based interval...will not "
0383 "store anything on the DB")
0384 << std::endl;
0385 return;
0386 }
0387 }
0388
0389
0390
0391 cond::Time_t timeOfLastIOV() { return Time_; }
0392
0393
0394 void setDoStore(const bool doStore) { doStore_ = doStore; }
0395
0396 private:
0397 unsigned int minRunRange_;
0398 unsigned int maxRunRange_;
0399
0400 bool SinceAppendMode_;
0401
0402 bool LumiBlockMode_;
0403 bool RunMode_;
0404 bool JobMode_;
0405 bool AlgoDrivenMode_;
0406 bool doStore_;
0407
0408 std::string Record_;
0409 cond::Time_t
0410 Time_;
0411
0412 bool setSinceTime_;
0413
0414 bool firstRun_;
0415
0416 bool timeFromEndRun_;
0417 bool timeFromStartOfRunRange_;
0418 };
0419
0420 #endif