CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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
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) {
164  edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
165  SinceAppendMode_ = iConfig.getParameter<bool>("SinceAppendMode");
166  std::string IOVMode = iConfig.getParameter<std::string>("IOVMode");
167  if (IOVMode == std::string("Job"))
168  JobMode_ = true;
169  else if (IOVMode == std::string("Run"))
170  RunMode_ = true;
171  else if (IOVMode == std::string("LumiBlock"))
172  LumiBlockMode_ = true;
173  else if (IOVMode == std::string("AlgoDriven"))
174  AlgoDrivenMode_ = true;
175  else
177  "ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything "
178  "on the DB")
179  << std::endl;
180  Record_ = iConfig.getParameter<std::string>("Record");
181  doStore_ = iConfig.getParameter<bool>("doStoreOnDB");
182  timeFromEndRun_ = iConfig.getUntrackedParameter<bool>("TimeFromEndRun", false);
183  timeFromStartOfRunRange_ = iConfig.getUntrackedParameter<bool>("TimeFromStartOfRunRange", false);
184 
185  if (!SinceAppendMode_)
186  edm::LogError("ConditionDBWriter::endJob(): ERROR - only SinceAppendMode support!!!!");
187  }
188 
189  ~ConditionDBWriter() override { edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl; }
190 
191  // utility method to validate configurations of inherited classes
193  desc.add<bool>("SinceAppendMode");
194  desc.add<std::string>("IOVMode");
195  desc.add<std::string>("Record");
196  desc.add<bool>("doStoreOnDB");
197  desc.addUntracked<bool>("TimeFromEndRun", false);
198  desc.addUntracked<bool>("TimeFromStartOfRunRange", false);
199  }
200 
201 private:
202  // 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!)
203 
204  virtual std::unique_ptr<T> getNewObject() = 0;
205 
206  // Optional methods that may be implemented (technically "overridden") in the derived classes if needed
207 
208  //Will be called at the beginning of the job
209  virtual void algoBeginJob(const edm::EventSetup &){};
210  //Will be called at the beginning of each run in the job
211  virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
212  //Will be called at the beginning of each luminosity block in the run
214  //Will be called at every event
215  virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &){};
216  //Will be called at the end of each run in the job
217  virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
218  //Will be called at the end of the job
219  virtual void algoEndJob(){};
220 
221  void beginJob() override {}
222 
223  void beginRun(const edm::Run &run, const edm::EventSetup &es) override {
224  if (firstRun_) {
225  edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
227  setSinceTime_ = true;
228  algoBeginJob(es);
229  firstRun_ = false;
230  }
231 
232  if (run.id().run() < minRunRange_)
233  minRunRange_ = run.id().run();
234  if (run.id().run() > maxRunRange_)
235  maxRunRange_ = run.id().run();
236 
237  edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
238  if (RunMode_ && SinceAppendMode_)
239  setSinceTime_ = true;
240  algoBeginRun(run, es);
241  }
242 
243  void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup) override {
244  edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
246  setSinceTime_ = true;
247  algoBeginLuminosityBlock(lumiBlock, iSetup);
248  }
249 
250  void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override {
251  if (setSinceTime_) {
252  setTime(); //set new since time for possible next upload to DB
253  setSinceTime_ = false;
254  }
255  algoAnalyze(event, iSetup);
256  }
257 
258  void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override {
259  edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
260  algoEndLuminosityBlock(lumiBlock, es);
261 
262  if (LumiBlockMode_) {
263  std::unique_ptr<T> objPointer = getNewObject();
264 
265  if (objPointer) {
266  storeOnDb(objPointer);
267  } else {
269  "ConditionDBWriter::endLuminosityblock(): ERROR - requested to store on DB on a Lumi Block based interval, "
270  "but received null pointer...will not store anything on the DB")
271  << std::endl;
272  }
273  }
274  }
275 
277 
278  void endRun(const edm::Run &run, const edm::EventSetup &es) override {
279  edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
280 
281  algoEndRun(run, es);
282 
283  if (RunMode_) {
284  std::unique_ptr<T> objPointer = getNewObject();
285 
286  if (objPointer) {
287  if (timeFromEndRun_)
288  Time_ = run.id().run();
289  storeOnDb(objPointer);
290  } else {
292  "ConditionDBWriter::endRun(): ERROR - requested to store on DB on a Run based interval, but received null "
293  "pointer...will not store anything on the DB")
294  << std::endl;
295  }
296  }
297  }
298 
299  void endJob() override {
300  edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
301 
302  algoEndJob();
303 
304  if (JobMode_) {
305  std::unique_ptr<T> objPointer = getNewObject();
306 
307  if (objPointer) {
308  storeOnDb(objPointer);
309  }
310 
311  else {
313  "ConditionDBWriter::endJob(): ERROR - requested to store on DB on a Job based interval, but received null "
314  "pointer...will not store anything on the DB")
315  << std::endl;
316  }
317  }
318  }
319 
320  void storeOnDb(std::unique_ptr<T> &objPointer) {
321  edm::LogInfo("ConditionDBWriter::storeOnDb ") << std::endl;
322 
323  setSinceTime_ = true;
324 
325  if (!objPointer) {
326  edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB");
327  return;
328  }
329 
330  //And now write data in DB
331  if (!doStore_)
332  return;
334  if (!mydbservice.isAvailable()) {
335  edm::LogError("ConditionDBWriter") << "PoolDBOutputService is unavailable" << std::endl;
336  return;
337  }
338 
340  (mydbservice->isNewTagRequest(Record_) && !timeFromEndRun_) ? mydbservice->beginOfTime() : Time_;
341 
342  //overwrite tim in the case we have the flag TimeFromStartOfRunRange set to on
345 
346  edm::LogInfo("ConditionDBWriter") << "appending a new object to tag " << Record_ << " in since mode " << std::endl;
347 
348  mydbservice->writeOneIOV<T>(*objPointer, since, Record_);
349  }
350 
351  void setTime() {
353 
354  if (mydbservice.isAvailable()) {
355  Time_ = mydbservice->currentTime();
356  edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
357  } else {
358  edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time")
359  << std::endl;
360  }
361  }
362 
363 protected:
364  // 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
365 
366  void storeOnDbNow() {
367  if (AlgoDrivenMode_) {
368  setSinceTime_ = true;
369 
370  std::unique_ptr<T> objPointer = getNewObject();
371 
372  if (!objPointer) {
374  "ConditionDBWriter::storeOnDbNow: ERROR - requested to store on DB a new object (module configuration is "
375  "algo driven based IOV), but received NULL pointer...will not store anything on the DB")
376  << std::endl;
377  return;
378  } else {
379  storeOnDb(objPointer);
380  }
381 
382  } else {
384  "ConditionDBWriter::storeOnDbNow(): ERROR - received a direct request from concrete algorithm to store on DB "
385  "a new object, but module configuration is not to store on DB on an algo driven based interval...will not "
386  "store anything on the DB")
387  << std::endl;
388  return;
389  }
390  }
391 
392  // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)
393 
395 
397  void setDoStore(const bool doStore) { doStore_ = doStore; }
398 
399 private:
400  unsigned int minRunRange_;
401  unsigned int maxRunRange_;
402 
403  bool SinceAppendMode_; // till or since append mode
404 
405  bool LumiBlockMode_; //LumiBlock since/till time
406  bool RunMode_; //
407  bool JobMode_;
409  bool doStore_;
410 
413  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.
414 
416 
417  bool firstRun_;
418 
421 };
422 
423 #endif
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
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)
static const std::string kSharedResource
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_