CMS 3D CMS Logo

ConditionDBWriter.h
Go to the documentation of this file.
1 #ifndef CommonTools_ConditionDBWriter_ConditionDBWriter_h
2 #define CommonTools_ConditionDBWriter_ConditionDBWriter_h
3 // -*- C++ -*-
4 //
5 // Package: ConditionDBWriter
6 // Class: ConditionDBWriter
7 //
8 // \class ConditionDBWriter
9 //
10 // Description:
11 
118 //
119 
120 //
121 // Original Author: Giacomo Bruno
122 // Created: May 23 10:04:31 CET 2007
123 //
124 //
125 
126 // system include files
127 #include <memory>
128 #include <string>
129 #include <cstdlib>
130 
131 // user include files
138 
141 
143 //#include "FWCore/Framework/interface/EventSetup.h"
144 
147 
148 template <class T>
150 public:
151  explicit ConditionDBWriter(const edm::ParameterSet &iConfig)
152  : minRunRange_(1 << 31),
153  maxRunRange_(0),
155  RunMode_(false),
156  JobMode_(false),
158  Time_(0),
160  firstRun_(true) {
161  edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
162  SinceAppendMode_ = iConfig.getParameter<bool>("SinceAppendMode");
163  std::string IOVMode = iConfig.getParameter<std::string>("IOVMode");
164  if (IOVMode == std::string("Job"))
165  JobMode_ = true;
166  else if (IOVMode == std::string("Run"))
167  RunMode_ = true;
168  else if (IOVMode == std::string("LumiBlock"))
169  LumiBlockMode_ = true;
170  else if (IOVMode == std::string("AlgoDriven"))
171  AlgoDrivenMode_ = true;
172  else
174  "ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything "
175  "on the DB")
176  << std::endl;
177  Record_ = iConfig.getParameter<std::string>("Record");
178  doStore_ = iConfig.getParameter<bool>("doStoreOnDB");
179  timeFromEndRun_ = iConfig.getUntrackedParameter<bool>("TimeFromEndRun", false);
180  timeFromStartOfRunRange_ = iConfig.getUntrackedParameter<bool>("TimeFromStartOfRunRange", false);
181 
182  if (!SinceAppendMode_)
183  edm::LogError("ConditionDBWriter::endJob(): ERROR - only SinceAppendMode support!!!!");
184  }
185 
186  ~ConditionDBWriter() override { edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl; }
187 
188 private:
189  // method to be implemented by derived class. Must return a pointer to the DB object to be stored, which must have been created with "new". The derived class looses control on it (must not "delete" it at any time in its code!)
190 
191  virtual std::unique_ptr<T> getNewObject() = 0;
192 
193  // Optional methods that may be implemented (technically "overridden") in the derived classes if needed
194 
195  //Will be called at the beginning of the job
196  virtual void algoBeginJob(const edm::EventSetup &){};
197  //Will be called at the beginning of each run in the job
198  virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
199  //Will be called at the beginning of each luminosity block in the run
201  //Will be called at every event
202  virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &){};
203  //Will be called at the end of each run in the job
204  virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
205  //Will be called at the end of the job
206  virtual void algoEndJob(){};
207 
208  void beginJob() override {}
209 
210  void beginRun(const edm::Run &run, const edm::EventSetup &es) override {
211  if (firstRun_) {
212  edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
214  setSinceTime_ = true;
215  algoBeginJob(es);
216  firstRun_ = false;
217  }
218 
219  if (run.id().run() < minRunRange_)
220  minRunRange_ = run.id().run();
221  if (run.id().run() > maxRunRange_)
222  maxRunRange_ = run.id().run();
223 
224  edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
225  if (RunMode_ && SinceAppendMode_)
226  setSinceTime_ = true;
227  algoBeginRun(run, es);
228  }
229 
230  void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup) override {
231  edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
233  setSinceTime_ = true;
234  algoBeginLuminosityBlock(lumiBlock, iSetup);
235  }
236 
237  void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override {
238  if (setSinceTime_) {
239  setTime(); //set new since time for possible next upload to DB
240  setSinceTime_ = false;
241  }
242  algoAnalyze(event, iSetup);
243  }
244 
245  void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override {
246  edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
247  algoEndLuminosityBlock(lumiBlock, es);
248 
249  if (LumiBlockMode_) {
250  std::unique_ptr<T> objPointer = getNewObject();
251 
252  if (objPointer) {
253  storeOnDb(objPointer);
254  } else {
256  "ConditionDBWriter::endLuminosityblock(): ERROR - requested to store on DB on a Lumi Block based interval, "
257  "but received null pointer...will not store anything on the DB")
258  << std::endl;
259  }
260  }
261  }
262 
264 
265  void endRun(const edm::Run &run, const edm::EventSetup &es) override {
266  edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
267 
268  algoEndRun(run, es);
269 
270  if (RunMode_) {
271  std::unique_ptr<T> objPointer = getNewObject();
272 
273  if (objPointer) {
274  if (timeFromEndRun_)
275  Time_ = run.id().run();
276  storeOnDb(objPointer);
277  } else {
279  "ConditionDBWriter::endRun(): ERROR - requested to store on DB on a Run based interval, but received null "
280  "pointer...will not store anything on the DB")
281  << std::endl;
282  }
283  }
284  }
285 
286  void endJob() override {
287  edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
288 
289  algoEndJob();
290 
291  if (JobMode_) {
292  std::unique_ptr<T> objPointer = getNewObject();
293 
294  if (objPointer) {
295  storeOnDb(objPointer);
296  }
297 
298  else {
300  "ConditionDBWriter::endJob(): ERROR - requested to store on DB on a Job based interval, but received null "
301  "pointer...will not store anything on the DB")
302  << std::endl;
303  }
304  }
305  }
306 
307  void storeOnDb(std::unique_ptr<T> &objPointer) {
308  edm::LogInfo("ConditionDBWriter::storeOnDb ") << std::endl;
309 
310  setSinceTime_ = true;
311 
312  if (!objPointer) {
313  edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB");
314  return;
315  }
316 
317  //And now write data in DB
318  if (!doStore_)
319  return;
321  if (!mydbservice.isAvailable()) {
322  edm::LogError("ConditionDBWriter") << "PoolDBOutputService is unavailable" << std::endl;
323  return;
324  }
325 
327  (mydbservice->isNewTagRequest(Record_) && !timeFromEndRun_) ? mydbservice->beginOfTime() : Time_;
328 
329  //overwrite tim in the case we have the flag TimeFromStartOfRunRange set to on
332 
333  edm::LogInfo("ConditionDBWriter") << "appending a new object to tag " << Record_ << " in since mode " << std::endl;
334 
335  // The Framework will take control over the DB object now, therefore the release.
336  mydbservice->writeOne<T>(objPointer.release(), since, Record_);
337  }
338 
339  void setTime() {
341 
342  if (mydbservice.isAvailable()) {
343  Time_ = mydbservice->currentTime();
344  edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
345  } else {
346  edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time")
347  << std::endl;
348  }
349  }
350 
351 protected:
352  // This method should be called by the derived class only if it support the algodriven mode; this method will trigger a call of the getNewObject method, but only if algoDrivenMode is chosen
353 
354  void storeOnDbNow() {
355  if (AlgoDrivenMode_) {
356  setSinceTime_ = true;
357 
358  std::unique_ptr<T> objPointer = getNewObject();
359 
360  if (!objPointer) {
362  "ConditionDBWriter::storeOnDbNow: ERROR - requested to store on DB a new object (module configuration is "
363  "algo driven based IOV), but received NULL pointer...will not store anything on the DB")
364  << std::endl;
365  return;
366  } else {
367  storeOnDb(objPointer);
368  }
369 
370  } else {
372  "ConditionDBWriter::storeOnDbNow(): ERROR - received a direct request from concrete algorithm to store on DB "
373  "a new object, but module configuration is not to store on DB on an algo driven based interval...will not "
374  "store anything on the DB")
375  << std::endl;
376  return;
377  }
378  }
379 
380  // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)
381 
383 
385  void setDoStore(const bool doStore) { doStore_ = doStore; }
386 
387 private:
388  unsigned int minRunRange_;
389  unsigned int maxRunRange_;
390 
391  bool SinceAppendMode_; // till or since append mode
392 
393  bool LumiBlockMode_; //LumiBlock since/till time
394  bool RunMode_; //
395  bool JobMode_;
397  bool doStore_;
398 
401  Time_; //time until which the DB object is valid. It is taken from the time of the first event analyzed. The end of the validity is infinity. However as soon as a new DB object with a later start time is inserted, the end time of this one becomes the start time of the new one.
402 
404 
405  bool firstRun_;
406 
409 };
410 
411 #endif
ConditionDBWriter::firstRun_
bool firstRun_
Definition: ConditionDBWriter.h:405
ConditionDBWriter::setTime
void setTime()
Definition: ConditionDBWriter.h:339
Handle.h
ConditionDBWriter::SinceAppendMode_
bool SinceAppendMode_
Definition: ConditionDBWriter.h:391
ConditionDBWriter::timeOfLastIOV
cond::Time_t timeOfLastIOV()
Definition: ConditionDBWriter.h:382
ConditionDBWriter::setDoStore
void setDoStore(const bool doStore)
When set to false the payload will not be written to the db.
Definition: ConditionDBWriter.h:385
MessageLogger.h
ConditionDBWriter::algoEndRun
virtual void algoEndRun(const edm::Run &, const edm::EventSetup &)
Definition: ConditionDBWriter.h:204
funct::false
false
Definition: Factorize.h:29
ConditionDBWriter::algoAnalyze
virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &)
Definition: ConditionDBWriter.h:202
cond::service::PoolDBOutputService::beginOfTime
cond::Time_t beginOfTime() const
Definition: PoolDBOutputService.cc:187
ESHandle.h
ConditionDBWriter::algoBeginJob
virtual void algoBeginJob(const edm::EventSetup &)
Definition: ConditionDBWriter.h:196
ConditionDBWriter::RunMode_
bool RunMode_
Definition: ConditionDBWriter.h:394
edm::LuminosityBlock
Definition: LuminosityBlock.h:50
edm::Run
Definition: Run.h:45
ConditionDBWriter::maxRunRange_
unsigned int maxRunRange_
Definition: ConditionDBWriter.h:389
ConditionDBWriter::~ConditionDBWriter
~ConditionDBWriter() override
Definition: ConditionDBWriter.h:186
ConditionDBWriter::analyze
void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override
Definition: ConditionDBWriter.h:237
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
EDAnalyzer.h
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
ConditionDBWriter::setSinceTime_
bool setSinceTime_
Definition: ConditionDBWriter.h:403
edm::EDAnalyzer
Definition: EDAnalyzer.h:28
ConditionDBWriter::endLuminosityBlock
void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override
Definition: ConditionDBWriter.h:245
PoolDBOutputService.h
ConditionDBWriter
Definition: ConditionDBWriter.h:149
Service.h
ConditionDBWriter::Time_
cond::Time_t Time_
Definition: ConditionDBWriter.h:401
ConditionDBWriter::getNewObject
virtual std::unique_ptr< T > getNewObject()=0
ConditionDBWriter::beginJob
void beginJob() override
Definition: ConditionDBWriter.h:208
Run.h
writeEcalDQMStatus.since
since
Definition: writeEcalDQMStatus.py:53
ConditionDBWriter::ConditionDBWriter
ConditionDBWriter(const edm::ParameterSet &iConfig)
Definition: ConditionDBWriter.h:151
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
ConditionDBWriter::beginRun
void beginRun(const edm::Run &run, const edm::EventSetup &es) override
Definition: ConditionDBWriter.h:210
funct::true
true
Definition: Factorize.h:173
edm::ParameterSet
Definition: ParameterSet.h:47
ConditionDBWriter::JobMode_
bool JobMode_
Definition: ConditionDBWriter.h:395
ConditionDBWriter::timeFromEndRun_
bool timeFromEndRun_
Definition: ConditionDBWriter.h:407
Event.h
cond::Time_t
unsigned long long Time_t
Definition: Time.h:14
edm::Service< cond::service::PoolDBOutputService >
ConditionDBWriter::doStore_
bool doStore_
Definition: ConditionDBWriter.h:397
ConditionDBWriter::timeFromStartOfRunRange_
bool timeFromStartOfRunRange_
Definition: ConditionDBWriter.h:408
ConditionDBWriter::endRun
void endRun(const edm::Run &run, const edm::EventSetup &es) override
Definition: ConditionDBWriter.h:265
edm::EventSetup
Definition: EventSetup.h:57
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
ConditionDBWriter::AlgoDrivenMode_
bool AlgoDrivenMode_
Definition: ConditionDBWriter.h:396
ConditionDBWriter::endJob
void endJob() override
Definition: ConditionDBWriter.h:286
ConditionDBWriter::storeOnDbNow
void storeOnDbNow()
Definition: ConditionDBWriter.h:354
writedatasetfile.run
run
Definition: writedatasetfile.py:27
Frameworkfwd.h
T
long double T
Definition: Basic3DVectorLD.h:48
ConditionDBWriter::LumiBlockMode_
bool LumiBlockMode_
Definition: ConditionDBWriter.h:393
ConditionDBWriter::storeOnDb
void storeOnDb(std::unique_ptr< T > &objPointer)
Definition: ConditionDBWriter.h:307
cond::service::PoolDBOutputService::writeOne
Hash writeOne(const T *payload, Time_t time, const std::string &recordName)
Definition: PoolDBOutputService.h:57
ConditionDBWriter::beginLuminosityBlock
void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup) override
Definition: ConditionDBWriter.h:230
ConditionDBWriter::Record_
std::string Record_
Definition: ConditionDBWriter.h:399
ConditionDBWriter::algoEndJob
virtual void algoEndJob()
Definition: ConditionDBWriter.h:206
cond::service::PoolDBOutputService::isNewTagRequest
bool isNewTagRequest(const std::string &recordName)
Definition: PoolDBOutputService.cc:100
ConditionDBWriter::algoEndLuminosityBlock
virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
Definition: ConditionDBWriter.h:263
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
Exception.h
ConditionDBWriter::minRunRange_
unsigned int minRunRange_
Definition: ConditionDBWriter.h:388
ConditionDBWriter::algoBeginRun
virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &)
Definition: ConditionDBWriter.h:198
ParameterSet.h
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
cond::service::PoolDBOutputService::currentTime
cond::Time_t currentTime() const
Definition: PoolDBOutputService.cc:189
ConditionDBWriter::algoBeginLuminosityBlock
virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
Definition: ConditionDBWriter.h:200