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.
TStoreoperator= (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 THolderholder (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::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.

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 45 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 166 of file TStore.h.

◆ Objects_t

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

Type of the internal container storing all the objects.

Definition at line 164 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:171
Objects_t m_objects
The object storage.
Definition TStore.h:169

◆ 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:105

Member Function Documentation

◆ clear()

void xAOD::TStore::clear ( )

Clear the store of all of its contents.

Definition at line 105 of file TStore.cxx.

105 {
106
107 // Delete all the managed objects:
108 Objects_t::iterator itr = m_objects.begin();
109 Objects_t::iterator end = m_objects.end();
110 for( ; itr != end; ++itr ) {
111 delete itr->second;
112 }
113
114 // And clear the maps:
115 m_objects.clear();
116 m_keys.clear();
117
118 return;
119 }

◆ 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 152 of file TStore.cxx.

153 {
154
155 // Look up this object:
156 Objects_t::const_iterator itr = m_objects.find( key );
157 if( itr == m_objects.end() ) {
158 return kFALSE;
159 }
160
161 // Check if the object is of the right type:
162 return itr->second->getAsConst( ti, kTRUE );
163 }

◆ 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.

This is a quite slow function.

It needs to check each managed object to possibly find which one of them has this pointer.

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 343 of file TStore.cxx.

343 {
344
345 // Loop over all the managed objects:
346 Objects_t::const_iterator itr = m_objects.begin();
347 Objects_t::const_iterator end = m_objects.end();
348 for( ; itr != end; ++itr ) {
349 // Check if this is the right object:
350 if( itr->second->get() == ptr ) {
351 return kTRUE;
352 }
353 }
354
355 // We didn't find it:
356 return kFALSE;
357 }

◆ 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 330 of file TStore.cxx.

330 {
331
332 // Do the check quickly:
333 return ( m_keys.find( hash ) != m_keys.end() );
334 }

◆ 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 211 of file TStore.cxx.

212 {
213
214 // Look up this object:
215 Objects_t::const_iterator itr = m_objects.find( key );
216 if( itr == m_objects.end() ) {
217 return 0;
218 }
219
220 // Try to retrieve it as the requested type:
221 return itr->second->getAsConst( ti );
222 }

◆ getName() [1/2]

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

Get the name of a managed object.

This is just as slow as the previous contains function.

It needs to look at all the managed objects one by one to find under what name it is managed.

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 390 of file TStore.cxx.

390 {
391
392 // Loop over all the managed objects:
393 Objects_t::const_iterator itr = m_objects.begin();
394 Objects_t::const_iterator end = m_objects.end();
395 for( ; itr != end; ++itr ) {
396 // Check if this is the right object:
397 if( itr->second->get() == ptr ) {
398 return itr->first;
399 }
400 }
401
402 // We didn't find the object:
403 ::Warning( "xAOD::TStore::getName", "Object %p is not held by the store",
404 ptr );
405 static const std::string dummy( "" );
406 return dummy;
407 }

◆ 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 366 of file TStore.cxx.

366 {
367
368 // Try to find the name associated with this key:
369 HashedKeys_t::const_iterator itr = m_keys.find( hash );
370 if( itr != m_keys.end() ) {
371 // We found it:
372 return itr->second;
373 }
374
375 // We didn't find it:
376 ::Warning( "xAOD::TStore::getName", "Key 0x%08x not known by the store",
377 hash );
378 static const std::string dummy( "" );
379 return dummy;
380 }

◆ 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 409 of file TStore.cxx.

410 {
411 std::set< std::string > keys;
412
413 for ( const auto& pair : m_objects ) {
414 const std::string& key = pair.first;
415 const ::TClass* cl = pair.second->getClass();
416 const std::type_info* ti = pair.second->getTypeInfo();
417 std::string typeName;
418 if (cl)
419 typeName = cl->GetName();
420 else if (ti)
422
423 if (!typeName.empty() && typeName == targetTypeName)
424 keys.insert( key );
425 }
426
427 vkeys.insert( vkeys.end(), keys.begin(), keys.end() );
428 }
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 191 of file TStore.cxx.

192 {
193
194 // Look up this object:
195 Objects_t::const_iterator itr = m_objects.find( key );
196 if( itr == m_objects.end() ) {
197 return 0;
198 }
199
200 if( itr->second->isConst() ) {
201 ::Error( "xAOD::TStore::getObject",
202 XAOD_MESSAGE( "Cannot retrieve object with key \"%s\" of type %s as non-const" ),
203 key.c_str(), SG::normalizedTypeinfoName( ti ).c_str() );
204 return nullptr;
205 }
206
207 // Try to retrieve it as the requested type:
208 return itr->second->getAs( ti );
209 }
#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 165 of file TStore.cxx.

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

◆ 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 124 of file TStore.cxx.

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

◆ 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 303 of file TStore.cxx.

303 {
304
305 // Make sure that the key is not yet taken:
306 if( m_objects.find( key ) != m_objects.end() ) {
307 ::Error( "xAOD::TStore::record",
308 XAOD_MESSAGE( "Trying to overwrite object with key \"%s\"" ),
309 key.c_str() );
310 // We delete the holder object at this point. It's quite ugly in terms
311 // of code readibility, but it results in fewer characters in the
312 // template code...
313 delete hldr;
314 return StatusCode::FAILURE;
315 }
316
317 // Register the new object:
318 m_objects[ key ] = hldr;
319 m_keys[ Utils::hash( key ) ] = key;
320 return StatusCode::SUCCESS;
321 }
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 234 of file TStore.cxx.

236 {
237
238 // Cache
239 using clCache_t = CxxUtils::ConcurrentStrMap<::TClass*, CxxUtils::SimpleUpdater>;
240 static clCache_t clMap ATLAS_THREAD_SAFE {clCache_t::Updater_t()};
241
242 // First check if we have this dictionary cached already:
243 ::TClass* cl = 0;
244 auto clItr = clMap.find( classname );
245 if( clItr != clMap.end() ) {
246 // If the cached value doesn't work, then bail now:
247 if( ( ! clItr->second ) || ( ! clItr->second->IsLoaded() ) ) {
248 return StatusCode::RECOVERABLE;
249 }
250 // Otherwise we're done:
251 cl = clItr->second;
252 }
253
254 // If it's not cached, ask ROOT for it:
255 if( ! cl ) {
256 cl = ::TClass::GetClass( classname.c_str(), kTRUE, kTRUE );
257 clMap.emplace( classname, cl );
258 }
259 if( ( ! cl ) || ( ! cl->IsLoaded() ) ) {
260 return StatusCode::RECOVERABLE;
261 }
262
263 // Make sure that the key is not yet taken:
264 if( m_objects.find( key ) != m_objects.end() ) {
265 ::Error( "xAOD::TStore::record",
266 XAOD_MESSAGE( "Trying to overwrite object with key \"%s\"" ),
267 key.c_str() );
268 return StatusCode::FAILURE;
269 }
270
271 // Register the new object:
272 if( isConst )
273 m_objects[ key ] = new THolder( const_cast<const void*>(obj), cl, isOwner );
274 else
275 m_objects[ key ] = new THolder( obj, cl, isOwner);
276
277 m_keys[ Utils::hash( key ) ] = key;
278 return StatusCode::SUCCESS;
279 }
#define ATLAS_THREAD_SAFE
::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 281 of file TStore.cxx.

283 {
284
285 // Make sure that the key is not yet taken:
286 if( m_objects.find( key ) != m_objects.end() ) {
287 ::Error( "xAOD::TStore::record",
288 XAOD_MESSAGE( "Trying to overwrite object with key \"%s\"" ),
289 key.c_str() );
290 return StatusCode::FAILURE;
291 }
292
293 // Register the new object:
294 if( isConst )
295 m_objects[ key ] = new THolder( const_cast<const void*>(obj), ti, isOwner );
296 else
297 m_objects[ key ] = new THolder( obj, ti, isOwner );
298
299 m_keys[ Utils::hash( key ) ] = key;
300 return StatusCode::SUCCESS;
301 }

◆ 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 // Delete the hoder object:
69 delete itr->second;
70
71 // Now remove this element from the maps:
72 m_objects.erase( itr );
73 m_keys.erase( Utils::hash( key ) );
74
75 // We were successful:
76 return StatusCode::SUCCESS;
77 }

◆ remove() [2/2]

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

Remove an object from the store by pointer.

Definition at line 79 of file TStore.cxx.

79 {
80
81 // Look for this object:
82 Objects_t::iterator itr = m_objects.begin();
83 Objects_t::iterator end = m_objects.end();
84 for( ; itr != end; ++itr ) {
85
86 // Check if this is the object in question:
87 if( itr->second->get() != ptr ) {
88 continue;
89 }
90
91 // Yes, it is...
92 delete itr->second;
93 m_keys.erase( Utils::hash( itr->first ) );
94 m_objects.erase( itr );
95 return StatusCode::SUCCESS;
96 }
97
98 // We didn't find the object in the store:
99 ::Warning( "xAOD::TStore::remove",
100 "Couldn't find object with pointer %p",
101 ptr );
102 return StatusCode::RECOVERABLE;
103 }

◆ 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 49 of file TStore.h.

◆ TEvent

friend class TEvent
friend

Make TEvent a friend of this class.

Definition at line 48 of file TStore.h.

Member Data Documentation

◆ m_keys

HashedKeys_t xAOD::TStore::m_keys
protected

The key map.

Definition at line 171 of file TStore.h.

◆ m_objects

Objects_t xAOD::TStore::m_objects
protected

The object storage.

Definition at line 169 of file TStore.h.


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