Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:26

0001 // -*- C++ -*-
0002 //
0003 // Package:    JetMETCorrections/FFTJetModules
0004 // Class:      FFTJetLookupTableESProducer
0005 //
0006 /**\class FFTJetLookupTableESProducer FFTJetLookupTableESProducer.cc JetMETCorrections/FFTJetModules/plugins/FFTJetLookupTableESProducer.cc
0007 
0008  Description: produces lookup tables for jet reconstruction
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Igor Volobouev
0015 //         Created:  Thu Aug  2 22:34:02 CDT 2012
0016 //
0017 //
0018 
0019 // system include files
0020 #include <sstream>
0021 #include <utility>
0022 
0023 #include <memory>
0024 
0025 #include "Alignment/Geners/interface/CompressedIO.hh"
0026 #include "Alignment/Geners/interface/StringArchive.hh"
0027 #include "Alignment/Geners/interface/Reference.hh"
0028 
0029 // user include files
0030 #include "FWCore/Framework/interface/ModuleFactory.h"
0031 #include "FWCore/Framework/interface/ESProducer.h"
0032 #include "FWCore/Framework/interface/ESProductHost.h"
0033 #include "FWCore/Framework/interface/ESTransientHandle.h"
0034 #include "FWCore/Utilities/interface/ReusableObjectHolder.h"
0035 
0036 #include "CondFormats/JetMETObjects/interface/FFTJetCorrectorParameters.h"
0037 #include "CondFormats/JetMETObjects/interface/FFTJetLUTTypes.h"
0038 
0039 #include "JetMETCorrections/FFTJetObjects/interface/FFTJetLookupTableRcd.h"
0040 #include "JetMETCorrections/FFTJetObjects/interface/FFTJetLookupTableSequence.h"
0041 
0042 typedef std::shared_ptr<npstat::StorableMultivariateFunctor> StorableFunctorPtr;
0043 
0044 static void insertLUTItem(FFTJetLookupTableSequence& seq,
0045                           StorableFunctorPtr fptr,
0046                           const std::string& name,
0047                           const std::string& category) {
0048   FFTJetLookupTableSequence::iterator it = seq.find(category);
0049   if (it == seq.end())
0050     it = seq.insert(std::make_pair(category, FFTJetDict<std::string, StorableFunctorPtr>())).first;
0051   it->second.insert(std::make_pair(name, fptr));
0052 }
0053 
0054 static void buildLookupTables(const FFTJetCorrectorParameters& tablePars,
0055                               const std::vector<edm::ParameterSet>& tableDefs,
0056                               const bool isArchiveCompressed,
0057                               const bool verbose,
0058                               FFTJetLookupTableSequence* ptr) {
0059   // Load the archive stored in the FFTJetCorrectorParameters object
0060   CPP11_auto_ptr<gs::StringArchive> ar;
0061   {
0062     std::istringstream is(tablePars.str());
0063     if (isArchiveCompressed)
0064       ar = gs::read_compressed_item<gs::StringArchive>(is);
0065     else
0066       ar = gs::read_item<gs::StringArchive>(is);
0067   }
0068 
0069   ptr->clear();
0070 
0071   // Avoid loading the same item more than once
0072   std::set<unsigned long long> loadedSet;
0073 
0074   const unsigned nTables = tableDefs.size();
0075   for (unsigned itab = 0; itab < nTables; ++itab) {
0076     const edm::ParameterSet& ps(tableDefs[itab]);
0077     gs::SearchSpecifier nameSearch(ps.getParameter<std::string>("name"), ps.getParameter<bool>("nameIsRegex"));
0078     gs::SearchSpecifier categorySearch(ps.getParameter<std::string>("category"),
0079                                        ps.getParameter<bool>("categoryIsRegex"));
0080     gs::Reference<npstat::StorableMultivariateFunctor> ref(*ar, nameSearch, categorySearch);
0081     const unsigned long nItems = ref.size();
0082     for (unsigned long item = 0; item < nItems; ++item) {
0083       const unsigned long long id = ref.id(item);
0084       if (loadedSet.insert(id).second) {
0085         CPP11_auto_ptr<npstat::StorableMultivariateFunctor> p(ref.get(item));
0086         StorableFunctorPtr fptr(p.release());
0087         std::shared_ptr<const gs::CatalogEntry> e = ar->catalogEntry(id);
0088         insertLUTItem(*ptr, fptr, e->name(), e->category());
0089         if (verbose)
0090           std::cout << "In buildLookupTables: loaded table with name \"" << e->name() << "\" and category \""
0091                     << e->category() << '"' << std::endl;
0092       }
0093     }
0094   }
0095 }
0096 
0097 //
0098 // class declaration
0099 //
0100 template <typename CT>
0101 class FFTJetLookupTableESProducer : public edm::ESProducer {
0102 public:
0103   typedef std::shared_ptr<FFTJetLookupTableSequence> ReturnType;
0104   typedef FFTJetLookupTableRcd<CT> MyRecord;
0105   typedef FFTJetCorrectorParametersRcd<CT> ParentRecord;
0106 
0107   FFTJetLookupTableESProducer(const edm::ParameterSet&);
0108   ~FFTJetLookupTableESProducer() override {}
0109 
0110   ReturnType produce(const MyRecord&);
0111 
0112 private:
0113   // Module parameters
0114   std::vector<edm::ParameterSet> tables;
0115   bool isArchiveCompressed;
0116   bool verbose;
0117 
0118   using HostType = edm::ESProductHost<FFTJetLookupTableSequence, ParentRecord>;
0119   edm::ReusableObjectHolder<HostType> holder_;
0120 };
0121 
0122 //
0123 // constructors and destructor
0124 //
0125 template <typename CT>
0126 FFTJetLookupTableESProducer<CT>::FFTJetLookupTableESProducer(const edm::ParameterSet& psIn)
0127     : tables(psIn.getParameter<std::vector<edm::ParameterSet> >("tables")),
0128       isArchiveCompressed(psIn.getParameter<bool>("isArchiveCompressed")),
0129       verbose(psIn.getUntrackedParameter<bool>("verbose")) {
0130   // The following line is needed to tell the framework what
0131   // data is being produced
0132   setWhatProduced(this);
0133 }
0134 
0135 // ------------ method called to produce the data  ------------
0136 template <typename CT>
0137 typename FFTJetLookupTableESProducer<CT>::ReturnType FFTJetLookupTableESProducer<CT>::produce(const MyRecord& iRecord) {
0138   auto host = holder_.makeOrGet([]() { return new HostType; });
0139 
0140   host->template ifRecordChanges<ParentRecord>(iRecord, [this, product = host.get()](auto const& rec) {
0141     edm::ESTransientHandle<FFTJetCorrectorParameters> parHandle;
0142     rec.get(parHandle);
0143     buildLookupTables(*parHandle, tables, isArchiveCompressed, verbose, product);
0144   });
0145 
0146   return host;
0147 }
0148 
0149 //
0150 // define this as a plug-in
0151 //
0152 typedef FFTJetLookupTableESProducer<fftluttypes::EtaFlatteningFactors> FFTEtaFlatteningFactorsTableESProducer;
0153 typedef FFTJetLookupTableESProducer<fftluttypes::PileupRhoCalibration> FFTPileupRhoCalibrationTableESProducer;
0154 typedef FFTJetLookupTableESProducer<fftluttypes::PileupRhoEtaDependence> FFTPileupRhoEtaDependenceTableESProducer;
0155 typedef FFTJetLookupTableESProducer<fftluttypes::LUT0> FFTLUT0TableESProducer;
0156 typedef FFTJetLookupTableESProducer<fftluttypes::LUT1> FFTLUT1TableESProducer;
0157 typedef FFTJetLookupTableESProducer<fftluttypes::LUT2> FFTLUT2TableESProducer;
0158 typedef FFTJetLookupTableESProducer<fftluttypes::LUT3> FFTLUT3TableESProducer;
0159 typedef FFTJetLookupTableESProducer<fftluttypes::LUT4> FFTLUT4TableESProducer;
0160 typedef FFTJetLookupTableESProducer<fftluttypes::LUT5> FFTLUT5TableESProducer;
0161 typedef FFTJetLookupTableESProducer<fftluttypes::LUT6> FFTLUT6TableESProducer;
0162 typedef FFTJetLookupTableESProducer<fftluttypes::LUT7> FFTLUT7TableESProducer;
0163 typedef FFTJetLookupTableESProducer<fftluttypes::LUT8> FFTLUT8TableESProducer;
0164 typedef FFTJetLookupTableESProducer<fftluttypes::LUT9> FFTLUT9TableESProducer;
0165 typedef FFTJetLookupTableESProducer<fftluttypes::LUT10> FFTLUT10TableESProducer;
0166 typedef FFTJetLookupTableESProducer<fftluttypes::LUT11> FFTLUT11TableESProducer;
0167 typedef FFTJetLookupTableESProducer<fftluttypes::LUT12> FFTLUT12TableESProducer;
0168 typedef FFTJetLookupTableESProducer<fftluttypes::LUT13> FFTLUT13TableESProducer;
0169 typedef FFTJetLookupTableESProducer<fftluttypes::LUT14> FFTLUT14TableESProducer;
0170 typedef FFTJetLookupTableESProducer<fftluttypes::LUT15> FFTLUT15TableESProducer;
0171 
0172 // =========================================================
0173 
0174 DEFINE_FWK_EVENTSETUP_MODULE(FFTEtaFlatteningFactorsTableESProducer);
0175 DEFINE_FWK_EVENTSETUP_MODULE(FFTPileupRhoCalibrationTableESProducer);
0176 DEFINE_FWK_EVENTSETUP_MODULE(FFTPileupRhoEtaDependenceTableESProducer);
0177 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT0TableESProducer);
0178 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT1TableESProducer);
0179 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT2TableESProducer);
0180 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT3TableESProducer);
0181 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT4TableESProducer);
0182 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT5TableESProducer);
0183 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT6TableESProducer);
0184 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT7TableESProducer);
0185 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT8TableESProducer);
0186 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT9TableESProducer);
0187 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT10TableESProducer);
0188 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT11TableESProducer);
0189 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT12TableESProducer);
0190 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT13TableESProducer);
0191 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT14TableESProducer);
0192 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT15TableESProducer);