Belle II Software light-2607-kasei
DataStore Class Reference

In the store you can park objects that have to be accessed by various modules. More...

#include <DataStore.h>

Collaboration diagram for DataStore:

Classes

class  SwitchableDataStoreContents
 Encapsulates DataStoreContents, but allows transparently switching between different versions ('DataStore IDs'). More...
 

Public Types

enum  EDurability {
  c_Event ,
  c_Persistent
}
 Durability types. More...
 
enum  EStoreFlags {
  c_WriteOut = 0 ,
  c_DontWriteOut = 1 ,
  c_ErrorIfAlreadyRegistered = 2
}
 Flags describing behaviours of objects etc. More...
 
enum  ESearchSide {
  c_FromSide ,
  c_ToSide ,
  c_BothSides
}
 Which side of relations should be returned? More...
 
typedef Belle2::StoreEntry StoreEntry
 Wraps a stored array/object, stored under unique (name, durability) key.
 
typedef std::map< std::string, StoreEntryStoreEntryMap
 Map for StoreEntries.
 
typedef StoreEntryMap::iterator StoreEntryIter
 Iterator for a StoreEntry map.
 
typedef StoreEntryMap::const_iterator StoreEntryConstIter
 const_iterator for a StoreEntry map.
 
typedef std::array< StoreEntryMap, c_NDurabilityTypesDataStoreContents
 StoreEntry maps for each durability.
 

Public Member Functions

bool registerEntry (const std::string &name, EDurability durability, TClass *objClass, bool array, EStoreFlags storeFlags)
 Register an entry in the DataStore map.
 
bool registerRelation (const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, EStoreFlags storeFlags, const std::string &namedRelation)
 Register a relation in the DataStore map.
 
bool hasRelation (const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, const std::string &namedRelation)
 Check for the existence of a relation in the DataStore map.
 
bool requireInput (const StoreAccessorBase &accessor)
 Produce ERROR message if no entry of the given type is registered in the DataStore.
 
bool requireRelation (const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, std::string const &namedRelation)
 Produce ERROR message if no relation of given durability exists between fromArray and toArray (in that direction).
 
bool optionalInput (const StoreAccessorBase &accessor)
 Register the given object/array as an optional input.
 
StoreEntrygetEntry (const StoreAccessorBase &accessor)
 Check whether an entry with the correct type is registered in the DataStore map and return it.
 
TObject ** getObject (const StoreAccessorBase &accessor)
 Get a pointer to a pointer of an object in the DataStore.
 
bool createObject (TObject *object, bool replace, const StoreAccessorBase &accessor)
 Create a new object/array in the DataStore or add an existing one.
 
void replaceData (const StoreAccessorBase &from, const StoreAccessorBase &to)
 For two StoreAccessors of same type, move all data in 'from' into 'to', discarding previous contents of 'to' and leaving 'from' empty.
 
StoreEntryMapgetStoreEntryMap (EDurability durability)
 Get a reference to the object/array map.
 
void addRelation (const TObject *fromObject, StoreEntry *&fromEntry, int &fromIndex, const TObject *toObject, StoreEntry *&toEntry, int &toIndex, float weight, const std::string &namedRelation)
 Add a relation from an object in a store array to another object in a store array.
 
RelationVectorBase getRelationsWith (ESearchSide searchSide, const TObject *object, StoreEntry *&entry, int &index, const TClass *withClass, const std::string &withName, const std::string &namedRelation)
 Get the relations between an object and other objects in a store array.
 
Belle2::RelationEntry getRelationWith (ESearchSide searchSide, const TObject *object, StoreEntry *&entry, int &index, const TClass *withClass, const std::string &withName, const std::string &namedRelation)
 Get the first relation between an object and another object in a store array.
 
bool findStoreEntry (const TObject *object, StoreEntry *&entry, int &index)
 Find an object in an array in the data store.
 
std::vector< std::string > getListOfRelatedArrays (const StoreAccessorBase &array) const
 Returns a list of names of arrays which have registered relations that point to or from 'array'.
 
std::vector< std::string > getListOfArrays (const TClass *arrayClass, EDurability durability) const
 Returns a list of names of arrays which are of type (or inherit from) arrayClass.
 
std::vector< std::string > getSortedListOfDataStore (EDurability durability) const
 Returns a (sorted) list of all the content of the DataStore (Objs-Arrays-Relations).
 
void setInitializeActive (bool active)
 Setter for m_initializeActive.
 
bool getInitializeActive () const
 Are we currently initializing modules?
 
void invalidateData (EDurability durability)
 Clears all registered StoreEntry objects of a specified durability, invalidating all objects.
 
void reset (EDurability durability)
 Frees memory occupied by data store items and removes all objects from the map.
 
void reset ()
 Clears contents of the datastore (all durabilities)
 
DependencyMapgetDependencyMap ()
 Return map of dependencies between modules.
 
void createNewDataStoreID (const std::string &id)
 creates new datastore with given id, copying the registered objects/arrays from the current one.
 
void createEmptyDataStoreID (const std::string &id)
 creates empty datastore with given id.
 
std::string currentID () const
 returns ID of current DataStore.
 
void switchID (const std::string &id)
 switch to DataStore with given ID.
 
void copyEntriesTo (const std::string &id, const std::vector< std::string > &entrylist_event={}, bool mergeEntries=false)
 copy entries (not contents) of current DataStore to the DataStore with given ID.
 
void copyContentsTo (const std::string &id, const std::vector< std::string > &entrylist_event={})
 copy contents (actual array / object contents) of current DataStore to the DataStore with given ID.
 
void mergeContentsTo (const std::string &id, const std::vector< std::string > &entrylist_event={})
 merge contents (actual array / object contents) of current DataStore to the DataStore with given ID.
 

Static Public Member Functions

static DataStoreInstance ()
 Instance of singleton Store.
 
static TClass * getTClassFromDefaultObjectName (const std::string &objectName)
 Tries to deduce the TClass from a default object name, which is generally the name of the C++ class.
 
static TClass * getTClassFromDefaultArrayName (const std::string &arrayName)
 Tries to deduce the TClass from a default array name, which is generally the name of the C++ class with an appended 's'.
 
static std::string defaultObjectName (const std::string &classname)
 Return the default storage name for given class name.
 
static std::string defaultObjectName (const TClass *t)
 Return the default storage name for an object of the given TClass.
 
template<class T>
static std::string defaultObjectName ()
 Return the default storage name for an object of the given type.
 
static std::string objectName (const TClass *t, const std::string &name)
 Return the storage name for an object of the given TClass and name.
 
template<class T>
static std::string objectName (const std::string &name)
 Return the storage name for an object of the given type and name.
 
static std::string defaultArrayName (const std::string &classname)
 Return the default storage name for an given class name.
 
static std::string defaultArrayName (const TClass *t)
 Return the default storage name for an array of the given TClass.
 
template<class T>
static std::string defaultArrayName ()
 Return the default storage name for an array of the given type.
 
static std::string arrayName (const TClass *t, const std::string &name)
 Return the storage name for an object of the given TClass and name.
 
template<class T>
static std::string arrayName (const std::string &name)
 Return the storage name for an object of the given type and name.
 
template<class FROM, class TO>
static std::string defaultRelationName ()
 Return the default storage name for a relation between the given types.
 
static std::string relationName (const std::string &fromName, const std::string &toName, std::string const &namedRelation="")
 Return storage name for a relation between two arrays of the given names.
 
static bool optionalRelation (const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, std::string const &namedRelation)
 Register the given relation as an optional input.
 
static void addRelationFromTo (const TObject *fromObject, const TObject *toObject, float weight=1.0, const std::string &namedRelation="")
 Add a relation from an object in a store array to another object in a store array.
 
template<class T>
static RelationVector< T > getRelationsWithObj (const TObject *object, const std::string &name="", const std::string &namedRelation="")
 Get the relations between an object and other objects in a store array.
 
template<class T>
static T * getRelated (const TObject *object, const std::string &name="", const std::string &namedRelation="")
 Get the object to or from which another object has a relation.
 
static std::vector< std::string > getListOfObjects (const TClass *objClass, EDurability durability)
 Returns a list of names of StoreObjPtr-objects whose class is (or inherits from) objClass.
 
static std::vector< std::string > getListOfRelations (EDurability durability)
 Returns a list of names of StoreObjPtr-objects whose class is (or inherits from) RelationContainer.
 

Static Public Attributes

static const int c_NDurabilityTypes = 2
 Number of Durability Types.
 
static bool s_DoCleanup = false
 Global flag to to decide if we can do normal cleanup.
 

