Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:36:02

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