ATLAS Offline Software
Loading...
Searching...
No Matches
xAOD::TStore Class Reference

A relatively simple transient store for objects created in analysis. More...

#include <TStore.h>

Inheritance diagram for xAOD::TStore:
Collaboration diagram for xAOD::TStore:

Public Member Functions

 TStore ()
 Default constructor.
 TStore (const TStore &)=delete
 Disallow copying the object.
virtual ~TStore ()
 Destructor.
TStore & operator= (const TStore &)=delete
 Disallow copying the object.
void setActive ()
 Set this as the active transient store in the application.
void print () const
 Print the current contents of the transient store.
Transient data accessor/modifier functions
template<typename T>
::Bool_t contains (const std::string &key) const
 Function checking if an object is available from the store.
template<typename T>
::Bool_t isConst (const std::string &key) const
 Function checking if an object with a given type is constant.
template<typename T>
StatusCode retrieve (const T *&obj, const std::string &key) const
 Retrieve either a constant or non-constant object from the store.
template<typename T>
StatusCode retrieve (T *&obj, const std::string &key) const
 Retrieve a non-constant object from the store.
template<typename T>
StatusCode record (T *obj, const std::string &key)
 Add an object to the store.
template<typename T>
StatusCode record (std::unique_ptr< T > obj, const std::string &key)
 Add an object to the store, explicitly taking ownership of it.
template<typename T>
StatusCode record (const T *obj, const std::string &key)
 Add a const object to the store.
template<typename T>
StatusCode record (std::unique_ptr< const T > obj, const std::string &key)
 Add a const object to the store, explicitly taking ownership of it.
template<typename T>
void keys (std::vector< std::string > &vkeys) const
 provide a list of keys associated with a type
const THolder * holder (const std::string &key) const
 return holder for key
StatusCode remove (const std::string &key)
 Remove an object from the store by name.
StatusCode remove (void *ptr)
 Remove an object from the store by pointer.
void clear ()
 Clear the store of all of its contents.

Protected Types

typedef std::unordered_map< std::string, THolder * > Objects_t
 Type of the internal container storing all the objects.
typedef SG::SGKeyMap< std::string > HashedKeys_t
 Type of the internal storage for the hashed keys of the object names.

Protected Member Functions

::Bool_t contains (const std::string &key, const std::type_info &ti) const
 Non-templated function implementing the containment check.
::Bool_t isConst (const std::string &key, const std::type_info &ti) const
 Non-templated function implementing the const-ness check.
void * getObject (const std::string &key, const std::type_info &ti) const
 Function retrieving a non-const object in a non-template way.
const void * getConstObject (const std::string &key, const std::type_info &ti) const
 Function retrieving a const object in a non-template way.
StatusCode record (void *obj, const std::string &key, const std::string &classname, ::Bool_t isOwner, ::Bool_t isConst)
 Function recording an object that has a dictionary available.
StatusCode record (void *obj, const std::string &key, const std::type_info &ti, ::Bool_t isOwner, ::Bool_t isConst)
 Function recording an object that has no dictionary.
template<class T>
StatusCode record (ConstDataVector< T > *obj, const std::string &key, const std::type_info &ti, ::Bool_t isOwner, ::Bool_t isConst)
 Function doing the first step of recording a ConstDataVector object.
StatusCode record (THolder *hldr, const std::string &key)
 Function doing the second step of recording a ConstDataVector object.
Functions mostly used by TEvent in the TVirtualEvent functions
::Bool_t contains (SG::sgkey_t hash) const
 Check if an object with a given hash is managed by the store.
::Bool_t contains (const void *ptr) const
 Check if an object with a given pointer is managed by the store.
const std::string & getName (SG::sgkey_t hash) const
 Get the name corresponding to a hashed key.
const std::string & getName (const void *ptr) const
 Get the name of a managed object.
void getNames (const std::string &targetClassName, std::vector< std::string > &vkeys) const
 Function determining the list keys associated with a type name.

Protected Attributes

Objects_t m_objects
 The object storage.
HashedKeys_t m_keys
 The key map.
std::unordered_map< const void *, const std::string * > m_names
 Key of each managed object, by object pointer (points into m_objects).

Private Member Functions

