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 //
122 // Original Author: Giacomo Bruno
123 // Created: May 23 10:04:31 CET 2007
124 //
125 //
126 
127 
128 // system include files
129 #include <memory>
130 #include <string>
131 #include <cstdlib>
132 
133 
134 // user include files
141 
144 
146 //#include "FWCore/Framework/interface/EventSetup.h"
147 
150 
151 
152 template< class T >
154 
155 public:
156 
158  {
159  edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
160  SinceAppendMode_=iConfig.getParameter<bool>("SinceAppendMode");
161  std::string IOVMode=iConfig.getParameter<std::string>("IOVMode");
162  if (IOVMode==std::string("Job")) JobMode_=true;
163  else if (IOVMode==std::string("Run")) RunMode_=true;
164  else if (IOVMode==std::string("LumiBlock")) LumiBlockMode_=true;
165  else if (IOVMode==std::string("AlgoDriven")) AlgoDrivenMode_=true;
166  else edm::LogError("ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything on the DB") << std::endl;
167  Record_=iConfig.getParameter<std::string>("Record");
168  doStore_=iConfig.getParameter<bool>("doStoreOnDB");
169  timeFromEndRun_=iConfig.getUntrackedParameter<bool>("TimeFromEndRun", false);
170  timeFromStartOfRunRange_=iConfig.getUntrackedParameter<bool>("TimeFromStartOfRunRange", false);
171 
172  if(! SinceAppendMode_ )
173  edm::LogError("ConditionDBWriter::endJob(): ERROR - only SinceAppendMode support!!!!");
174  }
175 
177  {
178  edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl;
179  }
180 
181 private:
182 
183  // 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!)
184 
185  virtual std::unique_ptr<T> getNewObject()=0;
186 
187 
188  // Optional methods that may be implemented (technically "overridden") in the derived classes if needed
189 
190  //Will be called at the beginning of the job
191  virtual void algoBeginJob(const edm::EventSetup&){};
192  //Will be called at the beginning of each run in the job
193  virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
194  //Will be called at the beginning of each luminosity block in the run
196  //Will be called at every event
197  virtual void algoAnalyze(const edm::Event&, const edm::EventSetup&){};
198  //Will be called at the end of each run in the job
199  virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
200  //Will be called at the end of the job
201  virtual void algoEndJob(){};
202 
203  void beginJob() override {}
204 
205  void beginRun(const edm::Run & run, const edm::EventSetup & es) override
206  {
207  if( firstRun_ ) {
208  edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
210  algoBeginJob(es);
211  firstRun_ = false;
212  }
213 
214  if( run.id().run() < minRunRange_ ) minRunRange_=run.id().run();
215  if( run.id().run() > maxRunRange_ ) maxRunRange_=run.id().run();
216 
217  edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
219  algoBeginRun(run,es);
220  }
221 
222  void beginLuminosityBlock(const edm::LuminosityBlock & lumiBlock, const edm::EventSetup & iSetup) override
223  {
224  edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
226  algoBeginLuminosityBlock(lumiBlock, iSetup);
227  }
228 
229  void analyze(const edm::Event& event, const edm::EventSetup& iSetup) override
230  {
231  if(setSinceTime_ ){
232  setTime(); //set new since time for possible next upload to DB
233  setSinceTime_=false;
234  }
235  algoAnalyze(event, iSetup);
236  }
237 
238  void endLuminosityBlock(const edm::LuminosityBlock & lumiBlock, const edm::EventSetup & es) override
239  {
240  edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
241  algoEndLuminosityBlock(lumiBlock, es);
242 
243  if(LumiBlockMode_){
244 
245  std::unique_ptr<T> objPointer = getNewObject();
246 
247  if(objPointer ){
248  storeOnDb(objPointer);
249  }
250  else {
251  edm::LogError("ConditionDBWriter::endLuminosityblock(): ERROR - requested to store on DB on a Lumi Block based interval, but received null pointer...will not store anything on the DB") << std::endl;
252  }
253  }
254  }
255 
257 
258  void endRun(const edm::Run & run, const edm::EventSetup & es) override
259  {
260  edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
261 
262  algoEndRun(run, es);
263 
264  if(RunMode_){
265 
266  std::unique_ptr<T> objPointer = getNewObject();
267 
268  if(objPointer ){
269  if( timeFromEndRun_ ) Time_ = run.id().run();
270  storeOnDb(objPointer);
271  }
272  else {
273  edm::LogError("ConditionDBWriter::endRun(): ERROR - requested to store on DB on a Run based interval, but received null pointer...will not store anything on the DB") << std::endl;
274  }
275  }
276  }
277 
278  void endJob() override
279  {
280  edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
281 
282  algoEndJob();
283 
284  if(JobMode_){
285 
286  std::unique_ptr<T> objPointer = getNewObject();
287 
288  if( objPointer ){
289  storeOnDb(objPointer);
290  }
291 
292  else {
293 
294  edm::LogError("ConditionDBWriter::endJob(): ERROR - requested to store on DB on a Job based interval, but received null pointer...will not store anything on the DB") << std::endl;
295  }
296  }
297  }
298 
299  void storeOnDb(std::unique_ptr<T>& objPointer)
300  {
301  edm::LogInfo("ConditionDBWriter::storeOnDb ") << std::endl;
302 
303  setSinceTime_=true;
304 
305  if(! objPointer) {
306  edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB") ;
307  return;
308  }
309 
310  //And now write data in DB
311  if( !doStore_ ) return;
313  if (! mydbservice.isAvailable() ) {
314  edm::LogError("ConditionDBWriter")<<"PoolDBOutputService is unavailable"<<std::endl;
315  return;
316  }
317 
319  ( mydbservice->isNewTagRequest(Record_) && !timeFromEndRun_ ) ? mydbservice->beginOfTime() : Time_;
320 
321  //overwrite tim in the case we have the flag TimeFromStartOfRunRange set to on
322  if (timeFromStartOfRunRange_ ) since = minRunRange_;
323 
324  edm::LogInfo("ConditionDBWriter") << "appending a new object to tag "
325  <<Record_ <<" in since mode " << std::endl;
326 
327  // The Framework will take control over the DB object now, therefore the release.
328  mydbservice->writeOne<T>(objPointer.release(), since, Record_);
329  }
330 
331  void setTime()
332  {
334 
335  if( mydbservice.isAvailable() ){
336  Time_ = mydbservice->currentTime();
337  edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
338  }
339  else{
340  edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time") << std::endl;
341  }
342  }
343 
344 protected:
345 
346  // 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
347 
349  {
350  if(AlgoDrivenMode_){
351 
352  setSinceTime_=true;
353 
354  std::unique_ptr<T> objPointer = getNewObject();
355 
356  if (!objPointer ) {
357  edm::LogError("ConditionDBWriter::storeOnDbNow: ERROR - requested to store on DB a new object (module configuration is algo driven based IOV), but received NULL pointer...will not store anything on the DB") << std::endl;
358  return;
359  }
360  else {
361  storeOnDb(objPointer);
362  }
363 
364  }
365  else {
366 
367  edm::LogError("ConditionDBWriter::storeOnDbNow(): ERROR - received a direct request from concrete algorithm to store on DB a new object, but module configuration is not to store on DB on an algo driven based interval...will not store anything on the DB") << std::endl;
368  return;
369  }
370  }
371 
372  // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)
373 
375 
377  void setDoStore(const bool doStore) {doStore_ = doStore;}
378 
379 private:
380 
381  unsigned int minRunRange_;
382  unsigned int maxRunRange_;
383 
384  bool SinceAppendMode_; // till or since append mode
385 
386  bool LumiBlockMode_; //LumiBlock since/till time
387  bool RunMode_; //
388  bool JobMode_;
390  bool doStore_;
391 
393  cond::Time_t 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.
394 
396 
397  bool firstRun_;
398 
401 };
402 
403 #endif
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
RunID const & id() const
Definition: RunBase.h:39
RunNumber_t run() const
Definition: RunID.h:39
void beginJob() override
virtual void algoBeginJob(const edm::EventSetup &)
virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
cond::Time_t timeOfLastIOV()
unsigned int maxRunRange_
void endJob() override
unsigned long long Time_t
Definition: Time.h:16
bool isNewTagRequest(const std::string &recordName)
void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override
bool isAvailable() const
Definition: Service.h:46
void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override
void writeOne(T *payload, Time_t time, const std::string &recordName, bool withlogging=false)
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
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 &)
~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:44
unsigned int minRunRange_