Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-05 02:38:53

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 
0142 #include "FWCore/Utilities/interface/Exception.h"
0143 //#include "FWCore/Framework/interface/EventSetup.h"
0144 
0145 #include "FWCore/Framework/interface/Event.h"
0146 #include "DataFormats/Common/interface/Handle.h"
0147 
0148 template <class T>
0149 class ConditionDBWriter : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
0150 public:
0151   explicit ConditionDBWriter(const edm::ParameterSet &iConfig)
0152       : minRunRange_(1 << 31),
0153         maxRunRange_(0),
0154         LumiBlockMode_(false),
0155         RunMode_(false),
0156         JobMode_(false),
0157         AlgoDrivenMode_(false),
0158         Time_(0),
0159         setSinceTime_(false),
0160         firstRun_(true) {
0161     edm::LogInfo("ConditionDBWriter::ConditionDBWriter()") << std::endl;
0162     SinceAppendMode_ = iConfig.getParameter<bool>("SinceAppendMode");
0163     std::string IOVMode = iConfig.getParameter<std::string>("IOVMode");
0164     if (IOVMode == std::string("Job"))
0165       JobMode_ = true;
0166     else if (IOVMode == std::string("Run"))
0167       RunMode_ = true;
0168     else if (IOVMode == std::string("LumiBlock"))
0169       LumiBlockMode_ = true;
0170     else if (IOVMode == std::string("AlgoDriven"))
0171       AlgoDrivenMode_ = true;
0172     else
0173       edm::LogError(
0174           "ConditionDBWriter::ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything "
0175           "on the DB")
0176           << std::endl;
0177     Record_ = iConfig.getParameter<std::string>("Record");
0178     doStore_ = iConfig.getParameter<bool>("doStoreOnDB");
0179     timeFromEndRun_ = iConfig.getUntrackedParameter<bool>("TimeFromEndRun", false);
0180     timeFromStartOfRunRange_ = iConfig.getUntrackedParameter<bool>("TimeFromStartOfRunRange", false);
0181 
0182     if (!SinceAppendMode_)
0183       edm::LogError("ConditionDBWriter::endJob(): ERROR - only SinceAppendMode support!!!!");
0184   }
0185 
0186   ~ConditionDBWriter() override { edm::LogInfo("ConditionDBWriter::~ConditionDBWriter()") << std::endl; }
0187 
0188   // utility method to validate configurations of inherited classes
0189   static void fillPSetDescription(edm::ParameterSetDescription &desc) {
0190     desc.add<bool>("SinceAppendMode");
0191     desc.add<std::string>("IOVMode");
0192     desc.add<std::string>("Record");
0193     desc.add<bool>("doStoreOnDB");
0194     desc.addUntracked<bool>("TimeFromEndRun", false);
0195     desc.addUntracked<bool>("TimeFromStartOfRunRange", false);
0196   }
0197 
0198 private:
0199   // 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!)
0200 
0201   virtual std::unique_ptr<T> getNewObject() = 0;
0202 
0203   // Optional methods that may be implemented (technically "overridden") in the derived classes if needed
0204 
0205   //Will be called at the beginning of the job
0206   virtual void algoBeginJob(const edm::EventSetup &){};
0207   //Will be called at the beginning of each run in the job
0208   virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){};
0209   //Will be called at the beginning of each luminosity block in the run
0210   virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
0211   //Will be called at every event
0212   virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &){};
0213   //Will be called at the end of each run in the job
0214   virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){};
0215   //Will be called at the end of the job
0216   virtual void algoEndJob(){};
0217 
0218   void beginJob() override {}
0219 
0220   void beginRun(const edm::Run &run, const edm::EventSetup &es) override {
0221     if (firstRun_) {
0222       edm::LogInfo("ConditionDBWriter::beginJob") << std::endl;
0223       if ((JobMode_ || AlgoDrivenMode_) && SinceAppendMode_)
0224         setSinceTime_ = true;
0225       algoBeginJob(es);
0226       firstRun_ = false;
0227     }
0228 
0229     if (run.id().run() < minRunRange_)
0230       minRunRange_ = run.id().run();
0231     if (run.id().run() > maxRunRange_)
0232       maxRunRange_ = run.id().run();
0233 
0234     edm::LogInfo("ConditionDBWriter::beginRun") << std::endl;
0235     if (RunMode_ && SinceAppendMode_)
0236       setSinceTime_ = true;
0237     algoBeginRun(run, es);
0238   }
0239 
0240   void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup) override {
0241     edm::LogInfo("ConditionDBWriter::beginLuminosityBlock") << std::endl;
0242     if (LumiBlockMode_ && SinceAppendMode_)
0243       setSinceTime_ = true;
0244     algoBeginLuminosityBlock(lumiBlock, iSetup);
0245   }
0246 
0247   void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override {
0248     if (setSinceTime_) {
0249       setTime();  //set new since time for possible next upload to DB
0250       setSinceTime_ = false;
0251     }
0252     algoAnalyze(event, iSetup);
0253   }
0254 
0255   void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override {
0256     edm::LogInfo("ConditionDBWriter::endLuminosityBlock") << std::endl;
0257     algoEndLuminosityBlock(lumiBlock, es);
0258 
0259     if (LumiBlockMode_) {
0260       std::unique_ptr<T> objPointer = getNewObject();
0261 
0262       if (objPointer) {
0263         storeOnDb(objPointer);
0264       } else {
0265         edm::LogError(
0266             "ConditionDBWriter::endLuminosityblock(): ERROR - requested to store on DB on a Lumi Block based interval, "
0267             "but received null pointer...will not store anything on the DB")
0268             << std::endl;
0269       }
0270     }
0271   }
0272 
0273   virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){};
0274 
0275   void endRun(const edm::Run &run, const edm::EventSetup &es) override {
0276     edm::LogInfo("ConditionDBWriter::endRun") << std::endl;
0277 
0278     algoEndRun(run, es);
0279 
0280     if (RunMode_) {
0281       std::unique_ptr<T> objPointer = getNewObject();
0282 
0283       if (objPointer) {
0284         if (timeFromEndRun_)
0285           Time_ = run.id().run();
0286         storeOnDb(objPointer);
0287       } else {
0288         edm::LogError(
0289             "ConditionDBWriter::endRun(): ERROR - requested to store on DB on a Run based interval, but received null "
0290             "pointer...will not store anything on the DB")
0291             << std::endl;
0292       }
0293     }
0294   }
0295 
0296   void endJob() override {
0297     edm::LogInfo("ConditionDBWriter::endJob") << std::endl;
0298 
0299     algoEndJob();
0300 
0301     if (JobMode_) {
0302       std::unique_ptr<T> objPointer = getNewObject();
0303 
0304       if (objPointer) {
0305         storeOnDb(objPointer);
0306       }
0307 
0308       else {
0309         edm::LogError(
0310             "ConditionDBWriter::endJob(): ERROR - requested to store on DB on a Job based interval, but received null "
0311             "pointer...will not store anything on the DB")
0312             << std::endl;
0313       }
0314     }
0315   }
0316 
0317   void storeOnDb(std::unique_ptr<T> &objPointer) {
0318     edm::LogInfo("ConditionDBWriter::storeOnDb ") << std::endl;
0319 
0320     setSinceTime_ = true;
0321 
0322     if (!objPointer) {
0323       edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB");
0324       return;
0325     }
0326 
0327     //And now write  data in DB
0328     if (!doStore_)
0329       return;
0330     edm::Service<cond::service::PoolDBOutputService> mydbservice;
0331     if (!mydbservice.isAvailable()) {
0332       edm::LogError("ConditionDBWriter") << "PoolDBOutputService is unavailable" << std::endl;
0333       return;
0334     }
0335 
0336     cond::Time_t since =
0337         (mydbservice->isNewTagRequest(Record_) && !timeFromEndRun_) ? mydbservice->beginOfTime() : Time_;
0338 
0339     //overwrite tim in the case we have the flag TimeFromStartOfRunRange set to on
0340     if (timeFromStartOfRunRange_)
0341       since = minRunRange_;
0342 
0343     edm::LogInfo("ConditionDBWriter") << "appending a new object to tag " << Record_ << " in since mode " << std::endl;
0344 
0345     mydbservice->writeOneIOV<T>(*objPointer, since, Record_);
0346   }
0347 
0348   void setTime() {
0349     edm::Service<cond::service::PoolDBOutputService> mydbservice;
0350 
0351     if (mydbservice.isAvailable()) {
0352       Time_ = mydbservice->currentTime();
0353       edm::LogInfo("ConditionDBWriter::setTime: time set to ") << Time_ << std::endl;
0354     } else {
0355       edm::LogError("ConditionDBWriter::setTime(): PoolDBOutputService is not available...cannot set current time")
0356           << std::endl;
0357     }
0358   }
0359 
0360 protected:
0361   // 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
0362 
0363   void storeOnDbNow() {
0364     if (AlgoDrivenMode_) {
0365       setSinceTime_ = true;
0366 
0367       std::unique_ptr<T> objPointer = getNewObject();
0368 
0369       if (!objPointer) {
0370         edm::LogError(
0371             "ConditionDBWriter::storeOnDbNow: ERROR - requested to store on DB a new object (module configuration is "
0372             "algo driven based IOV), but received NULL pointer...will not store anything on the DB")
0373             << std::endl;
0374         return;
0375       } else {
0376         storeOnDb(objPointer);
0377       }
0378 
0379     } else {
0380       edm::LogError(
0381           "ConditionDBWriter::storeOnDbNow(): ERROR - received a direct request from concrete algorithm to store on DB "
0382           "a new object, but module configuration is not to store on DB on an algo driven based interval...will not "
0383           "store anything on the DB")
0384           << std::endl;
0385       return;
0386     }
0387   }
0388 
0389   // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)
0390 
0391   cond::Time_t timeOfLastIOV() { return Time_; }
0392 
0393   /// When set to false the payload will not be written to the db
0394   void setDoStore(const bool doStore) { doStore_ = doStore; }
0395 
0396 private:
0397   unsigned int minRunRange_;
0398   unsigned int maxRunRange_;
0399 
0400   bool SinceAppendMode_;  // till or since append mode
0401 
0402   bool LumiBlockMode_;  //LumiBlock since/till time
0403   bool RunMode_;        //
0404   bool JobMode_;
0405   bool AlgoDrivenMode_;
0406   bool doStore_;
0407 
0408   std::string Record_;
0409   cond::Time_t
0410       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.
0411 
0412   bool setSinceTime_;
0413 
0414   bool firstRun_;
0415 
0416   bool timeFromEndRun_;
0417   bool timeFromStartOfRunRange_;
0418 };
0419 
0420 #endif