Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-10 22:32:56

0001 #ifndef CommonTools_ConditionDBWriter_ConditionDBWriter_h
0002 #define CommonTools_ConditionDBWriter_ConditionDBWriter_h
0003 // -*- C++ -*-
0004 //
0005 // Package:    ConditionDBWriter
0006 // Class:      ConditionDBWriter
0007 //
0008 // \class ConditionDBWriter
0009 //
0010 //  Description:
0011 
0012 /**
0013  *  Implementation:
0014  *
0015  *  This class can be very useful whenever a CMSSW application needs to store data
0016  *  to the offline DB. Typically such applications require access to event data 
0017  *  and/or need to be notified about the start of Run, Lumi section in order 
0018  *  to set a correct Interval Of Validity (IOV) for the data they have to store.
0019  *  Therefore the FWK EDAnalyzer is an excellent candidate for the implementation
0020  *  of such applications; this is the reason why this class inherits from 
0021  *  the EDAnalyzer class. 
0022  *
0023  *  The user class should inherit from this class. 
0024  *  The templated type must be the type of the object that
0025  *  has to be written on the DB (e.g. MyCalibration). Examples of use of
0026  *  this class can be found in package CalibTracker/SiStripChannelGain. Have a
0027  *  look also at the test/ directory for examples of full cfg files. 
0028  *
0029  *  The user must implement in his derived class the abstract method below
0030  *
0031  *  virtual std::unique_ptr<MyCalibration> getNewObject()=0;
0032  *
0033  *  The user can optionally implement the following methods 
0034  *
0035  *    //Will be called at the beginning of the job
0036  *    virtual void algoBeginJob(const edm::EventSetup&){};
0037  *    //Will be called at the beginning of each run in the job
0038  *    virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
0039  *    //Will be called at the beginning of each luminosity block in the run
0040  *    virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
0041  *    //Will be called at every event
0042  *    virtual void algoAnalyze(const edm::Event&, const edm::EventSetup&){};
0043  *    //Will be called at the end of each run in the job
0044  *    virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
0045  *    //Will be called at the end of the job
0046  *    virtual void algoEndJob(){};
0047  *
0048  *  where he can access information needed to build his object. For instance, if
0049  *  he is computing a calibration that is computed as the mean of a certain
0050  *  quantity that varies from event to event, he will implement the algoAnalyze 
0051  *  method.
0052  *
0053  *  The important part is the IOV setting. The advantage of using this class is 
0054  *  that this setting is performed almost automatically: the only thing
0055  *  that the user has to do is to pass prescriptions about the IOV setting
0056  *  in the configuration of his module. A typical
0057  *  configuration is as follows:
0058  *
0059  *
0060  *        module prod =  SiStripGainRandomCalculator {
0061  *
0062  *        #parameters of the derived class
0063  *          double MinPositiveGain = 0.1
0064  *          double MeanGain    = 1
0065  *          double SigmaGain   = 0
0066  *                  untracked bool   printDebug = true
0067  *
0068  *        #parameters of the base class
0069  *          string IOVMode       = "Run"
0070  *          bool SinceAppendMode = true
0071  *          string Record        = "SiStripApvGainRcd"
0072  *
0073  *                 }
0074  *
0075  *  Two subsets of parameters can be found. The first subset contains the specific
0076  *  parameters of the user class, which is called in this case 
0077  *  SiStripGainRandomCalculator. The second subset contains the parameters of
0078  *  the base class. These are the following:
0079  *
0080  *  1) string IOVMode
0081  *
0082  *  4 possible values can be given: "Job", "Run", "LumiBlock" and "AlgoDriven"
0083  *  This card determines the length of the IOV. In other words, together with  
0084  *  the number of Lumysections/runs the user has decided to run his application,
0085  *  this card determines the number of objects that will be stored on the DB
0086  *  (the getNewObject method will be called as many times).
0087  *  For example if the user is running on the events of one Run, which has 
0088  *  10 luminosity sections and chooses the "LumiBlock" mode, then 10 objects
0089  *  with corresponding IOV will be written. If the "Job" mode is chosen, only one 
0090  *  object will be stored irrespective of the dataset on which the user is 
0091  *  running.
0092  *  The  "AlgoDriven" option is special. If this choice is made, then it is 
0093  *  up to the user to tell in the code when the getNewObject method must be 
0094  *  called. This can be done by calling the method  below  void storeOnDbNow()
0095  *  must be invoked whenever a certain condition that justifies the start/end
0096  *  of an IOV is met.
0097  *
0098  *  2) bool SinceAppendMode
0099  *
0100  *  obsolete option
0101  *  now ONLY Since append mode is supported
0102  *
0103  *
0104  * 
0105  *      WARNING: due to the policy of the framework, objects SHALL be stored
0106  *      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.
0107  *
0108  *
0109  *  3)string Record 
0110  *
0111  *  this is the eventsetup record of your object.
0112  *
0113  * Note that the setDoStore method changes the doStore parameter read from configuration file.
0114  * This is sometimes needed e.g. to avoid filling bad payloads to the database.
0115  *
0116  */
0117 
0118 //
0119 
0120 //
0121 // Original Author:  Giacomo Bruno
0122 //         Created:  May 23 10:04:31 CET 2007
0123 //
0124 //
0125 
0126 // system include files
0127 #include <memory>
0128 #include <string>
0129 #include <cstdlib>
0130 
0131 // user include files
0132 #include "FWCore/Framework/interface/Frameworkfwd.h"
0133 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0134 #include "FWCore/Framework/interface/Run.h"
0135 #include "FWCore/Framework/interface/ESHandle.h"
0136 #include "FWCore/ServiceRegistry/interface/Service.h"
0137 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0138 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0139 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0140 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0141 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0142 
0143 #include "FWCore/Utilities/interface/Exception.h"
0144 //#include "FWCore/Framework/interface/EventSetup.h"
0145 
0146 #include "FWCore/Framework/interface/Event.h"
0147 #include "DataFormats/Common/interface/Handle.h"
0148 
0149 template <class T>
0150 class ConditionDBWriter
0151     : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks, edm::one::SharedResources> {
0152 public:
0153   explicit ConditionDBWriter(const edm::ParameterSet &iConfig)
0154       : minRunRange_(1 << 31),
0155         maxRunRange_(0),
0156         LumiBlockMode_(false),
0157         RunMode_(false),
0158         JobMode_(false),
0159         AlgoDrivenMode_(false),
0160         Time_(0),
0161         setSinceTime_(false),
0162         firstRun_(true) {
0163     edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
0164     SinceAppendMode_ = iConfig.getParameter<bool>("SinceAppendMode");
0165     std::string IOVMode = iConfig.getParameter<std::string>("IOVMode");
0166     if (IOVMode == std::string("Job"))
0167       JobMode_ = true;
0168     else if (IOVMode == std::string("Run"))
0169       RunMode_ = true;
0170     else if (IOVMode == std::string("LumiBlock"))
0171       LumiBlockMode_ = true;
0172     else if (IOVMode == std::string("AlgoDriven"))
0173       AlgoDrivenMode_ = true;
0174     else
0175       edm::LogError(
0176           "ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything "
0177           "on the DB")
0178           << std::endl;
0179     Record_ = iConfig.getParameter<std::string>("Record");
0180     doStore_ = iConfig.getParameter<bool>("doStoreOnDB");
0181     timeFromEndRun_ = iConfig.getUntrackedParameter<bool>("TimeFromEndRun", false);
0182     timeFromStartOfRunRange_ = iConfig.getUntrackedParameter<bool>("TimeFromStartOfRunRange", false);
0183 
0184     if (!SinceAppendMode_)
0185       edm::LogError("ConditionDBWriter::endJob(): ERROR - only SinceAppendMode support!!!!");
0186   }
0187 
0188   ~ConditionDBWriter() override { edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl; }
0189 
0190   // utility method to validate configurations of inherited classes
0191   static void fillPSetDescription(edm::ParameterSetDescription &desc) {
0192     desc.add<bool>("SinceAppendMode");
0193     desc.add<std::string>("IOVMode");
0194     desc.add<std::string>("Record");
0195     desc.add<bool>("doStoreOnDB");
0196     desc.addUntracked<bool>("TimeFromEndRun", false);
0197     desc.addUntracked<bool>("TimeFromStartOfRunRange", false);
0198   }
0199 
0200 private:
0201   // 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!)
0202 
0203   virtual std::unique_ptr<T> getNewObject() = 0;
0204 
0205   // Optional methods that may be implemented (technically "overridden") in the derived classes if needed
0206 
0207   //Will be called at the beginning of the job
0208   virtual void algoBeginJob(const edm::EventSetup &){};
0209   //Will be called at the beginning of each run in the job
0210   virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
0211   //Will be called at the beginning of each luminosity block in the run
0212   virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
0213   //Will be called at every event
0214   virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &){};
0215   //Will be called at the end of each run in the job
0216   virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
0217   //Will be called at the end of the job
0218   virtual void algoEndJob(){};
0219 
0220   void beginJob() override {}
0221 
0222   void beginRun(const edm::Run &run, const edm::EventSetup &es) override {
0223     if (firstRun_) {
0224       edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
0225       if ((JobMode_ || AlgoDrivenMode_) && SinceAppendMode_)
0226         setSinceTime_ = true;
0227       algoBeginJob(es);
0228       firstRun_ = false;
0229     }
0230 
0231     if (run.id().run() < minRunRange_)
0232       minRunRange_ = run.id().run();
0233     if (run.id().run() > maxRunRange_)
0234       maxRunRange_ = run.id().run();
0235 
0236     edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
0237     if (RunMode_ && SinceAppendMode_)
0238       setSinceTime_ = true;
0239     algoBeginRun(run, es);
0240   }
0241 
0242   void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup) override {
0243     edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
0244     if (LumiBlockMode_ && SinceAppendMode_)
0245       setSinceTime_ = true;
0246     algoBeginLuminosityBlock(lumiBlock, iSetup);
0247   }
0248 
0249   void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override {
0250     if (setSinceTime_) {
0251       setTime();  //set new since time for possible next upload to DB
0252       setSinceTime_ = false;
0253     }
0254     algoAnalyze(event, iSetup);
0255   }
0256 
0257   void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override {
0258     edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
0259     algoEndLuminosityBlock(lumiBlock, es);
0260 
0261     if (LumiBlockMode_) {
0262       std::unique_ptr<T> objPointer = getNewObject();
0263 
0264       if (objPointer) {
0265         storeOnDb(objPointer);
0266       } else {
0267         edm::LogError(
0268             "ConditionDBWriter::endLuminosityblock(): ERROR - requested to store on DB on a Lumi Block based interval, "
0269             "but received null pointer...will not store anything on the DB")
0270             << std::endl;
0271       }
0272     }
0273   }
0274 
0275   virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
0276 
0277   void endRun(const edm::Run &run, const edm::EventSetup &es) override {
0278     edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
0279 
0280     algoEndRun(run, es);
0281 
0282     if (RunMode_) {
0283       std::unique_ptr<T> objPointer = getNewObject();
0284 
0285       if (objPointer) {
0286         if (timeFromEndRun_)
0287           Time_ = run.id().run();
0288         storeOnDb(objPointer);
0289       } else {
0290         edm::LogError(
0291             "ConditionDBWriter::endRun(): ERROR - requested to store on DB on a Run based interval, but received null "
0292             "pointer...will not store anything on the DB")
0293             << std::endl;
0294       }
0295     }
0296   }
0297 
0298   void endJob() override {
0299     edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
0300 
0301     algoEndJob();
0302 
0303     if (JobMode_) {
0304       std::unique_ptr<T> objPointer = getNewObject();
0305 
0306       if (objPointer) {
0307         storeOnDb(objPointer);
0308       }
0309 
0310       else {
0311         edm::LogError(
0312             "ConditionDBWriter::endJob(): ERROR - requested to store on DB on a Job based interval, but received null "
0313             "pointer...will not store anything on the DB")
0314             << std::endl;
0315       }
0316     }
0317   }
0318 
0319   void storeOnDb(std::unique_ptr<T> &objPointer) {
0320     edm::LogInfo("ConditionDBWriter::storeOnDb ") << std::endl;
0321 
0322     setSinceTime_ = true;
0323 
0324     if (!objPointer) {
0325       edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB");
0326       return;
0327     }
0328 
0329     //And now write  data in DB
0330     if (!doStore_)
0331       return;
0332     edm::Service<cond::service::PoolDBOutputService> mydbservice;
0333     if (!mydbservice.isAvailable()) {
0334       edm::LogError("ConditionDBWriter") << "PoolDBOutputService is unavailable" << std::endl;
0335       return;
0336     }
0337 
0338     cond::Time_t since =
0339         (mydbservice->isNewTagRequest(Record_) && !timeFromEndRun_) ? mydbservice->beginOfTime() : Time_;
0340 
0341     //overwrite tim in the case we have the flag TimeFromStartOfRunRange set to on
0342     if (timeFromStartOfRunRange_)
0343       since = minRunRange_;
0344 
0345     edm::LogInfo("ConditionDBWriter") << "appending a new object to tag " << Record_ << " in since mode " << std::endl;
0346 
0347     mydbservice->writeOneIOV<T>(*objPointer, since, Record_);
0348   }
0349 
0350   void setTime() {
0351     edm::Service<cond::service::PoolDBOutputService> mydbservice;
0352 
0353     if (mydbservice.isAvailable()) {
0354       Time_ = mydbservice->currentTime();
0355       edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
0356     } else {
0357       edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time")
0358           << std::endl;
0359     }
0360   }
0361 
0362 protected:
0363   // 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
0364 
0365   void storeOnDbNow() {
0366     if (AlgoDrivenMode_) {
0367       setSinceTime_ = true;
0368 
0369       std::unique_ptr<T> objPointer = getNewObject();
0370 
0371       if (!objPointer) {
0372         edm::LogError(
0373             "ConditionDBWriter::storeOnDbNow: ERROR - requested to store on DB a new object (module configuration is "
0374             "algo driven based IOV), but received NULL pointer...will not store anything on the DB")
0375             << std::endl;
0376         return;
0377       } else {
0378         storeOnDb(objPointer);
0379       }
0380 
0381     } else {
0382       edm::LogError(
0383           "ConditionDBWriter::storeOnDbNow(): ERROR - received a direct request from concrete algorithm to store on DB "
0384           "a new object, but module configuration is not to store on DB on an algo driven based interval...will not "
0385           "store anything on the DB")
0386           << std::endl;
0387       return;
0388     }
0389   }
0390 
0391   // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)
0392 
0393   cond::Time_t timeOfLastIOV() { return Time_; }
0394 
0395   /// When set to false the payload will not be written to the db
0396   void setDoStore(const bool doStore) { doStore_ = doStore; }
0397 
0398 private:
0399   unsigned int minRunRange_;
0400   unsigned int maxRunRange_;
0401 
0402   bool SinceAppendMode_;  // till or since append mode
0403 
0404   bool LumiBlockMode_;  //LumiBlock since/till time
0405   bool RunMode_;        //
0406   bool JobMode_;
0407   bool AlgoDrivenMode_;
0408   bool doStore_;
0409 
0410   std::string Record_;
0411   cond::Time_t
0412       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.
0413 
0414   bool setSinceTime_;
0415 
0416   bool firstRun_;
0417 
0418   bool timeFromEndRun_;
0419   bool timeFromStartOfRunRange_;
0420 };
0421 
0422 #endif