template<typename T>
StatusCode record_impl (T *obj, const std::string &key, ::Bool_t isOwner, ::Bool_t isConst)
 Internal implementation of the templated record method.

Friends

class TEvent
 Make TEvent a friend of this class.
class Event

Detailed Description

A relatively simple transient store for objects created in analysis.

This is a very simple transient store for objects that are created during analysis, but don't have to be written to the output file(s). To make it easier for the analysis tools to communicate with each other similar to how they would do it in Athena (with StoreGateSvc), they can use this class.

The usage of this class is highly optional in analysis, it should only be used if really necessary. (Passing around objects in analysis code directly is usually a better approach than using a store in my mind...)

Author
Attila Krasznahorkay Attil.nosp@m.a.Kr.nosp@m.aszna.nosp@m.hork.nosp@m.ay@ce.nosp@m.rn.c.nosp@m.h

Definition at line 46 of file TStore.h.

Member Typedef Documentation

◆ HashedKeys_t

typedef SG::SGKeyMap< std::string > xAOD::TStore::HashedKeys_t
protected

Type of the internal storage for the hashed keys of the object names.

Definition at line 167 of file TStore.h.

◆ Objects_t

typedef std::unordered_map< std::string, THolder* > xAOD::TStore::Objects_t
protected

Type of the internal container storing all the objects.

Definition at line 165 of file TStore.h.

Constructor & Destructor Documentation

◆ TStore() [1/2]

xAOD::TStore::TStore ( )

Default constructor.

Definition at line 24 of file TStore.cxx.

25 : m_objects(), m_keys() {
26
27 setActive();
28 }
void setActive()
Set this as the active transient store in the application.
Definition TStore.cxx:43
HashedKeys_t m_keys
The key map.
Definition TStore.h:172
Objects_t m_objects
The object storage.
Definition TStore.h:170

◆ TStore() [2/2]

xAOD::TStore::TStore ( const TStore & )
delete

Disallow copying the object.

◆ ~TStore()

xAOD::TStore::~TStore ( )
virtual

Destructor.

Definition at line 30 of file TStore.cxx.

30 {
31
32 // Clear the store before being deleted:
33 clear();
34
35 // If this object is set up as the active store at the moment,
36 // notify the active store object that this object will no longer
37 // be available.
38 if( TActiveStore::store() == this ) {
39 TActiveStore::setStore( nullptr );
40 }
41 }
static TStore * store()
Access the currently active TStore object.
static void setStore(TStore *ptr)
Set the active store pointer.
void clear()
Clear the store of all of its contents.
Definition TStore.cxx:95

Member Function Documentation

◆ clear()

void xAOD::TStore::clear ( )

Clear the store of all of its contents.

Definition at line 95 of file TStore.cxx.

95 {
96
97 // Delete all the managed objects:
98 Objects_t::iterator itr = m_objects.begin();
99 Objects_t::iterator end = m_objects.end();
100 for( ; itr != end; ++itr ) {
101 delete itr->second;
102 }
103
104 // And clear the maps:
105 m_objects.clear();
106 m_keys.clear();
107 m_names.clear();
108
109 return;
110 }
std::unordered_map< const void *, const std::string * > m_names
Key of each managed object, by object pointer (points into m_objects).
Definition TStore.h:174

◆ contains() [1/4]

template<typename T>
::Bool_t xAOD::TStore::contains ( const std::string & key) const

Function checking if an object is available from the store.

◆ contains() [2/4]

Bool_t xAOD::TStore::contains ( const std::string & key,
const std::type_info & ti ) const
protected

Non-templated function implementing the containment check.

Definition at line 143 of file TStore.cxx.

144 {
145
146 // Look up this object:
147 Objects_t::const_iterator itr = m_objects.find( key );
148 if( itr == m_objects.end() ) {
149 return kFALSE;
150 }
151
152 // Check if the object is of the right type:
153 return itr->second->getAsConst( ti, kTRUE );
154 }

◆ contains() [3/4]

Bool_t xAOD::TStore::contains ( const void * ptr) const
protected

Check if an object with a given pointer is managed by the store.

Parameters
ptrPointer to the object that may or may not be in the store
Returns
kTRUE if the object is managed by the store, or kFALSE if it isn't

