CMS 3D CMS Logo

ConditionDBWriter.h

Go to the documentation of this file.
00001 #ifndef CommonTools_ConditionDBWriter_ConditionDBWriter_h
00002 #define CommonTools_ConditionDBWriter_ConditionDBWriter_h
00003 // -*- C++ -*-
00004 //
00005 // Package:    ConditionDBWriter
00006 // Class:      ConditionDBWriter
00007 // 
00008 // \class ConditionDBWriter 
00009 //
00010 //  Description: 
00011 
00012 //  Implementation:
00013 
00014 // This class can be very useful whenever a CMSSW application needs to store data
00015 // to the offline DB. Typically such applications require access to event data 
00016 // and/or need to be notified about the start of Run, Lumi section in order 
00017 // to set a correct Interval Of Validity (IOV) for the data they have to store.
00018 // Therefore the FWK EDAnalyzer is an excellent candidate for the implementation
00019 // of such applications; this is the reason why this class inherits from 
00020 // the EDAnalyzer class. 
00021 
00022 // The user class should inherit from this class. 
00023 // The templated type must be the type of the object that
00024 // has to be written on the DB (e.g. MyCalibration). Examples of use of
00025 // this class can be found in package CalibTracker/SiStripChannelGain. Have a
00026 // look also at the test/ directory for examples of full cfg files. 
00027 
00028 // The user must implement in his derived class the abstract method below
00029 
00030 // virtual MyCalibration * getNewObject()=0;
00031 
00032 // in this method, the user must create a new instance of the DB object and 
00033 // return a pointer to it. The object must be created with "new" and never 
00034 // be deleted by the user: it will be the FWK that takes control over it.
00035 
00036 // The user can optionally implement the following methods 
00037 
00038 //   //Will be called at the beginning of the job
00039 //   virtual void algoBeginJob(const edm::EventSetup&){};
00040 //   //Will be called at the beginning of each run in the job
00041 //   virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
00042 //   //Will be called at the beginning of each luminosity block in the run
00043 //   virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
00044 //   //Will be called at every event
00045 //   virtual void algoAnalyze(const edm::Event&, const edm::EventSetup&){};
00046 //   //Will be called at the end of each run in the job
00047 //   virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
00048 //   //Will be called at the end of the job
00049 //   virtual void algoEndJob(){};
00050 
00051 // where he can access information needed to build his object. For instance, if
00052 // he is computing a calibration that is computed as the mean of a certain
00053 // quantity that varies from event to event, he will implement the algoAnalyze 
00054 // method.
00055 
00056 // The important part is the IOV setting. The advantage of using this class is 
00057 // that this setting is performed almost automatically: the only thing
00058 // that the user has to do is to pass prescriptions about the IOV setting
00059 // in the configuration of his module. A typical
00060 // configuration is as follows:
00061 
00062 
00063 //       module prod =  SiStripGainRandomCalculator {
00064 
00065 //       #parameters of the derived class
00066 //              double MinPositiveGain = 0.1
00067 //              double MeanGain    = 1
00068 //              double SigmaGain   = 0
00069 //                 untracked bool   printDebug = true
00070 
00071 //       #parameters of the base class
00072 //              string IOVMode       = "Run"
00073 //              bool SinceAppendMode = true
00074 //              string Record        = "SiStripApvGainRcd"
00075 
00076 //                }
00077 
00078 // Two subsets of parameters can be found. The first subset contains the specific
00079 // parameters of the user class, which is called in this case 
00080 // SiStripGainRandomCalculator. The second subset contains the parameters of
00081 // the base class. These are the following:
00082 
00083 // 1) string IOVMode
00084 
00085 // 4 possible values can be given: "Job", "Run", "LumiBlock" and "AlgoDriven"
00086 // This card determines the length of the IOV. In other words, together with  
00087 // the number of Lumysections/runs the user has decided to run his application,
00088 // this card determines the number of objects that will be stored on the DB
00089 // (the getNewObject method will be called as many times).
00090 // For example if the user is running on the events of one Run, which has 
00091 // 10 luminosity sections and chooses the "LumiBlock" mode, then 10 objects
00092 // with corresponding IOV will be written. If the "Job" mode is chosen, only one 
00093 // object will be stored irrespective of the dataset on which the user is 
00094 // running.
00095 // The  "AlgoDriven" option is special. If this choice is made, then it is 
00096 // up to the user to tell in the code when the getNewObject method must be 
00097 // called. This can be done by calling the method  below  void storeOnDbNow()
00098 // must be invoked whenever a certain condition that justifies the start/end
00099 // of an IOV is met.
00100 
00101 // 2) bool SinceAppendMode
00102 
00103 // If this card is set to true, new objects are appended to the chosen tag (
00104 // to be specified in the configuration of the  PoolDBOutputService service,
00105 // which must always be included in the cfg file of your application).
00106 //     If instead this card is set to false, new objects are appended in till
00107 //     mode. For example if the user runs on 2 Runs (1 to 2), chooses 
00108 //     the "Run" IOVMode and the SinceAppendMode, then he will have two objects
00109 //     stored on the DB, 
00110 //     Object 1 valid for Run 1
00111 //     Object 2 valid from Run 2 on (unless new objects are appended later)
00112 //       If instead he had run in Till mode, then he would get two objects 
00113 //     stored:
00114 //     Object 1 valid for Run 1
00115 //     Object 2 valid for Run 2 only.
00116     
00117 //     WARNING: due to a current misfunctioning of the framework, if
00118 //     the *Since* mode is chosen, then it is highly recommended to store objects 
00119 //     in IOV chronological order. If you have 10 runs, then execute your application starting from run 1 and not for example in two steps: first from Run 6 to Run 10 and then from Run 1 to Run 6.
00120   
00121 
00122 // 3)string Record 
00123 
00124 // this is the eventsetup record of your object.
00125 
00126 
00127 
00128 //
00129 // Original Author:  Giacomo Bruno
00130 //         Created:  May 23 10:04:31 CET 2007
00131 // $Id: ConditionDBWriter.h,v 1.7 2008/08/06 18:15:18 giordano Exp $
00132 //
00133 //
00134 
00135 
00136 // system include files
00137 #include <memory>
00138 #include <string>
00139 #include <cstdlib>
00140 
00141 
00142 // user include files
00143 #include "FWCore/Framework/interface/Frameworkfwd.h"
00144 #include "FWCore/Framework/interface/EDAnalyzer.h"
00145 #include "FWCore/Framework/interface/Run.h"
00146 #include "CondCore/DBCommon/interface/Time.h"
00147 #include "FWCore/Framework/interface/ESHandle.h"
00148 #include "FWCore/ServiceRegistry/interface/Service.h"
00149 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00150 
00151 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00152 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00153 
00154 #include "FWCore/Utilities/interface/Exception.h"
00155 //#include "FWCore/Framework/interface/EventSetup.h"
00156 
00157 #include "FWCore/Framework/interface/Event.h"
00158 #include "DataFormats/Common/interface/Handle.h"
00159 
00160 
00161 template< class T >
00162 class ConditionDBWriter : public edm::EDAnalyzer {
00163 
00164 public:
00165 
00166   explicit ConditionDBWriter(const edm::ParameterSet& iConfig) : LumiBlockMode_(false), RunMode_(false), JobMode_(false), AlgoDrivenMode_(false), Time_(0), setSinceTime_(false) {
00167 
00168     edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
00169     SinceAppendMode_=iConfig.getParameter<bool>("SinceAppendMode");
00170     std::string IOVMode=iConfig.getParameter<std::string>("IOVMode");
00171     if (IOVMode==std::string("Job")) JobMode_=true;
00172     else if (IOVMode==std::string("Run")) RunMode_=true;
00173     else if (IOVMode==std::string("LumiBlock")) LumiBlockMode_=true;
00174     else if (IOVMode==std::string("AlgoDriven")) AlgoDrivenMode_=true;
00175     else  edm::LogError("ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything on the DB") << std::endl;
00176     Record_=iConfig.getParameter<std::string>("Record");
00177     doStore_=iConfig.getParameter<bool>("doStoreOnDB");
00178 
00179 
00180   }
00181 
00182 
00183   virtual ~ConditionDBWriter(){
00184   
00185     edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl;
00186 
00187   }
00188 
00189 
00190 private:
00191 
00192 
00193   // 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!) 
00194 
00195   virtual T * getNewObject()=0;
00196 
00197 
00198   // Optional methods that may be implemented (technically "overridden") in the derived classes if needed
00199 
00200   //Will be called at the beginning of the job
00201   virtual void algoBeginJob(const edm::EventSetup&){};
00202   //Will be called at the beginning of each run in the job
00203   virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
00204   //Will be called at the beginning of each luminosity block in the run
00205   virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
00206   //Will be called at every event
00207   virtual void algoAnalyze(const edm::Event&, const edm::EventSetup&){};
00208   //Will be called at the end of each run in the job
00209   virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
00210   //Will be called at the end of the job
00211   virtual void algoEndJob(){};
00212 
00213 
00214 
00215   void beginJob(const edm::EventSetup& iSetup){
00216 
00217     edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
00218     if( (JobMode_ || AlgoDrivenMode_) && SinceAppendMode_) setSinceTime_=true;
00219     algoBeginJob(iSetup);
00220 
00221   }
00222 
00223 
00224   void beginRun(const edm::Run & run, const edm::EventSetup &  es){
00225 
00226     edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
00227     if(RunMode_ && SinceAppendMode_) setSinceTime_=true;
00228     algoBeginRun(run,es);
00229 
00230   }
00231 
00232 
00233 
00234   void beginLuminosityBlock(const edm::LuminosityBlock & lumiBlock, const edm::EventSetup & iSetup){
00235 
00236     edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
00237     if(LumiBlockMode_ && SinceAppendMode_) setSinceTime_=true;
00238     algoBeginLuminosityBlock(lumiBlock, iSetup);
00239 
00240   }
00241 
00242 
00243   void analyze(const edm::Event& event, const edm::EventSetup& iSetup){
00244     
00245     if(SinceAppendMode_ && setSinceTime_ ){
00246       setTime(); //set new since time for possible next upload to DB  
00247       setSinceTime_=false;
00248     }
00249 
00250     algoAnalyze(event, iSetup);     
00251 
00252   }
00253 
00254 
00255 
00256   void endLuminosityBlock(const edm::LuminosityBlock & lumiBlock, const edm::EventSetup & es){
00257 
00258     edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
00259     algoEndLuminosityBlock(lumiBlock, es);
00260 
00261     if(LumiBlockMode_){
00262 
00263       T * objPointer = getNewObject();
00264       
00265       if(objPointer ){
00266         storeOnDb(objPointer);
00267       }
00268       else {
00269         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;
00270       }
00271 
00272     }
00273 
00274   }
00275 
00276 
00277   virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
00278 
00279 
00280   void endRun(const edm::Run & run, const edm::EventSetup & es){
00281 
00282     edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
00283 
00284     algoEndRun(run, es);
00285 
00286     if(RunMode_){
00287 
00288       T * objPointer = getNewObject();
00289 
00290       if(objPointer ){
00291         storeOnDb(objPointer);
00292       }
00293       else {
00294         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;
00295       }
00296 
00297     }
00298 
00299   }
00300 
00301 
00302 
00303   void endJob(){
00304 
00305     edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
00306 
00307     algoEndJob();
00308 
00309     if(JobMode_){
00310 
00311       T * objPointer = getNewObject();
00312 
00313       if( objPointer ){
00314         storeOnDb(objPointer);
00315       }
00316 
00317       else {
00318 
00319         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;
00320         
00321       }
00322 
00323     }
00324 
00325   }
00326 
00327 
00328   void storeOnDb(T * objPointer){
00329 
00330     edm::LogInfo("ConditionDBWriter::storeOnDb ")  << std::endl;
00331 
00332     if(! SinceAppendMode_ ) setTime();
00333     else setSinceTime_=true;
00334 
00335     if(! objPointer) {
00336       edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB") ;
00337       return;
00338     }
00339   
00340 
00341     //And now write  data in DB
00342     edm::Service<cond::service::PoolDBOutputService> mydbservice;
00343   
00344     if( doStore_ && mydbservice.isAvailable() ){
00345 
00346       try{
00347         
00348         bool tillDone=false;
00349 
00350         //if first time tag is populated
00351         if( mydbservice->isNewTagRequest(Record_) ){
00352         
00353           edm::LogInfo("ConditionDBWriter") << "first request for storing objects with Record "<< Record_ << std::endl;
00354           
00355           if(SinceAppendMode_) {
00356             //    edm::LogInfo("ConditionDBWriter") << "appending a new DUMMY object to new tag "<<Record_<<" in since mode " << std::endl;
00357             //    mydbservice->createNewIOV<T>(new T(), mydbservice->endOfTime(), Record_);
00358             edm::LogInfo("ConditionDBWriter") << "appending a new object to existing tag " <<Record_ <<" in since mode " << std::endl;
00359             mydbservice->createNewIOV<T>(objPointer, mydbservice->beginOfTime(), mydbservice->endOfTime(), Record_);
00360 
00361             // mydbservice->appendSinceTime<T>(objPointer, Time_, Record_); 
00362           }
00363           else{
00364             edm::LogInfo("ConditionDBWriter") << "appending a new object to new tag "<<Record_<< " in till mode " << std::endl;
00365             mydbservice->createNewIOV<T>(objPointer, mydbservice->beginOfTime(),Time_, Record_);      
00366             tillDone=true;
00367           }
00368           
00369         } 
00370         else {
00371           
00372           if(SinceAppendMode_){
00373             edm::LogInfo("ConditionDBWriter") << "appending a new object to existing tag " <<Record_ <<" in since mode " << std::endl;
00374             mydbservice->appendSinceTime<T>(objPointer, Time_, Record_); 
00375           }
00376           else if(!tillDone){
00377             edm::LogInfo("ConditionDBWriter") << "appending a new object to existing tag "<<Record_ <<" in till mode." << std::endl;
00378             //    mydbservice->appendTillTime<T>(objPointer,Time_,"TRcd");      
00379             mydbservice->appendTillTime<T>(objPointer, Time_, Record_);      
00380           }
00381 
00382         }
00383 
00384       }catch(const cond::Exception& er){
00385         edm::LogError("ConditionDBWriter")<<er.what()<<std::endl;
00386       }catch(const std::exception& er){
00387         edm::LogError("ConditionDBWriter")<<"caught std::exception "<<er.what()<<std::endl;
00388       }catch(...){
00389         edm::LogError("ConditionDBWriter")<<"Funny error"<<std::endl;
00390       }
00391     }
00392     else if (!  mydbservice.isAvailable() ) {
00393       edm::LogError("ConditionDBWriter")<<"PoolDBOutputService is unavailable"<<std::endl;
00394     }
00395     
00396     
00397   }
00398 
00399 
00400 
00401   void setTime(){
00402 
00403     edm::Service<cond::service::PoolDBOutputService> mydbservice;
00404     
00405     if( mydbservice.isAvailable() ){
00406       Time_ = mydbservice->currentTime();
00407       edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
00408     }
00409     else{
00410       edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time") << std::endl;
00411     }
00412     
00413   }
00414 
00415 
00416 protected:
00417 
00418   // 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
00419 
00420   void storeOnDbNow(){
00421 
00422     T * objPointer = 0;
00423 
00424     if(AlgoDrivenMode_){
00425 
00426       setSinceTime_=true;
00427 
00428       objPointer = getNewObject();
00429   
00430       if (!objPointer ) {
00431         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;
00432         return;
00433       }
00434       else {storeOnDb(objPointer);}
00435       
00436     }
00437     else {
00438       
00439       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;
00440       return;
00441     }
00442 
00443   }
00444 
00445 
00446   // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)
00447 
00448   cond::Time_t timeOfLastIOV(){return Time_;}
00449 
00450 
00451 
00452 private:
00453   
00454   bool SinceAppendMode_; // till or since append mode 
00455 
00456   bool LumiBlockMode_; //LumiBlock since/till time
00457   bool RunMode_; //
00458   bool JobMode_;
00459   bool AlgoDrivenMode_;
00460   bool doStore_;
00461 
00462   std::string Record_;
00463   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. 
00464 
00465   bool setSinceTime_;
00466 
00467 
00468 };
00469 
00470 #endif

Generated on Tue Jun 9 17:25:58 2009 for CMSSW by  doxygen 1.5.4