Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-30 02:33:12

0001 // -*- C++ -*-
0002 //
0003 // Package:    JetMETCorrections/FFTJetModules
0004 // Class:      FFTJetCorrectorDBReader
0005 //
0006 /**\class FFTJetCorrectorDBReader FFTJetCorrectorDBReader.cc JetMETCorrections/FFTJetModules/plugins/FFTJetCorrectorDBReader.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 <iostream>
0020 #include <sstream>
0021 #include <fstream>
0022 
0023 #include "Alignment/Geners/interface/StringArchive.hh"
0024 #include "Alignment/Geners/interface/CompressedIO.hh"
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/EventSetup.h"
0030 #include "FWCore/Framework/interface/MakerMacros.h"
0031 #include "FWCore/Framework/interface/ESHandle.h"
0032 
0033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0034 
0035 #include "CondFormats/JetMETObjects/interface/FFTJetCorrectorParameters.h"
0036 #include "CondFormats/DataRecord/interface/FFTJetCorrectorParametersRcdTypes.h"
0037 
0038 #include "JetMETCorrections/FFTJetObjects/interface/FFTJetCorrectorParametersLoader.h"
0039 
0040 #define init_param(type, varname) varname(ps.getParameter<type>(#varname))
0041 
0042 //
0043 // class declaration
0044 //
0045 class FFTJetCorrectorDBReader : public edm::EDAnalyzer {
0046 public:
0047   explicit FFTJetCorrectorDBReader(const edm::ParameterSet&);
0048   FFTJetCorrectorDBReader() = delete;
0049   FFTJetCorrectorDBReader(const FFTJetCorrectorDBReader&) = delete;
0050   FFTJetCorrectorDBReader& operator=(const FFTJetCorrectorDBReader&) = delete;
0051   ~FFTJetCorrectorDBReader() override {}
0052 
0053 private:
0054   void analyze(const edm::Event&, const edm::EventSetup&) override;
0055 
0056   std::string record;
0057   std::string outputFile;
0058   bool printAsString;
0059   bool readArchive;
0060   bool isArchiveCompressed;
0061 };
0062 
0063 FFTJetCorrectorDBReader::FFTJetCorrectorDBReader(const edm::ParameterSet& ps)
0064     : init_param(std::string, record),
0065       init_param(std::string, outputFile),
0066       init_param(bool, printAsString),
0067       init_param(bool, readArchive),
0068       init_param(bool, isArchiveCompressed) {}
0069 
0070 void FFTJetCorrectorDBReader::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0071   edm::ESHandle<FFTJetCorrectorParameters> JetCorParams;
0072   StaticFFTJetCorrectorParametersLoader::instance().load(iSetup, record, JetCorParams);
0073 
0074   if (printAsString || readArchive)
0075     std::cout << "++++ FFTJetCorrectorDBReader: info for record \"" << record << '"' << std::endl;
0076 
0077   if (printAsString)
0078     std::cout << "++++ String rep: \"" << JetCorParams->str() << '"' << std::endl;
0079   else if (readArchive) {
0080     CPP11_auto_ptr<gs::StringArchive> par;
0081 
0082     {
0083       std::istringstream is(JetCorParams->str());
0084       if (isArchiveCompressed)
0085         par = gs::read_compressed_item<gs::StringArchive>(is);
0086       else
0087         par = gs::read_item<gs::StringArchive>(is);
0088     }
0089 
0090     const unsigned long long idSmall = par->smallestId();
0091     if (!idSmall)
0092       std::cout << "++++ No valid records in the archive" << std::endl;
0093     else {
0094       std::cout << "++++ Archive metadata begins" << std::endl;
0095       const unsigned long long idLarge = par->largestId();
0096       unsigned long long count = 0;
0097       for (unsigned long long id = idSmall; id <= idLarge; ++id)
0098         if (par->itemExists(id)) {
0099           std::shared_ptr<const gs::CatalogEntry> e = par->catalogEntry(id);
0100           std::cout << '\n';
0101           e->humanReadable(std::cout);
0102           ++count;
0103         }
0104       std::cout << "\n++++ Archive metadata ends, " << count << " items total" << std::endl;
0105     }
0106   }
0107 
0108   if (!outputFile.empty()) {
0109     std::ofstream of(outputFile.c_str(), std::ios_base::binary);
0110     if (!of.is_open())
0111       throw cms::Exception("InvalidArgument") << "Failed to open file \"" << outputFile << '"' << std::endl;
0112     if (!JetCorParams->empty()) {
0113       of.write(JetCorParams->getBuffer(), JetCorParams->length());
0114       if (of.fail())
0115         throw cms::Exception("SystemError")
0116             << "Output stream failure while writing file \"" << outputFile << '"' << std::endl;
0117     }
0118   }
0119 }
0120 
0121 DEFINE_FWK_MODULE(FFTJetCorrectorDBReader);