Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-26 10:51:23

0001 // -*- C++ -*-
0002 //
0003 // Package:    JetMETCorrections/FFTJetModules
0004 // Class:      FFTJetCorrectorDBWriter
0005 //
0006 /**\class FFTJetCorrectorDBWriter FFTJetCorrectorDBWriter.cc JetMETCorrections/FFTJetModules/plugins/FFTJetCorrectorDBWriter.cc
0007 
0008  Description: writes a blob from a file into a database
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Igor Volobouev
0015 //         Created:  Wed Aug  1 20:59:12 CDT 2012
0016 //
0017 //
0018 
0019 #include <sys/types.h>
0020 #include <sys/stat.h>
0021 #include <unistd.h>
0022 
0023 #include <cassert>
0024 #include <fstream>
0025 
0026 #include "FWCore/Framework/interface/Frameworkfwd.h"
0027 #include "FWCore/Framework/interface/EDAnalyzer.h"
0028 #include "FWCore/Framework/interface/Event.h"
0029 #include "FWCore/Framework/interface/MakerMacros.h"
0030 
0031 #include "FWCore/ServiceRegistry/interface/Service.h"
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 
0034 #include "CondFormats/JetMETObjects/interface/FFTJetCorrectorParameters.h"
0035 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0036 
0037 #define init_param(type, varname) varname(ps.getParameter<type>(#varname))
0038 
0039 //
0040 // class declaration
0041 //
0042 class FFTJetCorrectorDBWriter : public edm::EDAnalyzer {
0043 public:
0044   explicit FFTJetCorrectorDBWriter(const edm::ParameterSet&);
0045   FFTJetCorrectorDBWriter() = delete;
0046   FFTJetCorrectorDBWriter(const FFTJetCorrectorDBWriter&) = delete;
0047   FFTJetCorrectorDBWriter& operator=(const FFTJetCorrectorDBWriter&) = delete;
0048   ~FFTJetCorrectorDBWriter() override {}
0049 
0050 private:
0051   void analyze(const edm::Event&, const edm::EventSetup&) override;
0052 
0053   std::string inputFile;
0054   std::string record;
0055 };
0056 
0057 FFTJetCorrectorDBWriter::FFTJetCorrectorDBWriter(const edm::ParameterSet& ps)
0058     : init_param(std::string, inputFile), init_param(std::string, record) {}
0059 
0060 void FFTJetCorrectorDBWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0061   std::unique_ptr<FFTJetCorrectorParameters> fcp;
0062 
0063   {
0064     std::ifstream input(inputFile.c_str(), std::ios_base::binary);
0065     if (!input.is_open())
0066       throw cms::Exception("InvalidArgument") << "Failed to open file \"" << inputFile << '"' << std::endl;
0067 
0068     struct stat st;
0069     if (stat(inputFile.c_str(), &st))
0070       throw cms::Exception("SystemError") << "Failed to stat file \"" << inputFile << '"' << std::endl;
0071 
0072     const std::size_t len = st.st_size;
0073     fcp = std::make_unique<FFTJetCorrectorParameters>(len);
0074     assert(fcp->length() == len);
0075     if (len)
0076       input.read(fcp->getBuffer(), len);
0077     if (input.fail())
0078       throw cms::Exception("SystemError")
0079           << "Input stream failure while reading file \"" << inputFile << '"' << std::endl;
0080   }
0081 
0082   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0083   if (poolDbService.isAvailable())
0084     poolDbService->writeOneIOV(*fcp, poolDbService->currentTime(), record);
0085   else
0086     throw cms::Exception("ConfigurationError") << "PoolDBOutputService is not available, "
0087                                                << "please configure it properly" << std::endl;
0088 }
0089 
0090 DEFINE_FWK_MODULE(FFTJetCorrectorDBWriter);