Private Member Functions

 DataStore ()
 Hidden constructor, as it is a singleton.
 
 DataStore (const DataStore &)=delete
 no copy constructor
 
DataStoreoperator= (const DataStore &)=delete
 no assignment operator
 
 ~DataStore ()
 Destructor.
 
const std::vector< std::string > & getArrayNames (const std::string &arrayName, const TClass *arrayClass, EDurability durability=c_Event) const
 Returns a vector with the names of store arrays matching the given name and class.
 

Static Private Member Functions

static void checkType (const StoreEntry &entry, const StoreAccessorBase &accessor)
 Check whether the given entry and the requested class match; if not, a B2FATAL is raised.
 
static void updateRelationsObjectCache (StoreEntry &entry)
 For an array containing RelationsObjects, update index and entry cache for entire contents.
 

Private Attributes

SwitchableDataStoreContents m_storeEntryMap
 Maps (name, durability) key to StoreEntry objects.
 
bool m_initializeActive
 True if modules are currently being initialized.
 
const std::regex m_regexNamedRelationCheck = std::regex("^[a-zA-Z]*$")
 Regular expression to check that no special characters and no white spaces are in the string given for namedRelations.
 
DependencyMapm_dependencyMap
 Collect information about the dependencies between modules.
 

Detailed Description

In the store you can park objects that have to be accessed by various modules.

Normal users should access the store via StoreAccessorBase-derived classes like StoreObjPtr or StoreArray.

Nomenclature:

See also
EDurability StoreObjPtr StoreArray RelationsObject

Definition at line 51 of file DataStore.h.

Member Typedef Documentation

◆ DataStoreContents

StoreEntry maps for each durability.

Definition at line 90 of file DataStore.h.

◆ StoreEntry

Wraps a stored array/object, stored under unique (name, durability) key.

Definition at line 84 of file DataStore.h.

◆ StoreEntryConstIter

typedef StoreEntryMap::const_iterator StoreEntryConstIter

const_iterator for a StoreEntry map.

Definition at line 89 of file DataStore.h.

◆ StoreEntryIter

typedef StoreEntryMap::iterator StoreEntryIter

Iterator for a StoreEntry map.

Definition at line 88 of file DataStore.h.

◆ StoreEntryMap

typedef std::map<std::string, StoreEntry> StoreEntryMap

Map for StoreEntries.

Definition at line 87 of file DataStore.h.

Member Enumeration Documentation

◆ EDurability

Durability types.

Defines how long an object should be valid.

Enumerator
c_Event 

Different object in each event, all objects/arrays are invalidated after event() function has been called on last module in path.

c_Persistent 

Object is available during entire execution time.

Objects are never invalidated and are accessible even after process(Path) returns.

Definition at line 58 of file DataStore.h.

58 {
59 c_Event,
60 c_Persistent
61 };

◆ ESearchSide

Which side of relations should be returned?

Enumerator
c_FromSide 

Return relations/objects pointed from (to a given object).

c_ToSide 

Return relations/objects pointed to (from a given object).

c_BothSides 

Combination of c_FromSide and c_ToSide.

Definition at line 76 of file DataStore.h.

76 {
77 c_FromSide,
78 c_ToSide,
79 c_BothSides
80 };

◆ EStoreFlags

Flags describing behaviours of objects etc.

Bitwise operators (|, &, etc.) are provided via ADD_BITMASK_OPERATORS.

Enumerator
c_WriteOut 

Object/array should be saved by output modules.

(default)

c_DontWriteOut 

Object/array should be NOT saved by output modules.

Can be overridden using the 'branchNames' parameter of RootOutput.

c_ErrorIfAlreadyRegistered 

If the object/array was already registered, produce an error (aborting initialisation).

Definition at line 69 of file DataStore.h.

69 {
70 c_WriteOut = 0,
71 c_DontWriteOut = 1,
72 c_ErrorIfAlreadyRegistered = 2,
73 };

Constructor & Destructor Documentation

◆ DataStore()

DataStore ( )
explicitprivate

Hidden constructor, as it is a singleton.

Definition at line 60 of file DataStore.cc.

60 : m_initializeActive(true), m_dependencyMap(new DependencyMap)
61{
62}
DependencyMap * m_dependencyMap
Collect information about the dependencies between modules.
Definition DataStore.h:637
bool m_initializeActive
True if modules are currently being initialized.
Definition DataStore.h:628

◆ ~DataStore()

~DataStore ( )
private

Destructor.

Definition at line 64 of file DataStore.cc.

65{
66 if (s_DoCleanup) {
67 //release all memory in data store
68 reset();
69 }
70 delete m_dependencyMap;
71}
static bool s_DoCleanup
Global flag to to decide if we can do normal cleanup.
Definition DataStore.h:100
void reset()
Clears contents of the datastore (all durabilities)
Definition DataStore.cc:73

Member Function Documentation

◆ addRelation()

void addRelation ( const TObject * fromObject,
StoreEntry *& fromEntry,
int & fromIndex,
const TObject * toObject,
StoreEntry *& toEntry,
int & toIndex,
float weight,
const std::string & namedRelation )

Add a relation from an object in a store array to another object in a store array.

Parameters
fromObjectPointer to the object from which the relation points.
fromEntryData store entry that contains the fromObject. Used for caching. Will be set if NULL.
fromIndexIndex in TClonesArray that contains the fromObject. Used for caching. Will be set if < 0.
toObjectPointer to the object to which the relation points.
toEntryData store entry that contains the toObject. Used for caching. Will be set if NULL.
toIndexIndex in TClonesArray that contains the toObject. Used for caching. Will be set if < 0.
weightWeight of the relation.
namedRelationAdditional name for the relation, or "" for the default naming

Definition at line 490 of file DataStore.cc.

492{
493 if (!fromObject or !toObject)
494 return;
495
496 // get entry from which the relation points
497 if (!findStoreEntry(fromObject, fromEntry, fromIndex)) {
498 B2FATAL("Couldn't find from-side entry for relation between " << fromObject->ClassName() << " and " << toObject->ClassName() <<
499 ". Please make sure the object is part of a StoreArray before adding a relation.");
500 }
501
502 // get entry to which the relation points
503 if (!findStoreEntry(toObject, toEntry, toIndex)) {
504 B2FATAL("Couldn't find to-side entry for relation between " << fromObject->ClassName() << " and " << toObject->ClassName() <<
505 ". Please make sure the object is part of a StoreArray before adding a relation.");
506 }
507
508 // get the relations from -> to
509 const string& relationsName = relationName(fromEntry->name, toEntry->name, namedRelation);
510 const StoreEntryIter& it = m_storeEntryMap[c_Event].find(relationsName);
511 if (it == m_storeEntryMap[c_Event].end()) {
512 B2FATAL("No relation '" << relationsName <<
513 "' found. Please register it (using StoreArray::registerRelationTo()) before trying to add relations.");
514 }
515 StoreEntry* entry = &(it->second);
516
517 // auto create relations if needed (both if null pointer, or uninitialised object read from TTree)
518 if (!entry->ptr)
519 entry->recreate();
520 auto* relContainer = static_cast<RelationContainer*>(entry->ptr);
521 if (relContainer->isDefaultConstructed()) {
522 relContainer->setFromName(fromEntry->name);
523 relContainer->setFromDurability(c_Event);
524 relContainer->setToName(toEntry->name);
525 relContainer->setToDurability(c_Event);
526 }
527
528 // add relation
529 TClonesArray& relations = relContainer->elements();
530 new (relations.AddrAt(relations.GetLast() + 1)) RelationElement(fromIndex, toIndex, weight);
531
532 std::shared_ptr<RelationIndexContainer<TObject, TObject>> relIndex =
533 RelationIndexManager::Instance().getIndexIfExists<TObject, TObject>(relationsName, c_Event);
534 if (relIndex) {
535 // add it to index (so we avoid expensive rebuilding later)
536 relIndex->index().emplace(fromIndex, toIndex, fromObject, toObject, weight);
537 } else {
538 //mark for rebuilding later on
539 relContainer->setModified(true);
540 }
541}
bool findStoreEntry(const TObject *object, StoreEntry *&entry, int &index)
Find an object in an array in the data store.
Definition DataStore.cc:396
StoreEntryMap::iterator StoreEntryIter
Iterator for a StoreEntry map.
Definition DataStore.h:88
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition DataStore.h:59
Belle2::StoreEntry StoreEntry
Wraps a stored array/object, stored under unique (name, durability) key.
Definition DataStore.h:84
static std::string relationName(const std::string &fromName, const std::string &toName, std::string const &namedRelation="")
Return storage name for a relation between two arrays of the given names.
Definition DataStore.h:180
SwitchableDataStoreContents m_storeEntryMap
Maps (name, durability) key to StoreEntry objects.
Definition DataStore.h:621
std::shared_ptr< RelationIndexContainer< FROM, TO > > getIndexIfExists(const std::string &name, DataStore::EDurability durability) const
if the index exists in the cache, it is returned; otherwise NULL.
static RelationIndexManager & Instance()
Returns the singleton instance.
TObject * ptr
The pointer to the returned object, either equal to 'object' or null, depending on whether the object...
Definition StoreEntry.h:51
std::string name
Name of the entry.
Definition StoreEntry.h:53
void recreate()
Reset stored object to defaults, set ptr to new object.
Definition StoreEntry.cc:68

