Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-14 11:45:05

0001 #ifndef FWCore_Framework_EventSetup_h
0002 #define FWCore_Framework_EventSetup_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Framework
0006 // Class:      EventSetup
0007 //
0008 /**\class edm::EventSetup
0009 
0010  Description: Container for all Records dealing with non-RunState info
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Author:      Chris Jones
0018 // Created:     Thu Mar 24 13:50:04 EST 2005
0019 //
0020 
0021 // system include files
0022 #include <cassert>
0023 #include <map>
0024 #include <optional>
0025 #include <string>
0026 #include <type_traits>
0027 #include <vector>
0028 
0029 // user include files
0030 #include "FWCore/Framework/interface/ESHandle.h"
0031 #include "FWCore/Framework/interface/ESTransientHandle.h"
0032 #include "FWCore/Framework/interface/EventSetupRecordKey.h"
0033 #include "FWCore/Framework/interface/EventSetupRecord.h"
0034 #include "FWCore/Framework/interface/EventSetupImpl.h"
0035 #include "FWCore/Framework/interface/HCMethods.h"
0036 #include "FWCore/Framework/interface/NoRecordException.h"
0037 #include "FWCore/Framework/interface/IOVSyncValue.h"
0038 #include "FWCore/Framework/interface/data_default_record_trait.h"
0039 #include "FWCore/Utilities/interface/Transition.h"
0040 #include "FWCore/Utilities/interface/ESIndices.h"
0041 #include "FWCore/Utilities/interface/deprecated_macro.h"
0042 
0043 // forward declarations
0044 
0045 namespace edm {
0046 
0047   class ESInputTag;
0048   template <class T, class R>
0049   class ESGetToken;
0050   class PileUp;
0051   class ESParentContext;
0052 
0053   namespace eventsetup {
0054     class EventSetupProvider;
0055     class EventSetupRecord;
0056     class EventSetupRecordImpl;
0057   }  // namespace eventsetup
0058 
0059   class EventSetup {
0060     ///Needed until a better solution can be found
0061     friend class edm::PileUp;
0062 
0063   public:
0064     template <typename T>
0065     explicit EventSetup(T const& info,
0066                         unsigned int iTransitionID,
0067                         ESProxyIndex const* iGetTokenIndices,
0068                         ESParentContext const& iContext)
0069         : EventSetup(info.eventSetupImpl(), iTransitionID, iGetTokenIndices, iContext) {}
0070 
0071     explicit EventSetup(EventSetupImpl const& iSetup,
0072                         unsigned int iTransitionID,
0073                         ESProxyIndex const* iGetTokenIndices,
0074                         ESParentContext const& iContext)
0075         : m_setup{iSetup}, m_getTokenIndices{iGetTokenIndices}, m_context(&iContext), m_id{iTransitionID} {}
0076     EventSetup(EventSetup const&) = delete;
0077     EventSetup& operator=(EventSetup const&) = delete;
0078 
0079     /** returns the Record of type T.  If no such record available
0080           a eventsetup::NoRecordException<T> is thrown */
0081     template <typename T>
0082     T get() const {
0083       using namespace eventsetup;
0084       using namespace eventsetup::heterocontainer;
0085       //NOTE: this will catch the case where T does not inherit from EventSetupRecord
0086       //  HOWEVER the error message under gcc 3.x is awful
0087       static_assert(std::is_base_of_v<edm::eventsetup::EventSetupRecord, T>,
0088                     "Trying to get a class that is not a Record from EventSetup");
0089 
0090       auto const temp = m_setup.findImpl(makeKey<typename type_from_itemtype<eventsetup::EventSetupRecordKey, T>::Type,
0091                                                  eventsetup::EventSetupRecordKey>());
0092       if (nullptr == temp) {
0093         throw eventsetup::NoRecordException<T>(recordDoesExist(m_setup, eventsetup::EventSetupRecordKey::makeKey<T>()));
0094       }
0095       T returnValue;
0096       returnValue.setImpl(temp, m_id, m_getTokenIndices, &m_setup, m_context);
0097       return returnValue;
0098     }
0099 
0100     /** returns the Record of type T.  If no such record available
0101        a null optional is returned */
0102     template <typename T>
0103     std::optional<T> tryToGet() const {
0104       using namespace eventsetup;
0105       using namespace eventsetup::heterocontainer;
0106 
0107       //NOTE: this will catch the case where T does not inherit from EventSetupRecord
0108       static_assert(std::is_base_of_v<edm::eventsetup::EventSetupRecord, T>,
0109                     "Trying to get a class that is not a Record from EventSetup");
0110       auto const temp = impl().findImpl(makeKey<typename type_from_itemtype<eventsetup::EventSetupRecordKey, T>::Type,
0111                                                 eventsetup::EventSetupRecordKey>());
0112       if (temp != nullptr) {
0113         T rec;
0114         rec.setImpl(temp, m_id, m_getTokenIndices, &m_setup, m_context);
0115         return rec;
0116       }
0117       return std::nullopt;
0118     }
0119 
0120     /** can directly access data if data_default_record_trait<> is defined for this data type **/
0121     template <typename T>
0122     CMS_DEPRECATED bool getData(T& iHolder) const {
0123       auto const& rec = this->get<eventsetup::default_record_t<T>>();
0124       return rec.get(std::string{}, iHolder);
0125     }
0126 
0127     template <typename T>
0128     CMS_DEPRECATED bool getData(const std::string& iLabel, T& iHolder) const {
0129       auto const& rec = this->get<eventsetup::default_record_t<T>>();
0130       return rec.get(iLabel, iHolder);
0131     }
0132 
0133     template <typename T>
0134     CMS_DEPRECATED bool getData(const ESInputTag& iTag, T& iHolder) const {
0135       auto const& rec = this->get<eventsetup::default_record_t<T>>();
0136       return rec.get(iTag, iHolder);
0137     }
0138 
0139     template <typename T, typename R>
0140     T const& getData(const ESGetToken<T, R>& iToken) const noexcept(false) {
0141       return this
0142           ->get<std::conditional_t<std::is_same_v<R, edm::DefaultRecord>, eventsetup::default_record_t<ESHandle<T>>, R>>()
0143           .get(iToken);
0144     }
0145     template <typename T, typename R>
0146     T const& getData(ESGetToken<T, R>& iToken) const noexcept(false) {
0147       return this->getData(const_cast<const ESGetToken<T, R>&>(iToken));
0148     }
0149 
0150     template <typename T, typename R>
0151     ESHandle<T> getHandle(const ESGetToken<T, R>& iToken) const {
0152       if constexpr (std::is_same_v<R, edm::DefaultRecord>) {
0153         auto const& rec = this->get<eventsetup::default_record_t<ESHandle<T>>>();
0154         return rec.getHandle(iToken);
0155       } else {
0156         auto const& rec = this->get<R>();
0157         return rec.getHandle(iToken);
0158       }
0159     }
0160 
0161     template <typename T, typename R>
0162     ESTransientHandle<T> getTransientHandle(const ESGetToken<T, R>& iToken) const {
0163       if constexpr (std::is_same_v<R, edm::DefaultRecord>) {
0164         auto const& rec = this->get<eventsetup::default_record_t<ESTransientHandle<T>>>();
0165         return rec.getTransientHandle(iToken);
0166       } else {
0167         auto const& rec = this->get<R>();
0168         return rec.getTransientHandle(iToken);
0169       }
0170     }
0171 
0172     std::optional<eventsetup::EventSetupRecordGeneric> find(const eventsetup::EventSetupRecordKey& iKey) const {
0173       return m_setup.find(iKey, m_id, m_getTokenIndices, *m_context);
0174     }
0175 
0176     ///clears the oToFill vector and then fills it with the keys for all available records
0177     void fillAvailableRecordKeys(std::vector<eventsetup::EventSetupRecordKey>& oToFill) const {
0178       m_setup.fillAvailableRecordKeys(oToFill);
0179     }
0180     ///returns true if the Record is provided by a Source or a Producer
0181     /// a value of true does not mean this EventSetup object holds such a record
0182     bool recordIsProvidedByAModule(eventsetup::EventSetupRecordKey const& iKey) const {
0183       return m_setup.recordIsProvidedByAModule(iKey);
0184     }
0185     // ---------- static member functions --------------------
0186 
0187   private:
0188     edm::EventSetupImpl const& impl() const { return m_setup; }
0189 
0190     // ---------- member data --------------------------------
0191     edm::EventSetupImpl const& m_setup;
0192     ESProxyIndex const* m_getTokenIndices;
0193     ESParentContext const* m_context;
0194     unsigned int m_id;
0195   };
0196 
0197   // Free functions to retrieve an object from the EventSetup.
0198   // Will throw an exception if the record or  object are not found.
0199 
0200   template <typename T, typename R = typename eventsetup::data_default_record_trait<typename T::value_type>::type>
0201   T const& get(EventSetup const& setup) {
0202     ESHandle<T> handle;
0203     // throw if the record is not available
0204     setup.get<R>().get(handle);
0205     // throw if the handle is not valid
0206     return *handle.product();
0207   }
0208 
0209   template <typename T,
0210             typename R = typename eventsetup::data_default_record_trait<typename T::value_type>::type,
0211             typename L>
0212   T const& get(EventSetup const& setup, L&& label) {
0213     ESHandle<T> handle;
0214     // throw if the record is not available
0215     setup.get<R>().get(std::forward(label), handle);
0216     // throw if the handle is not valid
0217     return *handle.product();
0218   }
0219 
0220 }  // namespace edm
0221 
0222 #endif  // FWCore_Framework_EventSetup_h