ATLAS Offline Software
Loading...
Searching...
No Matches
ReadCondHandle.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef STOREGATE_READCONDHANDLE_H
6#define STOREGATE_READCONDHANDLE_H 1
7
10
14
15#include "GaudiKernel/DataObjID.h"
16#include "GaudiKernel/EventIDBase.h"
17#include "GaudiKernel/EventContext.h"
18#include "GaudiKernel/ThreadLocalContext.h"
19
20#include <string>
21#include <stdexcept>
22#include <any>
23
24
25namespace SG {
26
27
34 void ReadCondHandleNotFound (const CondContBase& cc,
35 const EventIDBase& eid,
36 const std::string& key);
37
38 template <typename T>
40
41 public:
42 typedef T* pointer_type; // FIXME: better handling of
43 typedef const T* const_pointer_type; // qualified T type ?
44 typedef T& reference_type;
45 typedef const T& const_reference_type;
46
47 public:
50 const EventContext& ctx);
51
52 ReadCondHandle(SG::ReadCondHandleKey<T>&& key) = delete; // Not allowed from a temporary.
54 const EventContext& ctx) = delete; // Not allowed from a temporary.
55
57
58 const std::string& key() const { return m_hkey.key(); }
59 const DataObjID& fullKey() const { return m_hkey.fullKey(); }
60
62 const_pointer_type retrieve( const EventIDBase& t);
63
67
68
69 bool isValid();
70 bool isValid(const EventIDBase& t) const ;
71
72 bool range(EventIDRange& r);
73 bool range(const EventIDBase& t, EventIDRange& r) const;
74 const EventIDRange& getRange();
75
76 CondCont<T>* getCC() { return m_cc; }
77
78 private:
79
81
82 EventIDBase m_eid;
83 CondCont<T>* m_cc {nullptr};
84 const T* m_obj { nullptr };
85 const EventIDRange* m_range { nullptr };
86
88 };
89
90
91 //---------------------------------------------------------------------------
92
93 template <typename T>
95 ReadCondHandle(key, Gaudi::Hive::currentContext())
96 {
97 // cppcheck-suppress missingReturn; false positive
98 }
99
100 //---------------------------------------------------------------------------
101
102 template <typename T>
104 const EventContext& ctx):
105 m_eid( ctx.eventID() ),
106 m_cc( key.getCC() ),
107 m_hkey(key)
108 {
109 try {
110 EventIDBase::number_type conditionsRun =
112 if (conditionsRun != EventIDBase::UNDEFNUM) {
113 m_eid.set_run_number (conditionsRun);
114 }
115 }
116 catch (const std::bad_any_cast& e) {
117 throw SG::ExcBadContext (ctx, key.objKey());
118 }
119
120 if (!key.isInit()) [[unlikely]] {
121 throw SG::ExcUninitKey (key.clid(), key.key(), key.storeHandle().name(),
122 "", "ReadCond");
123 }
124
125 if (m_cc == 0) [[unlikely]] {
126 // try to retrieve it
127 CondContBase *cb(nullptr);
128 if (m_hkey.getCS()->retrieve(cb, m_hkey.key()).isFailure()) {
129 throw SG::ExcNoCondCont (m_hkey.fullKey().key(), "Can't retrieve.");
130 } else {
131 m_cc = dynamic_cast< CondCont<T>* > (cb);
132 if (m_cc == 0) {
133 throw SG::ExcNoCondCont (m_hkey.fullKey().key(), "Can't dcast CondContBase.");
134 }
135 }
136 }
137 }
138
139 //---------------------------------------------------------------------------
140
141 template <typename T>
142 bool
144
145 if (m_obj != 0) return true;
146
147 if (!m_cc->find(m_eid, m_obj, &m_range)) [[unlikely]] {
149 m_obj = nullptr;
150 return false;
151 }
152
153 return true;
154 }
155
156 //---------------------------------------------------------------------------
157
158 template <typename T>
159 const T*
161
162 if (m_obj == 0) {
163 if (!initCondHandle()) {
164 // std::ostringstream ost;
165 // m_cc->list(ost);
166 // MsgStream msg(Athena::getMessageSvc(), "ReadCondHandle");
167 // msg << MSG::ERROR
168 // << "ReadCondHandle::retrieve() could not find EventTime "
169 // << m_eid << " for key " << objKey() << "\n"
170 // << ost.str()
171 // << endmsg;
172 return nullptr;
173 }
174 }
175
176 return m_obj;
177 }
178
179 //---------------------------------------------------------------------------
180
181 template <typename T>
182 const T*
183 ReadCondHandle<T>::retrieve(const EventIDBase& eid) {
184 if (eid == m_eid) {
185 return retrieve();
186 }
187
188 // pointer_type obj(0);
189 const_pointer_type cobj(0);
190 if (! (m_cc->find(eid, cobj) ) ) {
191 ReadCondHandleNotFound (*m_cc, eid, m_hkey.objKey());
192 return nullptr;
193 }
194
195 // const_pointer_type cobj = const_cast<const_pointer_type>( obj );
196
197 return cobj;
198 }
199
200 //---------------------------------------------------------------------------
201
202 template <typename T>
203 bool
205
206 return initCondHandle();
207 }
208
209 //---------------------------------------------------------------------------
210
211 template <typename T>
212 bool
213 ReadCondHandle<T>::isValid(const EventIDBase& t) const {
214
215 return (m_cc->valid(t));
216 }
217
218 //---------------------------------------------------------------------------
219
220 template <typename T>
221 bool
223
224 if (m_obj == 0) {
225 if (!initCondHandle()) {
226 return false;
227 }
228 }
229
230 if (m_range) {
231 r = *m_range;
232 return true;
233 }
234
235 return false;
236 }
237
238 template <typename T>
239 const EventIDRange&
241
242 if (m_obj == 0) {
243 if (!initCondHandle()) {
245 }
246 }
247
248 if (!m_range) {
249 throw SG::ExcNoRange();
250 }
251 return *m_range;
252
253 }
254
255
256 //---------------------------------------------------------------------------
257
258 template <typename T>
259 bool
260 ReadCondHandle<T>::range(const EventIDBase& eid, EventIDRange& r) const {
261
262 return ( m_cc->range(eid, r) );
263 }
264
265
266 // helper methods to create a read cond handle from the corresponding key.
267 template <class T>
269 const EventContext& ctx = Gaudi::Hive::currentContext()) {
270 return SG::ReadCondHandle<T>(key, ctx);
271 }
272
280 template <class T>
281 const T* get (const ReadCondHandleKey<T>& key,
282 const EventContext& ctx)
283 {
284 if (key.key().empty()) return nullptr;
285 try {
286 ReadCondHandle<T> h (key, ctx);
287 return h.cptr();
288 }
289 catch (SG::ExcNoCondCont&) {
290 return nullptr;
291 }
292 }
293
294
303 template <class T>
304 StatusCode get (const T*& ptr,
305 const ReadCondHandleKey<T>& key,
306 const EventContext& ctx)
307 {
308 ptr = get(key, ctx);
309 return (ptr || key.empty()) ? StatusCode::SUCCESS : StatusCode::FAILURE;
310 }
311
312}
313
314#endif
315
Hold mappings of ranges to condition objects.
Exceptions that can be thrown from StoreGate.
if(pathvar)
Handle class for reading from StoreGate.
Header file for AthHistogramAlgorithm.
EventIDBase::number_type conditionsRun() const
Base class for all conditions containers.
Definition CondCont.h:140
Hold mapping of ranges to condition objects.
Definition CondCont.h:889
Exception — Bad EventContext extension while building ReadCondHandle.
Exception — ReadCondHandle didn't initialize in getRange().
Exception — Can't retrieve CondCont from ReadCondHandle.
Exception — Range not set in ReadCondHandle::getRange().
const_pointer_type operator->()
const_pointer_type operator*()
ReadCondHandle(const SG::ReadCondHandleKey< T > &key)
const_pointer_type retrieve()
bool range(EventIDRange &r)
const EventIDRange * m_range
const SG::ReadCondHandleKey< T > & m_hkey
CondCont< T > * getCC()
ReadCondHandle(SG::ReadCondHandleKey< T > &&key)=delete
const DataObjID & fullKey() const
const T & const_reference_type
CondCont< T > * m_cc
const T * const_pointer_type
ReadCondHandle(SG::ReadCondHandleKey< T > &&key, const EventContext &ctx)=delete
const EventIDRange & getRange()
const_pointer_type cptr()
int r
Definition globals.cxx:22
const ExtendedEventContext & getExtendedEventContext(const EventContext &ctx)
Retrieve an extended context from a context object.
=============================================================================
Forward declaration.
void ReadCondHandleNotFound(const CondContBase &cc, const EventIDBase &eid, const std::string &key)
Report a conditions container lookup failure.
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
#define unlikely(x)