◆ addRelationFromTo()

static void addRelationFromTo ( const TObject * fromObject,
const TObject * toObject,
float weight = 1.0,
const std::string & namedRelation = "" )
inlinestatic

Add a relation from an object in a store array to another object in a store array.

Note
If possible, use RelationsObject members instead, as they allow more efficient caching. Currently this should only be necessary if fromObject is of type genfit::...
See also
RelationsObject::addRelationTo
Parameters
fromObjectPointer to the object from which the relation points.
toObjectPointer to the object to which the relation points.
weightWeight of the relation.
namedRelationAdditional name for the relation, or "" for the default naming

Definition at line 387 of file DataStore.h.

389 {
390 DataStore::StoreEntry* fromEntry = nullptr;
391 int fromIndex = -1;
392 StoreEntry* toEntry = nullptr;
393 int toIndex = -1;
394 Instance().addRelation(fromObject, fromEntry, fromIndex, toObject, toEntry, toIndex, weight, namedRelation);
395 }

◆ arrayName() [1/2]

template<class T>
static std::string arrayName ( const std::string & name)
inlinestatic

Return the storage name for an object of the given type and name.

Definition at line 167 of file DataStore.h.

168 {
169 return ((name.empty()) ? defaultArrayName<T>() : name);
170 }

◆ arrayName() [2/2]

std::string arrayName ( const TClass * t,
const std::string & name )
static

Return the storage name for an object of the given TClass and name.

Definition at line 163 of file DataStore.cc.

164{
165 return ((name.empty()) ? defaultArrayName(t) : name);
166}
static std::string defaultArrayName(const std::string &classname)
Return the default storage name for an given class name.
Definition DataStore.h:143

◆ checkType()

void checkType ( const StoreEntry & entry,
const StoreAccessorBase & accessor )
staticprivate

Check whether the given entry and the requested class match; if not, a B2FATAL is raised.

Name and durability are not checked.

Parameters
entryThe existing DataStore entry.
accessorEncapsulates name, durability, and type

Definition at line 169 of file DataStore.cc.

170{
171 // Check whether the existing entry and the requested object are both arrays or both single objects
172 const char* entryType = (entry.isArray) ? "array" : "object";
173 if (entry.isArray != accessor.isArray()) {
174 B2FATAL("Existing entry '" << entry.name << "' is an " << entryType << " and the requested one an " << ((
175 accessor.isArray()) ? "array" : "object"));
176 }
177
178 // Check whether the existing entry has the same type
179 const TClass* entryClass = entry.objClass;
180 if (!entryClass->InheritsFrom(accessor.getClass())) {
181 B2FATAL("Existing " << accessor.readableName() << " of type " << entryClass->GetName() << " doesn't match requested type " <<
182 accessor.getClass()->GetName());
183 }
184}
std::string readableName() const
Convert this accessor into a readable string (for messages).
TClass * getClass() const
The underlying object's type.
bool isArray() const
Is this an accessor for an array?
bool isArray
Flag that indicates whether the object is a TClonesArray.
Definition StoreEntry.h:39
TClass * objClass
class of object.
Definition StoreEntry.h:41

◆ copyContentsTo()

void copyContentsTo ( const std::string & id,
const std::vector< std::string > & entrylist_event = {} )

copy contents (actual array / object contents) of current DataStore to the DataStore with given ID.

Definition at line 808 of file DataStore.cc.

809{
810 m_storeEntryMap.copyContentsTo(id, entrylist_event);
811}

◆ copyEntriesTo()

void copyEntriesTo ( const std::string & id,
const std::vector< std::string > & entrylist_event = {},
bool mergeEntries = false )

copy entries (not contents) of current DataStore to the DataStore with given ID.

Definition at line 803 of file DataStore.cc.

804{
805 m_storeEntryMap.copyEntriesTo(id, entrylist_event, mergeEntries);
806}

◆ createEmptyDataStoreID()

void createEmptyDataStoreID ( const std::string & id)

creates empty datastore with given id.

Definition at line 782 of file DataStore.cc.

783{
784 m_storeEntryMap.createEmptyDataStoreID(id);
785}

◆ createNewDataStoreID()

void createNewDataStoreID ( const std::string & id)

creates new datastore with given id, copying the registered objects/arrays from the current one.

Definition at line 777 of file DataStore.cc.

778{
779 m_storeEntryMap.createNewDataStoreID(id);
780}

◆ createObject()

bool createObject ( TObject * object,
bool replace,
const StoreAccessorBase & accessor )

Create a new object/array in the DataStore or add an existing one.

A matching map entry must already exist. Otherwise an error will be generated.

Parameters
objectPointer to the object that should be stored. If 0, a new default object is created.
replaceIf an object already exists, it will be replaced if this is true. If false, an error will be printed.
accessorEncapsulates name, durability, and type
Returns
Whether the object was successfully inserted/created

Definition at line 314 of file DataStore.cc.

315{
316 StoreEntry* entry = getEntry(accessor);
317 if (!entry) {
318 B2ERROR("No " << accessor.readableName() <<
319 " exists in the DataStore, did you forget to register it in your Module's initialize() function? Note: direct accesses to it will crash!");
320 return false;
321 }
322
323 if (entry->ptr && !replace && object != entry->object) {
324 B2ERROR("An " << accessor.readableName() << " was already created in the DataStore.");
325 return false;
326 }
327
328 if (object) {
329 if (object != entry->object) {
330 delete entry->object;
331 entry->object = object;
332 }
333 entry->ptr = entry->object;
334 } else {
335 entry->recreate();
336 }
337
338 return true;
339}
StoreEntry * getEntry(const StoreAccessorBase &accessor)
Check whether an entry with the correct type is registered in the DataStore map and return it.
Definition DataStore.cc:291
TObject * object
The pointer to the actual object.
Definition StoreEntry.h:48

◆ currentID()

std::string currentID ( ) const

returns ID of current DataStore.

Definition at line 787 of file DataStore.cc.

788{
789 return m_storeEntryMap.currentID();
790}

◆ defaultArrayName() [1/3]

template<class T>
static std::string defaultArrayName ( )
inlinestatic

Return the default storage name for an array of the given type.

Definition at line 157 of file DataStore.h.

158 {
159 const static std::string s = defaultArrayName(defaultObjectName<T>());
160 return s;
161 }

◆ defaultArrayName() [2/3]

static std::string defaultArrayName ( const std::string & classname)
inlinestatic

Return the default storage name for an given class name.

Definition at line 143 of file DataStore.h.

144 {
145 const std::string& objName = defaultObjectName(classname);
146 std::string s;
147 s.reserve(objName.length() + 1);
148 s += objName;
149 s += 's';
150 return s;
151 }

◆ defaultArrayName() [3/3]

std::string defaultArrayName ( const TClass * t)
static

Return the default storage name for an array of the given TClass.

Definition at line 156 of file DataStore.cc.

157{
158 const std::string s = defaultArrayName(defaultObjectName(t));
159 return s;
160}
static std::string defaultObjectName()
Return the default storage name for an object of the given type.
Definition DataStore.h:127
static std::string defaultArrayName()
Return the default storage name for an array of the given type.
Definition DataStore.h:157

◆ defaultObjectName() [1/3]

template<class T>
static std::string defaultObjectName ( )
inlinestatic

Return the default storage name for an object of the given type.

Definition at line 127 of file DataStore.h.

128 {
129 const static std::string s = defaultObjectName(T::Class_Name());
130 return s;
131 }

◆ defaultObjectName() [2/3]

std::string defaultObjectName ( const std::string & classname)
static

Return the default storage name for given class name.

Definition at line 127 of file DataStore.cc.

