diff --git a/Control/StoreGate/CMakeLists.txt b/Control/StoreGate/CMakeLists.txt index 0f774c3a3cb863ae411d09b8b64e77c0c4cd3e1f..13715232e0afdd5f2693848daf1ded05db08c43b 100644 --- a/Control/StoreGate/CMakeLists.txt +++ b/Control/StoreGate/CMakeLists.txt @@ -22,161 +22,60 @@ atlas_add_library( SGtests test/SGtests.cxx NO_PUBLIC_HEADERS LINK_LIBRARIES CxxUtils StoreGateLib TestTools ) -# Declare the package's tests:o -atlas_add_test( ActiveStore_test - SOURCES test/ActiveStore_test.cxx - LINK_LIBRARIES SGtests ) +# Declare the package's tests: +# Helper function setting up the test(s) in the package: +function( _add_test name ) + cmake_parse_arguments( ARG "" "LOG_IGNORE_PATTERN" "" ${ARGN} ) + if( ARG_LOG_IGNORE_PATTERN ) + set( extra_arg LOG_IGNORE_PATTERN ${ARG_LOG_IGNORE_PATTERN} ) + endif() + atlas_add_test( ${name} + SOURCES test/${name}.cxx + INCLUDE_DIRS ${Boost_INCLUDE_DIRS} + LINK_LIBRARIES SGtests AthContainers StoreGateLib TestTools + ${extra_arg} ) +endfunction( _add_test ) + +set( _hivePatterns "JobOptionsSvc +INFO|StoreGateSvc.*DEBUG|HiveMgrSvc +DEBUG" ) + + +_add_test( ActiveStore_test ) +_add_test( SGTiming_test LOG_IGNORE_PATTERN "${_hivePatterns}|rec:|averages" ) +_add_test( SGHive_test LOG_IGNORE_PATTERN "${_hivePatterns}" ) +_add_test( DataHandle_test ) +_add_test( SGIterator_test ) +_add_test( KeyConcept_test ) +_add_test( StoreClearedIncident_test ) +_add_test( SegMemSvc_test ) +_add_test( exceptions_test ) +_add_test( VarHandleKey_parseKey_test ) +_add_test( VarHandleKey_test ) +_add_test( VarHandleKeyProperty_test ) +_add_test( VarHandleProperty_test ) +_add_test( ReadHandleKey_test ) +_add_test( WriteHandleKey_test ) +_add_test( UpdateHandleKey_test ) +_add_test( TypelessWriteHandleKey_test ) +_add_test( VarHandleBase_test ) +_add_test( VarHandles_test ) +_add_test( WriteHandle_test ) +_add_test( ReadHandle_test ) +_add_test( UpdateHandle_test ) +_add_test( TypelessWriteHandle_test ) +_add_test( DecorKeyHelpers_test ) +_add_test( ReadDecorHandleKey_test ) +_add_test( WriteDecorHandleKey_test ) +_add_test( ReadDecorHandle_test ) +_add_test( WriteDecorHandle_test ) +_add_test( CondHandleKeyArray_test ) +_add_test( WriteCondHandle_test ) +_add_test( WriteDecorHandleKeyArray_test ) +_add_test( ReadDecorHandleKeyArray_test ) +_add_test( ThinningHandleKey_test ) +_add_test( ThinningHandleBase_test ) +_add_test( ThinningHandle_test ) +_add_test( ShallowCopyDecorDeps_test ) - set( _hivePatterns "JobOptionsSvc +INFO|StoreGateSvc.*DEBUG|HiveMgrSvc +DEBUG" ) - -atlas_add_test( SGTiming_test - SOURCES test/SGTiming_test.cxx - LINK_LIBRARIES SGtests - LOG_IGNORE_PATTERN "${_hivePatterns}|rec:|averages" ) - -atlas_add_test( SGHive_test - SOURCES test/SGHive_test.cxx - LINK_LIBRARIES SGtests - LOG_IGNORE_PATTERN "${_hivePatterns}" ) - -atlas_add_test( DataHandle_test - SOURCES test/DataHandle_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( SGIterator_test - SOURCES test/SGIterator_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( KeyConcept_test - SOURCES test/KeyConcept_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( StoreClearedIncident_test - SOURCES test/StoreClearedIncident_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( SegMemSvc_test - SOURCES test/SegMemSvc_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( exceptions_test - SOURCES test/exceptions_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( VarHandleKey_parseKey_test - SOURCES test/VarHandleKey_parseKey_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( VarHandleKey_test - SOURCES test/VarHandleKey_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( VarHandleKeyProperty_test - SOURCES test/VarHandleKeyProperty_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( VarHandleProperty_test - SOURCES test/VarHandleProperty_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( ReadHandleKey_test - SOURCES test/ReadHandleKey_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( WriteHandleKey_test - SOURCES test/WriteHandleKey_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( UpdateHandleKey_test - SOURCES test/UpdateHandleKey_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( TypelessWriteHandleKey_test - SOURCES test/TypelessWriteHandleKey_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( VarHandleBase_test - SOURCES test/VarHandleBase_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( VarHandles_test - SOURCES test/VarHandles_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( WriteHandle_test - SOURCES test/WriteHandle_test.cxx - LINK_LIBRARIES SGtests AthContainers ) - -atlas_add_test( ReadHandle_test - SOURCES test/ReadHandle_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( UpdateHandle_test - SOURCES test/UpdateHandle_test.cxx - LINK_LIBRARIES SGtests ) - -atlas_add_test( TypelessWriteHandle_test - SOURCES test/TypelessWriteHandle_test.cxx - LINK_LIBRARIES SGtests AthContainers ) - -atlas_add_test( DecorKeyHelpers_test - SOURCES test/DecorKeyHelpers_test.cxx - LINK_LIBRARIES StoreGateLib ) - -atlas_add_test( ReadDecorHandleKey_test - SOURCES test/ReadDecorHandleKey_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib TestTools ) - -atlas_add_test( WriteDecorHandleKey_test - SOURCES test/WriteDecorHandleKey_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib TestTools ) - -atlas_add_test( ReadDecorHandle_test - SOURCES test/ReadDecorHandle_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib AthContainers TestTools ) - -atlas_add_test( WriteDecorHandle_test - SOURCES test/WriteDecorHandle_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib AthContainers TestTools ) - -atlas_add_test( CondHandleKeyArray_test - SOURCES test/CondHandleKeyArray_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib AthContainers TestTools ) - -atlas_add_test( WriteCondHandle_test - SOURCES test/WriteCondHandle_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib AthContainers TestTools ) - -atlas_add_test( WriteDecorHandleKeyArray_test - SOURCES test/WriteDecorHandleKeyArray_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib TestTools ) - -atlas_add_test( ReadDecorHandleKeyArray_test - SOURCES test/ReadDecorHandleKeyArray_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib TestTools ) - -atlas_add_test( ThinningHandleKey_test - SOURCES test/ThinningHandleKey_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib AthContainers TestTools ) - -atlas_add_test( ThinningHandleBase_test - SOURCES test/ThinningHandleBase_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib AthContainers TestTools ) - -atlas_add_test( ThinningHandle_test - SOURCES test/ThinningHandle_test.cxx - INCLUDE_DIRS ${Boost_INCLUDE_DIRS} - LINK_LIBRARIES StoreGateLib AthContainers TestTools ) # Install files from the package: atlas_install_joboptions( share/*.txt diff --git a/Control/StoreGate/StoreGate/ShallowCopyDecorDeps.h b/Control/StoreGate/StoreGate/ShallowCopyDecorDeps.h new file mode 100644 index 0000000000000000000000000000000000000000..3c18fc2528ede1b3e279178773cdf5d0d0e4478c --- /dev/null +++ b/Control/StoreGate/StoreGate/ShallowCopyDecorDeps.h @@ -0,0 +1,129 @@ +// 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 StoreGate/ShallowCopyDecorDeps.h + * @author scott snyder <snyder@bnl.gov> + * @date Nov, 2020 + * @brief Helper to propagate decoration dependencies to a shallow copy. + */ + + +#ifndef STOREGATE_SHALLOWCOPYDECORDEPS_H +#define STOREGATE_SHALLOWCOPYDECORDEPS_H + + +#include "StoreGate/ReadHandleKeyArray.h" +#include "StoreGate/WriteHandleKeyArray.h" +#include "StoreGate/ReadHandle.h" +#include "StoreGate/ReadHandleKey.h" +#include "StoreGate/WriteHandle.h" +#include "StoreGate/WriteHandleKey.h" +#include "AthenaKernel/errorcheck.h" +#include "GaudiKernel/ThreadLocalContext.h" + + +namespace SG { + + +/** + * @brief Helper to propagate decoration dependencies to a shallow copy. + * + * When we do a shallow copy of ORIG to COPY, any decorations of ORIG + * are readable via COPY. However, the scheduler doesn't know about this. + * The scheduler keeps track of decoration dependencies via aliases + * of the form ORIG.DECOR. If one tries to use a ReadDecorHandle + * for COPY.DECOR, the scheduler won't be able to resolve that dependency + * because that alias doesn't exist --- only ORIG.DECOR. + * + * This class will help in propagating the decoration aliases + * from the original object to the shallow copy. The set of decorations + * does need to be specified in advance. + * + * It is assumed that you have a Gaudi component which does the shallow copy, + * and that the original and copy are specified by a ReadHandleKey + * and a WriteHandleKey, respectively. + * + * In the definition of the component class, add a ShallowCopyDecorDeps + * member as a property. The value of the property should be the list + * of decorations to be copied: + * + *@code + * SG::ShallowCopyDecorDeps<MyObj> m_decorDeps + * { this, "DecorDeps", {"decor1", "decor2"}, + * "List of decorations to propagate through the shallow copy." }; + @endcode + * + * In the @c initialize() method, call @c initialize on the + * ShallowCopyDecorDeps object, passing in the read and write + * handle keys for the original object and the shallow copy: + * + *@code + * ATH_CHECK( m_decorDeps.initialize (m_origKey, m_copyKey) ); + @endcode + * + * Finally, after the copy has been recorded, call @c linkDecors, + * passing the read handle key for the original object: + * + *@code + * ATH_CHECK( m_decorDeps.linkDecors (m_origKey, ctx) ); + @endcode + */ +template <class T> +class ShallowCopyDecorDeps +{ +public: + /** + * @brief Auto-declaring Property constructor. + * @param owner Owning component. + * @param name Name of the Property. + * @param l Default list of decorations to propagate. + * @param doc Documentation string. + */ + template <class OWNER> + ShallowCopyDecorDeps (OWNER* owner, + const std::string& name, + std::initializer_list<std::string> l, + const std::string& doc = ""); + + + /** + * @brief Initialize this property. Call this from initialize(). + * @param origKey Key for the source of the shallow copy. + * @param copyKey Key for the result of the shallow copy. + * @param used If false, then this handle is not to be used. + * Instead of normal initialization, the keys will be cleared. + */ + StatusCode initialize (const SG::ReadHandleKey<T>& origKey, + const SG::WriteHandleKey<T>& copyKey, + bool used = true); + + + /** + * @brief Create alias for the decorations, linked to the shallow copy. + * @param origKey Key for the source of the shallow copy. + * @param ctx The current EventContext. + * + * Call this after the shallow copy has been recorded in SG. + */ + StatusCode linkDecors (const SG::ReadHandleKey<T>& origKey, + const EventContext& ctx = Gaudi::Hive::currentContext()) const; + + +private: + /// Keys for the decorations on the original object. + SG::ReadHandleKeyArray<T> m_readKeys; + + /// Keys for decorations to alias for the shallow copy. + SG::WriteHandleKeyArray<T> m_writeKeys; +}; + + +} // namespace SG + + +#include "StoreGate/ShallowCopyDecorDeps.icc" + + +#endif // not STOREGATE_SHALLOWCOPYDECORDEPS_H diff --git a/Control/StoreGate/StoreGate/ShallowCopyDecorDeps.icc b/Control/StoreGate/StoreGate/ShallowCopyDecorDeps.icc new file mode 100644 index 0000000000000000000000000000000000000000..8944a4347caef330d6691c4751dafd72e114e387 --- /dev/null +++ b/Control/StoreGate/StoreGate/ShallowCopyDecorDeps.icc @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration. + */ +/** + * @file StoreGate/ShallowCopyDecorDeps.icc + * @author scott snyder <snyder@bnl.gov> + * @date Nov, 2020 + * @brief Helper to propagate decoration dependencies to a shallow copy. + */ + + +namespace SG { + + +/** + * @brief Auto-declaring Property constructor. + * @param owner Owning component. + * @param name Name of the Property. + * @param l Default list of decorations to propagate. + * @param doc Documentation string. + */ +template <class T> +template <class OWNER> +ShallowCopyDecorDeps<T>::ShallowCopyDecorDeps (OWNER* owner, + const std::string& name, + std::initializer_list<std::string> l, + const std::string& doc /*= ""*/) + : m_readKeys (owner, name + "ReadKeys", {}, "[Internal property]"), + m_writeKeys (owner, name, l, doc) +{ +} + + +/** + * @brief Initialize this property. Call this from initialize(). + * @param origKey Key for the source of the shallow copy. + * @param copyKey Key for the result of the shallow copy. + * @param used If false, then this handle is not to be used. + * Instead of normal initialization, the keys will be cleared. + */ +template <class T> +StatusCode +ShallowCopyDecorDeps<T>::initialize (const SG::ReadHandleKey<T>& origKey, + const SG::WriteHandleKey<T>& copyKey, + bool used /*= true*/) +{ + m_readKeys.clear(); + if (used) { + for (SG::WriteHandleKey<T>& k : m_writeKeys) { + m_readKeys.emplace_back (origKey.key() + "." + k.key()); + k = copyKey.key() + "." + k.key(); + } + CHECK( m_readKeys.initialize() ); + CHECK( m_writeKeys.initialize() ); + } + else { + m_writeKeys.clear(); + } + return StatusCode::SUCCESS; +} + + +/** + * @brief Create alias for the decorations, linked to the shallow copy. + * @param origKey Key for the source of the shallow copy. + * @param ctx The current EventContext. + * + * Call this after the shallow copy has been recorded in SG. + */ +template <class T> +StatusCode +ShallowCopyDecorDeps<T>::linkDecors (const SG::ReadHandleKey<T>& origKey, + const EventContext& ctx /*= Gaudi::Hive::currentContext()*/) const +{ + SG::ReadHandle<T> orig (origKey, ctx); + for (const SG::WriteHandleKey<T>& k : m_writeKeys) { + CHECK( orig.alias (k) ); + } + return StatusCode::SUCCESS; +} + + +} // namespace SG diff --git a/Control/StoreGate/share/ShallowCopyDecorDeps_test.ref b/Control/StoreGate/share/ShallowCopyDecorDeps_test.ref new file mode 100644 index 0000000000000000000000000000000000000000..0c5e0952444d59bdea5ae672068422e9c0c71d7e --- /dev/null +++ b/Control/StoreGate/share/ShallowCopyDecorDeps_test.ref @@ -0,0 +1,12 @@ +StoreGate/ShallowCopyDecorDeps_test +ApplicationMgr SUCCESS +==================================================================================================================================== + Welcome to ApplicationMgr (GaudiCoreSvc v27r1p99) + running on karma on Wed Nov 25 15:44:43 2020 +==================================================================================================================================== +ApplicationMgr INFO Application Manager Configured successfully +EventLoopMgr WARNING Unable to locate service "EventSelector" +EventLoopMgr WARNING No events will be processed from external input. +ApplicationMgr INFO Application Manager Initialized successfully +ApplicationMgr Ready +test1 diff --git a/Control/StoreGate/test/ShallowCopyDecorDeps_test.cxx b/Control/StoreGate/test/ShallowCopyDecorDeps_test.cxx new file mode 100644 index 0000000000000000000000000000000000000000..20d0a9181a28dffb3c676c08ecedcc2810c48e89 --- /dev/null +++ b/Control/StoreGate/test/ShallowCopyDecorDeps_test.cxx @@ -0,0 +1,87 @@ +/* + Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration +*/ +/** + * @file StoreGate/test/ShallowCopyDecorDeps_test.cxx + * @author scott snyder <snyder@bnl.gov> + * @date Nov, 2020 + * @brief Tests for ShallowCopyDecorDeps. + */ + + +#undef NDEBUG +#include "StoreGate/ShallowCopyDecorDeps.h" +#include "StoreGate/StoreGateSvc.h" +#include "SGTools/TestStore.h" +#include "AthenaKernel/ExtendedEventContext.h" +#include "AthenaKernel/CLASS_DEF.h" +#include "TestTools/initGaudi.h" +#include "TestOwner.h" +#include <cassert> +#include <iostream> + + +class MyObj {}; +CLASS_DEF (MyObj, 293847295, 1) + + +void test1 (ISvcLocator* svcloc) +{ + std::cout << "test1\n"; + + TestOwner owner; + SG::ShallowCopyDecorDeps<MyObj> scdd (&owner, "SCDD", {"d1", "d2"}, "doc string"); + SG::ReadHandleKey<MyObj> origKey (&owner, "ReadKey", "myObj"); + SG::WriteHandleKey<MyObj> copyKey (&owner, "WriteKey", "scopy_myObj"); + + assert( origKey.initialize().isSuccess() ); + assert( copyKey.initialize().isSuccess() ); + assert( scdd.initialize (origKey, copyKey) ); + + assert (owner.getProperty ("SCDD").name() == "SCDD"); + assert (owner.getProperty ("SCDD").documentation() == "doc string"); + assert (owner.getProperty ("SCDD").type_info() == &typeid(SG::VarHandleKeyArray)); + assert (owner.getProperty ("SCDD").toString() == "['StoreGateSvc+scopy_myObj.d1','StoreGateSvc+scopy_myObj.d2']"); + assert (owner.getProperty ("SCDD").ownerTypeName() == "TestOwner"); + + assert (owner.getProperty ("SCDDReadKeys").name() == "SCDDReadKeys"); + assert (owner.getProperty ("SCDDReadKeys").documentation() == "[Internal property]"); + assert (owner.getProperty ("SCDDReadKeys").type_info() == &typeid(SG::VarHandleKeyArray)); + assert (owner.getProperty ("SCDDReadKeys").toString() == "['StoreGateSvc+myObj.d1','StoreGateSvc+myObj.d2']"); + assert (owner.getProperty ("SCDDReadKeys").ownerTypeName() == "TestOwner"); + + StoreGateSvc* sg = nullptr; + assert (svcloc->service ("StoreGateSvc", sg).isSuccess()); + assert (sg->record (std::make_unique<MyObj>(), "myObj", false).isSuccess()); + + EventContext ctx; + ctx.setExtension( Atlas::ExtendedEventContext(sg) ); + + assert( scdd.linkDecors (origKey, ctx).isSuccess() ); + + const MyObj* myObj = nullptr; + assert (sg->retrieve (myObj, "myObj").isSuccess()); + assert (myObj != nullptr); + + const MyObj* myObj2 = nullptr; + assert (sg->retrieve (myObj2, "scopy_myObj.d1").isSuccess()); + assert (myObj == myObj2); + + myObj2 = nullptr; + assert (sg->retrieve (myObj2, "scopy_myObj.d2").isSuccess()); + assert (myObj == myObj2); +} + + +int main() +{ + std::cout << "StoreGate/ShallowCopyDecorDeps_test\n"; + + ISvcLocator* svcloc = nullptr; + if (!Athena_test::initGaudi("StoreGate/VarHandleBase_test.txt", svcloc)) { + return 1; + } + + test1 (svcloc); + return 0; +} diff --git a/Control/StoreGate/test/TestOwner.h b/Control/StoreGate/test/TestOwner.h index e289c93db20b7a0b65f775f702b0f5d8551bc18e..cab216579942ca9a82745847e5ee10c14bd2a621 100644 --- a/Control/StoreGate/test/TestOwner.h +++ b/Control/StoreGate/test/TestOwner.h @@ -1,6 +1,6 @@ // This file's extension implies that it's C, but it's really -*- C++ -*-. /* - Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration */ /** * @file StoreGate/test/TestOwner.h @@ -18,6 +18,7 @@ #include "StoreGate/VarHandleKeyProperty.h" +#include "StoreGate/VarHandleKeyArrayProperty.h" #include "GaudiKernel/PropertyHolder.h" #include "GaudiKernel/IProperty.h"