CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/CommonTools/ConditionDBWriter/interface/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 // obsolete option
00104 // now ONLY Since append mode is supported
00105 
00106 
00107 //
00108 //     WARNING: due to the policy of the framework, objects SHALL be stored
00109 //     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.
00110   
00111 
00112 // 3)string Record 
00113 
00114 // this is the eventsetup record of your object.
00115 
00116 
00117 
00118 //
00119 // Original Author:  Giacomo Bruno
00120 //         Created:  May 23 10:04:31 CET 2007
00121 //
00122 //
00123 
00124 
00125 // system include files
00126 #include <memory>
00127 #include <string>
00128 #include <cstdlib>
00129 
00130 
00131 // user include files
00132 #include "FWCore/Framework/interface/Frameworkfwd.h"
00133 #include "FWCore/Framework/interface/EDAnalyzer.h"
00134 #include "FWCore/Framework/interface/Run.h"
00135 #include "CondCore/DBCommon/interface/Time.h"
00136 #include "FWCore/Framework/interface/ESHandle.h"
00137 #include "FWCore/ServiceRegistry/interface/Service.h"
00138 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00139 
00140 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00141 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00142 
00143 #include "FWCore/Utilities/interface/Exception.h"
00144 //#include "FWCore/Framework/interface/EventSetup.h"
00145 
00146 #include "FWCore/Framework/interface/Event.h"
00147 #include "DataFormats/Common/interface/Handle.h"
00148 
00149 
00150 template< class T >
00151 class ConditionDBWriter : public edm::EDAnalyzer {
00152   
00153 public:
00154 
00155   explicit ConditionDBWriter(const edm::ParameterSet& iConfig) : LumiBlockMode_(false), RunMode_(false), JobMode_(false), AlgoDrivenMode_(false), Time_(0), setSinceTime_(false), firstRun_(true)
00156   {
00157     edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
00158     SinceAppendMode_=iConfig.getParameter<bool>("SinceAppendMode");
00159     std::string IOVMode=iConfig.getParameter<std::string>("IOVMode");
00160     if (IOVMode==std::string("Job")) JobMode_=true;
00161     else if (IOVMode==std::string("Run")) RunMode_=true;
00162     else if (IOVMode==std::string("LumiBlock")) LumiBlockMode_=true;
00163     else if (IOVMode==std::string("AlgoDriven")) AlgoDrivenMode_=true;
00164     else  edm::LogError("ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything on the DB") << std::endl;
00165     Record_=iConfig.getParameter<std::string>("Record");
00166     doStore_=iConfig.getParameter<bool>("doStoreOnDB");
00167     
00168     if(! SinceAppendMode_ ) 
00169       edm::LogError("ConditionDBWriter::endJob(): ERROR - only SinceAppendMode support!!!!");
00170   }
00171   
00172   virtual ~ConditionDBWriter()
00173   {
00174     edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl;
00175   }
00176   
00177 private:
00178   
00179   // 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!) 
00180   
00181   virtual T * getNewObject()=0;
00182   
00183   
00184   // Optional methods that may be implemented (technically "overridden") in the derived classes if needed
00185   
00186   //Will be called at the beginning of the job
00187   virtual void algoBeginJob(const edm::EventSetup&){};
00188   //Will be called at the beginning of each run in the job
00189   virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
00190   //Will be called at the beginning of each luminosity block in the run
00191   virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
00192   //Will be called at every event
00193   virtual void algoAnalyze(const edm::Event&, const edm::EventSetup&){};
00194   //Will be called at the end of each run in the job
00195   virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
00196   //Will be called at the end of the job
00197   virtual void algoEndJob(){};
00198 
00199   void beginJob() {}
00200 
00201   void beginRun(const edm::Run & run, const edm::EventSetup &  es)
00202   {
00203     if( firstRun_ ) {
00204       edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
00205       if( (JobMode_ || AlgoDrivenMode_) && SinceAppendMode_) setSinceTime_=true;
00206       algoBeginJob(es);
00207       firstRun_ = false;
00208     }
00209     edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
00210     if(RunMode_ && SinceAppendMode_) setSinceTime_=true;
00211     algoBeginRun(run,es);
00212   }
00213   
00214   void beginLuminosityBlock(const edm::LuminosityBlock & lumiBlock, const edm::EventSetup & iSetup)
00215   {
00216     edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
00217     if(LumiBlockMode_ && SinceAppendMode_) setSinceTime_=true;
00218     algoBeginLuminosityBlock(lumiBlock, iSetup);
00219   }
00220 
00221   void analyze(const edm::Event& event, const edm::EventSetup& iSetup)
00222   {
00223     if(setSinceTime_ ){
00224       setTime(); //set new since time for possible next upload to DB  
00225       setSinceTime_=false;
00226     }
00227     algoAnalyze(event, iSetup);
00228   }
00229   
00230   void endLuminosityBlock(const edm::LuminosityBlock & lumiBlock, const edm::EventSetup & es)
00231   {
00232     edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
00233     algoEndLuminosityBlock(lumiBlock, es);
00234     
00235     if(LumiBlockMode_){
00236       
00237       T * objPointer = getNewObject();
00238       
00239       if(objPointer ){
00240         storeOnDb(objPointer);
00241       }
00242       else {
00243         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;
00244       }
00245     }
00246   }
00247   
00248   virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
00249 
00250   void endRun(const edm::Run & run, const edm::EventSetup & es)
00251   {
00252     edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
00253     
00254     algoEndRun(run, es);
00255     
00256     if(RunMode_){
00257       
00258       T * objPointer = getNewObject();
00259       
00260       if(objPointer ){
00261         storeOnDb(objPointer);
00262       }
00263       else {
00264         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;
00265       }
00266     }
00267   }
00268   
00269   void endJob()
00270   {
00271     edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
00272     
00273     algoEndJob();
00274     
00275     if(JobMode_){
00276       
00277       T * objPointer = getNewObject();
00278       
00279       if( objPointer ){
00280         storeOnDb(objPointer);
00281       }
00282       
00283       else {
00284         
00285         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;
00286       }
00287     }
00288   }
00289 
00290   void storeOnDb(T * objPointer)
00291   {
00292     edm::LogInfo("ConditionDBWriter::storeOnDb ")  << std::endl;
00293     
00294     setSinceTime_=true;
00295     
00296     if(! objPointer) {
00297       edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB") ;
00298       return;
00299     }
00300     
00301     //And now write  data in DB
00302     if( !doStore_ ) return;
00303     edm::Service<cond::service::PoolDBOutputService> mydbservice;
00304     if (!  mydbservice.isAvailable() ) {
00305       edm::LogError("ConditionDBWriter")<<"PoolDBOutputService is unavailable"<<std::endl;
00306       return;
00307     }
00308     
00309     cond::Time_t since = 
00310       ( mydbservice->isNewTagRequest(Record_) ) ? mydbservice->beginOfTime() : Time_;
00311     
00312     edm::LogInfo("ConditionDBWriter") << "appending a new object to tag " 
00313                                       <<Record_ <<" in since mode " << std::endl;
00314     mydbservice->writeOne<T>(objPointer, since, Record_);
00315   }
00316 
00317   void setTime()
00318   {
00319     edm::Service<cond::service::PoolDBOutputService> mydbservice;
00320     
00321     if( mydbservice.isAvailable() ){
00322       Time_ = mydbservice->currentTime();
00323       edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
00324     }
00325     else{
00326       edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time") << std::endl;
00327     }
00328   }
00329 
00330 protected:
00331 
00332   // 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
00333 
00334   void storeOnDbNow()
00335   {
00336     T * objPointer = 0;
00337     
00338     if(AlgoDrivenMode_){
00339       
00340       setSinceTime_=true;
00341       
00342       objPointer = getNewObject();
00343       
00344       if (!objPointer ) {
00345         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;
00346         return;
00347       }
00348       else {storeOnDb(objPointer);}
00349       
00350     }
00351     else {
00352       
00353       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;
00354       return;
00355     }
00356   }
00357 
00358   // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)
00359 
00360   cond::Time_t timeOfLastIOV(){return Time_;}
00361 
00362 private:
00363   
00364   bool SinceAppendMode_; // till or since append mode 
00365 
00366   bool LumiBlockMode_; //LumiBlock since/till time
00367   bool RunMode_; //
00368   bool JobMode_;
00369   bool AlgoDrivenMode_;
00370   bool doStore_;
00371 
00372   std::string Record_;
00373   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. 
00374 
00375   bool setSinceTime_;
00376 
00377   bool firstRun_;
00378 };
00379 
00380 #endif