BufferedBoostIODBWriter

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
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <cassert>
#include <fstream>
#include <memory>

#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "CondFormats/HcalObjects/interface/OOTPileupCorrectionBuffer.h"
#include "CondCore/DBOutputService/interface/PoolDBOutputService.h"

//
// class declaration
//
class BufferedBoostIODBWriter : public edm::one::EDAnalyzer<> {
public:
  explicit BufferedBoostIODBWriter(const edm::ParameterSet&);
  ~BufferedBoostIODBWriter() override {}

  BufferedBoostIODBWriter() = delete;
  BufferedBoostIODBWriter(const BufferedBoostIODBWriter&) = delete;
  BufferedBoostIODBWriter& operator=(const BufferedBoostIODBWriter&) = delete;

private:
  void analyze(const edm::Event&, const edm::EventSetup&) override;

  std::string inputFile;
  std::string record;
};

BufferedBoostIODBWriter::BufferedBoostIODBWriter(const edm::ParameterSet& ps)
    : inputFile(ps.getParameter<std::string>("inputFile")), record(ps.getParameter<std::string>("record")) {}

void BufferedBoostIODBWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
  std::unique_ptr<OOTPileupCorrectionBuffer> fcp;

  {
    std::ifstream input(inputFile.c_str(), std::ios_base::binary);
    if (!input.is_open())
      throw cms::Exception("InvalidArgument") << "Failed to open file \"" << inputFile << '"' << std::endl;

    struct stat st;
    if (stat(inputFile.c_str(), &st))
      throw cms::Exception("SystemError") << "Failed to stat file \"" << inputFile << '"' << std::endl;

    const std::size_t len = st.st_size;
    fcp = std::make_unique<OOTPileupCorrectionBuffer>(len);
    assert(fcp->length() == len);
    if (len)
      input.read(fcp->getBuffer(), len);
    if (input.fail())
      throw cms::Exception("SystemError")
          << "Input stream failure while reading file \"" << inputFile << '"' << std::endl;
  }

  edm::Service<cond::service::PoolDBOutputService> poolDbService;
  if (poolDbService.isAvailable())
    poolDbService->writeOneIOV(*fcp, poolDbService->currentTime(), record);
  else
    throw cms::Exception("ConfigurationError") << "PoolDBOutputService is not available, "
                                               << "please configure it properly" << std::endl;
}

DEFINE_FWK_MODULE(BufferedBoostIODBWriter);