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