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