128{
129 const static string gfclass = "genfit::Track";
130 const static string gfobjectname = "GF2Track";
131 if (classname == gfclass)
132 return gfobjectname;
133 //Strip qualifiers like namespaces
134 size_t colon = classname.rfind(':');
135 if (colon != std::string::npos) {
136 return classname.substr(colon + 1);
137 }
138 return classname;
139}

◆ defaultObjectName() [3/3]

std::string defaultObjectName ( const TClass * t)
static

Return the default storage name for an object of the given TClass.

Definition at line 142 of file DataStore.cc.

143{
144 B2ASSERT("Cannot deduce default object name from null pointer TClass", t);
145 const std::string s = defaultObjectName(t->GetName());
146 return s;
147}

◆ defaultRelationName()

template<class FROM, class TO>
static std::string defaultRelationName ( )
inlinestatic

Return the default storage name for a relation between the given types.

Definition at line 173 of file DataStore.h.

174 {
175 const static std::string s = relationName(defaultArrayName<FROM>(), defaultArrayName<TO>());
176 return s;
177 }

◆ findStoreEntry()

bool findStoreEntry ( const TObject * object,
DataStore::StoreEntry *& entry,
int & index )

Find an object in an array in the data store.

entry/index are used to return the found array and index, should be set to NULL and -1, or some cached values (will be checked).

Given non-NULL entry and index>=0, this function returns immediately if the object is found at the given position.

Parameters
objectPointer to the object.
entryThe DataStore entry that contains the object.
indexThe index of the object in the array.
Returns
True if the object was found in the data store

Definition at line 396 of file DataStore.cc.

397{
398 if (!entry or index < 0) {
399 //usually entry/index should be passed for RelationsObject,
400 //but there are exceptions -> let's check again
401 const auto* relObj = dynamic_cast<const RelationsObject*>(object);
402 if (relObj) {
403 entry = relObj->m_cacheDataStoreEntry;
404 index = relObj->m_cacheArrayIndex;
405 }
406 }
407 // check whether the cached information is (still) valid
408 if (entry && entry->ptr && (index >= 0)) {
409 const TClonesArray* array = static_cast<TClonesArray*>(entry->ptr);
410 if (array->At(index) == object) return true;
411 B2INFO("findStoreEntry: cached index invalid, probably harmless but odd : " << entry->name << " idx " << index);
412 index = array->IndexOf(object);
413 if (index >= 0) return true;
414 B2INFO("findStoreEntry: cached entry was also wrong");
415 }
416 entry = nullptr;
417 index = -1;
418
419 //searching for nullptr should be safe
420 if (!object)
421 return false;
422
423 // search for the object and set the entry and index
424 const TClass* objectClass = object->IsA();
425 for (auto& mapEntry : m_storeEntryMap[c_Event]) {
426 if (mapEntry.second.ptr && mapEntry.second.isArray) {
427 const TClass* arrayClass = mapEntry.second.objClass;
428 if (arrayClass != objectClass)
429 continue;
430
431 const TClonesArray* array = static_cast<TClonesArray*>(mapEntry.second.ptr);
432 if (object == array->Last()) {
433 //quickly find entry if it's at the end of the array
434 index = array->GetLast();
435 } else {
436 if (arrayClass->InheritsFrom(RelationsObject::Class())) {
437 //update cache for entire array
438 updateRelationsObjectCache(mapEntry.second);
439
440 //if found, m_cacheArrayIndex is now correct, otherwise still -1
441 StoreEntry* objEntry = static_cast<const RelationsObject*>(object)->m_cacheDataStoreEntry;
442 index = static_cast<const RelationsObject*>(object)->m_cacheArrayIndex;
443 if (index >= 0 and objEntry) {
444 //if the cache of 'object' is filled, make sure to also set entry!
445 entry = objEntry;
446 return true;
447 }
448 } else {
449 //not a RelationsObject, so no caching
450 index = array->IndexOf(object);
451 }
452 }
453
454 if (index >= 0) {
455 entry = &mapEntry.second;
456 return true;
457 }
458 }
459 }
460 return false;
461}
static void updateRelationsObjectCache(StoreEntry &entry)
For an array containing RelationsObjects, update index and entry cache for entire contents.
Definition DataStore.cc:385
RelationsInterface< TObject > RelationsObject
Provides interface for getting/adding relations to objects in StoreArrays.

◆ getArrayNames()

const std::vector< std::string > & getArrayNames ( const std::string & arrayName,
const TClass * arrayClass,
EDurability durability = c_Event ) const
private

Returns a vector with the names of store arrays matching the given name and class.

Note that the returned reference is only valid until the next call.

Parameters
arrayNameA given array name, the special string "ALL" for all arrays deriving from the given class, or an empty string for the default array name.
arrayClassThe class of the array(s).
durabilityDecide with which durability map you want to perform the requested action.

Definition at line 463 of file DataStore.cc.

465{
466 static vector<string> arrayNames;
467 arrayNames.clear();
468 if (name.empty()) {
469 static std::unordered_map<const TClass*, string> classToArrayName;
470 const auto& it = classToArrayName.find(arrayClass);
471 if (it != classToArrayName.end()) {
472 arrayNames.emplace_back(it->second);
473 } else {
474 const std::string& result = defaultArrayName(arrayClass->GetName());
475 classToArrayName[arrayClass] = result;
476 arrayNames.emplace_back(result);
477 }
478 } else if (name == "ALL") {
479 for (auto& mapEntry : m_storeEntryMap[durability]) {
480 if (mapEntry.second.object and mapEntry.second.isArray and mapEntry.second.objClass->InheritsFrom(arrayClass)) {
481 arrayNames.emplace_back(mapEntry.second.name);
482 }
483 }
484 } else {
485 arrayNames.emplace_back(name);
486 }
487 return arrayNames;
488}

◆ getDependencyMap()

DependencyMap & getDependencyMap ( )
inline

Return map of dependencies between modules.

Definition at line 524 of file DataStore.h.

524{ return *m_dependencyMap; }

◆ getEntry()

DataStore::StoreEntry * getEntry ( const StoreAccessorBase & accessor)

Check whether an entry with the correct type is registered in the DataStore map and return it.

If the map of requested durability already contains an object under the key name with a DIFFERENT type than the given type one, an error will be reported.

Parameters
accessorEncapsulates name, durability, and type
Returns
StoreEntry, or NULL if not found

Definition at line 291 of file DataStore.cc.

292{
293 const auto& it = m_storeEntryMap[accessor.getDurability()].find(accessor.getName());
294
295 if (it != m_storeEntryMap[accessor.getDurability()].end()) {
296 checkType((it->second), accessor);
297 return &(it->second);
298 } else {
299 return nullptr;
300 }
301}
static void checkType(const StoreEntry &entry, const StoreAccessorBase &accessor)
Check whether the given entry and the requested class match; if not, a B2FATAL is raised.
Definition DataStore.cc:169
DataStore::EDurability getDurability() const
Return durability with which the object is saved in the DataStore.
const std::string & getName() const
Return name under which the object is saved in the DataStore.

◆ getInitializeActive()

bool getInitializeActive ( ) const
inline

Are we currently initializing modules?

Definition at line 502 of file DataStore.h.

502{ return m_initializeActive; }

◆ getListOfArrays()

std::vector< std::string > getListOfArrays ( const TClass * arrayClass,
EDurability durability ) const

Returns a list of names of arrays which are of type (or inherit from) arrayClass.

Definition at line 664 of file DataStore.cc.

665{
666 return getArrayNames("ALL", arrayClass, durability);
667}
const std::vector< std::string > & getArrayNames(const std::string &arrayName, const TClass *arrayClass, EDurability durability=c_Event) const
Returns a vector with the names of store arrays matching the given name and class.
Definition DataStore.cc:463

◆ getListOfObjects()

std::vector< std::string > getListOfObjects ( const TClass * objClass,
EDurability durability )
static

Returns a list of names of StoreObjPtr-objects whose class is (or inherits from) objClass.

(this does NOT include the internal RelationContainer objects.)

Definition at line 669 of file DataStore.cc.

670{
671 vector<string> list;
673 for (const auto& entrypair : map) {
674 if (!entrypair.second.isArray) {
675 const TObject* obj = entrypair.second.object;
676 if (dynamic_cast<const RelationContainer*>(obj))
677 continue; //ignore relations in list
678
679 if (obj and obj->IsA()->InheritsFrom(objClass))
680 list.emplace_back(entrypair.first);
681 }
682 }
683 return list;
684}
StoreEntryMap & getStoreEntryMap(EDurability durability)
Get a reference to the object/array map.
Definition DataStore.h:325
EDurability
Durability types.
Definition DataStore.h:58
static DataStore & Instance()
Instance of singleton Store.
Definition DataStore.cc:53
std::map< std::string, StoreEntry > StoreEntryMap
Map for StoreEntries.
Definition DataStore.h:87