Definition at line 325 of file TStore.cxx.

325 {
326
327 return m_names.contains( ptr );
328 }

◆ contains() [4/4]

Bool_t xAOD::TStore::contains ( SG::sgkey_t hash) const
protected

Check if an object with a given hash is managed by the store.

This is a reasonably fast function.

It checks whether an object with the specified hashed key is managed by the store.

Parameters
hashThe hashed key of the object that we are looking for
Returns
kTRUE if the object is managed by the store, or kFALSE if it is not

Definition at line 315 of file TStore.cxx.

315 {
316
317 // Do the check quickly:
318 return ( m_keys.find( hash ) != m_keys.end() );
319 }

◆ getConstObject()

const void * xAOD::TStore::getConstObject ( const std::string & key,
const std::type_info & ti ) const
protected

Function retrieving a const object in a non-template way.

Definition at line 202 of file TStore.cxx.

203 {
204
205 // Look up this object:
206 Objects_t::const_iterator itr = m_objects.find( key );
207 if( itr == m_objects.end() ) {
208 return 0;
209 }
210
211 // Try to retrieve it as the requested type:
212 return itr->second->getAsConst( ti );
213 }

◆ getName() [1/2]

const std::string & xAOD::TStore::getName ( const void * ptr) const
protected

Get the name of a managed object.

Parameters
ptrPointer to the object that we want to know the name of
Returns
The name with which this object was recorded into the store. Or an empty string of the object is not known to the store.

Definition at line 357 of file TStore.cxx.

357 {
358
359 auto itr = m_names.find( ptr );
360 if( itr != m_names.end() ) {
361 return *itr->second;
362 }
363
364 // We didn't find the object:
365 ::Warning( "xAOD::TStore::getName", "Object %p is not held by the store",
366 ptr );
367 static const std::string dummy( "" );
368 return dummy;
369 }

◆ getName() [2/2]

const std::string & xAOD::TStore::getName ( SG::sgkey_t hash) const
protected

Get the name corresponding to a hashed key.

This is a fairly fast function.

Used by ElementLinks that point to objects in this store.

Parameters
hashThe hashed key for which we want to find the string key
Returns
The name corresponding to this hashed key if the hashed key is known, or an empty string if it isn't.

Definition at line 337 of file TStore.cxx.

337 {
338
339 // Try to find the name associated with this key:
340 HashedKeys_t::const_iterator itr = m_keys.find( hash );
341 if( itr != m_keys.end() ) {
342 // We found it:
343 return itr->second;
344 }
345
346 // We didn't find it:
347 ::Warning( "xAOD::TStore::getName", "Key 0x%08x not known by the store",
348 hash );
349 static const std::string dummy( "" );
350 return dummy;
351 }

◆ getNames()

void xAOD::TStore::getNames ( const std::string & targetClassName,
std::vector< std::string > & vkeys ) const
protected

Function determining the list keys associated with a type name.

Definition at line 371 of file TStore.cxx.

372 {
373 std::set< std::string > keys;
374
375 for ( const auto& pair : m_objects ) {
376 const std::string& key = pair.first;
377 const ::TClass* cl = pair.second->getClass();
378 const std::type_info* ti = pair.second->getTypeInfo();
379 std::string typeName;
380 if (cl)
381 typeName = cl->GetName();
382 else if (ti)
384
385 if (!typeName.empty() && typeName == targetTypeName)
386 keys.insert( key );
387 }
388
389 vkeys.insert( vkeys.end(), keys.begin(), keys.end() );
390 }
void keys(std::vector< std::string > &vkeys) const
provide a list of keys associated with a type
std::string normalizedTypeinfoName(const std::type_info &info)
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
cl
print [x.__class__ for x in toList(dqregion.getSubRegions()) ]

◆ getObject()

void * xAOD::TStore::getObject ( const std::string & key,
const std::type_info & ti ) const
protected

Function retrieving a non-const object in a non-template way.

Definition at line 182 of file TStore.cxx.

