CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
141 
143 //#include "FWCore/Framework/interface/EventSetup.h"
144 
147 
148 template <class T>
149 class ConditionDBWriter : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
150 public:
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  // utility method to validate configurations of inherited classes
190  desc.add<bool>("SinceAppendMode");
191  desc.add<std::string>("IOVMode");
192  desc.add<std::string>("Record");
193  desc.add<bool>("doStoreOnDB");
194  desc.addUntracked<bool>("TimeFromEndRun", false);
195  desc.addUntracked<bool>("TimeFromStartOfRunRange", false);
196  }
197 
198 private:
199  // 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!)
200 
201  virtual std::unique_ptr<T> getNewObject() = 0;
202 
203  // Optional methods that may be implemented (technically "overridden") in the derived classes if needed
204 
205  //Will be called at the beginning of the job
206  virtual void algoBeginJob(const edm::EventSetup &){};
207  //Will be called at the beginning of each run in the job
208  virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
209  //Will be called at the beginning of each luminosity block in the run
211  //Will be called at every event
212  virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &){};
213  //Will be called at the end of each run in the job
214  virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
215  //Will be called at the end of the job
216  virtual void algoEndJob(){};
217 
218  void beginJob() override {}
219 
220  void beginRun(const edm::Run &run, const edm::EventSetup &es) override {
221  if (firstRun_) {
222  edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
224  setSinceTime_ = true;
225  algoBeginJob(es);
226  firstRun_ = false;
227  }
228 
229  if (run.id().run() < minRunRange_)
230  minRunRange_ = run.id().run();
231  if (run.id().run() > maxRunRange_)
232  maxRunRange_ = run.id().run();
233 
234  edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
235  if (RunMode_ && SinceAppendMode_)
236  setSinceTime_ = true;
237  algoBeginRun(run, es);
238  }
239 
240  void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup) override {
241  edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
243  setSinceTime_ = true;
244  algoBeginLuminosityBlock(lumiBlock, iSetup);
245  }
246 
247  void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override {
248  if (setSinceTime_) {
249  setTime(); //set new since time for possible next upload to DB
250  setSinceTime_ = false;
251  }
252  algoAnalyze(event, iSetup);
253  }
254 
255  void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override {
256  edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
257  algoEndLuminosityBlock(lumiBlock, es);
258 
259  if (LumiBlockMode_) {
260  std::unique_ptr<T> objPointer = getNewObject();
261 
262  if (objPointer) {
263  storeOnDb(objPointer);
264  } else {
266  "ConditionDBWriter::endLuminosityblock(): ERROR - requested to store on DB on a Lumi Block based interval, "
267  "but received null pointer...will not store anything on the DB")
268  << std::endl;
269  }
270  }
271  }
272 
274 
275  void endRun(const edm::Run &run, const edm::EventSetup &es) override {
276  edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
277 
278  algoEndRun(run, es);
279 
280  if (RunMode_) {
281  std::unique_ptr<T> objPointer = getNewObject();
282 
283  if (objPointer) {
284  if (timeFromEndRun_)
285  Time_ = run.id().run();
286  storeOnDb(objPointer);
287  } else {
289  "ConditionDBWriter::endRun(): ERROR - requested to store on DB on a Run based interval, but received null "
290  "pointer...will not store anything on the DB")
291  << std::endl;
292  }
293  }
294  }
295 
296  void endJob() override {
297  edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
298 
299  algoEndJob();
300 
301  if (JobMode_) {
302  std::unique_ptr<T> objPointer = getNewObject();
303 
304  if (objPointer) {
305  storeOnDb(objPointer);
306  }
307 
308  else {
310  "ConditionDBWriter::endJob(): ERROR - requested to store on DB on a Job based interval, but received null "
311  "pointer...will not store anything on the DB")
312  << std::endl;
313  }
314  }
315  }
316 
317  void storeOnDb(std::unique_ptr<T> &objPointer) {
318  edm::LogInfo("ConditionDBWriter::storeOnDb ") << std::endl;
319 
320  setSinceTime_ = true;
321 
322  if (!objPointer) {
323  edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB");
324  return;
325  }
326 
327  //And now write data in DB
328  if (!doStore_)
329  return;
331  if (!mydbservice.isAvailable()) {
332  edm::LogError("ConditionDBWriter") << "PoolDBOutputService is unavailable" << std::endl;
333  return;
334  }
335 
337  (mydbservice->isNewTagRequest(Record_) && !timeFromEndRun_) ? mydbservice->beginOfTime() : Time_;
338 
339  //overwrite tim in the case we have the flag TimeFromStartOfRunRange set to on
341  since = minRunRange_;
342 
343  edm::LogInfo("ConditionDBWriter") << "appending a new object to tag " << Record_ << " in since mode " << std::endl;
344 
345  mydbservice->writeOneIOV<T>(*objPointer, since, Record_);
346  }
347 
348  void setTime() {
350 
351  if (mydbservice.isAvailable()) {
352  Time_ = mydbservice->currentTime();
353  edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
354  } else {
355  edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time")
356  << std::endl;
357  }
358  }
359 
360 protected:
361  // 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
362 
363  void storeOnDbNow() {
364  if (AlgoDrivenMode_) {
365  setSinceTime_ = true;
366 
367  std::unique_ptr<T> objPointer = getNewObject();
368 
369  if (!objPointer) {
371  "ConditionDBWriter::storeOnDbNow: ERROR - requested to store on DB a new object (module configuration is "
372  "algo driven based IOV), but received NULL pointer...will not store anything on the DB")
373  << std::endl;
374  return;
375  } else {
376  storeOnDb(objPointer);
377  }
378 
379  } else {
381  "ConditionDBWriter::storeOnDbNow(): ERROR - received a direct request from concrete algorithm to store on DB "
382  "a new object, but module configuration is not to store on DB on an algo driven based interval...will not "
383  "store anything on the DB")
384  << std::endl;
385  return;
386  }
387  }
388 
389  // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)
390 
392 
394  void setDoStore(const bool doStore) { doStore_ = doStore; }
395 
396 private:
397  unsigned int minRunRange_;
398  unsigned int maxRunRange_;
399 
400  bool SinceAppendMode_; // till or since append mode
401 
402  bool LumiBlockMode_; //LumiBlock since/till time
403  bool RunMode_; //
404  bool JobMode_;
406  bool doStore_;
407 
410  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.
411 
413 
414  bool firstRun_;
415 
418 };
419 
420 #endif
T getUntrackedParameter(std::string const &, T const &) const
RunID const & id() const
Definition: RunBase.h:39
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
RunNumber_t run() const
Definition: RunID.h:36
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_
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
bool isAvailable() const
Definition: Service.h:40
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 &)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
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
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
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 &)
~ConditionDBWriter() override
virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &)
long double T
virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
Definition: Run.h:45
unsigned int minRunRange_