◆ getListOfRelatedArrays()

std::vector< std::string > getListOfRelatedArrays ( const StoreAccessorBase & array) const

Returns a list of names of arrays which have registered relations that point to or from 'array'.

This may be useful for internal purposes when modifying array contents.

Definition at line 638 of file DataStore.cc.

639{
640 std::vector<std::string> arrays;
641 if (!array.isArray()) {
642 B2ERROR("getListOfRelatedArrays(): " << array.readableName() << " is not an array!");
643 return arrays;
644 }
645
646 //loop over all arrays
647 EDurability durability = array.getDurability();
648 for (const auto& mapEntry : m_storeEntryMap[durability]) {
649 if (mapEntry.second.isArray) {
650 const std::string& name = mapEntry.second.name;
651
652 //check both from & to 'array'
653 for (int searchSide = 0; searchSide < c_BothSides; searchSide++) {
654 const string& relationsName = (searchSide == c_ToSide) ? relationName(array.getName(), name) : relationName(name, array.getName());
655 const StoreEntryConstIter& it = m_storeEntryMap[durability].find(relationsName);
656 if (it != m_storeEntryMap[durability].end())
657 arrays.emplace_back(name);
658 }
659 }
660 }
661
662 return arrays;
663}
StoreEntryMap::const_iterator StoreEntryConstIter
const_iterator for a StoreEntry map.
Definition DataStore.h:89
@ c_BothSides
Combination of c_FromSide and c_ToSide.
Definition DataStore.h:79
@ c_ToSide
Return relations/objects pointed to (from a given object).
Definition DataStore.h:78

◆ getListOfRelations()

std::vector< std::string > getListOfRelations ( EDurability durability)
static

Returns a list of names of StoreObjPtr-objects whose class is (or inherits from) RelationContainer.

Definition at line 686 of file DataStore.cc.

687{
688 vector<string> list;
690 for (const auto& entrypair : map) {
691 if (!entrypair.second.isArray) {
692 const TObject* obj = entrypair.second.object;
693 if (dynamic_cast<const RelationContainer*>(obj))
694 list.emplace_back(entrypair.first);
695
696 }
697 }
698 return list;
699}

◆ getObject()

TObject ** getObject ( const StoreAccessorBase & accessor)

Get a pointer to a pointer of an object in the DataStore.

If the map of requested durability already contains an object under the key name with a DIFFERENT type than the given type one, an error will be reported.

Parameters
accessorEncapsulates name, durability, and type
Returns
Pointer to pointer to object, NULL if none exists

Definition at line 304 of file DataStore.cc.

305{
306 StoreEntry* entry = getEntry(accessor);
307 if (!entry) {
308 return nullptr;
309 }
310 return &(entry->ptr);
311}

◆ getRelated()

template<class T>
static T * getRelated ( const TObject * object,
const std::string & name = "",
const std::string & namedRelation = "" )
inlinestatic

Get the object to or from which another object has a relation.

Note
If possible, use RelationsObject members instead, as they allow more efficient caching. Currently this should only be necessary if type(object) == genfit::..
Parameters
objectPointer to the object to or from which the relation points.
Template Parameters
TThe class of objects to or from which the relation points.
Parameters
nameThe name of the store array to or from which the relation points. If empty the default store array name for class T will be used. If the special name "ALL" is given all store arrays containing objects of type T are considered.
namedRelationAdditional name for the relation, or "" for the default naming
Returns
The related object or a null pointer.

Definition at line 432 of file DataStore.h.

433 {
434 if (!object) return nullptr;
435 StoreEntry* storeEntry = nullptr;
436 int index = -1;
437 return static_cast<T*>(DataStore::Instance().getRelationWith(c_BothSides, object, storeEntry, index, T::Class(), name,
438 namedRelation).object);
439 }

◆ getRelationsWith()

RelationVectorBase getRelationsWith ( ESearchSide searchSide,
const TObject * object,
DataStore::StoreEntry *& entry,
int & index,
const TClass * withClass,
const std::string & withName,
const std::string & namedRelation )

Get the relations between an object and other objects in a store array.

Parameters
searchSideSearch objects of type withClass on this side of relations (with 'object' on the other side).
objectPointer to the object from or to which the relations point.
entryData store entry that contains the object. Used for caching. Will be set if 0.
indexIndex in TClonesArray that contains the object. Used for caching. Will be set if < 0.
withClassClass of the objects to or from which the relations point.
withNameThe name of the store array to or from which the relations point. If empty the default store array name for withClass will be used. If the special name "ALL" is given all store arrays containing object of type withClass are considered.
namedRelationAdditional name for the relation, or "" for the default naming. If withName is "ALL", the namedRelation parameter is ignored and all related objects are returned.
Returns
Vector of relation entry objects (not type-safe).

Definition at line 543 of file DataStore.cc.

545{
546 if (searchSide == c_BothSides) {
547 auto result = getRelationsWith(c_ToSide, object, entry, index, withClass, withName, namedRelation);
548 const auto& fromResult = getRelationsWith(c_FromSide, object, entry, index, withClass, withName, namedRelation);
549 result.add(fromResult);
550 return result;
551 }
552
553 std::vector<RelationEntry> result;
554
555 // get StoreEntry for 'object'
556 if (!findStoreEntry(object, entry, index)) return RelationVectorBase();
557
558 // get names of store arrays to search
559 const std::vector<string>& names = getArrayNames(withName, withClass);
560 vector<string> relationNames;
561
562 // loop over found store arrays
563 for (const std::string& name : names) {
564 // get the relations from -> to
565 const string& relationsName = (searchSide == c_ToSide) ? relationName(entry->name, name, namedRelation) : relationName(name,
566 entry->name, namedRelation);
567 RelationIndex<TObject, TObject> relIndex(relationsName, c_Event);
568 if (!relIndex)
569 continue;
570
571 const size_t prevsize = result.size();
572
573 //get relations with object
574 if (searchSide == c_ToSide) {
575 for (const auto& rel : relIndex.getElementsFrom(object)) {
576 auto* const toObject = const_cast<TObject*>(rel.to);
577 if (toObject)
578 result.emplace_back(toObject, rel.weight);
579 }
580 } else {
581 for (const auto& rel : relIndex.getElementsTo(object)) {
582 auto* const fromObject = const_cast<TObject*>(rel.from);
583 if (fromObject)
584 result.emplace_back(fromObject, rel.weight);
585 }
586 }
587
588 if (result.size() != prevsize)
589 relationNames.push_back(relationsName);
590 }
591
592 return RelationVectorBase(entry->name, index, result, relationNames);
593}
@ c_FromSide
Return relations/objects pointed from (to a given object).
Definition DataStore.h:77
RelationVectorBase getRelationsWith(ESearchSide searchSide, const TObject *object, StoreEntry *&entry, int &index, const TClass *withClass, const std::string &withName, const std::string &namedRelation)
Get the relations between an object and other objects in a store array.
Definition DataStore.cc:543

◆ getRelationsWithObj()

template<class T>
static RelationVector< T > getRelationsWithObj ( const TObject * object,
const std::string & name = "",
const std::string & namedRelation = "" )
inlinestatic

Get the relations between an object and other objects in a store array.

Relations in both directions are returned.

Note
Using this function should only be necessary if type(object) == genfit::.... If possible, use RelationsObject members instead, as they allow more efficient caching.
See also
RelationsObject::getRelationsWith
Parameters
objectPointer to the object from or to which the relations point.
Template Parameters
TClass of the objects to or from which the relations point.
Parameters
nameThe name of the store array to or from which the relations point. If empty the default store array name for withClass will be used. If the special name "ALL" is given all store arrays containing objects of type T are considered.
namedRelationAdditional name for the relation, or "" for the default naming
Returns
Vector of relation entry objects.

Definition at line 412 of file DataStore.h.

414 {
415 StoreEntry* storeEntry = nullptr;
416 int index = -1;
417 return RelationVector<T>(Instance().getRelationsWith(c_BothSides, object, storeEntry, index, T::Class(), name, namedRelation));
418 }

◆ getRelationWith()

RelationEntry getRelationWith ( ESearchSide searchSide,
const TObject * object,
DataStore::StoreEntry *& entry,
int & index,
const TClass * withClass,
const std::string & withName,
const std::string & namedRelation )

Get the first relation between an object and another object in a store array.