183 {
184
185 // Look up this object:
186 Objects_t::const_iterator itr = m_objects.find( key );
187 if( itr == m_objects.end() ) {
188 return 0;
189 }
190
191 if( itr->second->isConst() ) {
192 ::Error( "xAOD::TStore::getObject",
193 XAOD_MESSAGE( "Cannot retrieve object with key \"%s\" of type %s as non-const" ),
194 key.c_str(), SG::normalizedTypeinfoName( ti ).c_str() );
195 return nullptr;
196 }
197
198 // Try to retrieve it as the requested type:
199 return itr->second->getAs( ti );
200 }
#define XAOD_MESSAGE(MESSAGE)
Simple macro for printing error/verbose messages.

◆ holder()

const THolder * xAOD::TStore::holder ( const std::string & key) const

return holder for key

Definition at line 50 of file TStore.cxx.

50 {
51 // Look up this object:
52 Objects_t::const_iterator itr = m_objects.find( key );
53 return (itr != m_objects.end() ? itr->second : nullptr);
54 }

◆ isConst() [1/2]

template<typename T>
::Bool_t xAOD::TStore::isConst ( const std::string & key) const

Function checking if an object with a given type is constant.

◆ isConst() [2/2]

Bool_t xAOD::TStore::isConst ( const std::string & key,
const std::type_info & ti ) const
protected

Non-templated function implementing the const-ness check.

Definition at line 156 of file TStore.cxx.

157 {
158
159 // Look up this object:
160 Objects_t::const_iterator itr = m_objects.find( key );
161 if( itr == m_objects.end() ) {
162 ::Warning( "xAOD::TStore::isConst",
163 "Object with key \"%s\" not available in store",
164 key.c_str() );
165 return kFALSE;
166 }
167
168 // Check if it can even be retrieved as a constant object of this type:
169 if( ! itr->second->getAsConst( ti, kTRUE ) ) {
170 const std::string typeName = SG::normalizedTypeinfoName( ti );
171 ::Warning( "xAOD::TStore::isConst",
172 "Object with key \"%s\" can't be retrieved as type: %s",
173 key.c_str(), typeName.c_str() );
174 return kFALSE;
175 }
176
177 // Now the question is just whether it can be retrieved as a non-const
178 // object as well:
179 return ( ! itr->second->getAs( ti, kTRUE ) );
180 }

◆ keys()

template<typename T>
void xAOD::TStore::keys ( std::vector< std::string > & vkeys) const

provide a list of keys associated with a type

◆ operator=()

TStore & xAOD::TStore::operator= ( const TStore & )
delete

Disallow copying the object.

◆ print()

void xAOD::TStore::print ( ) const

Print the current contents of the transient store.

This is really just meant for debugging an analysis code, to see what's going on in it.

Behaves a bit similar to StoreGateSvc::dump().

Definition at line 115 of file TStore.cxx.

115 {
116
117 ::Info( "xAOD::TStore::print", "Contents of transient store:" );
118 Objects_t::const_iterator itr = m_objects.begin();
119 Objects_t::const_iterator end = m_objects.end();
120 for( ; itr != end; ++itr ) {
121 ::Info( "xAOD::TStore::print", "-----------------------------------" );
122 ::Info( "xAOD::TStore::print", " Name: %s", itr->first.c_str() );
123 const ::TClass* cl = itr->second->getClass();
124 const std::type_info* ti = itr->second->getTypeInfo();
125 ::Info( "xAOD::TStore::print", " Type: %s",
126 ( cl ? cl->GetName() :
127 ( ti ? SG::normalizedTypeinfoName( *ti ).c_str() :
128 "Unknown" ) ) );
129 ::Info( "xAOD::TStore::print", " Pointer: %p",
130 itr->second->get() );
131 ::Info( "xAOD::TStore::print", " IsOwner: %s",
132 ( itr->second->isOwner() ? "Yes" : "No" ) );
133 ::Info( "xAOD::TStore::print", " IsConst: %s",
134 ( itr->second->isConst() ? "Yes" : "No" ) );
135 ::Info( "xAOD::TStore::print", " HasDictionary: %s",
136 ( cl ? "Yes" : "No" ) );
137 }
138 ::Info( "xAOD::TStore::print", "-----------------------------------" );
139
140 return;
141 }

◆ record() [1/8]

template<typename T>
StatusCode xAOD::TStore::record ( const T * obj,
const std::string & key )

Add a const object to the store.

◆ record() [2/8]

