CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/JetMETCorrections/FFTJetModules/plugins/FFTJetLookupTableESProducer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    JetMETCorrections/FFTJetModules
00004 // Class:      FFTJetLookupTableESProducer
00005 // 
00013 //
00014 // Original Author:  Igor Volobouev
00015 //         Created:  Thu Aug  2 22:34:02 CDT 2012
00016 // $Id: FFTJetLookupTableESProducer.cc,v 1.1 2012/11/14 22:34:57 igv Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <sstream>
00023 #include <utility>
00024 
00025 #include "boost/shared_ptr.hpp"
00026 
00027 #include "Alignment/Geners/interface/CompressedIO.hh"
00028 #include "Alignment/Geners/interface/StringArchive.hh"
00029 #include "Alignment/Geners/interface/Reference.hh"
00030 
00031 // user include files
00032 #include "FWCore/Framework/interface/ModuleFactory.h"
00033 #include "FWCore/Framework/interface/ESProducer.h"
00034 #include "FWCore/Framework/interface/ESTransientHandle.h"
00035 
00036 #include "CondFormats/JetMETObjects/interface/FFTJetCorrectorParameters.h"
00037 #include "CondFormats/JetMETObjects/interface/FFTJetLUTTypes.h"
00038 
00039 #include "JetMETCorrections/FFTJetObjects/interface/FFTJetLookupTableRcd.h"
00040 #include "JetMETCorrections/FFTJetObjects/interface/FFTJetLookupTableSequence.h"
00041 
00042 typedef boost::shared_ptr<npstat::StorableMultivariateFunctor> StorableFunctorPtr;
00043 
00044 static void insertLUTItem(FFTJetLookupTableSequence& seq, 
00045                           StorableFunctorPtr fptr,
00046                           const std::string& name,
00047                           const std::string& category)
00048 {
00049     FFTJetLookupTableSequence::iterator it = seq.find(category);
00050     if (it == seq.end())
00051         it = seq.insert(std::make_pair(
00052             category, FFTJetDict<std::string,StorableFunctorPtr>())).first;
00053     it->second.insert(std::make_pair(name, fptr));
00054 }
00055 
00056 static boost::shared_ptr<FFTJetLookupTableSequence>
00057 buildLookupTables(
00058     const FFTJetCorrectorParameters& tablePars,
00059     const std::vector<edm::ParameterSet>& tableDefs,
00060     const bool isArchiveCompressed, const bool verbose)
00061 {
00062     // Load the archive stored in the FFTJetCorrectorParameters object
00063     CPP11_auto_ptr<gs::StringArchive> ar;
00064     {
00065         std::istringstream is(tablePars.str());
00066         if (isArchiveCompressed)
00067             ar = gs::read_compressed_item<gs::StringArchive>(is);
00068         else
00069             ar = gs::read_item<gs::StringArchive>(is);
00070     }
00071 
00072     boost::shared_ptr<FFTJetLookupTableSequence> ptr(
00073         new FFTJetLookupTableSequence());
00074 
00075     // Avoid loading the same item more than once
00076     std::set<unsigned long long> loadedSet;
00077 
00078     const unsigned nTables = tableDefs.size();
00079     for (unsigned itab=0; itab<nTables; ++itab)
00080     {
00081         const edm::ParameterSet& ps(tableDefs[itab]);
00082         gs::SearchSpecifier nameSearch(ps.getParameter<std::string>("name"),
00083                                        ps.getParameter<bool>("nameIsRegex"));
00084         gs::SearchSpecifier categorySearch(ps.getParameter<std::string>("category"),
00085                                            ps.getParameter<bool>("categoryIsRegex"));
00086         gs::Reference<npstat::StorableMultivariateFunctor> ref(
00087             *ar, nameSearch, categorySearch);
00088         const unsigned long nItems = ref.size();
00089         for (unsigned long item=0; item<nItems; ++item)
00090         {
00091             const unsigned long long id = ref.id(item);
00092             if (loadedSet.insert(id).second)
00093             {
00094                 CPP11_auto_ptr<npstat::StorableMultivariateFunctor> p(ref.get(item));
00095                 StorableFunctorPtr fptr(p.release());
00096                 CPP11_shared_ptr<const gs::CatalogEntry> e = ar->catalogEntry(id);
00097                 insertLUTItem(*ptr, fptr, e->name(), e->category());
00098                 if (verbose)
00099                     std::cout << "In buildLookupTables: loaded table with name \""
00100                               << e->name() << "\" and category \""
00101                               << e->category() << '"' << std::endl;
00102             }
00103         }
00104     }
00105 
00106     return ptr;
00107 }
00108 
00109 //
00110 // class declaration
00111 //
00112 template<typename CT>
00113 class FFTJetLookupTableESProducer : public edm::ESProducer
00114 {
00115 public:
00116     typedef boost::shared_ptr<FFTJetLookupTableSequence> ReturnType;
00117     typedef FFTJetLookupTableRcd<CT> MyRecord;
00118     typedef FFTJetCorrectorParametersRcd<CT> ParentRecord;
00119 
00120     FFTJetLookupTableESProducer(const edm::ParameterSet&);
00121     virtual ~FFTJetLookupTableESProducer() {}
00122 
00123     ReturnType produce(const MyRecord&);
00124 
00125 private:
00126     inline void doWhenChanged(const ParentRecord&)
00127         {remakeProduct = true;}
00128 
00129     // Module parameters
00130     std::vector<edm::ParameterSet> tables;
00131     bool isArchiveCompressed;
00132     bool verbose;
00133 
00134     // Other module variables
00135     bool remakeProduct;
00136     ReturnType product;
00137 };
00138 
00139 //
00140 // constructors and destructor
00141 //
00142 template<typename CT>
00143 FFTJetLookupTableESProducer<CT>::FFTJetLookupTableESProducer(
00144     const edm::ParameterSet& psIn)
00145     : tables(psIn.getParameter<std::vector<edm::ParameterSet> >("tables")),
00146       isArchiveCompressed(psIn.getParameter<bool>("isArchiveCompressed")),
00147       verbose(psIn.getUntrackedParameter<bool>("verbose")),
00148       remakeProduct(true)
00149 {
00150     // The following line is needed to tell the framework what
00151     // data is being produced
00152     setWhatProduced(this, dependsOn(&FFTJetLookupTableESProducer::doWhenChanged));
00153 }
00154 
00155 // ------------ method called to produce the data  ------------
00156 template<typename CT>
00157 typename FFTJetLookupTableESProducer<CT>::ReturnType
00158 FFTJetLookupTableESProducer<CT>::produce(const MyRecord& iRecord)
00159 {
00160     if (remakeProduct)
00161     {
00162         // According to:
00163         // https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideHowToGetDependentRecord
00164         //
00165         // If ARecord is dependent on BRecord then you can
00166         // call the method getRecord of ARecord:
00167         //
00168         // const BRecord& b = aRecord.getRecord<BRecord>();
00169         //
00170         const ParentRecord& rec = iRecord.template getRecord<ParentRecord>();
00171         edm::ESTransientHandle<FFTJetCorrectorParameters> parHandle;
00172         rec.get(parHandle);
00173         product = buildLookupTables(
00174             *parHandle, tables, isArchiveCompressed, verbose);
00175         remakeProduct = false;
00176     }
00177     return product;
00178 }
00179 
00180 //
00181 // define this as a plug-in
00182 //
00183 typedef FFTJetLookupTableESProducer<fftluttypes::EtaFlatteningFactors> FFTEtaFlatteningFactorsTableESProducer;
00184 typedef FFTJetLookupTableESProducer<fftluttypes::PileupRhoCalibration> FFTPileupRhoCalibrationTableESProducer;
00185 typedef FFTJetLookupTableESProducer<fftluttypes::PileupRhoEtaDependence> FFTPileupRhoEtaDependenceTableESProducer;
00186 typedef FFTJetLookupTableESProducer<fftluttypes::LUT0>  FFTLUT0TableESProducer;
00187 typedef FFTJetLookupTableESProducer<fftluttypes::LUT1>  FFTLUT1TableESProducer;
00188 typedef FFTJetLookupTableESProducer<fftluttypes::LUT2>  FFTLUT2TableESProducer;
00189 typedef FFTJetLookupTableESProducer<fftluttypes::LUT3>  FFTLUT3TableESProducer;
00190 typedef FFTJetLookupTableESProducer<fftluttypes::LUT4>  FFTLUT4TableESProducer;
00191 typedef FFTJetLookupTableESProducer<fftluttypes::LUT5>  FFTLUT5TableESProducer;
00192 typedef FFTJetLookupTableESProducer<fftluttypes::LUT6>  FFTLUT6TableESProducer;
00193 typedef FFTJetLookupTableESProducer<fftluttypes::LUT7>  FFTLUT7TableESProducer;
00194 typedef FFTJetLookupTableESProducer<fftluttypes::LUT8>  FFTLUT8TableESProducer;
00195 typedef FFTJetLookupTableESProducer<fftluttypes::LUT9>  FFTLUT9TableESProducer;
00196 typedef FFTJetLookupTableESProducer<fftluttypes::LUT10> FFTLUT10TableESProducer;
00197 typedef FFTJetLookupTableESProducer<fftluttypes::LUT11> FFTLUT11TableESProducer;
00198 typedef FFTJetLookupTableESProducer<fftluttypes::LUT12> FFTLUT12TableESProducer;
00199 typedef FFTJetLookupTableESProducer<fftluttypes::LUT13> FFTLUT13TableESProducer;
00200 typedef FFTJetLookupTableESProducer<fftluttypes::LUT14> FFTLUT14TableESProducer;
00201 typedef FFTJetLookupTableESProducer<fftluttypes::LUT15> FFTLUT15TableESProducer;
00202 
00203 // =========================================================
00204 
00205 DEFINE_FWK_EVENTSETUP_MODULE(FFTEtaFlatteningFactorsTableESProducer);
00206 DEFINE_FWK_EVENTSETUP_MODULE(FFTPileupRhoCalibrationTableESProducer);
00207 DEFINE_FWK_EVENTSETUP_MODULE(FFTPileupRhoEtaDependenceTableESProducer);
00208 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT0TableESProducer);
00209 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT1TableESProducer);
00210 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT2TableESProducer);
00211 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT3TableESProducer);
00212 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT4TableESProducer);
00213 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT5TableESProducer);
00214 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT6TableESProducer);
00215 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT7TableESProducer);
00216 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT8TableESProducer);
00217 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT9TableESProducer);
00218 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT10TableESProducer);
00219 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT11TableESProducer);
00220 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT12TableESProducer);
00221 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT13TableESProducer);
00222 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT14TableESProducer);
00223 DEFINE_FWK_EVENTSETUP_MODULE(FFTLUT15TableESProducer);