2 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
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.
12#ifndef ASG_DATA_HANDLES_READ_HANDLE_ICC
13#define ASG_DATA_HANDLES_READ_HANDLE_ICC
16#include "xAODRootAccessInterfaces/TActiveEvent.h"
17#include "xAODRootAccessInterfaces/TVirtualEvent.h"
24//************************************************************************
30// * @brief Default constructor.
32// * The handle will not be usable until a non-blank key is assigned.
36// ReadHandle<T>::ReadHandle()
37// : VarHandleBase(ClassID_traits<T>::ID(), Gaudi::DataHandle::Reader)
43 * @brief Constructor with full arguments.
44 * @param sgkey StoreGate key of the referenced object.
48ReadHandle<T>::ReadHandle(const std::string& sgkey)
49 : VarHandleBase (sgkey)
55 * @brief Constructor from a ReadHandleKey.
56 * @param key The key object holding the clid/key/store.
58 * This will raise an exception if the StoreGate key is blank,
59 * or if the event store cannot be found.
63ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key)
64 : VarHandleBase (key, nullptr)
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.
74 * This will raise an exception if the StoreGate key is blank,
75 * or if the event store cannot be found.
77 * If the default event store has been requested, then the thread-specific
78 * store from the event context will be used.
82ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key,
83 const EventContext& ctx)
84 : VarHandleBase (key, &ctx)
90 * @brief Dereference the pointer.
91 * Throws ExcNullReadHandle on failure.
95typename ReadHandle<T>::const_pointer_type
96ReadHandle<T>::operator->()
103 * @brief Dereference the pointer.
104 * Throws ExcNullReadHandle on failure.
108typename ReadHandle<T>::const_reference_type
109ReadHandle<T>::operator*()
111 return *checkedCPtr();
116 * @brief Dereference the pointer.
117 * Returns nullptr on failure.
121typename ReadHandle<T>::const_pointer_type
129 * @brief Dereference the pointer.
130 * Returns nullptr on failure.
134typename ReadHandle<T>::const_pointer_type
142// * @brief Return the cached pointer directly; no lookup.
146// typename ReadHandle<T>::const_pointer_type
147// ReadHandle<T>::cachedPtr() const
149// return reinterpret_cast<const_pointer_type>(this->m_ptr);
154 * @brief Can the handle be successfully dereferenced?
158bool ReadHandle<T>::isValid()
160 return isPresent_impl(key());
165 * @brief Dereference the pointer, but don't cache anything.
169typename ReadHandle<T>::const_pointer_type
170ReadHandle<T>::get() const
177 * @brief Dereference the pointer, but don't cache anything.
178 * @param ctx The event context to use.
182typename ReadHandle<T>::const_pointer_type
183ReadHandle<T>::get (const EventContext& /*ctx*/) const
190* @brief Is the referenced object present in SG?
192* Const method; the handle does not change as a result of this.
195bool ReadHandle<T>::isPresent() const
197return isPresent_impl (key());
202 * @brief Is the referenced object present in SG?
203 * @param key SG key to test.
205 * Const method; the handle does not change as a result of this.
208bool ReadHandle<T>::isPresent_impl (const std::string& key) const
210 const T *result = nullptr;
211 xAOD::TVirtualEvent* event = xAOD::TActiveEvent::event();
213 throw std::runtime_error ("No active event present! ReadHandles cannot be used.");
214 return event->retrieve (result, key, true);
219 * @brief Helper: dereference the pointer.
220 * Throws ExcNullReadHandle on failure.
224typename ReadHandle<T>::const_pointer_type
225ReadHandle<T>::checkedCPtr()
227 const_pointer_type p = this->cptr();
229 throw std::runtime_error ("failed to read object: " + key());
230 // throwExcNullReadHandle (clid(), key(), store());
236 * @brief Helper: dereference the pointer.
237 * Throws ExcNullReadHandle on failure.
241typename ReadHandle<T>::const_pointer_type
242ReadHandle<T>::getCPtr() const
244 const T *result = nullptr;
245 (void) xAOD::TActiveEvent::event()->retrieve (result, key(), true);
251 * @brief Return a @c ReadHandle referencing @c key.
252 * @param key The key object holding the clid/key/store.
254 * This will raise an exception if the StoreGate key is blank,
255 * or if the event store cannot be found.
258ReadHandle<T> makeHandle (const ReadHandleKey<T>& key)
260 return ReadHandle<T> (key);
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.
269 * This will raise an exception if the StoreGate key is blank,
270 * or if the event store cannot be found.
272 * If the default event store has been requested, then the thread-specific
273 * store from the event context will be used.
276ReadHandle<T> makeHandle (const ReadHandleKey<T>& key,
277 const EventContext& ctx)
279 return ReadHandle<T> (key, ctx);
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.
288 * Returns the object. Returns nullptr if the key is null or if there's an error.
292const T* get (const ReadHandleKey<T>& key)
294 if (key.key().empty()) return nullptr;
295 ReadHandle<T> h (key);
301 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
302 * @param key The key to retrieve.
304 * Returns the object. Returns nullptr if the key is null or if there's an error.
308const T* get (const ReadHandleKey<T>& key,
309 const EventContext& ctx)
311 if (key.key().empty()) return nullptr;
312 ReadHandle<T> h (key, ctx);
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.
324ReadHandle<T>::ReadHandle (const VarHandleKey& key, const EventContext* ctx)
325 : VarHandleBase (key, ctx)