ATLAS Offline Software
AthToolSupport/AsgDataHandles/AsgDataHandles/ReadHandle.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /**
6  * @file AsgDataHandles/ReadHandle.icc
7  * @author Nils Krumnack <Nils.Erik.Krumnack@cern.h>
8  * @author S. Binet, P. Calafiura, scott snyder <snyder@bnl.gov> (for original)
9  * @brief Handle class for reading from StoreGate.
10  */
11 
12 #ifndef ASG_DATA_HANDLES_READ_HANDLE_ICC
13 #define ASG_DATA_HANDLES_READ_HANDLE_ICC
14 
15 
16 #include "xAODRootAccessInterfaces/TActiveEvent.h"
17 #include "xAODRootAccessInterfaces/TVirtualEvent.h"
18 #include <stdexcept>
19 
20 
21 namespace SG {
22 
23 
24 //************************************************************************
25 // Constructors, etc.
26 //
27 
28 
29 // /**
30 // * @brief Default constructor.
31 // *
32 // * The handle will not be usable until a non-blank key is assigned.
33 // */
34 // template <class T>
35 // inline
36 // ReadHandle<T>::ReadHandle()
37 // : VarHandleBase(ClassID_traits<T>::ID(), Gaudi::DataHandle::Reader)
38 // {
39 // }
40 
41 
42 /**
43  * @brief Constructor with full arguments.
44  * @param sgkey StoreGate key of the referenced object.
45  */
46 template <class T>
47 inline
48 ReadHandle<T>::ReadHandle(const std::string& sgkey)
49  : VarHandleBase (sgkey)
50 {
51 }
52 
53 
54 /**
55  * @brief Constructor from a ReadHandleKey.
56  * @param key The key object holding the clid/key/store.
57  *
58  * This will raise an exception if the StoreGate key is blank,
59  * or if the event store cannot be found.
60  */
61 template <class T>
62 inline
63 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key)
64  : VarHandleBase (key, nullptr)
65 {
66 }
67 
68 
69 /**
70  * @brief Constructor from a ReadHandleKey and an explicit event context.
71  * @param key The key object holding the clid/key.
72  * @param ctx The event context.
73  *
74  * This will raise an exception if the StoreGate key is blank,
75  * or if the event store cannot be found.
76  *
77  * If the default event store has been requested, then the thread-specific
78  * store from the event context will be used.
79  */
80 template <class T>
81 inline
82 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key,
83  const EventContext& ctx)
84  : VarHandleBase (key, &ctx)
85 {
86 }
87 
88 
89 /**
90  * @brief Dereference the pointer.
91  * Throws ExcNullReadHandle on failure.
92  */
93 template <class T>
94 inline
95 typename ReadHandle<T>::const_pointer_type
96 ReadHandle<T>::operator->()
97 {
98  return checkedCPtr();
99 }
100 
101 
102 /**
103  * @brief Dereference the pointer.
104  * Throws ExcNullReadHandle on failure.
105  */
106 template <class T>
107 inline
108 typename ReadHandle<T>::const_reference_type
109 ReadHandle<T>::operator*()
110 {
111  return *checkedCPtr();
112 }
113 
114 
115 /**
116  * @brief Dereference the pointer.
117  * Returns nullptr on failure.
118  */
119 template <class T>
120 inline
121 typename ReadHandle<T>::const_pointer_type
122 ReadHandle<T>::cptr()
123 {
124  return getCPtr();
125 }
126 
127 
128 /**
129  * @brief Dereference the pointer.
130  * Returns nullptr on failure.
131  */
132 template <class T>
133 inline
134 typename ReadHandle<T>::const_pointer_type
135 ReadHandle<T>::ptr()
136 {
137  return cptr();
138 }
139 
140 
141 // /**
142 // * @brief Return the cached pointer directly; no lookup.
143 // */
144 // template <class T>
145 // inline
146 // typename ReadHandle<T>::const_pointer_type
147 // ReadHandle<T>::cachedPtr() const
148 // {
149 // return reinterpret_cast<const_pointer_type>(this->m_ptr);
150 // }
151 
152 
153 /**
154  * @brief Can the handle be successfully dereferenced?
155  */
156 template <class T>
157 inline
158 bool ReadHandle<T>::isValid()
159 {
160  return isPresent_impl(key());
161 }
162 
163 
164 /**
165  * @brief Dereference the pointer, but don't cache anything.
166  */
167 template <class T>
168 inline
169 typename ReadHandle<T>::const_pointer_type
170 ReadHandle<T>::get() const
171 {
172  return getCPtr();
173 }
174 
175 
176 /**
177  * @brief Dereference the pointer, but don't cache anything.
178  * @param ctx The event context to use.
179  */
180 template <class T>
181 inline
182 typename ReadHandle<T>::const_pointer_type
183 ReadHandle<T>::get (const EventContext& /*ctx*/) const
184 {
185  return cptr();
186 }
187 
188 
189 /**
190 * @brief Is the referenced object present in SG?
191 *
192 * Const method; the handle does not change as a result of this.
193 */
194 template <class T>
195 bool ReadHandle<T>::isPresent() const
196 {
197 return isPresent_impl (key());
198 }
199 
200 
201 /**
202  * @brief Is the referenced object present in SG?
203  * @param key SG key to test.
204  *
205  * Const method; the handle does not change as a result of this.
206  */
207 template <class T>
208 bool ReadHandle<T>::isPresent_impl (const std::string& key) const
209 {
210  const T *result = nullptr;
211  xAOD::TVirtualEvent* event = xAOD::TActiveEvent::event();
212  if(event == nullptr)
213  throw std::runtime_error ("No active event present! ReadHandles cannot be used.");
214  return event->retrieve (result, key, true);
215 }
216 
217 
218 /**
219  * @brief Helper: dereference the pointer.
220  * Throws ExcNullReadHandle on failure.
221  */
222 template <class T>
223 inline
224 typename ReadHandle<T>::const_pointer_type
225 ReadHandle<T>::checkedCPtr()
226 {
227  const_pointer_type p = this->cptr();
228  if (!p)
229  throw std::runtime_error ("failed to read object: " + key());
230  // throwExcNullReadHandle (clid(), key(), store());
231  return p;
232 }
233 
234 
235 /**
236  * @brief Helper: dereference the pointer.
237  * Throws ExcNullReadHandle on failure.
238  */
239 template <class T>
240 inline
241 typename ReadHandle<T>::const_pointer_type
242 ReadHandle<T>::getCPtr() const
243 {
244  const T *result = nullptr;
245  (void) xAOD::TActiveEvent::event()->retrieve (result, key(), true);
246  return result;
247 }
248 
249 
250 /**
251  * @brief Return a @c ReadHandle referencing @c key.
252  * @param key The key object holding the clid/key/store.
253  *
254  * This will raise an exception if the StoreGate key is blank,
255  * or if the event store cannot be found.
256  */
257 template <class T>
258 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key)
259 {
260  return ReadHandle<T> (key);
261 }
262 
263 
264 /**
265  * @brief Return a @c ReadHandle referencing @c key for an explicit context.
266  * @param key The key object holding the clid/key/store.
267  * @param ctx The event context.
268  *
269  * This will raise an exception if the StoreGate key is blank,
270  * or if the event store cannot be found.
271  *
272  * If the default event store has been requested, then the thread-specific
273  * store from the event context will be used.
274  */
275 template <class T>
276 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key,
277  const EventContext& ctx)
278 {
279  return ReadHandle<T> (key, ctx);
280 }
281 
282 
283 /**
284  * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
285  * @param key The key to retrieve.
286  * @param ctx The event context.
287  *
288  * Returns the object. Returns nullptr if the key is null or if there's an error.
289  */
290 template <class T>
291 inline
292 const T* get (const ReadHandleKey<T>& key)
293 {
294  if (key.key().empty()) return nullptr;
295  ReadHandle<T> h (key);
296  return h.get();
297 }
298 
299 
300 /**
301  * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
302  * @param key The key to retrieve.
303  *
304  * Returns the object. Returns nullptr if the key is null or if there's an error.
305  */
306 template <class T>
307 inline
308 const T* get (const ReadHandleKey<T>& key,
309  const EventContext& ctx)
310 {
311  if (key.key().empty()) return nullptr;
312  ReadHandle<T> h (key, ctx);
313  return h.get();
314 }
315 
316 
317 /**
318  * @brief Protected constructor used by WriteDecorHandle.
319  * @param key The key object holding the clid/key.
320  * @param ctx The event context, or nullptr to use the global default.
321  */
322 template <class T>
323 inline
324 ReadHandle<T>::ReadHandle (const VarHandleKey& key, const EventContext* ctx)
325  : VarHandleBase (key, ctx)
326 {
327 }
328 
329 
330 } /* namespace SG */
331 
332 
333 #endif