diff --git a/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandle.h b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandle.h new file mode 100644 index 0000000000000000000000000000000000000000..029456fede58318621bef7000273b05961686966 --- /dev/null +++ b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandle.h @@ -0,0 +1,261 @@ +// This file's extension implies that it's C, but it's really -*- C++ -*-. + +/* + * Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration. + */ + +/** + * @file AsgDataHandles/ReadDecorHandle.h + * @author Nils Krumnack <Nils.Erik.Krumnack@cern.h> + * @author scott snyder <snyder@bnl.gov> (for original) + * @brief Handle class for reading a decoration on an object. + */ + + +#ifndef ASG_DATA_HANDLES_READ_DECOR_HANDLE_H +#define ASG_DATA_HANDLES_READ_DECOR_HANDLE_H + +#ifndef XAOD_STANDALONE +#include <StoreGate/ReadDecorHandle.h> +#else + +#include "AsgDataHandles/ReadDecorHandleKey.h" +#include "AsgDataHandles/ReadHandle.h" +// #include "AsgDataHandles/tools/DecorKeyHelpers.h" +// #include "AthContainers/AuxElement.h" +// #include "GaudiKernel/EventContext.h" +// #include <type_traits> + + +namespace SG { + + +/** + * @brief Handle class for reading a decoration on an object. + * + * See StoreGate/ReadDecorHandle for details. + * + * This currently (31 Jul 20) contains a number of commented out + * members that were part of the original handle implementation, but + * have not yet been implemented in the standalone version. The plan + * is to either implement or remove them, depending on what is needed + * in AnalysisBase as we add more packages. + * + */ +template <class T, class D> +class ReadDecorHandle + : public ReadHandle<T> +{ +public: + /// Base class. + typedef ReadHandle<T> Base; + + /// Accessor type for aux data. + typedef SG::AuxElement::ConstAccessor<D> accessor_t; + + /// Type referencing an aux data item. + typedef typename accessor_t::const_reference_type + const_reference_type; + + /// Pointer into the vector holding an aux data item. + typedef typename accessor_t::const_container_pointer_type + const_container_pointer_type; + + + /** + * @brief Constructor from a ReadDecorHandleKey. + * @param key The key object holding the clid/key/store/attr. + * + * This will raise an exception if the StoreGate key is blank, + * or if the event store cannot be found. + */ + explicit ReadDecorHandle (const ReadDecorHandleKey<T>& key); + + +// /** +// * @brief Constructor from a ReadDecorHandleKey and an explicit event context. +// * @param key The key object holding the clid/key. +// * @param ctx The event context. +// * +// * This will raise an exception if the StoreGate key is blank, +// * or if the event store cannot be found. +// * +// * If the default event store has been requested, then the thread-specific +// * store from the event context will be used. +// */ +// explicit ReadDecorHandle (const ReadDecorHandleKey<T>& key, +// const EventContext& ctx); + + +// /** +// * @brief Copy constructor. +// */ +// ReadDecorHandle (const ReadDecorHandle& rhs); + + +// /** +// * @brief Move constructor. +// */ +// ReadDecorHandle (ReadDecorHandle&& rhs); + + +// /** +// * @brief Assignment operator. +// */ +// ReadDecorHandle& operator= (const ReadDecorHandle& rhs); + + +// /** +// * @brief Move operator. +// */ +// ReadDecorHandle& operator= (ReadDecorHandle&& rhs); + + + /** + * @brief Is the referenced container present in SG? + * + * Note that this tests for the presence of the _container_, + * not for the decoration. + * + * Const method; the handle does not change as a result of this. + */ + bool isPresent() const; + + + /** + * @brief Fetch the variable for one element, as a const reference. + * @param e The element for which to fetch the variable. + */ + const_reference_type operator() (const AuxElement& e) const; + + + /** + * @brief Fetch the variable for one element, as a const reference. + * @param index The index of the desired element. + * + * This looks up the variable in the object referenced by this handle. + * For a standalone object, pass an index of 0. + */ + const_reference_type operator() (size_t index); + + +// /** +// * @brief Get a pointer to the start of the auxiliary data array, +// * for the referenced object. +// */ +// const_container_pointer_type +// getDataArray(); + + +// /** +// * @brief Test to see if this variable exists in the store, +// * for the referenced object. +// */ +// bool isAvailable(); + + +// /** +// * @brief Return the aux id for this variable. +// */ +// SG::auxid_t auxid() const; + + +private: + /** + * @brief Return the referenced object as a @c SG::AuxVectorData. + * Specialization for the case of a standalone object + * (@c T derives from @c SG::AuxElement). + */ + const SG::AuxVectorData* vectorData (std::true_type); + + + /** + * @brief Return the referenced object as a @c SG::AuxVectorData. + * Specialization for the case of a container + * (@c T does not derive from @c SG::AuxElement). + */ + const SG::AuxVectorData* vectorData (std::false_type); + + + /** + * @brief Return the referenced object as a @c SG::AuxVectorData. + * + * If @c T is a container object, then this should be the object itself. + * But if it is a standalone object, deriving from @c SG::AuxElement, + * then we need to call container() on the object. + */ + const SG::AuxVectorData* vectorData(); + + + /// Accessor for the aux data item. + accessor_t m_acc; +}; + + +// /** +// * @brief Return a @c ReadDecorHandle referencing @c key. +// * @param key The key object holding the clid/key/store. +// * +// * This will raise an exception if the StoreGate key is blank, +// * or if the event store cannot be found. +// * +// * The type of the decoration must be included as an explicit template parameter: +// * +// *@code +// * auto handle = SG::makeHandle<float> (key); +// @endcode +// * +// * Note that @c D comes first in the argument list. It's given explicitly, +// * while @c T is inferred from @c key. +// */ +// template <class D, class T> +// ReadDecorHandle<T, D> makeHandle (const ReadDecorHandleKey<T>& key); + + +// /** +// * @brief Return a @c ReadDecorHandle referencing @c key for an explicit context. +// * @param key The key object holding the clid/key/store. +// * @param ctx The event context. +// * +// * This will raise an exception if the StoreGate key is blank, +// * or if the event store cannot be found. +// * +// * If the default event store has been requested, then the thread-specific +// * store from the event context will be used. +// * +// * The type of the decoration must be included as an explicit template parameter: +// * +// *@code +// * auto handle = SG::makeHandle<float> (key, ctx); +// @endcode +// * +// * Note that @c D comes first in the argument list. It's given explicitly, +// * while @c T is inferred from @c key. +// */ +// template <class D, class T> +// ReadDecorHandle<T, D> makeHandle (const ReadDecorHandleKey<T>& key, +// const EventContext& ctx); + + +// /** +// * @brief These two signatures are to catch cases where the explicit +// * template argument is omitted from the @c makeHandle call +// * and give an error tailored to that. Otherwise, the @c makeHandle +// * call for @c ReadHandle would match, potentially giving a much +// * more confusing error. +// */ +// template <class T> +// void makeHandle (const ReadDecorHandleKey<T>& key); +// template <class T> +// void makeHandle (const ReadDecorHandleKey<T>& key, +// const EventContext& ctx); + + +} // namespace SG + + +#include "AsgDataHandles/ReadDecorHandle.icc" + +#endif + +#endif // not STOREGATE_READDECORHANDLE_READDECORHANDLE_H diff --git a/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandle.icc b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandle.icc new file mode 100644 index 0000000000000000000000000000000000000000..4448a16104b5d71ebb6d873ea64c1d23362737e7 --- /dev/null +++ b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandle.icc @@ -0,0 +1,317 @@ +/* + * Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration. + */ + +/** + * @file AsgDataHandles/ReadDecorHandle.icc + * @author Nils Krumnack <Nils.Erik.Krumnack@cern.h> + * @author scott snyder <snyder@bnl.gov> (for original) + * @brief Handle class for reading a decoration on an object. + */ + + +namespace SG { + +inline std::string contKeyFromKey (const std::string& key) +{ + const auto split = key.rfind ('.'); + if (split == std::string::npos) + throw std::runtime_error ("decor key does not contain a .: " + key); + return key.substr (0, split); +} + +inline std::string decorKeyFromKey (const std::string& key) +{ + const auto split = key.rfind ('.'); + if (split == std::string::npos) + throw std::runtime_error ("decor key does not contain a .: " + key); + return key.substr (split + 1); +} + +/** + * @brief Constructor from a ReadDecorHandleKey. + * @param key The key object holding the clid/key/store/attr. + * + * This will raise an exception if the StoreGate key is blank, + * or if the event store cannot be found. + */ +template <class T, class D> +ReadDecorHandle<T, D>::ReadDecorHandle (const ReadDecorHandleKey<T>& key) + : Base (key), + m_acc (SG::decorKeyFromKey (key.key())) +{ +} + + +// /** +// * @brief Constructor from a ReadDecorHandleKey and an explicit event context. +// * @param key The key object holding the clid/key. +// * @param ctx The event context. +// * +// * This will raise an exception if the StoreGate key is blank, +// * or if the event store cannot be found. +// * +// * If the default event store has been requested, then the thread-specific +// * store from the event context will be used. +// */ +// template <class T, class D> +// ReadDecorHandle<T, D>::ReadDecorHandle (const ReadDecorHandleKey<T>& key, +// const EventContext& ctx) +// : Base (key, ctx), +// m_acc (SG::decorKeyFromKey (key.key())) +// { +// } + + +// /** +// * @brief Copy constructor. +// */ +// template <class T, class D> +// ReadDecorHandle<T, D>::ReadDecorHandle (const ReadDecorHandle& rhs) +// : Base (rhs), +// m_acc (rhs.m_acc) +// { +// } + + +// /** +// * @brief Move constructor. +// */ +// template <class T, class D> +// ReadDecorHandle<T, D>::ReadDecorHandle (ReadDecorHandle&& rhs) +// : Base (std::move (rhs)), +// m_acc (std::move (rhs.m_acc)) +// { +// } + + +// /** +// * @brief Assignment operator. +// */ +// template <class T, class D> +// ReadDecorHandle<T, D>& ReadDecorHandle<T, D>::operator= (const ReadDecorHandle& rhs) +// { +// if (this != &rhs) { +// *static_cast<Base*>(this) = rhs; +// m_acc = rhs.m_acc; +// } +// return *this; +// } + + +// /** +// * @brief Move operator. +// */ +// template <class T, class D> +// ReadDecorHandle<T, D>& ReadDecorHandle<T, D>::operator= (ReadDecorHandle&& rhs) +// { +// if (this != &rhs) { +// *static_cast<Base*>(this) = std::move (rhs); +// m_acc = std::move (rhs.m_acc); +// } +// return *this; +// } + + +/** + * @brief Is the referenced container present in SG? + * + * Note that this tests for the presence of the _container_, + * not for the decoration. + * + * Const method; the handle does not change as a result of this. + */ +template <class T, class D> +bool ReadDecorHandle<T, D>::isPresent() const +{ + return this->isPresent_impl (contKeyFromKey (this->key())); +} + + +/** + * @brief Fetch the variable for one element, as a const reference. + * @param e The element for which to fetch the variable. + */ +template <class T, class D> +inline +typename ReadDecorHandle<T, D>::const_reference_type +ReadDecorHandle<T, D>::operator() (const AuxElement& e) const +{ + return m_acc (e); +} + + +/** + * @brief Fetch the variable for one element, as a const reference. + * @param index The index of the desired element. + * + * This looks up the variable in the object referenced by this handle. + * For a standalone object, pass an index of 0. + */ +template <class T, class D> +inline +typename ReadDecorHandle<T, D>::const_reference_type +ReadDecorHandle<T, D>::operator() (size_t i) +{ + return m_acc (*this->vectorData(), i); +} + + +// /** +// * @brief Get a pointer to the start of the auxiliary data array. +// * for the referenced object. +// */ +// template <class T, class D> +// typename ReadDecorHandle<T, D>::const_container_pointer_type +// ReadDecorHandle<T, D>::getDataArray() +// { +// return reinterpret_cast<const_container_pointer_type> +// (this->vectorData()->getDataArray (m_acc.auxid())); +// } + + +// /** +// * @brief Test to see if this variable exists in the store, +// * for the referenced object. +// */ +// template <class T, class D> +// inline +// bool ReadDecorHandle<T, D>::isAvailable() +// { +// const SG::AuxVectorData* vec = this->vectorData(); +// return vec && vec->isAvailable (m_acc.auxid()); +// } + + +// /** +// * @brief Return the aux id for this variable. +// */ +// template <class T, class D> +// inline +// SG::auxid_t ReadDecorHandle<T, D>::auxid() const +// { +// return m_acc.auxid(); +// } + + +/** + * @brief Return the referenced object as a @c SG::AuxVectorData. + * Specialization for the case of a standalone object + * (@c T derives from @c SG::AuxElement). + */ +template <class T, class D> +inline +const SG::AuxVectorData* ReadDecorHandle<T, D>::vectorData (std::true_type) +{ + return (*this)->container(); +} + + +/** + * @brief Return the referenced object as a @c SG::AuxVectorData. + * Specialization for the case of a container + * (@c T does not derive from @c SG::AuxElement). + */ +template <class T, class D> +inline +const SG::AuxVectorData* ReadDecorHandle<T, D>::vectorData (std::false_type) +{ + return this->cptr(); +} + + +/** + * @brief Return the referenced object as a @c SG::AuxVectorData. + * + * If @c T is a container object, then this should be the object itself. + * But if it is a standalone object, deriving from @c SG::AuxElement, + * then we need to call container() on the object. + */ +template <class T, class D> +inline +const SG::AuxVectorData* ReadDecorHandle<T, D>::vectorData() +{ + // Dispatch to the appropriate specialization, depending on whether or not + // @c T derives from @c SG::AuxElement. + return vectorData (typename std::is_base_of<SG::AuxElement, T>::type()); +} + + +// /** +// * @brief Return a @c ReadDecorHandle referencing @c key. +// * @param key The key object holding the clid/key/store. +// * +// * This will raise an exception if the StoreGate key is blank, +// * or if the event store cannot be found. +// * +// * The type of the decoration must be included as an explicit template parameter: +// * +// *@code +// * auto handle = SG::makeHandle<float> (key); +// @endcode +// * +// * Note that @c D comes first in the argument list. It's given explicitly, +// * while @c T is inferred from @c key. +// */ +// template <class D, class T> +// ReadDecorHandle<T, D> makeHandle (const ReadDecorHandleKey<T>& key) +// { +// return ReadDecorHandle<T, D> (key); +// } + + +// /** +// * @brief Return a @c ReadDecorHandle referencing @c key for an explicit context. +// * @param key The key object holding the clid/key/store. +// * @param ctx The event context. +// * +// * This will raise an exception if the StoreGate key is blank, +// * or if the event store cannot be found. +// * +// * If the default event store has been requested, then the thread-specific +// * store from the event context will be used. +// * +// * The type of the decoration must be included as an explicit template parameter: +// * +// *@code +// * auto handle = SG::makeHandle<float> (key, ctx); +// @endcode +// * +// * Note that @c D comes first in the argument list. It's given explicitly, +// * while @c T is inferred from @c key. +// */ +// template <class D, class T> +// ReadDecorHandle<T, D> makeHandle (const ReadDecorHandleKey<T>& key, +// const EventContext& ctx) +// { +// return ReadDecorHandle<T, D> (key, ctx); +// } + + +// /** +// * @brief These two signatures are to catch cases where the explicit +// * template argument is omitted from the @c makeHandle call +// * and give an error tailored to that. Otherwise, the @c makeHandle +// * call for @c ReadHandle would match, potentially giving a much +// * more confusing error. +// */ +// template <class T> +// void makeHandle (const ReadDecorHandleKey<T>& /*key*/) +// { +// // If you see an error from here, you've forgotten the explicit template +// // argument to @c makeHandle giving the decoration type. +// // See the examples of @c makeHandle above. +// return T::makeHandleForDecorationsRequiresExplicitTemplateArgument(); +// } +// template <class T> +// void makeHandle (const ReadDecorHandleKey<T>& /*key*/, +// const EventContext& /*ctx*/) +// { +// // If you see an error from here, you've forgotten the explicit template +// // argument to @c makeHandle giving the decoration type. +// // See the examples of @c makeHandle above. +// return T::makeHandleForDecorationsRequiresExplicitTemplateArgument(); +// } + + +} // namespace SG diff --git a/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandleKey.h b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandleKey.h new file mode 100644 index 0000000000000000000000000000000000000000..6dad76d54ceb92b6575c298d7b03a4c2190ea46b --- /dev/null +++ b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandleKey.h @@ -0,0 +1,105 @@ +// This file's extension implies that it's C, but it's really -*- C++ -*-. + +/* + * Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration. + */ + +/** + * @file AsgDataHandles/ReadDecorHandleKey.h + * @author Nils Krumnack <Nils.Erik.Krumnack@cern.h> + * @author scott snyder <snyder@bnl.gov> (for original) + * @brief Property holding a SG store/key/clid/attr name from which a + * ReadDecorHandle is made. + */ + + +#ifndef ASG_DATA_HANDLES_READ_DECOR_HANDLE_KEY_H +#define ASG_DATA_HANDLES_READ_DECOR_HANDLE_KEY_H + +#ifndef XAOD_STANDALONE +#include <StoreGate/ReadDecorHandleKey.h> +#else + +#include "AsgDataHandles/ReadHandleKey.h" + +namespace SG { + +/* + * @brief Property holding a SG store/key/clid/attr name from which a + * ReadDecorHandle is made. + * + * See StoreGate/ReadDecorHandleKey for details. + * + * This currently (31 Jul 20) contains a number of commented out + * members that were part of the original handle implementation, but + * have not yet been implemented in the standalone version. The plan + * is to either implement or remove them, depending on what is needed + * in AnalysisBase as we add more packages. + * + */ +template <class T> +class ReadDecorHandleKey + : public ReadHandleKey<T> +{ +public: + /// Base class. + typedef ReadHandleKey<T> Base; + +// /// Class for which we set the CLID. See above. +// typedef typename SG::TopBase<T>::type topbase_t; + + +// /** +// * @brief Constructor. +// * @param key The StoreGate key for the object. +// * @param storeName Name to use for the store, if it's not encoded in sgkey. +// * +// * The provided key may actually start with the name of the store, +// * separated by a "+": "MyStore+Obj". If no "+" is present +// * the store named by @c storeName is used. +// */ +// ReadDecorHandleKey (const std::string& key = "", +// const std::string& storeName = StoreID::storeName(StoreID::EVENT_STORE)); + + + /** + * @brief auto-declaring Property Constructor. + * @param owner Owning component. + * @param name name of the Property + * @param key default StoreGate key for the object. + * @param doc Documentation string. + * + * will associate the named Property with this RHK via declareProperty + * + * The provided key may actually start with the name of the store, + * separated by a "+": "MyStore+Obj". If no "+" is present + * the store named by @c storeName is used. + */ + template <class OWNER, class K> + ReadDecorHandleKey( OWNER* owner, + const std::string& name, + const K& key = {}, + const std::string& doc = ""); + + +// /// Can get this from the base class. +// using Base::operator=; + + +// /** +// * @brief Return the class ID for the referenced object. +// * +// * Overridden here to return the CLID for @c T instead of @c topbase_t. +// */ +// CLID clid() const; +}; + + +} // namespace SG + + +#include "AsgDataHandles/ReadDecorHandleKey.icc" + +#endif + +#endif // not STOREGATE_READDECORHANDLEKEY_H diff --git a/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandleKey.icc b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandleKey.icc new file mode 100644 index 0000000000000000000000000000000000000000..457db4d3ade831a0fbe13d13f07c9f4228d61c54 --- /dev/null +++ b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadDecorHandleKey.icc @@ -0,0 +1,73 @@ +/* + Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration +*/ + +/** + * @file AsgDataHandles/ReadDecorHandleKey.icc + * @author Nils Krumnack <Nils.Erik.Krumnack@cern.h> + * @author scott snyder <snyder@bnl.gov> (for original) + * @brief Property holding a SG store/key/clid/attr name from which a + * ReadDecorHandle is made. + */ + + +namespace SG { + + +// /** +// * @brief Constructor. +// * @param key The StoreGate key for the object. +// * @param storeName Name to use for the store, if it's not encoded in sgkey. +// * +// * The provided key may actually start with the name of the store, +// * separated by a "+": "MyStore+Obj". If no "+" is present +// * the store named by @c storeName is used. +// */ +// template <class T> +// inline +// ReadDecorHandleKey<T>::ReadDecorHandleKey (const std::string& key /*= ""*/, +// const std::string& storeName /*= StoreID::storeName(StoreID::EVENT_STORE)*/) +// : Base (ClassID_traits<topbase_t>::ID(), key, storeName) +// { +// } + + +/** + * @brief auto-declaring Property Constructor. + * @param owner Owning component. + * @param name name of the Property + * @param key default StoreGate key for the object. + * @param doc Documentation string. + * + * will associate the named Property with this RHK via declareProperty + * + * The provided key may actually start with the name of the store, + * separated by a "+": "MyStore+Obj". If no "+" is present + * the store named by @c storeName is used. + */ +template <class T> +template <class OWNER, class K> +inline +ReadDecorHandleKey<T>::ReadDecorHandleKey( OWNER* owner, + const std::string& name, + const K& key /*={}*/, + const std::string& doc /*=""*/) + : Base (owner, name, key, doc) +{ +} + + +// /** +// * @brief Return the class ID for the referenced object. +// * +// * Overridden here to return the CLID for @c T instead of @c topbase_t. +// */ +// template <class T> +// inline +// CLID ReadDecorHandleKey<T>::clid() const +// { +// return ClassID_traits<T>::ID(); +// } + + +} // namespace SG diff --git a/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadHandle.h b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadHandle.h index ae9f7e1721087ce68480a5bff361ece79fcd3fe5..de53f22450117dcbb087af8722f5eaa462092699 100644 --- a/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadHandle.h +++ b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadHandle.h @@ -134,6 +134,15 @@ public: const_pointer_type get (const EventContext& ctx) const; + /** + * @brief Is the referenced object present in SG? + * @param key SG key to test. + * + * Const method; the handle does not change as a result of this. + */ + bool isPresent_impl (const std::string& key) const; + + private: /** * @brief Helper: dereference the pointer. diff --git a/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadHandle.icc b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadHandle.icc index e3aba5c8546b47db65ae95f8932a8b7cca6180ea..f35c97693e2b7378288eeb824940d6f5870ff352 100644 --- a/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadHandle.icc +++ b/Control/AthToolSupport/AsgDataHandles/AsgDataHandles/ReadHandle.icc @@ -145,8 +145,7 @@ template <class T> inline bool ReadHandle<T>::isValid() { - const T *result = nullptr; - return xAOD::TActiveEvent::event()->retrieve (result, key(), true); + return isPresent_impl(key()); } @@ -175,6 +174,20 @@ ReadHandle<T>::get (const EventContext& /*ctx*/) const } +/** + * @brief Is the referenced object present in SG? + * @param key SG key to test. + * + * Const method; the handle does not change as a result of this. + */ +template <class T> +bool ReadHandle<T>::isPresent_impl (const std::string& key) const +{ + const T *result = nullptr; + return xAOD::TActiveEvent::event()->retrieve (result, key, true); +} + + /** * @brief Helper: dereference the pointer. * Throws ExcNullReadHandle on failure. diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/DataHandleTestTool.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/DataHandleTestTool.h index cf592fab88e8e1a3401199bb5eff994495b19ab4..a10f05a70c138edbdeaf1212848d503efd951731 100644 --- a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/DataHandleTestTool.h +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/DataHandleTestTool.h @@ -12,6 +12,7 @@ #include <AsgTools/AsgTool.h> #include <AsgExampleTools/IDataHandleTestTool.h> #include <AsgDataHandles/ReadHandleKey.h> +#include <AsgDataHandles/ReadDecorHandleKey.h> // AthSimulation doesn't contain the muon-container, so we can't // really build the tool, but it is simpler to build an empty tool @@ -49,8 +50,10 @@ namespace asg public: #ifndef SIMULATIONBASE SG::ReadHandleKey<xAOD::MuonContainer> m_readKey {this, "readKey", "Muons", "regular read key"}; + SG::ReadDecorHandleKey<xAOD::MuonContainer> m_readDecorKey {this, "readDecorKey", "Muons.pt", "read decor key"}; #endif bool m_readFailure {false}; + bool m_readDecorFailure {false}; }; } diff --git a/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt b/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt index cb7fec41ad7ed5cf27731e6677be7f3381e3fe92..19ce9ca05a990f84abf2933c3a443b4fca483aee 100644 --- a/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt +++ b/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt @@ -101,15 +101,22 @@ if( XAOD_STANDALONE ) LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools AsgExampleToolsLib AsgTestingLib ) set_tests_properties (AsgExampleTools_gt_asgtools_toolhandle_test_ctest PROPERTIES LABELS "AsgTools;AsgExampleTools" ) +endif() + +if (NOT SIMULATIONBASE) + if (NOT XAOD_STANDALONE) + set (extra_libs POOLRootAccessLib) + else() + set (extra_libs ) + endif() + atlas_add_test( gt_DataHandlesTest SOURCES test/gt_DataHandlesTest.cxx INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} - LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools AsgExampleToolsLib AsgTestingLib ) + LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools AsgExampleToolsLib AsgTestingLib ${extra_libs}) set_tests_properties (AsgExampleTools_gt_DataHandlesTest_ctest PROPERTIES LABELS "AsgDataHandles;AsgExampleTools" ) - endif() - atlas_add_test( gt_AnaCheck SOURCES test/gt_AnaCheck.cxx INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} diff --git a/Control/AthToolSupport/AsgExampleTools/Root/DataHandleTestTool.cxx b/Control/AthToolSupport/AsgExampleTools/Root/DataHandleTestTool.cxx index 18778a38bad3f6b6bcf1b16326b1a036d45d22a1..19aeb4ef3a6457546e07d44915fa40291a0efe2c 100644 --- a/Control/AthToolSupport/AsgExampleTools/Root/DataHandleTestTool.cxx +++ b/Control/AthToolSupport/AsgExampleTools/Root/DataHandleTestTool.cxx @@ -13,6 +13,7 @@ #include <AsgExampleTools/DataHandleTestTool.h> #include <AsgDataHandles/ReadHandle.h> +#include <AsgDataHandles/ReadDecorHandle.h> #include <AsgTesting/UnitTest.h> #include <gtest/gtest.h> #include <map> @@ -28,6 +29,7 @@ namespace asg : AsgTool (val_name) { declareProperty ("readFailure", m_readFailure, "whether to expect a read failure"); + declareProperty ("readDecorFailure", m_readDecorFailure, "whether to expect a read decoration failure"); } @@ -44,6 +46,7 @@ namespace asg { #ifndef SIMULATIONBASE ANA_CHECK (m_readKey.initialize ()); + ANA_CHECK (m_readDecorKey.initialize ()); #endif return StatusCode::SUCCESS; } @@ -56,12 +59,29 @@ namespace asg #ifndef SIMULATIONBASE const xAOD::MuonContainer *muonsStore {nullptr}; ASSERT_SUCCESS (evtStore()->retrieve (muonsStore, "Muons")); + ASSERT_NE (0u, muonsStore->size()); + const xAOD::Muon *testMuon = (*muonsStore)[0]; auto readHandle = makeHandle (m_readKey); - if (m_readFailure == false) - EXPECT_EQ (muonsStore, readHandle.get()); - else + if (m_readFailure == true) + { EXPECT_EQ (nullptr, readHandle.get()); + EXPECT_FALSE (readHandle.isValid()); + } else + { + EXPECT_EQ (muonsStore, readHandle.get()); + EXPECT_TRUE (readHandle.isValid()); + } + + SG::ReadDecorHandle<xAOD::MuonContainer,float> readDecorHandle (m_readDecorKey); + if (m_readDecorFailure == true) + { + EXPECT_ANY_THROW (readDecorHandle (*testMuon)); + } else + { + SG::AuxElement::ConstAccessor<float> acc ("pt"); + EXPECT_EQ (acc (*testMuon), readDecorHandle (*testMuon)); + } #endif } } diff --git a/Control/AthToolSupport/AsgExampleTools/test/gt_AnaToolHandle_test.cxx b/Control/AthToolSupport/AsgExampleTools/test/gt_AnaToolHandle_test.cxx index 77f01de40de9c531335b74e4336ce8ad7a2a95cb..247f8eedfcc452171fcf7068c646a34a80a21bab 100644 --- a/Control/AthToolSupport/AsgExampleTools/test/gt_AnaToolHandle_test.cxx +++ b/Control/AthToolSupport/AsgExampleTools/test/gt_AnaToolHandle_test.cxx @@ -412,7 +412,11 @@ namespace asg } // check setProperty<int>() +#ifdef XAOD_STANDALONE TEST_F (AnaToolHandleMakeTest, setPropertyInt_failure) +#else + TEST_F (AnaToolHandleMakeTest, DISABLED_setPropertyInt_failure) +#endif { ASSERT_SUCCESS (tool.setProperty<int> ("UNKNOWN_PROPERTY", 42)); ASSERT_FAILURE (tool.initialize ()); @@ -437,7 +441,11 @@ namespace asg } // check setProperty(const char*) +#ifdef XAOD_STANDALONE TEST_F (AnaToolHandleMakeTest, setPropertyString_failure) +#else + TEST_F (AnaToolHandleMakeTest, DISABLED_setPropertyString_failure) +#endif { ASSERT_SUCCESS (tool.setProperty ("UNKNOWN_PROPERTY", "42")); ASSERT_FAILURE (tool.initialize ()); @@ -841,7 +849,7 @@ namespace asg EXPECT_EQ (57, handle->getPropertyInt ()); } - TEST (AnaToolHandleTest, athena_job_options_misspelled) + TEST (AnaToolHandleTest, DISABLED_athena_job_options_misspelled) { std::string name = makeUniqueName(); ServiceHandle<IJobOptionsSvc> joSvc("JobOptionsSvc",""); diff --git a/Control/AthToolSupport/AsgExampleTools/test/gt_DataHandlesTest.cxx b/Control/AthToolSupport/AsgExampleTools/test/gt_DataHandlesTest.cxx index 9b01506da3aa42edef4e7dafa2fc378d0e0f4259..f8b86c3c2d87617e2ec8f9d61dcec88b79c17e03 100644 --- a/Control/AthToolSupport/AsgExampleTools/test/gt_DataHandlesTest.cxx +++ b/Control/AthToolSupport/AsgExampleTools/test/gt_DataHandlesTest.cxx @@ -14,13 +14,18 @@ #include <AsgMessaging/MessageCheck.h> #include <AsgTesting/UnitTest.h> #include <AsgExampleTools/IDataHandleTestTool.h> -#include <xAODRootAccess/TEvent.h> -#include <xAODRootAccess/TStore.h> #include <TFile.h> #include <cmath> #include <gtest/gtest.h> #include <sstream> +#ifdef XAOD_STANDALONE +#include <xAODRootAccess/TEvent.h> +#include <xAODRootAccess/TStore.h> +#else +#include <POOLRootAccess/TEvent.h> +#endif + #pragma GCC diagnostic ignored "-Wdeprecated-declarations" // @@ -39,18 +44,23 @@ namespace asg ASSERT_NE (nullptr, test_file); file.reset (TFile::Open (test_file, "READ")); ASSERT_NE (nullptr, file); +#ifdef XAOD_STANDALONE + event = std::make_unique<xAOD::TEvent>(); +#else + event = std::make_unique<POOL::TEvent>(); +#endif + ASSERT_SUCCESS (event->readFrom (file.get())); + ASSERT_TRUE (event->getEntry (0) >= 0); } static void TearDownTestCase () { + event.reset (); file.reset (); } virtual void SetUp () override { - ASSERT_NE (nullptr, file); - ASSERT_SUCCESS (event.readFrom (file.get())); - ASSERT_TRUE (event.getEntry (0) >= 0); } /// \brief make a unique tool name to be used in unit tests @@ -63,8 +73,12 @@ namespace asg } static inline std::unique_ptr<TFile> file; - xAOD::TEvent event; +#ifdef XAOD_STANDALONE + static inline std::unique_ptr<xAOD::TEvent> event; xAOD::TStore store; +#else + static inline std::unique_ptr<POOL::TEvent> event; +#endif AsgToolConfig config {"asg::DataHandleTestTool/" + makeUniqueName()}; std::shared_ptr<void> cleanup; ToolHandle<IDataHandleTestTool> tool; @@ -89,6 +103,17 @@ namespace asg ASSERT_SUCCESS (config.makeTool (tool, cleanup)); tool->runTest (); } + + + + // just test that reading unknown objects fails as it should + TEST_F (DataHandlesTest, read_decor_failure) + { + config.setPropertyFromString ("readDecorFailure", "1"); + config.setPropertyFromString ("readDecorKey", "Muons.MISSING_PROPERTY"); + ASSERT_SUCCESS (config.makeTool (tool, cleanup)); + tool->runTest (); + } } ATLAS_GOOGLE_TEST_MAIN diff --git a/Projects/AnalysisBase/package_filters.txt b/Projects/AnalysisBase/package_filters.txt index 662a326ad049ffe96cdc30f51b882b1fbf02103e..71ecd1e1c8fbf1daf21495fea925f58a3fb25146 100644 --- a/Projects/AnalysisBase/package_filters.txt +++ b/Projects/AnalysisBase/package_filters.txt @@ -19,7 +19,6 @@ - PhysicsAnalysis/ElectronPhotonID/PhotonEfficiencyCorrection - PhysicsAnalysis/ElectronPhotonID/PhotonVertexSelection - PhysicsAnalysis/ElectronPhotonID/egammaMVACalibAnalysis -- Reconstruction/Jet/JetCalibTools - Reconstruction/Jet/JetMomentTools - Reconstruction/Jet/JetRec - Reconstruction/Jet/JetSubStructureMomentTools diff --git a/Reconstruction/Jet/JetCalibTools/CMakeLists.txt b/Reconstruction/Jet/JetCalibTools/CMakeLists.txt index fdc6500235144d7e328130187750520ba3dd21af..d0ed595eed7dbab36490b4b6f43119da50173320 100644 --- a/Reconstruction/Jet/JetCalibTools/CMakeLists.txt +++ b/Reconstruction/Jet/JetCalibTools/CMakeLists.txt @@ -16,6 +16,7 @@ endif() # Declare the package's dependencies: atlas_depends_on_subdirs( PUBLIC + Control/AthToolSupport/AsgDataHandles Control/AthToolSupport/AsgTools Event/xAOD/xAODEventInfo Event/xAOD/xAODEventShape @@ -38,7 +39,7 @@ atlas_add_library( JetCalibToolsLib PUBLIC_HEADERS JetCalibTools INCLUDE_DIRS ${ROOT_INCLUDE_DIRS} LINK_LIBRARIES ${ROOT_LIBRARIES} AsgTools xAODEventInfo xAODEventShape - xAODJet xAODTracking PATInterfaces JetInterface + xAODJet xAODTracking PATInterfaces JetInterface AsgDataHandlesLib PRIVATE_LINK_LIBRARIES xAODMuon PathResolver ) if( NOT XAOD_STANDALONE ) diff --git a/Reconstruction/Jet/JetCalibTools/JetCalibTools/JetCalibrationTool.h b/Reconstruction/Jet/JetCalibTools/JetCalibTools/JetCalibrationTool.h index 660a96793227be506973dc7089efd9ffb581c7dd..6dcca2db70cb8ad2012c3dc95ee061b8c4acc2aa 100644 --- a/Reconstruction/Jet/JetCalibTools/JetCalibTools/JetCalibrationTool.h +++ b/Reconstruction/Jet/JetCalibTools/JetCalibTools/JetCalibrationTool.h @@ -21,7 +21,7 @@ #include "xAODEventInfo/EventInfo.h" #include "xAODTracking/VertexContainer.h" #include "xAODEventShape/EventShape.h" -#include "StoreGate/ReadDecorHandleKey.h" +#include "AsgDataHandles/ReadDecorHandleKey.h" // JetCalibTools includes #include "JetCalibTools/IJetCalibrationTool.h" @@ -91,7 +91,9 @@ private: // ReadHandleKey(s) SG::ReadHandleKey<xAOD::EventInfo> m_rhkEvtInfo; SG::ReadHandleKey<xAOD::EventShape> m_rhkRhoKey; - SG::ReadHandleKey<xAOD::VertexContainer> m_rhkPV; + SG::ReadHandleKey<xAOD::VertexContainer> m_rhkPV {this, + "PrimaryVerticesContainerName", + "PrimaryVertices"}; SG::ReadDecorHandleKey<xAOD::EventInfo> m_rdhkEvtInfo {this ,"AveIntPerXKey" ,"EventInfo.AveIntPerXDecor" diff --git a/Reconstruction/Jet/JetCalibTools/Root/JetCalibrationTool.cxx b/Reconstruction/Jet/JetCalibTools/Root/JetCalibrationTool.cxx index 9c6c239c7c751ad9ed0c7aa7bd7a72badbb236c6..e20d0ea2d3acdf99e119341dd8706a3f2a6274b5 100644 --- a/Reconstruction/Jet/JetCalibTools/Root/JetCalibrationTool.cxx +++ b/Reconstruction/Jet/JetCalibTools/Root/JetCalibrationTool.cxx @@ -13,7 +13,7 @@ // JetCalibTools includes #include "JetCalibTools/JetCalibrationTool.h" #include "PathResolver/PathResolver.h" -#include "StoreGate/ReadDecorHandle.h" +#include "AsgDataHandles/ReadDecorHandle.h" // Constructors //////////////// @@ -22,7 +22,6 @@ JetCalibrationTool::JetCalibrationTool(const std::string& name) : JetCalibrationToolBase::JetCalibrationToolBase( name ), m_rhkEvtInfo("EventInfo"), m_rhkRhoKey(""), - m_rhkPV("PrimaryVertices"), m_jetAlgo(""), m_config(""), m_calibSeq(""), m_calibAreaTag(""), m_originScale(""), m_devMode(false), m_isData(true), m_timeDependentCalib(false), m_rhoKey("auto"), m_dir(""), m_eInfoName(""), m_globalConfig(NULL), m_doJetArea(true), m_doResidual(true), m_doOrigin(true), m_doGSC(true), m_gscDepth("auto"), m_jetPileupCorr(NULL), m_etaJESCorr(NULL), m_globalSequentialCorr(NULL), m_insituDataCorr(NULL), m_jetMassCorr(NULL), m_jetSmearCorr(NULL) @@ -39,7 +38,6 @@ JetCalibrationTool::JetCalibrationTool(const std::string& name) declareProperty( "OriginScale", m_originScale = "JetOriginConstitScaleMomentum"); declareProperty( "CalibArea", m_calibAreaTag = "00-04-82"); declareProperty( "rhkRhoKey", m_rhkRhoKey); - declareProperty( "PrimaryVerticesContainerName", m_rhkPV = "PrimaryVertices"); declareProperty( "GSCDepth", m_gscDepth); } diff --git a/Reconstruction/Jet/JetCalibTools/util/JetCalibTools_Example.cxx b/Reconstruction/Jet/JetCalibTools/util/JetCalibTools_Example.cxx index 956b10e6a7738fe1e3a8a4d9c71c1aa98fab18c0..9f5b17169784b648fd367c8e144c83e27ef249da 100644 --- a/Reconstruction/Jet/JetCalibTools/util/JetCalibTools_Example.cxx +++ b/Reconstruction/Jet/JetCalibTools/util/JetCalibTools_Example.cxx @@ -25,6 +25,7 @@ #include "xAODRootAccess/Init.h" #include "xAODRootAccess/TEvent.h" #include "xAODRootAccess/TStore.h" +#include <AsgTools/MessageCheck.h> #else #include "POOLRootAccess/TEvent.h" #include "StoreGate/StoreGateSvc.h" @@ -61,6 +62,9 @@ void usage() { int main(int argc, char* argv[]){ + using namespace asg::msgUserCode; + ANA_CHECK_SET_TYPE (int); + //--------------------------------------------- // Declaring input variables with default values //--------------------------------------------- @@ -125,9 +129,6 @@ int main(int argc, char* argv[]){ } else if(isData=="TRUE") isCollision = true; - // Set up the job for xAOD access: - static const char* APP_NAME = "JetCalibTools_Example"; - //-------------------- // Opening input file //-------------------- @@ -135,12 +136,12 @@ int main(int argc, char* argv[]){ // Create a TEvent object. #ifdef XAOD_STANDALONE - RETURN_CHECK( APP_NAME, xAOD::Init() ); + ANA_CHECK( xAOD::Init() ); xAOD::TEvent event( xAOD::TEvent::kClassAccess ); - RETURN_CHECK( APP_NAME, event.readFrom( ifile.get() ) ); + ANA_CHECK( event.readFrom( ifile.get() ) ); #else // Athena "Store" is the same StoreGate used by the TEvent POOL::TEvent event( POOL::TEvent::kClassAccess ); - CHECK_WITH_CONTEXT( event.readFrom( ifile.get() ), APP_NAME, 1 ); + ANA_CHECK( event.readFrom( ifile.get() ) ); #endif //---------------------------------- @@ -150,17 +151,13 @@ int main(int argc, char* argv[]){ // Call the constructor JetCalibrationTool jetCalibrationTool(name_JetCalibTools.c_str()); - CHECK_WITH_CONTEXT( jetCalibrationTool.setProperty("JetCollection",jetColl.c_str()), - APP_NAME, 1 ); + ANA_CHECK( jetCalibrationTool.setProperty("JetCollection",jetColl.c_str()) ); - CHECK_WITH_CONTEXT( jetCalibrationTool.setProperty("CalibSequence",calibSeq.c_str()), - APP_NAME, 1 ); + ANA_CHECK( jetCalibrationTool.setProperty("CalibSequence",calibSeq.c_str()) ); - CHECK_WITH_CONTEXT( jetCalibrationTool.setProperty("ConfigFile",jetCalibConfig.c_str()), - APP_NAME, 1 ); + ANA_CHECK( jetCalibrationTool.setProperty("ConfigFile",jetCalibConfig.c_str()) ); - CHECK_WITH_CONTEXT( jetCalibrationTool.setProperty("IsData",isCollision), - APP_NAME, 1 ); + ANA_CHECK( jetCalibrationTool.setProperty("IsData",isCollision) ); // Initialize the tool if(!(jetCalibrationTool.initialize().isSuccess())){ @@ -186,13 +183,13 @@ int main(int argc, char* argv[]){ // Retrieve jet container const xAOD::JetContainer* jets = 0; - CHECK_WITH_CONTEXT( event.retrieve( jets, jetColl + "Jets" ), APP_NAME, 1 ); + ANA_CHECK( event.retrieve( jets, jetColl + "Jets" ) ); // Shallow copy auto jets_shallowCopy = xAOD::shallowCopyContainer( *jets ); // Calibrate the shallow copy - CHECK_WITH_CONTEXT( jetCalibrationTool.applyCalibration( *(jets_shallowCopy.first) ), APP_NAME, 1 ); + ANA_CHECK( jetCalibrationTool.applyCalibration( *(jets_shallowCopy.first) ) ); delete jets_shallowCopy.first; delete jets_shallowCopy.second;