CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/JetMETCorrections/FFTJetObjects/interface/FFTJetRcdMapper.h

Go to the documentation of this file.
00001 #ifndef JetMETCorrections_FFTJetObjects_FFTJetRcdMapper_h
00002 #define JetMETCorrections_FFTJetObjects_FFTJetRcdMapper_h
00003 
00004 //
00005 // A factory to combat the proliferation of ES record types
00006 // (multiple record types are necessary due to deficiencies
00007 // in the record dependency tracking mechanism). Templated
00008 // upon the data type which records hold.
00009 //
00010 // Igor Volobouev
00011 // 08/03/2012
00012 
00013 #include <map>
00014 #include <string>
00015 
00016 #include "FWCore/Framework/interface/EventSetup.h"
00017 #include "FWCore/Framework/interface/ESHandle.h"
00018 #include "FWCore/Utilities/interface/Exception.h"
00019 
00020 template<class DataType>
00021 struct AbsFFTJetRcdMapper
00022 {
00023     virtual ~AbsFFTJetRcdMapper() {}
00024 
00025     virtual void load(const edm::EventSetup& iSetup,
00026                       edm::ESHandle<DataType>& handle) const = 0;
00027 
00028     virtual void load(const edm::EventSetup& iSetup,
00029                       const std::string& label,
00030                       edm::ESHandle<DataType>& handle) const = 0;
00031 };
00032 
00033 template<class DataType, class RecordType>
00034 struct ConcreteFFTJetRcdMapper : public AbsFFTJetRcdMapper<DataType>
00035 {
00036     virtual ~ConcreteFFTJetRcdMapper() {}
00037 
00038     inline void load(const edm::EventSetup& iSetup,
00039                       edm::ESHandle<DataType>& handle) const
00040         {iSetup.get<RecordType>().get(handle);}
00041 
00042     inline void load(const edm::EventSetup& iSetup,
00043                      const std::string& label,
00044                      edm::ESHandle<DataType>& handle) const
00045         {iSetup.get<RecordType>().get(label, handle);}
00046 };
00047 
00048 template<class DataType>
00049 struct DefaultFFTJetRcdMapper : 
00050     public std::map<std::string, AbsFFTJetRcdMapper<DataType>*>
00051 {
00052     typedef DataType data_type;
00053 
00054     inline DefaultFFTJetRcdMapper()
00055         : std::map<std::string, AbsFFTJetRcdMapper<DataType>*>() {}
00056     
00057     virtual ~DefaultFFTJetRcdMapper()
00058     {
00059         for (typename std::map<std::string, AbsFFTJetRcdMapper<DataType>*>::
00060                  iterator it = this->begin(); it != this->end(); ++it)
00061             delete it->second;
00062     }
00063 
00064     inline void load(const edm::EventSetup& iSetup,
00065                      const std::string& record,
00066                      edm::ESHandle<DataType>& handle) const
00067     {
00068         typename std::map<std::string, AbsFFTJetRcdMapper<DataType>*>::
00069             const_iterator it = this->find(record);
00070         if (it == this->end())
00071             throw cms::Exception("KeyNotFound")
00072                 << "Record \"" << record << "\" is not registered\n";
00073         it->second->load(iSetup, handle);
00074     }
00075 
00076     inline void load(const edm::EventSetup& iSetup,
00077                      const std::string& record,
00078                      const std::string& label,
00079                      edm::ESHandle<DataType>& handle) const
00080     {
00081         typename std::map<std::string, AbsFFTJetRcdMapper<DataType>*>::
00082             const_iterator it = this->find(record);
00083         if (it == this->end())
00084             throw cms::Exception("KeyNotFound")
00085                 << "Record \"" << record << "\" is not registered\n";
00086         it->second->load(iSetup, label, handle);
00087     }
00088 
00089 private:
00090     DefaultFFTJetRcdMapper(const DefaultFFTJetRcdMapper&);
00091     DefaultFFTJetRcdMapper& operator=(const DefaultFFTJetRcdMapper&);
00092 };
00093 
00094 //
00095 // Singleton for the mapper
00096 //
00097 template <class Mapper>
00098 class StaticFFTJetRcdMapper
00099 {
00100 public:
00101     typedef typename Mapper::Base::data_type data_type;
00102 
00103     static const Mapper& instance()
00104     {
00105         static Mapper obj;
00106         return obj;
00107     }
00108 
00109     template <class Record>
00110     static void registerRecord(const std::string& record)
00111     {
00112         Mapper& rd = const_cast<Mapper&>(instance());
00113         delete rd[record];
00114         rd[record] = new ConcreteFFTJetRcdMapper<data_type,Record>();
00115     }
00116 
00117 private:
00118     StaticFFTJetRcdMapper();
00119 };
00120 
00121 #endif // JetMETCorrections_FFTJetObjects_FFTJetRcdMapper_h