Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:17

0001 #ifndef JetMETCorrections_FFTJetObjects_FFTJetRcdMapper_h
0002 #define JetMETCorrections_FFTJetObjects_FFTJetRcdMapper_h
0003 
0004 //
0005 // A factory to combat the proliferation of ES record types
0006 // (multiple record types are necessary due to deficiencies
0007 // in the record dependency tracking mechanism). Templated
0008 // upon the data type which records hold.
0009 //
0010 // Igor Volobouev
0011 // 08/03/2012
0012 
0013 #include <map>
0014 #include <string>
0015 
0016 #include "FWCore/Framework/interface/ConsumesCollector.h"
0017 #include "FWCore/Framework/interface/EventSetup.h"
0018 #include "FWCore/Framework/interface/ESHandle.h"
0019 #include "FWCore/Utilities/interface/Exception.h"
0020 #include "FWCore/Utilities/interface/ESInputTag.h"
0021 
0022 template <class DataType>
0023 struct AbsFFTJetRcdMapper {
0024   virtual ~AbsFFTJetRcdMapper() {}
0025 
0026   virtual void acquireToken(edm::ConsumesCollector iC) = 0;
0027 
0028   virtual void acquireToken(edm::ConsumesCollector iC, const std::string& label) = 0;
0029 
0030   virtual edm::ESHandle<DataType> load(const edm::EventSetup& iSetup) const = 0;
0031 };
0032 
0033 template <class DataType, class RecordType>
0034 class ConcreteFFTJetRcdMapper : public AbsFFTJetRcdMapper<DataType> {
0035 public:
0036   ~ConcreteFFTJetRcdMapper() override {}
0037 
0038   inline void acquireToken(edm::ConsumesCollector iC) override {
0039     if (tokenAcquired_)
0040       throw cms::Exception("ESGetTokenAlreadyAcquired");
0041     token_ = iC.esConsumes<DataType, RecordType>();
0042     tokenAcquired_ = true;
0043   }
0044 
0045   inline void acquireToken(edm::ConsumesCollector iC, const std::string& label) override {
0046     if (tokenAcquired_)
0047       throw cms::Exception("ESGetTokenAlreadyAcquired");
0048     token_ = iC.esConsumes<DataType, RecordType>(edm::ESInputTag("", label));
0049     tokenAcquired_ = true;
0050   }
0051 
0052   inline edm::ESHandle<DataType> load(const edm::EventSetup& iSetup) const override {
0053     if (!tokenAcquired_)
0054       throw cms::Exception("ESGetTokenNotAcquired");
0055     return iSetup.getHandle(token_);
0056   }
0057 
0058 private:
0059   edm::ESGetToken<DataType, RecordType> token_;
0060   bool tokenAcquired_ = false;
0061 };
0062 
0063 template <class DataType>
0064 struct DefaultFFTJetRcdMapper : public std::map<std::string, AbsFFTJetRcdMapper<DataType>*> {
0065   typedef DataType data_type;
0066 
0067   inline DefaultFFTJetRcdMapper() : std::map<std::string, AbsFFTJetRcdMapper<DataType>*>() {}
0068 
0069   virtual ~DefaultFFTJetRcdMapper() {
0070     for (typename std::map<std::string, AbsFFTJetRcdMapper<DataType>*>::iterator it = this->begin(); it != this->end();
0071          ++it)
0072       delete it->second;
0073   }
0074 
0075   inline void acquireToken(const std::string& record, edm::ConsumesCollector iC) {
0076     typename std::map<std::string, AbsFFTJetRcdMapper<DataType>*>::iterator it = this->find(record);
0077     if (it == this->end())
0078       throw cms::Exception("KeyNotFound") << "Record \"" << record << "\" is not registered\n";
0079     it->second->acquireToken(iC);
0080   }
0081 
0082   inline void acquireToken(const std::string& record, edm::ConsumesCollector iC, const std::string& label) {
0083     typename std::map<std::string, AbsFFTJetRcdMapper<DataType>*>::iterator it = this->find(record);
0084     if (it == this->end())
0085       throw cms::Exception("KeyNotFound") << "Record \"" << record << "\" is not registered\n";
0086     it->second->acquireToken(iC, label);
0087   }
0088 
0089   inline edm::ESHandle<DataType> load(const std::string& record, const edm::EventSetup& iSetup) const {
0090     typename std::map<std::string, AbsFFTJetRcdMapper<DataType>*>::const_iterator it = this->find(record);
0091     if (it == this->end())
0092       throw cms::Exception("KeyNotFound") << "Record \"" << record << "\" is not registered\n";
0093     return it->second->load(iSetup);
0094   }
0095 
0096   DefaultFFTJetRcdMapper(const DefaultFFTJetRcdMapper&) = delete;
0097   DefaultFFTJetRcdMapper& operator=(const DefaultFFTJetRcdMapper&) = delete;
0098 };
0099 
0100 #endif  // JetMETCorrections_FFTJetObjects_FFTJetRcdMapper_h