ATLAS Offline Software
TestStore.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 /*
3  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 */
13 #ifndef SGTOOLS_TESTSTORE_H
14 #define SGTOOLS_TESTSTORE_H
15 
16 
18 #include "SGTools/StringPool.h"
19 #include "SGTools/DataProxy.h"
22 #include <unordered_map>
23 #include <mutex>
24 
25 
26 namespace SGTest {
27 
28 
30 
31 
33 {
34  TestStoreRemap (SG::sgkey_t the_key=0, size_t the_index = 0)
35  : key (the_key), index (the_index) {}
36  bool operator== (const TestStoreRemap& other) const
37  { return other.key == key && other.index == index; }
39  size_t index;
40 };
41 
42 
44 {
45  size_t operator() (const TestStoreRemap& m) const
46  { return m.key + m.index; }
47 };
48 
49 
50 class TestStore
51  : public implements<IProxyDict>
52 {
53 public:
54  virtual std::vector<const SG::DataProxy*> proxies() const override;
55  virtual const std::string* keyToString (sgkey_t /*key*/) const override;
56  virtual void registerKey (sgkey_t /*key*/,
57  const std::string& /*str*/,
58  CLID /*clid*/) override;
59 
60 
61  // These have dummy implementations.
62  virtual const std::string& name() const override;
63  virtual SG::DataProxy* proxy(const void* const pTransient) const override;
64  virtual SG::DataProxy* proxy(const CLID& id, const std::string& key) const override;
65  virtual SG::DataProxy* proxy_exact (SG::sgkey_t sgkey) const override;
66  virtual sgkey_t stringToKey (const std::string& str, CLID clid) override;
67  virtual const std::string* keyToString (sgkey_t key, CLID& clid) const override;
68  virtual bool tryELRemap (sgkey_t sgkey_in, size_t index_in,
69  sgkey_t& sgkey_out, size_t& index_out) override;
70  virtual StatusCode addToStore (CLID /*id*/, SG::DataProxy* proxy) override;
71  virtual void boundHandle (IResetable* handle) override;
72  virtual void unboundHandle (IResetable* handle) override;
73 
75  const std::string& key,
76  bool allowMods,
77  bool returnExisting) override;
78 
79 
80  template <class T>
81  void record (const T* p, const std::string& key)
82  {
83  lock_t lock (m_mutex);
84  T* p_nc ATLAS_THREAD_SAFE = const_cast<T*>(p);
85  DataObject* obj = SG::asStorable<T>(p_nc);
86  CLID clid = ClassID_traits<T>::ID();
87  record1 (lock, p, obj, clid, key);
88  }
89 
90 
91  template <class T>
92  void record (std::unique_ptr<T> up, const std::string& key)
93  {
94  lock_t lock (m_mutex);
95  T* p = up.get();
96  DataObject* obj = SG::asStorable<T>(std::move (up));
97  CLID clid = ClassID_traits<T>::ID();
98  record1 (lock, p, obj, clid, key);
99  }
100 
101 
102  void remap (sgkey_t sgkey_in, sgkey_t sgkey_out,
103  size_t index_in = 0, size_t index_out = 0);
104 
105 
106  template <class T>
107  void remap (const std::string& key_in, const std::string& key_out,
108  size_t index_in = 0, size_t index_out = 0)
109  {
110  CLID clid = ClassID_traits<T>::ID();
111  sgkey_t sgkey_in = stringToKey (key_in, clid);
112  sgkey_t sgkey_out = stringToKey (key_out, clid);
113  remap (sgkey_in, sgkey_out, index_in, index_out);
114  }
115 
116  void alias (SG::DataProxy* proxy, const std::string& newKey);
117 
118  typedef std::unordered_map<const void*, SG::DataProxy*> tmap_t;
120 
123 
124  typedef std::unordered_map<TestStoreRemap, TestStoreRemap, TestStoreRemapHash> remap_t;
126 
127  mutable SG::StringPool m_stringPool ATLAS_THREAD_SAFE;
128 
129  std::vector<IResetable*> m_boundHandles;
130 
131  // Log failed calls to proxy(CLID, std::string).
132  mutable std::vector<std::pair<CLID, std::string> > m_missedProxies ATLAS_THREAD_SAFE;
133 
135  typedef std::lock_guard<mutex_t> lock_t;
136  mutable mutex_t m_mutex;
137 
138  SG::DataProxy* proxy(lock_t&, const void* const pTransient) const;
139  SG::DataProxy* proxy(lock_t&, const CLID& id, const std::string& key) const;
140  SG::DataProxy* record1 (lock_t&, const void* p, DataObject* obj,
141  CLID clid, const std::string& key);
142 };
143 
144 
145 // Initialize the global, static test store.
146 extern TestStore store;
147 void initTestStore ATLAS_NOT_THREAD_SAFE ();
148 
149 
150 // Create a new test store and install it.
151 std::unique_ptr<TestStore> getTestStore();
152 
153 
154 } // namespace SGTest
155 
156 
157 #endif // not SGTOOLS_TESTSTORE_H
SGTest::TestStore::mutex_t
std::mutex mutex_t
Definition: TestStore.h:134
SGTest::store
TestStore store
Definition: TestStore.cxx:23
common.sgkey
def sgkey(tool)
Definition: common.py:1028
CurrentEventStore.h
Hold a pointer to the current event store.
SGTest
Definition: TestStore.h:26
SGTest::TestStoreRemap::TestStoreRemap
TestStoreRemap(SG::sgkey_t the_key=0, size_t the_index=0)
Definition: TestStore.h:34
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
SGTest::TestStore::boundHandle
virtual void boundHandle(IResetable *handle) override
Definition: TestStore.cxx:186
SGTest::TestStore::record1
SG::DataProxy * record1(lock_t &, const void *p, DataObject *obj, CLID clid, const std::string &key)
Definition: TestStore.cxx:203
SG::StringPool
Definition: StringPool.h:35
SGTest::TestStore::lock_t
std::lock_guard< mutex_t > lock_t
Definition: TestStore.h:135
index
Definition: index.py:1
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
SGTest::TestStoreRemap::operator==
bool operator==(const TestStoreRemap &other) const
Definition: TestStore.h:36
SGTest::TestStore::m_remap
remap_t m_remap
Definition: TestStore.h:125
SGTest::TestStore::proxy_exact
virtual SG::DataProxy * proxy_exact(SG::sgkey_t sgkey) const override
Definition: TestStore.cxx:140
SGTest::TestStore::tryELRemap
virtual bool tryELRemap(sgkey_t sgkey_in, size_t index_in, sgkey_t &sgkey_out, size_t &index_out) override
Definition: TestStore.cxx:164
SGTest::TestStore::stringToKey
virtual sgkey_t stringToKey(const std::string &str, CLID clid) override
Definition: TestStore.cxx:150
SGTest::TestStore::remap
void remap(sgkey_t sgkey_in, sgkey_t sgkey_out, size_t index_in=0, size_t index_out=0)
Definition: TestStore.cxx:227
SGTest::TestStoreRemap::key
SG::sgkey_t key
Definition: TestStore.h:38
SGTest::TestStore
Definition: TestStore.h:52
SGTest::TestStoreRemapHash
Definition: TestStore.h:44
SGTest::TestStore::remap
void remap(const std::string &key_in, const std::string &key_out, size_t index_in=0, size_t index_out=0)
Definition: TestStore.h:107
SGTest::TestStore::addToStore
virtual StatusCode addToStore(CLID, SG::DataProxy *proxy) override
Definition: TestStore.cxx:176
SGTest::TestStore::proxy
virtual SG::DataProxy * proxy(const void *const pTransient) const override
Definition: TestStore.cxx:104
ClassID_traits::ID
static const CLID & ID()
the CLID of T
Definition: Control/AthenaKernel/AthenaKernel/ClassID_traits.h:50
SGTest::TestStore::m_mutex
mutex_t m_mutex
Definition: TestStore.h:136
IResetable
a resetable object (e.g. a SG DataHandle)
Definition: IResetable.h:15
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
SGTest::TestStore::ATLAS_THREAD_SAFE
std::vector< std::pair< CLID, std::string > > m_missedProxies ATLAS_THREAD_SAFE
Definition: TestStore.h:132
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SGTest::TestStoreRemapHash::operator()
size_t operator()(const TestStoreRemap &m) const
Definition: TestStore.h:45
SGTest::TestStore::m_boundHandles
std::vector< IResetable * > m_boundHandles
Definition: TestStore.h:129
SGTest::TestStore::proxies
virtual std::vector< const SG::DataProxy * > proxies() const override
Definition: TestStore.cxx:26
SGTest::sgkey_t
SG::sgkey_t sgkey_t
Definition: TestStore.h:29
CalibCoolCompareRT.up
up
Definition: CalibCoolCompareRT.py:109
StringPool.h
Maintain a mapping of strings to 64-bit ints.
SGTest::TestStore::m_tmap
tmap_t m_tmap
Definition: TestStore.h:119
SGTest::TestStore::name
virtual const std::string & name() const override
Definition: TestStore.cxx:97
SGTest::TestStore::unboundHandle
virtual void unboundHandle(IResetable *handle) override
Definition: TestStore.cxx:193
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
IProxyDict.h
SGTest::ATLAS_NOT_THREAD_SAFE
void initTestStore ATLAS_NOT_THREAD_SAFE()
Definition: TestStore.cxx:247
SGTest::TestStore::remap_t
std::unordered_map< TestStoreRemap, TestStoreRemap, TestStoreRemapHash > remap_t
Definition: TestStore.h:124
SGTest::TestStore::keyToString
virtual const std::string * keyToString(sgkey_t) const override
Definition: TestStore.cxx:32
SGTest::TestStore::registerKey
virtual void registerKey(sgkey_t, const std::string &, CLID) override
Definition: TestStore.cxx:38
SGTest::TestStore::kmap_t
SG::SGKeyMap< SG::DataProxy * > kmap_t
Definition: TestStore.h:121
SGTest::TestStore::record
void record(const T *p, const std::string &key)
Definition: TestStore.h:81
SG::sgkey_t
uint32_t sgkey_t
Type used for hashed StoreGate key+CLID pairs.
Definition: CxxUtils/CxxUtils/sgkey_t.h:32
SGTest::TestStoreRemap::index
size_t index
Definition: TestStore.h:39
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
SGTest::TestStore::ATLAS_THREAD_SAFE
SG::StringPool m_stringPool ATLAS_THREAD_SAFE
Definition: TestStore.h:127
SG::SGKeyMap
std::unordered_map< sgkey_t, T > SGKeyMap
A map using sgkey_t as a key.
Definition: CxxUtils/CxxUtils/sgkey_t.h:93
SGTest::getTestStore
std::unique_ptr< TestStore > getTestStore()
Definition: TestStore.cxx:253
SGTest::TestStore::m_kmap
kmap_t m_kmap
Definition: TestStore.h:122
SGTest::TestStore::recordObject
virtual SG::DataProxy * recordObject(SG::DataObjectSharedPtr< DataObject > obj, const std::string &key, bool allowMods, bool returnExisting) override
Definition: TestStore.cxx:46
str
Definition: BTagTrackIpAccessor.cxx:11
SGTest::TestStoreRemap
Definition: TestStore.h:33
SG::DataObjectSharedPtr
Smart pointer to manage DataObject reference counts.
Definition: DataObjectSharedPtr.h:46
SGTest::TestStore::tmap_t
std::unordered_map< const void *, SG::DataProxy * > tmap_t
Definition: TestStore.h:118
checker_macros.h
Define macros for attributes used to control the static checker.
python.PyAthena.obj
obj
Definition: PyAthena.py:132
SG::DataProxy
Definition: DataProxy.h:44
SGTest::TestStore::record
void record(std::unique_ptr< T > up, const std::string &key)
Definition: TestStore.h:92
SGTest::TestStore::alias
void alias(SG::DataProxy *proxy, const std::string &newKey)
Definition: TestStore.cxx:236
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
DataProxy.h