CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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 
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
146 
149 
151 //#include "FWCore/Framework/interface/EventSetup.h"
152 
155 
156 
157 template< class T >
159 
160 public:
161 
163  {
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")) JobMode_=true;
168  else if (IOVMode==std::string("Run")) RunMode_=true;
169  else if (IOVMode==std::string("LumiBlock")) LumiBlockMode_=true;
170  else if (IOVMode==std::string("AlgoDriven")) AlgoDrivenMode_=true;
171  else edm::LogError("ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything on the DB") << std::endl;
172  Record_=iConfig.getParameter<std::string>("Record");
173  doStore_=iConfig.getParameter<bool>("doStoreOnDB");
174  timeFromEndRun_=iConfig.getUntrackedParameter<bool>("TimeFromEndRun", 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() {}
208 
209  void beginRun(const edm::Run & run, const edm::EventSetup & es)
210  {
211  if( firstRun_ ) {
212  edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
214  algoBeginJob(es);
215  firstRun_ = false;
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)
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)
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)
239  {
240  edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
241  algoEndLuminosityBlock(lumiBlock, es);
242 
243  if(LumiBlockMode_){
244 
245  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)
259  {
260  edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
261 
262  algoEndRun(run, es);
263 
264  if(RunMode_){
265 
266  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()
279  {
280  edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
281 
282  algoEndJob();
283 
284  if(JobMode_){
285 
286  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(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 
318  cond::Time_t since =
319  ( mydbservice->isNewTagRequest(Record_) && !timeFromEndRun_ ) ? mydbservice->beginOfTime() : Time_;
320 
321  edm::LogInfo("ConditionDBWriter") << "appending a new object to tag "
322  <<Record_ <<" in since mode " << std::endl;
323  mydbservice->writeOne<T>(objPointer, since, Record_);
324  }
325 
326  void setTime()
327  {
329 
330  if( mydbservice.isAvailable() ){
331  Time_ = mydbservice->currentTime();
332  edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
333  }
334  else{
335  edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time") << std::endl;
336  }
337  }
338 
339 protected:
340 
341  // 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
342 
344  {
345  T * objPointer = 0;
346 
347  if(AlgoDrivenMode_){
348 
349  setSinceTime_=true;
350 
351  objPointer = getNewObject();
352 
353  if (!objPointer ) {
354  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;
355  return;
356  }
357  else {storeOnDb(objPointer);}
358 
359  }
360  else {
361 
362  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;
363  return;
364  }
365  }
366 
367  // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)
368 
370 
372  void setDoStore(const bool doStore) {doStore_ = doStore;}
373 
374 private:
375 
376  bool SinceAppendMode_; // till or since append mode
377 
378  bool LumiBlockMode_; //LumiBlock since/till time
379  bool RunMode_; //
380  bool JobMode_;
382  bool doStore_;
383 
385  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.
386 
388 
389  bool firstRun_;
390 
392 };
393 
394 #endif
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
RunID const & id() const
Definition: RunBase.h:41
RunNumber_t run() const
Definition: RunID.h:39
void storeOnDb(T *objPointer)
virtual T * getNewObject()=0
virtual void algoBeginJob(const edm::EventSetup &)
virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es)
cond::Time_t timeOfLastIOV()
unsigned long long Time_t
Definition: Time.h:16
bool isNewTagRequest(const std::string &recordName)
bool isAvailable() const
Definition: Service.h:46
void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup)
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 &)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
void beginRun(const edm::Run &run, const edm::EventSetup &es)
void analyze(const edm::Event &event, const edm::EventSetup &iSetup)
ConditionDBWriter(const edm::ParameterSet &iConfig)
void setDoStore(const bool doStore)
When set to false the payload will not be written to the db.
virtual void algoEndRun(const edm::Run &, const edm::EventSetup &)
void endRun(const edm::Run &run, const edm::EventSetup &es)
volatile std::atomic< bool > shutdown_flag false
virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &)
long double T
virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
Definition: Run.h:41
virtual ~ConditionDBWriter()