template<class T>
StatusCode xAOD::TStore::record ( ConstDataVector< T > * obj,
const std::string & key,
const std::type_info & ti,
::Bool_t isOwner,
::Bool_t isConst )
protected

Function doing the first step of recording a ConstDataVector object.

◆ record() [3/8]

template<typename T>
StatusCode xAOD::TStore::record ( std::unique_ptr< const T > obj,
const std::string & key )

Add a const object to the store, explicitly taking ownership of it.

◆ record() [4/8]

template<typename T>
StatusCode xAOD::TStore::record ( std::unique_ptr< T > obj,
const std::string & key )

Add an object to the store, explicitly taking ownership of it.

◆ record() [5/8]

template<typename T>
StatusCode xAOD::TStore::record ( T * obj,
const std::string & key )

Add an object to the store.

◆ record() [6/8]

StatusCode xAOD::TStore::record ( THolder * hldr,
const std::string & key )
protected

Function doing the second step of recording a ConstDataVector object.

Definition at line 286 of file TStore.cxx.

286 {
287
288 // Make sure that the key is not yet taken:
289 if( m_objects.find( key ) != m_objects.end() ) {
290 ::Error( "xAOD::TStore::record",
291 XAOD_MESSAGE( "Trying to overwrite object with key \"%s\"" ),
292 key.c_str() );
293 // We delete the holder object at this point. It's quite ugly in terms
294 // of code readibility, but it results in fewer characters in the
295 // template code...
296 delete hldr;
297 return StatusCode::FAILURE;
298 }
299
300 // Register the new object:
301 const auto itr = m_objects.emplace( key, hldr ).first;
302 m_keys[ Utils::hash( key ) ] = key;
303 // shortcut: one key per pointer, an object recorded twice (non-owning, from TPyStore) is lost by either remove; index all keys if that is ever used
304 m_names.emplace( hldr->get(), &itr->first );
305 return StatusCode::SUCCESS;
306 }
SG::sgkey_t hash(const std::string &name)
This function provides a hashed version of the key (branch) names used in the xAOD file,...

◆ record() [7/8]

StatusCode xAOD::TStore::record ( void * obj,
const std::string & key,
const std::string & classname,
::Bool_t isOwner,
::Bool_t isConst )
protected

Function recording an object that has a dictionary available.

This internal function does the heavy lifting of recording objects into the store that have a proper ROOT dictionary.

Parameters
objTypeless pointer to the object being recorded
keyKey to record the object with
classnameThe type name of the object being recorded
isOwnerIf kTRUE, the store takes ownership of the object, otherwise it doesn't
Returns
The usual StatusCode types

Definition at line 225 of file TStore.cxx.

227 {
228
229 // Cache
230 using clCache_t = CxxUtils::ConcurrentStrMap<::TClass*, CxxUtils::SimpleUpdater>;
231 static clCache_t clMap ATLAS_THREAD_SAFE {clCache_t::Updater_t()};
232
233 // First check if we have this dictionary cached already:
234 ::TClass* cl = 0;
235 auto clItr = clMap.find( classname );
236 if( clItr != clMap.end() ) {
237 // If the cached value doesn't work, then bail now:
238 if( ( ! clItr->second ) || ( ! clItr->second->IsLoaded() ) ) {
239 return StatusCode::RECOVERABLE;
240 }
241 // Otherwise we're done:
242 cl = clItr->second;
243 }
244
245 // If it's not cached, ask ROOT for it:
246 if( ! cl ) {
247 cl = ::TClass::GetClass( classname.c_str(), kTRUE, kTRUE );
248 clMap.emplace( classname, cl );
249 }
250 if( ( ! cl ) || ( ! cl->IsLoaded() ) ) {
251 return StatusCode::RECOVERABLE;
252 }
253
254 // Make sure that the key is not yet taken:
255 if( m_objects.find( key ) != m_objects.end() ) {
256 ::Error( "xAOD::TStore::record",
257 XAOD_MESSAGE( "Trying to overwrite object with key \"%s\"" ),
258 key.c_str() );
259 return StatusCode::FAILURE;
260 }
261
262 // Register the new object:
263 if( isConst )
264 return record( new THolder( const_cast<const void*>(obj), cl, isOwner ), key );
265 return record( new THolder( obj, cl, isOwner ), key );
266 }
#define ATLAS_THREAD_SAFE
StatusCode record(T *obj, const std::string &key)
Add an object to the store.
::Bool_t isConst(const std::string &key) const
Function checking if an object with a given type is constant.

