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
142 
144 //#include "FWCore/Framework/interface/EventSetup.h"
145 
148 
149 template <class T>
151  : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks, edm::one::SharedResources> {
152 public:
153  explicit ConditionDBWriter(const edm::ParameterSet &iConfig)
154  : minRunRange_(1 << 31),
155  maxRunRange_(0),
157  RunMode_(false),
158  JobMode_(false),
160  Time_(0),
162  firstRun_(true) {
163  edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
164  SinceAppendMode_ = iConfig.getParameter<bool>("SinceAppendMode");
165  std::string IOVMode = iConfig.getParameter<std::string>("IOVMode");
166  if (IOVMode == std::string("Job"))
167  JobMode_ = true;
168  else if (IOVMode == std::string("Run"))
169  RunMode_ = true;
170  else if (IOVMode == std::string("LumiBlock"))
171  LumiBlockMode_ = true;
172  else if (IOVMode == std::string("AlgoDriven"))
173  AlgoDrivenMode_ = true;
174  else
176  "ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything "
177  "on the DB")
178  << std::endl;
179  Record_ = iConfig.getParameter<std::string>("Record");
180  doStore_ = iConfig.getParameter<bool>("doStoreOnDB");
181  timeFromEndRun_ = iConfig.getUntrackedParameter<bool>("TimeFromEndRun", false);
182  timeFromStartOfRunRange_ = iConfig.getUntrackedParameter<bool>("TimeFromStartOfRunRange", false);
183 
184  if (!SinceAppendMode_)
185  edm::LogError("ConditionDBWriter::endJob(): ERROR - only SinceAppendMode support!!!!");
186  }
187 
188  ~ConditionDBWriter() override { edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl; }
189 
190  // utility method to validate configurations of inherited classes
192  desc.add<bool>("SinceAppendMode");
193  desc.add<std::string>("IOVMode");
194  desc.add<std::string>("Record");
195  desc.add<bool>("doStoreOnDB");
196  desc.addUntracked<bool>("TimeFromEndRun", false);
197  desc.addUntracked<bool>("TimeFromStartOfRunRange", false);
198  }
199 
200 private:
201  // 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!)
202 
203  virtual std::unique_ptr<T> getNewObject() = 0;
204 
205  // Optional methods that may be implemented (technically "overridden") in the derived classes if needed
206 
207  //Will be called at the beginning of the job
208  virtual void algoBeginJob(const edm::EventSetup &){};
209  //Will be called at the beginning of each run in the job
210  virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
211  //Will be called at the beginning of each luminosity block in the run
213  //Will be called at every event
214  virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &){};
215  //Will be called at the end of each run in the job
216  virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
217  //Will be called at the end of the job
218  virtual void algoEndJob(){};
219 
220  void beginJob() override {}
221 
222  void beginRun(const edm::Run &run, const edm::EventSetup &es) override {
223  if (firstRun_) {
224  edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
226  setSinceTime_ = true;
227  algoBeginJob(es);
228  firstRun_ = false;
229  }
230 
231  if (run.id().run() < minRunRange_)
232  minRunRange_ = run.id().run();
233  if (run.id().run() > maxRunRange_)
234  maxRunRange_ = run.id().run();
235 
236  edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
237  if (RunMode_ && SinceAppendMode_)
238  setSinceTime_ = true;
239  algoBeginRun(run, es);
240  }
241 
242  void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup) override {
243  edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
245  setSinceTime_ = true;
246  algoBeginLuminosityBlock(lumiBlock, iSetup);
247  }
248 
249  void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override {
250  if (setSinceTime_) {
251  setTime(); //set new since time for possible next upload to DB
252  setSinceTime_ = false;
253  }
254  algoAnalyze(event, iSetup);
255  }
256 
257  void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override {
258  edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
259  algoEndLuminosityBlock(lumiBlock, es);
260 
261  if (LumiBlockMode_) {
262  std::unique_ptr<T> objPointer = getNewObject();
263 
264  if (objPointer) {
265  storeOnDb(objPointer);
266  } else {
268  "ConditionDBWriter::endLuminosityblock(): ERROR - requested to store on DB on a Lumi Block based interval, "
269  "but received null pointer...will not store anything on the DB")
270  << std::endl;
271  }
272  }
273  }
274 
276 
277  void endRun(const edm::Run &run, const edm::EventSetup &es) override {
278  edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
279 
280  algoEndRun(run, es);
281 
282  if (RunMode_) {
283  std::unique_ptr<T> objPointer = getNewObject();
284 
285  if (objPointer) {
286  if (timeFromEndRun_)
287  Time_ = run.id().run();
288  storeOnDb(objPointer);
289  } else {
291  "ConditionDBWriter::endRun(): ERROR - requested to store on DB on a Run based interval, but received null "
292  "pointer...will not store anything on the DB")
293  << std::endl;
294  }
295  }
296  }
297 
298  void endJob() override {
299  edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
300 
301  algoEndJob();
302 
303  if (JobMode_) {
304  std::unique_ptr<T> objPointer = getNewObject();
305 
306  if (objPointer) {
307  storeOnDb(objPointer);
308  }
309 
310  else {
312  "ConditionDBWriter::endJob(): ERROR - requested to store on DB on a Job based interval, but received null "
313  "pointer...will not store anything on the DB")
314  << std::endl;
315  }
316  }
317  }
318 
319  void storeOnDb(std::unique_ptr<T> &objPointer) {
320  edm::LogInfo("ConditionDBWriter::storeOnDb ") << std::endl;
321 
322  setSinceTime_ = true;
323 
324  if (!objPointer) {
325  edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB");
326  return;
327  }
328 
329  //And now write data in DB
330  if (!doStore_)
331  return;
333  if (!mydbservice.isAvailable()) {
334  edm::LogError("ConditionDBWriter") << "PoolDBOutputService is unavailable" << std::endl;
335  return;
336  }
337 
339  (mydbservice->isNewTagRequest(Record_) && !timeFromEndRun_) ? mydbservice->beginOfTime() : Time_;
340 
341  //overwrite tim in the case we have the flag TimeFromStartOfRunRange set to on
344 
345  edm::LogInfo("ConditionDBWriter") << "appending a new object to tag " << Record_ << " in since mode " << std::endl;
346 
347  mydbservice->writeOneIOV<T>(*objPointer, since, Record_);
348  }
349 
350  void setTime() {
352 
353  if (mydbservice.isAvailable()) {
354  Time_ = mydbservice->currentTime();
355  edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
356  } else {
357  edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time")
358  << std::endl;
359  }
360  }
361 
362 protected:
363  // 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
364 
365  void storeOnDbNow() {
366  if (AlgoDrivenMode_) {
367  setSinceTime_ = true;
368 
369  std::unique_ptr<T> objPointer = getNewObject();
370 
371  if (!objPointer) {
373  "ConditionDBWriter::storeOnDbNow: ERROR - requested to store on DB a new object (module configuration is "
374  "algo driven based IOV), but received NULL pointer...will not store anything on the DB")
375  << std::endl;
376  return;
377  } else {
378  storeOnDb(objPointer);
379  }
380 
381  } else {
383  "ConditionDBWriter::storeOnDbNow(): ERROR - received a direct request from concrete algorithm to store on DB "
384  "a new object, but module configuration is not to store on DB on an algo driven based interval...will not "
385  "store anything on the DB")
386  << std::endl;
387  return;
388  }
389  }
390 
391  // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)
392 
394 
396  void setDoStore(const bool doStore) { doStore_ = doStore; }
397 
398 private:
399  unsigned int minRunRange_;
400  unsigned int maxRunRange_;
401 
402  bool SinceAppendMode_; // till or since append mode
403 
404  bool LumiBlockMode_; //LumiBlock since/till time
405  bool RunMode_; //
406  bool JobMode_;
408  bool doStore_;
409 
412  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.
413 
415 
416  bool firstRun_;
417 
420 };
421 
422 #endif
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void beginJob() override
virtual void algoBeginJob(const edm::EventSetup &)
Log< level::Error, false > LogError
virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
cond::Time_t timeOfLastIOV()
unsigned int maxRunRange_
T getUntrackedParameter(std::string const &, T const &) const
static void fillPSetDescription(edm::ParameterSetDescription &desc)
void endJob() override
unsigned long long Time_t
Definition: Time.h:14
bool isNewTagRequest(const std::string &recordName)
void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override
void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
virtual void algoEndJob()
void storeOnDb(std::unique_ptr< T > &objPointer)
virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &)
virtual std::unique_ptr< T > getNewObject()=0
Log< level::Info, false > LogInfo
void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup) override
void beginRun(const edm::Run &run, const edm::EventSetup &es) override
ConditionDBWriter(const edm::ParameterSet &iConfig)
void setDoStore(const bool doStore)
When set to false the payload will not be written to the db.
void endRun(const edm::Run &run, const edm::EventSetup &es) override
virtual void algoEndRun(const edm::Run &, const edm::EventSetup &)
bool isAvailable() const
Definition: Service.h:40
~ConditionDBWriter() override
virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &)
long double T
virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
Definition: event.py:1
Definition: Run.h:45
unsigned int minRunRange_