File indexing completed on 2024-04-06 12:19:16
0001 #ifndef JetMETCorrections_FFTJetModules_plugins_FFTJetCorrectionESProducer_h
0002 #define JetMETCorrections_FFTJetModules_plugins_FFTJetCorrectionESProducer_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <sstream>
0024
0025 #include <memory>
0026
0027 #include "Alignment/Geners/interface/CompressedIO.hh"
0028
0029
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
0048
0049
0050
0051
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
0063 std::unique_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
0075
0076
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
0082
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
0097
0098 template <typename CT>
0099 class FFTJetCorrectionESProducer : public edm::ESProducer {
0100 public:
0101
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
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 edm::ESGetToken<FFTJetCorrectorParameters, ParentRecord> token_;
0124 };
0125
0126
0127
0128
0129 template <typename CT>
0130 FFTJetCorrectionESProducer<CT>::FFTJetCorrectionESProducer(const edm::ParameterSet& psIn)
0131 : sequence(psIn.getParameter<std::vector<edm::ParameterSet> >("sequence")),
0132 isArchiveCompressed(psIn.getParameter<bool>("isArchiveCompressed")),
0133 verbose(psIn.getUntrackedParameter<bool>("verbose", false)) {
0134
0135
0136 auto cc = setWhatProduced(this);
0137 token_ = cc.consumes();
0138 }
0139
0140
0141 template <typename CT>
0142 typename FFTJetCorrectionESProducer<CT>::ReturnType FFTJetCorrectionESProducer<CT>::produce(const MyRecord& iRecord) {
0143 auto host = holder_.makeOrGet([]() { return new HostType; });
0144
0145 host->template ifRecordChanges<ParentRecord>(iRecord, [this, product = host.get()](auto const& rec) {
0146 auto parHandle = rec.getTransientHandle(token_);
0147 buildCorrectorSequence<CorrectorSequence>(*parHandle, sequence, isArchiveCompressed, verbose, product);
0148 });
0149
0150 return host;
0151 }
0152
0153 #endif