Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef JetMETCorrections_FFTJetModules_plugins_FFTJetCorrectionESProducer_h
0002 #define JetMETCorrections_FFTJetModules_plugins_FFTJetCorrectionESProducer_h
0003 
0004 // -*- C++ -*-
0005 //
0006 // Package:    FFTJetModules
0007 // Class:      FFTJetCorrectionESProducer
0008 //
0009 /**\class FFTJetCorrectionESProducer FFTJetCorrectionESProducer.h JetMETCorrections/FFTJetModules/plugins/FFTJetCorrectionESProducer.h
0010 
0011  Description: produces the correction sequence
0012 
0013  Implementation:
0014      [Notes on implementation]
0015 */
0016 //
0017 // Original Author:  Igor Volobouev
0018 //         Created:  Thu Aug  2 22:34:02 CDT 2012
0019 //
0020 //
0021 
0022 // system include files
0023 #include <sstream>
0024 
0025 #include <memory>
0026 
0027 #include "Alignment/Geners/interface/CompressedIO.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/FFTJetCorrTypes.h"
0038 
0039 #include "JetMETCorrections/FFTJetObjects/interface/FFTJetCorrectorSequenceRcd.h"
0040 #include "JetMETCorrections/FFTJetObjects/interface/FFTJetCorrectorTransientFromJet.h"
0041 #include "JetMETCorrections/FFTJetObjects/interface/FFTJetCorrectorResultFromTransient.h"
0042 #include "JetMETCorrections/FFTJetObjects/interface/FFTJetCorrectorSequence.h"
0043 
0044 #include "JetMETCorrections/FFTJetModules/interface/FFTJetESParameterParser.h"
0045 
0046 //
0047 // Don't make the function which builds the sequence
0048 // a member of the producer class -- this way it gets
0049 // instantiated only as many times as there are corrector
0050 // sequence types but not as many times as there are
0051 // record types.
0052 //
0053 template <class CorrectorSequence>
0054 static void buildCorrectorSequence(const FFTJetCorrectorParameters& tablePars,
0055                                    const std::vector<edm::ParameterSet>& sequence,
0056                                    const bool isArchiveCompressed,
0057                                    const bool verbose,
0058                                    CorrectorSequence* ptr) {
0059   typedef typename CorrectorSequence::Corrector Corrector;
0060   typedef typename CorrectorSequence::jet_type jet_type;
0061 
0062   // Load the archive stored in the FFTJetCorrectorParameters object
0063   CPP11_auto_ptr<gs::StringArchive> ar;
0064   {
0065     std::istringstream is(tablePars.str());
0066     if (isArchiveCompressed)
0067       ar = gs::read_compressed_item<gs::StringArchive>(is);
0068     else
0069       ar = gs::read_item<gs::StringArchive>(is);
0070   }
0071 
0072   ptr->clear();
0073 
0074   // Go over the parameter sets in the VPSet and
0075   // configure all correction levels. Add each new
0076   // level to the sequence.
0077   const unsigned nLevels = sequence.size();
0078   for (unsigned lev = 0; lev < nLevels; ++lev)
0079     ptr->addCorrector(parseFFTJetCorrector<Corrector>(sequence[lev], *ar, verbose));
0080 
0081   // Check for proper level order.
0082   // Assume that level 0 is special and can happen anywhere.
0083   unsigned previousLevel = 0;
0084   for (unsigned lev = 0; lev < nLevels; ++lev) {
0085     const unsigned level = (*ptr)[lev].level();
0086     if (level) {
0087       if (level <= previousLevel)
0088         throw cms::Exception("FFTJetBadConfig") << "Error in buildCorrectorSequence: "
0089                                                 << "correction levels are out of order\n";
0090       previousLevel = level;
0091     }
0092   }
0093 }
0094 
0095 //
0096 // class declaration
0097 //
0098 template <typename CT>
0099 class FFTJetCorrectionESProducer : public edm::ESProducer {
0100 public:
0101   // Lookup various types
0102   typedef typename FFTJetCorrectionsTypemap<CT>::jet_type jet_type;
0103   typedef FFTJetCorrectorSequence<jet_type, FFTJetCorrectorTransientFromJet, FFTJetCorrectorResultFromTransient>
0104       CorrectorSequence;
0105   typedef std::shared_ptr<CorrectorSequence> ReturnType;
0106   typedef FFTJetCorrectorSequenceRcd<CT> MyRecord;
0107   typedef FFTJetCorrectorParametersRcd<CT> ParentRecord;
0108 
0109   FFTJetCorrectionESProducer(const edm::ParameterSet&);
0110   ~FFTJetCorrectionESProducer() override {}
0111 
0112   ReturnType produce(const MyRecord&);
0113 
0114 private:
0115   // Module parameters
0116   std::vector<edm::ParameterSet> sequence;
0117   bool isArchiveCompressed;
0118   bool verbose;
0119 
0120   using HostType = edm::ESProductHost<CorrectorSequence, ParentRecord>;
0121   edm::ReusableObjectHolder<HostType> holder_;
0122 };
0123 
0124 //
0125 // constructors and destructor
0126 //
0127 template <typename CT>
0128 FFTJetCorrectionESProducer<CT>::FFTJetCorrectionESProducer(const edm::ParameterSet& psIn)
0129     : sequence(psIn.getParameter<std::vector<edm::ParameterSet> >("sequence")),
0130       isArchiveCompressed(psIn.getParameter<bool>("isArchiveCompressed")),
0131       verbose(psIn.getUntrackedParameter<bool>("verbose", false)) {
0132   // The following line is needed to tell the framework what
0133   // data is being produced
0134   setWhatProduced(this);
0135 }
0136 
0137 // ------------ method called to produce the data  ------------
0138 template <typename CT>
0139 typename FFTJetCorrectionESProducer<CT>::ReturnType FFTJetCorrectionESProducer<CT>::produce(const MyRecord& iRecord) {
0140   auto host = holder_.makeOrGet([]() { return new HostType; });
0141 
0142   host->template ifRecordChanges<ParentRecord>(iRecord, [this, product = host.get()](auto const& rec) {
0143     edm::ESTransientHandle<FFTJetCorrectorParameters> parHandle;
0144     rec.get(parHandle);
0145     buildCorrectorSequence<CorrectorSequence>(*parHandle, sequence, isArchiveCompressed, verbose, product);
0146   });
0147 
0148   return host;
0149 }
0150 
0151 #endif  // JetMETCorrections_FFTJetModules_plugins_FFTJetCorrectionESProducer_h