CMS 3D CMS Logo

EventSetup.h
Go to the documentation of this file.
1 #ifndef FWCore_Framework_EventSetup_h
2 #define FWCore_Framework_EventSetup_h
3 // -*- C++ -*-
4 //
5 // Package: Framework
6 // Class: EventSetup
7 //
16 //
17 // Author: Chris Jones
18 // Created: Thu Mar 24 13:50:04 EST 2005
19 //
20 
21 // system include files
22 #include <cassert>
23 #include <map>
24 #include <optional>
25 #include <string>
26 #include <type_traits>
27 #include <vector>
28 
29 // user include files
41 
42 // forward declarations
43 
44 namespace edm {
45  class ActivityRegistry;
46  class ESInputTag;
47  template <class T, class R>
48  class ESGetToken;
49  class PileUp;
50 
51  namespace eventsetup {
52  class EventSetupProvider;
53  class EventSetupRecord;
55  } // namespace eventsetup
56 
57  class EventSetup {
59  friend class edm::PileUp;
60 
61  public:
62  explicit EventSetup(EventSetupImpl const& iSetup, unsigned int iTransitionID, ESProxyIndex const* iGetTokenIndices)
63  : m_setup{iSetup}, m_getTokenIndices{iGetTokenIndices}, m_id{iTransitionID} {}
64  EventSetup(EventSetup const&) = delete;
65  EventSetup& operator=(EventSetup const&) = delete;
66 
67  // ---------- const member functions ---------------------
70  template <typename T>
71  T get() const {
72  using namespace eventsetup;
73  using namespace eventsetup::heterocontainer;
74  //NOTE: this will catch the case where T does not inherit from EventSetupRecord
75  // HOWEVER the error message under gcc 3.x is awful
76  static_assert(std::is_base_of_v<edm::eventsetup::EventSetupRecord, T>,
77  "Trying to get a class that is not a Record from EventSetup");
78 
79  auto const temp = m_setup.findImpl(makeKey<typename type_from_itemtype<eventsetup::EventSetupRecordKey, T>::Type,
81  if (nullptr == temp) {
82  throw eventsetup::NoRecordException<T>(recordDoesExist(m_setup, eventsetup::EventSetupRecordKey::makeKey<T>()));
83  }
84  T returnValue;
85  returnValue.setImpl(temp, m_id, m_getTokenIndices);
86  return returnValue;
87  }
88 
91  template <typename T>
92  std::optional<T> tryToGet() const {
93  using namespace eventsetup;
94  using namespace eventsetup::heterocontainer;
95 
96  //NOTE: this will catch the case where T does not inherit from EventSetupRecord
97  static_assert(std::is_base_of_v<edm::eventsetup::EventSetupRecord, T>,
98  "Trying to get a class that is not a Record from EventSetup");
101  if (temp != nullptr) {
102  T rec;
103  rec.setImpl(temp, m_id, m_getTokenIndices);
104  return rec;
105  }
106  return std::nullopt;
107  }
108 
110  template <typename T>
111  bool getData(T& iHolder) const {
112  return getData(std::string{}, iHolder);
113  }
114 
115  template <typename T>
116  bool getData(const std::string& iLabel, T& iHolder) const {
117  auto const& rec = this->get<eventsetup::default_record_t<T>>();
118  return rec.get(iLabel, iHolder);
119  }
120 
121  template <typename T>
122  bool getData(const ESInputTag& iTag, T& iHolder) const {
123  auto const& rec = this->get<eventsetup::default_record_t<T>>();
124  return rec.get(iTag, iHolder);
125  }
126 
127  template <typename T, typename R>
128  T const& getData(const ESGetToken<T, R>& iToken) const noexcept(false) {
129  return this
130  ->get<std::conditional_t<std::is_same_v<R, edm::DefaultRecord>, eventsetup::default_record_t<ESHandle<T>>, R>>()
131  .get(iToken);
132  }
133  template <typename T, typename R>
134  T const& getData(ESGetToken<T, R>& iToken) const noexcept(false) {
135  return this->getData(const_cast<const ESGetToken<T, R>&>(iToken));
136  }
137 
138  template <typename T, typename R>
139  ESHandle<T> getHandle(const ESGetToken<T, R>& iToken) const {
140  if constexpr (std::is_same_v<R, edm::DefaultRecord>) {
141  auto const& rec = this->get<eventsetup::default_record_t<ESHandle<T>>>();
142  return rec.getHandle(iToken);
143  } else {
144  auto const& rec = this->get<R>();
145  return rec.getHandle(iToken);
146  }
147  }
148 
149  template <typename T, typename R>
151  if constexpr (std::is_same_v<R, edm::DefaultRecord>) {
152  auto const& rec = this->get<eventsetup::default_record_t<ESTransientHandle<T>>>();
153  return rec.getTransientHandle(iToken);
154  } else {
155  auto const& rec = this->get<R>();
156  return rec.getTransientHandle(iToken);
157  }
158  }
159 
160  std::optional<eventsetup::EventSetupRecordGeneric> find(const eventsetup::EventSetupRecordKey& iKey) const {
161  return m_setup.find(iKey, m_id, m_getTokenIndices);
162  }
163 
165  void fillAvailableRecordKeys(std::vector<eventsetup::EventSetupRecordKey>& oToFill) const {
166  m_setup.fillAvailableRecordKeys(oToFill);
167  }
171  return m_setup.recordIsProvidedByAModule(iKey);
172  }
173  // ---------- static member functions --------------------
174 
175  private:
176  edm::EventSetupImpl const& impl() const { return m_setup; }
177 
178  // ---------- member data --------------------------------
181  unsigned int m_id;
182  };
183 
184  // Free functions to retrieve an object from the EventSetup.
185  // Will throw an exception if the record or object are not found.
186 
188  T const& get(EventSetup const& setup) {
190  // throw if the record is not available
191  setup.get<R>().get(handle);
192  // throw if the handle is not valid
193  return *handle.product();
194  }
195 
196  template <typename T,
198  typename L>
199  T const& get(EventSetup const& setup, L&& label) {
201  // throw if the record is not available
202  setup.get<R>().get(std::forward(label), handle);
203  // throw if the handle is not valid
204  return *handle.product();
205  }
206 
207 } // namespace edm
208 
209 #endif // FWCore_Framework_EventSetup_h
Key makeKey(const IdTag &iIdTag)
Definition: HCMethods.h:28
std::optional< eventsetup::EventSetupRecordGeneric > find(const eventsetup::EventSetupRecordKey &iKey) const
Definition: EventSetup.h:160
type
Definition: HCALResponse.h:21
std::optional< T > tryToGet() const
Definition: EventSetup.h:92
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:2
def getData(doc, options, dataset, site)
bool recordIsProvidedByAModule(eventsetup::EventSetupRecordKey const &iKey) const
Definition: EventSetup.h:170
ESProxyIndex const * m_getTokenIndices
Definition: EventSetup.h:180
bool getData(const std::string &iLabel, T &iHolder) const
Definition: EventSetup.h:116
bool getData(T &iHolder) const
Definition: EventSetup.h:111
char const * label
typename default_record< T >::RecordT default_record_t
T const & getData(const ESGetToken< T, R > &iToken) const (false)
Definition: EventSetup.h:128
EventSetup(EventSetupImpl const &iSetup, unsigned int iTransitionID, ESProxyIndex const *iGetTokenIndices)
Definition: EventSetup.h:62
#define noexcept
edm::EventSetupImpl const & impl() const
Definition: EventSetup.h:176
edm::EventSetupImpl const & m_setup
Definition: EventSetup.h:179
unsigned int m_id
Definition: EventSetup.h:181
void fillAvailableRecordKeys(std::vector< eventsetup::EventSetupRecordKey > &oToFill) const
clears the oToFill vector and then fills it with the keys for all available records ...
Definition: EventSetup.h:165
bool getData(const ESInputTag &iTag, T &iHolder) const
Definition: EventSetup.h:122
HLT enums.
ESTransientHandle< T > getTransientHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:150
T const & getData(ESGetToken< T, R > &iToken) const (false)
Definition: EventSetup.h:134
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:139
long double T
bool recordDoesExist(edm::EventSetupImpl const &, edm::eventsetup::EventSetupRecordKey const &)
T const * product() const
Definition: ESHandle.h:86
#define constexpr