◆ record() [8/8]

StatusCode xAOD::TStore::record ( void * obj,
const std::string & key,
const std::type_info & ti,
::Bool_t isOwner,
::Bool_t isConst )
protected

Function recording an object that has no dictionary.

Definition at line 268 of file TStore.cxx.

270 {
271
272 // Make sure that the key is not yet taken:
273 if( m_objects.find( key ) != m_objects.end() ) {
274 ::Error( "xAOD::TStore::record",
275 XAOD_MESSAGE( "Trying to overwrite object with key \"%s\"" ),
276 key.c_str() );
277 return StatusCode::FAILURE;
278 }
279
280 // Register the new object:
281 if( isConst )
282 return record( new THolder( const_cast<const void*>(obj), ti, isOwner ), key );
283 return record( new THolder( obj, ti, isOwner ), key );
284 }

◆ record_impl()

template<typename T>
StatusCode xAOD::TStore::record_impl ( T * obj,
const std::string & key,
::Bool_t isOwner,
::Bool_t isConst )
private

Internal implementation of the templated record method.

◆ remove() [1/2]

StatusCode xAOD::TStore::remove ( const std::string & key)

Remove an object from the store by name.

Definition at line 57 of file TStore.cxx.

57 {
58
59 // Look up this object:
60 Objects_t::iterator itr = m_objects.find( key );
61 if( itr == m_objects.end() ) {
62 ::Warning( "xAOD::TStore::remove",
63 "Couldn't find object with key \"%s\"",
64 key.c_str() );
65 return StatusCode::RECOVERABLE;
66 }
67
68 // Forget its pointer, and delete the holder object:
69 m_names.erase( itr->second->get() );
70 delete itr->second;
71
72 // Now remove this element from the maps:
73 m_objects.erase( itr );
74 m_keys.erase( Utils::hash( key ) );
75
76 // We were successful:
77 return StatusCode::SUCCESS;
78 }

◆ remove() [2/2]

StatusCode xAOD::TStore::remove ( void * ptr)

Remove an object from the store by pointer.

Definition at line 80 of file TStore.cxx.

80 {
81
82 // Look for this object:
83 auto itr = m_names.find( ptr );
84 if( itr == m_names.end() ) {
85 ::Warning( "xAOD::TStore::remove",
86 "Couldn't find object with pointer %p",
87 ptr );
88 return StatusCode::RECOVERABLE;
89 }
90
91 // Copy the key, removing the object destroys the string it points to:
92 return remove( std::string( *itr->second ) );
93 }
StatusCode remove(const std::string &key)
Remove an object from the store by name.
Definition TStore.cxx:57

◆ retrieve() [1/2]

template<typename T>
StatusCode xAOD::TStore::retrieve ( const T *& obj,
const std::string & key ) const

Retrieve either a constant or non-constant object from the store.

◆ retrieve() [2/2]

template<typename T>
StatusCode xAOD::TStore::retrieve ( T *& obj,
const std::string & key ) const

Retrieve a non-constant object from the store.

◆ setActive()

void xAOD::TStore::setActive ( )

Set this as the active transient store in the application.

Definition at line 43 of file TStore.cxx.

43 {
44
46 return;
47 }

◆ Event

friend class Event
friend

Definition at line 50 of file TStore.h.

◆ TEvent

friend class TEvent
friend

Make TEvent a friend of this class.

Definition at line 49 of file TStore.h.

Member Data Documentation

◆ m_keys

HashedKeys_t xAOD::TStore::m_keys
protected

The key map.

Definition at line 172 of file TStore.h.

◆ m_names

std::unordered_map< const void*, const std::string* > xAOD::TStore::m_names
protected

Key of each managed object, by object pointer (points into m_objects).

Definition at line 174 of file TStore.h.

◆ m_objects

Objects_t xAOD::TStore::m_objects
protected

The object storage.

Definition at line 170 of file TStore.h.


The documentation for this class was generated from the following files: