ConditionDBWriter

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
#ifndef CommonTools_ConditionDBWriter_ConditionDBWriter_h
#define CommonTools_ConditionDBWriter_ConditionDBWriter_h
// -*- C++ -*-
//
// Package:    ConditionDBWriter
// Class:      ConditionDBWriter
//
// \class ConditionDBWriter
//
//  Description:

/**
 *  Implementation:
 *
 *  This class can be very useful whenever a CMSSW application needs to store data
 *  to the offline DB. Typically such applications require access to event data 
 *  and/or need to be notified about the start of Run, Lumi section in order 
 *  to set a correct Interval Of Validity (IOV) for the data they have to store.
 *  Therefore the FWK EDAnalyzer is an excellent candidate for the implementation
 *  of such applications; this is the reason why this class inherits from 
 *  the EDAnalyzer class. 
 *
 *  The user class should inherit from this class. 
 *  The templated type must be the type of the object that
 *  has to be written on the DB (e.g. MyCalibration). Examples of use of
 *  this class can be found in package CalibTracker/SiStripChannelGain. Have a
 *  look also at the test/ directory for examples of full cfg files. 
 *
 *  The user must implement in his derived class the abstract method below
 *
 *  virtual std::unique_ptr<MyCalibration> getNewObject()=0;
 *
 *  The user can optionally implement the following methods 
 *
 *    //Will be called at the beginning of the job
 *    virtual void algoBeginJob(const edm::EventSetup&){}
 *    //Will be called at the beginning of each run in the job
 *    virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &){}
 *    //Will be called at the beginning of each luminosity block in the run
 *    virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &){}
 *    //Will be called at every event
 *    virtual void algoAnalyze(const edm::Event&, const edm::EventSetup&){}
 *    //Will be called at the end of each run in the job
 *    virtual void algoEndRun(const edm::Run &, const edm::EventSetup &){}
 *    //Will be called at the end of the job
 *    virtual void algoEndJob(){}
 *
 *  where he can access information needed to build his object. For instance, if
 *  he is computing a calibration that is computed as the mean of a certain
 *  quantity that varies from event to event, he will implement the algoAnalyze 
 *  method.
 *
 *  The important part is the IOV setting. The advantage of using this class is 
 *  that this setting is performed almost automatically: the only thing
 *  that the user has to do is to pass prescriptions about the IOV setting
 *  in the configuration of his module. A typical
 *  configuration is as follows:
 *
 *
 *        module prod =  SiStripGainRandomCalculator {
 *
 *        #parameters of the derived class
 *  		double MinPositiveGain = 0.1
 *  		double MeanGain    = 1
 *  		double SigmaGain   = 0
 *                  untracked bool   printDebug = true
 *
 *        #parameters of the base class
 *  		string IOVMode	     = "Run"
 *  		bool SinceAppendMode = true
 *  		string Record        = "SiStripApvGainRcd"
 *
 *                 }
 *
 *  Two subsets of parameters can be found. The first subset contains the specific
 *  parameters of the user class, which is called in this case 
 *  SiStripGainRandomCalculator. The second subset contains the parameters of
 *  the base class. These are the following:
 *
 *  1) string IOVMode
 *
 *  4 possible values can be given: "Job", "Run", "LumiBlock" and "AlgoDriven"
 *  This card determines the length of the IOV. In other words, together with  
 *  the number of Lumysections/runs the user has decided to run his application,
 *  this card determines the number of objects that will be stored on the DB
 *  (the getNewObject method will be called as many times).
 *  For example if the user is running on the events of one Run, which has 
 *  10 luminosity sections and chooses the "LumiBlock" mode, then 10 objects
 *  with corresponding IOV will be written. If the "Job" mode is chosen, only one 
 *  object will be stored irrespective of the dataset on which the user is 
 *  running.
 *  The  "AlgoDriven" option is special. If this choice is made, then it is 
 *  up to the user to tell in the code when the getNewObject method must be 
 *  called. This can be done by calling the method  below  void storeOnDbNow()
 *  must be invoked whenever a certain condition that justifies the start/end
 *  of an IOV is met.
 *
 *  2) bool SinceAppendMode
 *
 *  obsolete option
 *  now ONLY Since append mode is supported
 *
 *
 * 
 *      WARNING: due to the policy of the framework, objects SHALL be stored
 *      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.
 *
 *
 *  3)string Record 
 *
 *  this is the eventsetup record of your object.
 *
 * Note that the setDoStore method changes the doStore parameter read from configuration file.
 * This is sometimes needed e.g. to avoid filling bad payloads to the database.
 *
 */

//

//
// Original Author:  Giacomo Bruno
//         Created:  May 23 10:04:31 CET 2007
//
//

// system include files
#include <memory>
#include <string>
#include <cstdlib>

// user include files
#include "CommonTools/UtilAlgos/interface/TFileService.h"
#include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/Exception.h"

template <class T>
class ConditionDBWriter
    : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks, edm::one::SharedResources> {
public:
  explicit ConditionDBWriter(const edm::ParameterSet &iConfig)
      : minRunRange_(1 << 31),
        maxRunRange_(0),
        LumiBlockMode_(false),
        RunMode_(false),
        JobMode_(false),
        AlgoDrivenMode_(false),
        Time_(0),
        setSinceTime_(false),
        firstRun_(true) {
    usesResource(cond::service::PoolDBOutputService::kSharedResource);
    edm::LogInfo("ConditionDBWriter") << "ConditionDBWriter()";
    SinceAppendMode_ = iConfig.getParameter<bool>("SinceAppendMode");
    std::string IOVMode = iConfig.getParameter<std::string>("IOVMode");
    if (IOVMode == std::string("Job"))
      JobMode_ = true;
    else if (IOVMode == std::string("Run"))
      RunMode_ = true;
    else if (IOVMode == std::string("LumiBlock"))
      LumiBlockMode_ = true;
    else if (IOVMode == std::string("AlgoDriven"))
      AlgoDrivenMode_ = true;
    else
      edm::LogError("ConditionDBWriter")
          << "ConditionDBWriter(): ERROR - unknown IOV interval write mode...will not store anything "
             "on the DB";
    Record_ = iConfig.getParameter<std::string>("Record");
    doStore_ = iConfig.getParameter<bool>("doStoreOnDB");
    timeFromEndRun_ = iConfig.getUntrackedParameter<bool>("TimeFromEndRun", false);
    timeFromStartOfRunRange_ = iConfig.getUntrackedParameter<bool>("TimeFromStartOfRunRange", false);

    if (!SinceAppendMode_)
      edm::LogError("ConditionDBWriter") << "endJob(): ERROR - only SinceAppendMode support!!!!";
  }

  ~ConditionDBWriter() override { edm::LogInfo("ConditionDBWriter") << "~ConditionDBWriter()"; }

  // utility method to validate configurations of inherited classes
  static void fillPSetDescription(edm::ParameterSetDescription &desc) {
    desc.add<bool>("SinceAppendMode");
    desc.add<std::string>("IOVMode");
    desc.add<std::string>("Record");
    desc.add<bool>("doStoreOnDB");
    desc.addUntracked<bool>("TimeFromEndRun", false);
    desc.addUntracked<bool>("TimeFromStartOfRunRange", false);
  }

private:
  // 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!)

  virtual std::unique_ptr<T> getNewObject() = 0;

  // Optional methods that may be implemented (technically "overridden") in the derived classes if needed

  //Will be called at the beginning of the job
  virtual void algoBeginJob(const edm::EventSetup &) {}
  //Will be called at the beginning of each run in the job
  virtual void algoBeginRun(const edm::Run &, const edm::EventSetup &) {}
  //Will be called at the beginning of each luminosity block in the run
  virtual void algoBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) {}
  //Will be called at every event
  virtual void algoAnalyze(const edm::Event &, const edm::EventSetup &) {}
  //Will be called at the end of each run in the job
  virtual void algoEndRun(const edm::Run &, const edm::EventSetup &) {}
  //Will be called at the end of the job
  virtual void algoEndJob() {}

  void beginJob() override {}

  void beginRun(const edm::Run &run, const edm::EventSetup &es) override {
    if (firstRun_) {
      edm::LogInfo("ConditionDBWriter") << "beginJob";
      if ((JobMode_ || AlgoDrivenMode_) && SinceAppendMode_)
        setSinceTime_ = true;
      algoBeginJob(es);
      firstRun_ = false;
    }

    if (run.id().run() < minRunRange_)
      minRunRange_ = run.id().run();
    if (run.id().run() > maxRunRange_)
      maxRunRange_ = run.id().run();

    edm::LogInfo("ConditionDBWriter") << "beginRun";
    if (RunMode_ && SinceAppendMode_)
      setSinceTime_ = true;
    algoBeginRun(run, es);
  }

  void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup) override {
    edm::LogInfo("ConditionDBWriter") << "beginLuminosityBlock";
    if (LumiBlockMode_ && SinceAppendMode_)
      setSinceTime_ = true;
    algoBeginLuminosityBlock(lumiBlock, iSetup);
  }

  void analyze(const edm::Event &event, const edm::EventSetup &iSetup) override {
    if (setSinceTime_) {
      setTime();  //set new since time for possible next upload to DB
      setSinceTime_ = false;
    }
    algoAnalyze(event, iSetup);
  }

  void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &es) override {
    edm::LogInfo("ConditionDBWriter") << "endLuminosityBlock";
    algoEndLuminosityBlock(lumiBlock, es);

    if (LumiBlockMode_) {
      std::unique_ptr<T> objPointer = getNewObject();

      if (objPointer) {
        storeOnDb(objPointer);
      } else {
        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";
      }
    }
  }

  virtual void algoEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) {}

  void endRun(const edm::Run &run, const edm::EventSetup &es) override {
    edm::LogInfo("ConditionDBWriter") << "endRun";

    algoEndRun(run, es);

    if (RunMode_) {
      std::unique_ptr<T> objPointer = getNewObject();

      if (objPointer) {
        if (timeFromEndRun_)
          Time_ = run.id().run();
        storeOnDb(objPointer);
      } else {
        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";
      }
    }
  }

  void endJob() override {
    edm::LogInfo("ConditionDBWriter") << "endJob";

    algoEndJob();

    if (JobMode_) {
      std::unique_ptr<T> objPointer = getNewObject();

      if (objPointer) {
        storeOnDb(objPointer);
      }

      else {
        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";
      }
    }
  }

  void storeOnDb(std::unique_ptr<T> &objPointer) {
    edm::LogInfo("ConditionDBWriter)") << "storeOnDb ";

    setSinceTime_ = true;

    if (!objPointer) {
      edm::LogError("ConditionDBWriter: Pointer to object has not been set...storing no data on DB");
      return;
    }

    //And now write  data in DB
    if (!doStore_)
      return;
    edm::Service<cond::service::PoolDBOutputService> mydbservice;
    if (!mydbservice.isAvailable()) {
      edm::LogError("ConditionDBWriter") << "PoolDBOutputService is unavailable";
      return;
    }

    cond::Time_t since =
        (mydbservice->isNewTagRequest(Record_) && !timeFromEndRun_) ? mydbservice->beginOfTime() : Time_;

    //overwrite tim in the case we have the flag TimeFromStartOfRunRange set to on
    if (timeFromStartOfRunRange_)
      since = minRunRange_;

    edm::LogInfo("ConditionDBWriter") << "appending a new object to tag " << Record_ << " in since mode ";

    mydbservice->writeOneIOV<T>(*objPointer, since, Record_);
  }

  void setTime() {
    edm::Service<cond::service::PoolDBOutputService> mydbservice;

    if (mydbservice.isAvailable()) {
      Time_ = mydbservice->currentTime();
      edm::LogInfo("ConditionDBWriter") << "setTime: time set to " << Time_;
    } else {
      edm::LogError("ConditionDBWriter") << "setTime(): PoolDBOutputService is not available...cannot set current time";
    }
  }

protected:
  // 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

  void storeOnDbNow() {
    if (AlgoDrivenMode_) {
      setSinceTime_ = true;

      std::unique_ptr<T> objPointer = getNewObject();

      if (!objPointer) {
        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";
        return;
      } else {
        storeOnDb(objPointer);
      }

    } else {
      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";
      return;
    }
  }

  // utility method: it returns the lastly set IOV time (till or since according to what was chosen in the configuration)

  cond::Time_t timeOfLastIOV() { return Time_; }

  /// When set to false the payload will not be written to the db
  void setDoStore(const bool doStore) { doStore_ = doStore; }

private:
  unsigned int minRunRange_;
  unsigned int maxRunRange_;

  bool SinceAppendMode_;  // till or since append mode

  bool LumiBlockMode_;  //LumiBlock since/till time
  bool RunMode_;
  bool JobMode_;
  bool AlgoDrivenMode_;
  bool doStore_;

  std::string Record_;
  //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.
  cond::Time_t Time_;

  bool setSinceTime_;

  bool firstRun_;

  bool timeFromEndRun_;
  bool timeFromStartOfRunRange_;
};

#endif