Parameters
searchSideSearch objects of type withClass on this side of relations (with 'object' on the other side).
objectPointer to the object from or to which the relation points.
entryData store entry that contains the object. Used for caching. Will be set if 0.
indexIndex in TClonesArray that contains the object. Used for caching. Will be set if < 0.
withClassClass of the objects to or from which the relation points.
withNameThe name of the store array to or from which the relation points. If empty the default store array name for withClass will be used. If the special name "ALL" is given all store arrays containing object of type withClass are considered.
namedRelationAdditional name for the relation, or "" for the default naming. If withName is "ALL", the namedRelation parameter is ignored and all related objects are returned.
Returns
The entry of the first related object.

Definition at line 595 of file DataStore.cc.

597{
598 if (searchSide == c_BothSides) {
599 RelationEntry result = getRelationWith(c_ToSide, object, entry, index, withClass, withName, namedRelation);
600 if (!result.object) {
601 result = getRelationWith(c_FromSide, object, entry, index, withClass, withName, namedRelation);
602 }
603 return result;
604 }
605
606 // get StoreEntry for 'object'
607 if (!findStoreEntry(object, entry, index)) return RelationEntry(nullptr);
608
609 // get names of store arrays to search
610 const std::vector<string>& names = getArrayNames(withName, withClass);
611
612 // loop over found store arrays
613 for (const std::string& name : names) {
614 // get the relations from -> to
615 const string& relationsName = (searchSide == c_ToSide) ? relationName(entry->name, name, namedRelation) : relationName(name,
616 entry->name, namedRelation);
617 RelationIndex<TObject, TObject> relIndex(relationsName, c_Event);
618 if (!relIndex)
619 continue;
620
621 // get first element
622 if (searchSide == c_ToSide) {
623 const RelationIndex<TObject, TObject>::Element* element = relIndex.getFirstElementFrom(object);
624 if (element && element->to) {
625 return RelationEntry(const_cast<TObject*>(element->to), element->weight);
626 }
627 } else {
628 const RelationIndex<TObject, TObject>::Element* element = relIndex.getFirstElementTo(object);
629 if (element && element->from) {
630 return RelationEntry(const_cast<TObject*>(element->from), element->weight);
631 }
632 }
633 }
634
635 return RelationEntry(nullptr);
636}
Belle2::RelationEntry getRelationWith(ESearchSide searchSide, const TObject *object, StoreEntry *&entry, int &index, const TClass *withClass, const std::string &withName, const std::string &namedRelation)
Get the first relation between an object and another object in a store array.
Definition DataStore.cc:595
RelationIndexContainer< FROM, TO >::Element Element
Struct representing a single element in the index.
const FROM * from
pointer of the element from which the relation points.
const TO * to
pointer of the element to which the relation points.
RelationElement::weight_type weight
weight of the relation.

◆ getSortedListOfDataStore()

std::vector< std::string > getSortedListOfDataStore ( EDurability durability) const

Returns a (sorted) list of all the content of the DataStore (Objs-Arrays-Relations).

Definition at line 701 of file DataStore.cc.

702{
703 std::vector<string> list;
704 std::vector<std::string> mergeContent = getListOfObjects(TObject::Class(), durability);
705 list.insert(std::end(list), std::begin(mergeContent), std::end(mergeContent));
706 mergeContent = getListOfArrays(TObject::Class(), durability);
707 list.insert(std::end(list), std::begin(mergeContent), std::end(mergeContent));
708 mergeContent = getListOfRelations(durability);
709 list.insert(std::end(list), std::begin(mergeContent), std::end(mergeContent));
710 return list;
711}
std::vector< std::string > getListOfArrays(const TClass *arrayClass, EDurability durability) const
Returns a list of names of arrays which are of type (or inherit from) arrayClass.
Definition DataStore.cc:664
static std::vector< std::string > getListOfRelations(EDurability durability)
Returns a list of names of StoreObjPtr-objects whose class is (or inherits from) RelationContainer.
Definition DataStore.cc:686
static std::vector< std::string > getListOfObjects(const TClass *objClass, EDurability durability)
Returns a list of names of StoreObjPtr-objects whose class is (or inherits from) objClass.
Definition DataStore.cc:669

◆ getStoreEntryMap()

StoreEntryMap & getStoreEntryMap ( EDurability durability)
inline

Get a reference to the object/array map.

This is intended to be used for input/output or other framework-internal modules.

Definition at line 325 of file DataStore.h.

325{ return m_storeEntryMap[durability]; }

◆ getTClassFromDefaultArrayName()

TClass * getTClassFromDefaultArrayName ( const std::string & arrayName)
static

Tries to deduce the TClass from a default array name, which is generally the name of the C++ class with an appended 's'.

The namespace qualification Belle2:: can be omitted

Definition at line 115 of file DataStore.cc.

116{
117 if (arrayName.empty()) {
118 return nullptr;
119 } else if ('s' == arrayName.back()) {
120 std::string objectNameStr = arrayName.substr(0, arrayName.size() - 1);
121 return getTClassFromDefaultObjectName(objectNameStr);
122 } else {
123 return nullptr;
124 }
125}
static TClass * getTClassFromDefaultObjectName(const std::string &objectName)
Tries to deduce the TClass from a default object name, which is generally the name of the C++ class.
Definition DataStore.cc:104
static std::string arrayName(const TClass *t, const std::string &name)
Return the storage name for an object of the given TClass and name.
Definition DataStore.cc:163

◆ getTClassFromDefaultObjectName()

TClass * getTClassFromDefaultObjectName ( const std::string & objectName)
static

Tries to deduce the TClass from a default object name, which is generally the name of the C++ class.

The namespace qualification Belle2:: can be omitted.

Definition at line 104 of file DataStore.cc.

105{
106 // First look for an name without the namespace Belle2::
107 TClass* cl = TClass::GetClass(("Belle2::" + objectName).c_str());
108 if (not cl) {
109 // If this fails look for a name that already has the full namespace.
110 cl = TClass::GetClass(objectName.c_str());
111 }
112 return cl;
113}
static std::string objectName(const TClass *t, const std::string &name)
Return the storage name for an object of the given TClass and name.
Definition DataStore.cc:150

◆ hasRelation()

bool hasRelation ( const StoreAccessorBase & fromArray,
const StoreAccessorBase & toArray,
EDurability durability,
const std::string & namedRelation )

Check for the existence of a relation in the DataStore map.

Parameters
fromArrayOrigin of the relation
toArrayTarget of the relation
durabilityDecide with which durability map you want to perform the requested action.
namedRelationAdditional name for the relation, or "" for the default naming.
Returns
True if there is a registered relation.
See also
DependencyMap

Definition at line 268 of file DataStore.cc.

270{
271 // check that the input makes sense
272 if (!fromArray.isArray())
273 B2FATAL(fromArray.readableName() << " is not an array!");
274 if (!toArray.isArray())
275 B2FATAL(toArray.readableName() << " is not an array!");
276
277 // check the the namedRelation only contains regular characters
278 if (!std::regex_match(namedRelation, m_regexNamedRelationCheck))
279 B2FATAL("Named Relations can only contain alphabetic characters, given was: " << namedRelation);
280
281 // get the internal relation name from the name provided
282 const std::string& realname = relationName(fromArray.getName(), toArray.getName(), namedRelation);
283
284 // check whether the map entry exists
285 const auto& it = m_storeEntryMap[durability].find(realname);
286 if (it != m_storeEntryMap[durability].end()) return true;
287
288 return false;
289}
const std::regex m_regexNamedRelationCheck
Regular expression to check that no special characters and no white spaces are in the string given fo...
Definition DataStore.h:634

◆ Instance()

DataStore & Instance ( )
static

Instance of singleton Store.

This method is used to access the DataStore directly. It is used internally in the Store accessor classes.

Definition at line 53 of file DataStore.cc.

54{
55 static DataStore instance;
56 return instance;
57}
DataStore()
Hidden constructor, as it is a singleton.
Definition DataStore.cc:60

◆ invalidateData()

void invalidateData ( EDurability durability)

Clears all registered StoreEntry objects of a specified durability, invalidating all objects.

Called by the framework once the given durability is over. Users should usually not use this function without a good reason.

Definition at line 713 of file DataStore.cc.

714{
715 B2DEBUG(100, "Invalidating objects for durability " << durability);
716 m_storeEntryMap.invalidateData(durability);
718}
void clear(DataStore::EDurability durability=DataStore::c_Event)
Clear the cache of RelationIndexContainers with the given durability.

◆ mergeContentsTo()

void mergeContentsTo ( const std::string & id,
const std::vector< std::string > & entrylist_event = {} )

merge contents (actual array / object contents) of current DataStore to the DataStore with given ID.

Definition at line 813 of file DataStore.cc.

814{
815 m_storeEntryMap.mergeContentsTo(id, entrylist_event);
816}

◆ objectName() [1/2]

template<class T>
static std::string objectName ( const std::string & name)
inlinestatic

Return the storage name for an object of the given type and name.

Definition at line 137 of file DataStore.h.

138 {
139 return ((name.empty()) ? defaultObjectName<T>() : name);
140 }

◆ objectName() [2/2]

std::string objectName ( const TClass * t,
const std::string & name )
static

Return the storage name for an object of the given TClass and name.

Definition at line 150 of file DataStore.cc.

151{
152 return ((name.empty()) ? defaultObjectName(t) : name);
153}
static std::string defaultObjectName(const std::string &classname)
Return the default storage name for given class name.
Definition DataStore.cc:127

◆ optionalInput()

bool optionalInput ( const StoreAccessorBase & accessor)

Register the given object/array as an optional input.

Mainly useful for creating diagrams of module inputs and outputs.

Parameters
accessorEncapsulates name, durability, and type
Returns
True if the requested object exists.
See also
DependencyMap

Definition at line 737 of file DataStore.cc.

738{
739 if (m_initializeActive) {
740 m_dependencyMap->getCurrentModuleInfo().addEntry(accessor.getName(), DependencyMap::c_OptionalInput,
741 (accessor.getClass() == RelationContainer::Class()));
742 }
743
744 return (getEntry(accessor) != nullptr);
745}
@ c_OptionalInput
optional input.

◆ optionalRelation()

bool optionalRelation ( const StoreAccessorBase & fromArray,
const StoreAccessorBase & toArray,
EDurability durability,
std::string const & namedRelation )
static

Register the given relation as an optional input.

Mainly useful for creating diagrams of module inputs and outputs.

Parameters
fromArrayOrigin of the relation
toArrayTarget of the relation
durabilityDecide with which durability map you want to perform the requested action.
namedRelationAdditional name for the relation, or "" for the default naming1
Returns
True if the requested object exists.
See also
DependencyMap

Definition at line 764 of file DataStore.cc.

766{
767 if (!fromArray.isArray())
768 B2FATAL(fromArray.readableName() << " is not an array!");
769 if (!toArray.isArray())
770 B2FATAL(toArray.readableName() << " is not an array!");
771
772 const std::string& relName = relationName(fromArray.getName(), toArray.getName(), namedRelation);
773 return DataStore::Instance().optionalInput(StoreAccessorBase(relName, durability, RelationContainer::Class(), false));
774}
bool optionalInput(const StoreAccessorBase &accessor)
Register the given object/array as an optional input.
Definition DataStore.cc:737

◆ registerEntry()

bool registerEntry ( const std::string & name,
EDurability durability,
TClass * objClass,
bool array,
EStoreFlags storeFlags )

Register an entry in the DataStore map.

If the map of requested durability already contains an object under the key name with a DIFFERENT type than the given type, an error will be reported.
Otherwise a new map slot is created. This must be called in the initialization phase. Otherwise an error is returned.

Parameters
nameName under which you want to save the object in the DataStore.
durabilityDecide with which durability map you want to perform the requested action.
objClassThe class of the object.
arrayWhether it is a TClonesArray or not.
storeFlagsORed combination of DataStore::EStoreFlags.
Returns
True if the registration succeeded.
See also
DependencyMap

Definition at line 187 of file DataStore.cc.

189{
190 const StoreAccessorBase accessor(name, durability, objClass, array);
191 // Check whether this method is called in the initialization phase
192 if (!m_initializeActive) {
193 B2ERROR("Attempt to register " << accessor.readableName() <<
194 " outside of the initialization phase. Please move calls to registerInDataStore() into your Module's initialize() function.");
195 return false;
196 }
197 const bool dontwriteout = storeFlags & c_DontWriteOut;
198
199 //add to current module's outputs
200 m_dependencyMap->getCurrentModuleInfo().addEntry(name, DependencyMap::c_Output, (objClass == RelationContainer::Class()));
201
202 // Check whether the map entry already exists
203 const auto& it = m_storeEntryMap[durability].find(name);
204 if (it != m_storeEntryMap[durability].end()) {
205 StoreEntry& entry = it->second;
206
207 // Complain about existing entry
208 if (storeFlags & c_ErrorIfAlreadyRegistered) {
209 B2ERROR("An " << accessor.readableName() << " of type " << entry.object->ClassName() <<
210 " was already registered before. (Multiple calls to registerInDataStore() are fine if the c_ErrorIfAlreadyRegistered flag is not set. For objects you will want to make sure that you don't discard existing data from other modules in that case.");
211 return false;
212 }
213
214 // Check whether the types match
215 checkType(entry, accessor);
216
217 // Check whether the persistency type matches
218 if (entry.dontWriteOut != dontwriteout) {
219 B2WARNING("Existing " << accessor.readableName() << " has different persistency type than requested. Changing to " <<
220 (dontwriteout ? "c_DontWriteOut" : "c_WriteOut") << ".");
221 entry.dontWriteOut = dontwriteout;
222 }
223
224 B2DEBUG(100, "An " << accessor.readableName() << " was registered once more.");
225 return true;
226 }
227
228 // check reserved names
229 if (array and name == "ALL") {
230 B2ERROR("Creating an array with the reserved name 'ALL' is not allowed!");
231 return false;
232 }
233
234 // Add the DataStore entry
235 m_storeEntryMap[durability][name] = StoreEntry(array, objClass, name, dontwriteout);
236
237 B2DEBUG(100, "Successfully registered " << accessor.readableName());
238 return true;
239}
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition DataStore.h:71
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition DataStore.h:72
@ c_Output
registered output.
bool dontWriteOut
Flag that indicates whether the object should be written to the output by default.
Definition StoreEntry.h:40

◆ registerRelation()

bool registerRelation ( const StoreAccessorBase & fromArray,
const StoreAccessorBase & toArray,
EDurability durability,
EStoreFlags storeFlags,
const std::string & namedRelation )

Register a relation in the DataStore map.

This must be called in the initialization phase. Otherwise an error is returned.

Parameters
fromArrayOrigin of the relation
toArrayTarget of the relation
durabilityDecide with which durability map you want to perform the requested action.
storeFlagsORed combination of DataStore::EStoreFlags.
namedRelationAdditional name for the relation, or "" for the default naming
Returns
True if the registration succeeded.
See also
DependencyMap

Definition at line 241 of file DataStore.cc.

243{
244 if (!fromArray.isArray())
245 B2FATAL(fromArray.readableName() << " is not an array!");
246 if (!toArray.isArray())
247 B2FATAL(toArray.readableName() << " is not an array!");
248
249 // check the the namedRelation only contains regular characters
250 if (!std::regex_match(namedRelation, m_regexNamedRelationCheck))
251 B2FATAL("Named Relations can only contain alphabetic characters, given was: " << namedRelation);
252
253 const std::string& relName = relationName(fromArray.getName(), toArray.getName(), namedRelation);
254 /*
255 if ((fromArray.notWrittenOut() or toArray.notWrittenOut()) and !(storeFlags & c_DontWriteOut)) {
256 B2WARNING("You're trying to register a persistent relation " << relName << " from/to an array which is not written out (DataStore::c_DontWriteOut flag)! Relation will also not be saved!");
257 storeFlags |= c_DontWriteOut;
258 }
259 */
260
261 if (fromArray.getDurability() > durability or toArray.getDurability() > durability) {
262 B2FATAL("Tried to create a relation '" << relName << "' with a durability larger than the StoreArrays it relates");
263 }
264
265 return DataStore::Instance().registerEntry(relName, durability, RelationContainer::Class(), false, storeFlags);
266}
bool registerEntry(const std::string &name, EDurability durability, TClass *objClass, bool array, EStoreFlags storeFlags)
Register an entry in the DataStore map.
Definition DataStore.cc:187

◆ relationName()

static std::string relationName ( const std::string & fromName,
const std::string & toName,
std::string const & namedRelation = "" )
inlinestatic

Return storage name for a relation between two arrays of the given names.

Definition at line 180 of file DataStore.h.

182 {
183 std::string s;
184 s.reserve(fromName.length() + toName.length() + 2);
185 s += fromName;
186 s += "To";
187 s += toName;
188 if (namedRelation.length() > 0) {
189 s += "Named";
190 // Characters are not escaped here, because in registerRelation, the namedRelation
191 // given is checked to contain no special characters or white spaces
192 s += namedRelation;
193 }
194 return s;
195 }

◆ replaceData()

void replaceData ( const StoreAccessorBase & from,
const StoreAccessorBase & to )

For two StoreAccessors of same type, move all data in 'from' into 'to', discarding previous contents of 'to' and leaving 'from' empty.

Meta-data like c_DontWriteOut flags or info about associated arrays for RelationContainers is not replaced.

Definition at line 341 of file DataStore.cc.

342{
343 StoreEntry* fromEntry = getEntry(from);
344 StoreEntry* toEntry = getEntry(to);
345 if (!fromEntry)
346 B2FATAL("No " << from.readableName() << " exists in the DataStore!");
347 if (!toEntry)
348 B2FATAL("No " << to.readableName() << " exists in the DataStore!");
349 if (from.isArray() != to.isArray() or from.getClass() != to.getClass())
350 B2FATAL("cannot replace " << to.readableName() << " with " << from.readableName() << " (incompatible types)!");
351
352 if (!fromEntry->ptr) {
353 //since we don't need to move any data, just invalidate toEntry instead.
354 toEntry->ptr = nullptr;
355 } else if (fromEntry->isArray) {
356 if (!toEntry->ptr)
357 toEntry->ptr = toEntry->object;
358 toEntry->getPtrAsArray()->Delete();
359
360 fixAbsorbObjects(fromEntry->getPtrAsArray(), toEntry->getPtrAsArray());
362 } else if (fromEntry->objClass == RelationContainer::Class()) {
363 if (!toEntry->ptr)
364 toEntry->ptr = toEntry->object;
365 auto* fromRel = static_cast<RelationContainer*>(fromEntry->ptr);
366 auto* toRel = static_cast<RelationContainer*>(toEntry->ptr);
367
368 toRel->elements().Delete();
369
370 fixAbsorbObjects(&fromRel->elements(), &toRel->elements());
371
372 //indices need a rebuild
373 fromRel->setModified(true);
374 toRel->setModified(true);
375 } else {
376 delete toEntry->object;
377
378 toEntry->object = fromEntry->ptr->Clone();
379 toEntry->ptr = toEntry->object;
380
381 fromEntry->ptr = nullptr;
382 }
383}
TClonesArray * getPtrAsArray() const
Return ptr cast to TClonesArray.
Definition StoreEntry.cc:83

◆ requireInput()

bool requireInput ( const StoreAccessorBase & accessor)

Produce ERROR message if no entry of the given type is registered in the DataStore.

Note
can only be used in initialize() function
Parameters
accessorEncapsulates name, durability, and type
Returns
True if the requested object exists.
See also
DependencyMap

Definition at line 720 of file DataStore.cc.

721{
722 if (m_initializeActive) {
723 m_dependencyMap->getCurrentModuleInfo().addEntry(accessor.getName(), DependencyMap::c_Input,
724 (accessor.getClass() == RelationContainer::Class()));
725 } else {
726 B2FATAL("Attempt to require input " << accessor.readableName() <<
727 " outside of the initialization phase. Please move isRequired() calls into your Module's initialize() function.");
728 }
729
730 if (!getEntry(accessor)) {
731 B2ERROR("The required " << accessor.readableName() << " does not exist. Maybe you forgot the module that registers it?");
732 return false;
733 }
734 return true;
735}
@ c_Input
required input.

◆ requireRelation()

bool requireRelation ( const StoreAccessorBase & fromArray,
const StoreAccessorBase & toArray,
EDurability durability,
std::string const & namedRelation )

Produce ERROR message if no relation of given durability exists between fromArray and toArray (in that direction).

Note
can only be used in initialize() function
Parameters
fromArrayOrigin of the relation
toArrayTarget of the relation
durabilityDecide with which durability map you want to perform the requested action.
namedRelationAdditional name for the relation, or "" for the default naming
Returns
True if the requested object exists.
See also
DependencyMap

Definition at line 747 of file DataStore.cc.

749{
750 if (!m_initializeActive) {
751 B2FATAL("Attempt to require relation " << fromArray.readableName() << " -> " << toArray.readableName() <<
752 " outside of the initialization phase. Please move requireRelationTo() calls into your Module's initialize() function.");
753 }
754
755 if (!fromArray.isArray())
756 B2FATAL(fromArray.readableName() << " is not an array!");
757 if (!toArray.isArray())
758 B2FATAL(toArray.readableName() << " is not an array!");
759
760 const std::string& relName = relationName(fromArray.getName(), toArray.getName(), namedRelation);
761 return DataStore::Instance().requireInput(StoreAccessorBase(relName, durability, RelationContainer::Class(), false));
762}
bool requireInput(const StoreAccessorBase &accessor)
Produce ERROR message if no entry of the given type is registered in the DataStore.
Definition DataStore.cc:720

◆ reset() [1/2]

void reset ( )

Clears contents of the datastore (all durabilities)

Called by the framework. Users should usually not use this function without good reason.

Definition at line 73 of file DataStore.cc.

74{
75 B2DEBUG(31, "DataStore::reset(): Removing all elements from DataStore");
76 m_initializeActive = true;
77 m_dependencyMap->clear();
78
79 for (int i = 0; i < c_NDurabilityTypes; i++)
81
82 m_storeEntryMap.clear();
83}
static const int c_NDurabilityTypes
Number of Durability Types.
Definition DataStore.h:63

◆ reset() [2/2]

void reset ( EDurability durability)

Frees memory occupied by data store items and removes all objects from the map.

Afterwards, m_storeEntryMap[durability] is empty. Called by the framework. Users should usually not use this function without good reason.

Definition at line 85 of file DataStore.cc.

86{
87 m_storeEntryMap.reset(durability);
88
89 //invalidate any cached relations (expect RelationArrays to remain valid)
91}
void reset()
Reset the cache completely, that is clear all caches and don't even keep the Index objects around.

◆ setInitializeActive()

void setInitializeActive ( bool active)

Setter for m_initializeActive.

This should only be called by EventProcessor.

Definition at line 93 of file DataStore.cc.

94{
95 m_initializeActive = active;
96
97 static bool firstCall = true;
98 if (firstCall) {
99 atexit(cleanDataStore);
100 firstCall = false;
101 }
102}

◆ switchID()

void switchID ( const std::string & id)

switch to DataStore with given ID.

Definition at line 792 of file DataStore.cc.

793{
794 if (id == m_storeEntryMap.currentID())
795 return;
796
797 //remember to clear caches
799
800 m_storeEntryMap.switchID(id);
801}

◆ updateRelationsObjectCache()

void updateRelationsObjectCache ( StoreEntry & entry)
staticprivate

For an array containing RelationsObjects, update index and entry cache for entire contents.

You must ensure the array actually contains objects inheriting from RelationsObject!

Definition at line 385 of file DataStore.cc.

386{
387 const TClonesArray* array = static_cast<TClonesArray*>(entry.ptr);
388 const int nEntries = array->GetEntriesFast();
389 for (int i = 0; i < nEntries; i++) {
390 auto* relobj = static_cast<RelationsObject*>(array->At(i));
391 relobj->m_cacheArrayIndex = i;
392 relobj->m_cacheDataStoreEntry = &entry;
393 }
394}
int m_cacheArrayIndex
Cache of the index in the TClonesArray to which this object belongs.

Member Data Documentation

◆ c_NDurabilityTypes

const int c_NDurabilityTypes = 2
static

Number of Durability Types.

Definition at line 63 of file DataStore.h.

◆ m_dependencyMap

DependencyMap* m_dependencyMap
private

Collect information about the dependencies between modules.

Definition at line 637 of file DataStore.h.

◆ m_initializeActive

bool m_initializeActive
private

True if modules are currently being initialized.

Creating new map slots is only allowed in a Module's initialize() function.

Definition at line 628 of file DataStore.h.

◆ m_regexNamedRelationCheck

const std::regex m_regexNamedRelationCheck = std::regex("^[a-zA-Z]*$")
private

Regular expression to check that no special characters and no white spaces are in the string given for namedRelations.

Definition at line 634 of file DataStore.h.

◆ m_storeEntryMap

SwitchableDataStoreContents m_storeEntryMap
private

Maps (name, durability) key to StoreEntry objects.

Definition at line 621 of file DataStore.h.

◆ s_DoCleanup

bool s_DoCleanup = false
static

Global flag to to decide if we can do normal cleanup.

If the normal basf2 execution is bypassed, e.g. in tests or separate binaries, we cannot guarantee that exit handlers are called in the correct order. Since this may lead to double deletion of objects through ROOT and misleading error stack traces, we will skip cleanup in these exceptional cases.

Definition at line 100 of file DataStore.h.


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