Belle II Software  release-06-00-14
DataStore.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <framework/datastore/StoreEntry.h>
11 #include <framework/core/BitMask.h>
12 
13 #if defined(__CINT__) || defined(__ROOTCLING__) || defined(R__DICTIONARY_FILENAME)
14 //a few methods use these, but are only included in dictionaries
15 #include <framework/datastore/RelationVector.h>
16 #include <framework/datastore/RelationEntry.h>
17 #endif
18 
19 #include <regex>
20 #include <array>
21 #include <vector>
22 #include <string>
23 #include <map>
24 
25 class TObject;
26 class TClass;
27 
28 namespace Belle2 {
33  class StoreAccessorBase;
34  class DependencyMap;
35  class RelationVectorBase;
36  template <class T> class RelationVector;
37  struct RelationEntry;
38 
51  class DataStore {
52  public:
53  //----------------------------------- enums and typedefs ---------------------------------------------------
58  enum EDurability {
61  };
63  const static int c_NDurabilityTypes = 2;
64 
69  enum EStoreFlags {
70  c_WriteOut = 0,
73  };
74 
76  enum ESearchSide {
80  };
81 
82 
85 
86  // Convenient typedefs.
87  typedef std::map<std::string, StoreEntry> StoreEntryMap;
88  typedef StoreEntryMap::iterator StoreEntryIter;
89  typedef StoreEntryMap::const_iterator StoreEntryConstIter;
90  typedef std::array<StoreEntryMap, c_NDurabilityTypes> DataStoreContents;
100  static bool s_DoCleanup;
101 
102 
103  //--------------------------------- Instance ---------------------------------------------------------------
108  static DataStore& Instance();
109 
110  //--------------------------------- default name stuff -----------------------------------------------------
111 
114  static TClass* getTClassFromDefaultObjectName(const std::string& objectName);
115 
118  static TClass* getTClassFromDefaultArrayName(const std::string& arrayName);
119 
121  static std::string defaultObjectName(const std::string& classname);
122 
124  static std::string defaultObjectName(const TClass* t);
125 
127  template<class T> static std::string defaultObjectName()
128  {
129  const static std::string s = defaultObjectName(T::Class_Name());
130  return s;
131  }
132 
134  static std::string objectName(const TClass* t, const std::string& name);
135 
137  template<class T> static std::string objectName(const std::string& name)
138  {
139  return ((name.empty()) ? defaultObjectName<T>() : name);
140  }
141 
143  static std::string defaultArrayName(const std::string& classname)
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  }
152 
154  static std::string defaultArrayName(const TClass* t);
155 
157  template<class T> static std::string defaultArrayName()
158  {
159  const static std::string s = defaultArrayName(defaultObjectName<T>());
160  return s;
161  }
162 
164  static std::string arrayName(const TClass* t, const std::string& name);
165 
167  template<class T> static std::string arrayName(const std::string& name)
168  {
169  return ((name.empty()) ? defaultArrayName<T>() : name);
170  }
171 
173  template<class FROM, class TO> static std::string defaultRelationName()
174  {
175  const static std::string s = relationName(defaultArrayName<FROM>(), defaultArrayName<TO>());
176  return s;
177  }
178 
180  static std::string relationName(const std::string& fromName, const std::string& toName,
181  std::string const& namedRelation = "")
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  }
196 
197  //------------------------------ Accessing objects and arrays ----------------------------------------------
212  bool registerEntry(const std::string& name, EDurability durability,
213  TClass* objClass, bool array, EStoreFlags storeFlags);
214 
226  bool registerRelation(const StoreAccessorBase& fromArray, const StoreAccessorBase& toArray, EDurability durability,
227  EStoreFlags storeFlags, const std::string& namedRelation);
228 
238  bool hasRelation(const StoreAccessorBase& fromArray, const StoreAccessorBase& toArray, EDurability durability,
239  const std::string& namedRelation);
240 
248  bool requireInput(const StoreAccessorBase& accessor);
249 
260  bool requireRelation(const StoreAccessorBase& fromArray, const StoreAccessorBase& toArray, EDurability durability,
261  std::string const& namedRelation);
262 
272  bool optionalInput(const StoreAccessorBase& accessor);
273 
284  bool optionalRelation(const StoreAccessorBase& fromArray, const StoreAccessorBase& toArray, EDurability durability,
285  std::string const& namedRelation);
286 
295  StoreEntry* getEntry(const StoreAccessorBase& accessor);
296 
304  TObject** getObject(const StoreAccessorBase& accessor);
305 
314  bool createObject(TObject* object, bool replace, const StoreAccessorBase& accessor);
315 
320  void replaceData(const StoreAccessorBase& from, const StoreAccessorBase& to);
321 
326  StoreEntryMap& getStoreEntryMap(EDurability durability) { return m_storeEntryMap[durability]; }
327 
328 
340  void addRelation(const TObject* fromObject, StoreEntry*& fromEntry, int& fromIndex, const TObject* toObject, StoreEntry*& toEntry,
341  int& toIndex, float weight, const std::string& namedRelation);
342 
343 
358  RelationVectorBase getRelationsWith(ESearchSide searchSide, const TObject* object, StoreEntry*& entry, int& index,
359  const TClass* withClass, const std::string& withName, const std::string& namedRelation);
360 
375  Belle2::RelationEntry getRelationWith(ESearchSide searchSide, const TObject* object, StoreEntry*& entry, int& index,
376  const TClass* withClass, const std::string& withName, const std::string& namedRelation);
377 
388  static void addRelationFromTo(const TObject* fromObject, const TObject* toObject, float weight = 1.0,
389  const std::string& namedRelation = "")
390  {
391  DataStore::StoreEntry* fromEntry = nullptr;
392  int fromIndex = -1;
393  StoreEntry* toEntry = nullptr;
394  int toIndex = -1;
395  Instance().addRelation(fromObject, fromEntry, fromIndex, toObject, toEntry, toIndex, weight, namedRelation);
396  }
397 
413  template <class T> static RelationVector<T> getRelationsWithObj(const TObject* object, const std::string& name = "",
414  const std::string& namedRelation = "")
415  {
416  StoreEntry* storeEntry = nullptr;
417  int index = -1;
418  return RelationVector<T>(Instance().getRelationsWith(c_BothSides, object, storeEntry, index, T::Class(), name, namedRelation));
419  }
420 
433  template <class T> static T* getRelated(const TObject* object, const std::string& name = "", const std::string& namedRelation = "")
434  {
435  if (!object) return nullptr;
436  StoreEntry* storeEntry = nullptr;
437  int index = -1;
438  return static_cast<T*>(DataStore::Instance().getRelationWith(c_BothSides, object, storeEntry, index, T::Class(), name,
439  namedRelation).object);
440  }
441 
442 #if defined(__CINT__) || defined(__ROOTCLING__) || defined(R__DICTIONARY_FILENAME)
443 
448  static RelationVector<TObject> getRelationsWithObj(const TObject* object, const std::string& name)
449  {
450  return getRelationsWithObj<TObject>(object, name);
451  }
452  static TObject* getRelated(const TObject* object, const std::string& name) { return getRelated<TObject>(object, name); }
454 #endif
455 
469  bool findStoreEntry(const TObject* object, StoreEntry*& entry, int& index);
470 
475  std::vector<std::string> getListOfRelatedArrays(const StoreAccessorBase& array) const;
476 
478  std::vector<std::string> getListOfArrays(const TClass* arrayClass, EDurability durability) const;
479 
484  std::vector<std::string> getListOfObjects(const TClass* objClass, EDurability durability) const;
485 
486 
487  //------------------------------ For internal use --------------------------------------------------
492  void setInitializeActive(bool active);
493 
495  bool getInitializeActive() const { return m_initializeActive; }
496 
501  void invalidateData(EDurability durability);
502 
508  void reset(EDurability durability);
509 
514  void reset();
515 
518 
519 
521  void createNewDataStoreID(const std::string& id);
523  std::string currentID() const;
525  void switchID(const std::string& id);
527  void copyEntriesTo(const std::string& id, const std::vector<std::string>& entrylist_event = {});
529  void copyContentsTo(const std::string& id, const std::vector<std::string>& entrylist_event = {});
530 
531  private:
533  explicit DataStore();
535  DataStore(const DataStore&) = delete;
537  DataStore& operator=(const DataStore&) = delete;
539  ~DataStore();
540 
549  bool checkType(const StoreEntry& entry, const StoreAccessorBase& accessor) const;
550 
557  const std::vector<std::string>& getArrayNames(const std::string& arrayName, const TClass* arrayClass,
558  EDurability durability = c_Event) const;
559 
564  static void updateRelationsObjectCache(StoreEntry& entry);
565 
571  public:
574  void clear();
576  void reset(EDurability durability);
578  void invalidateData(EDurability durability);
580  const StoreEntryMap& operator [](int durability) const { return m_entries[m_currentIdx][durability]; }
582  StoreEntryMap& operator [](int durability)
583  {
584  //reuse const implementation
585  const SwitchableDataStoreContents* this2 = this;
586  return const_cast<StoreEntryMap&>((*this2)[durability]);
587  }
588 
590  void switchID(const std::string& id);
592  const std::string& currentID() const { return m_currentID; }
594  void copyEntriesTo(const std::string& id, const std::vector<std::string>& entrylist_event = {});
596  void copyContentsTo(const std::string& id, const std::vector<std::string>& entrylist_event = {});
598  void createNewDataStoreID(const std::string& id);
599  private:
600  std::vector<DataStoreContents> m_entries;
601  std::map<std::string, int> m_idToIndexMap;
602  std::string m_currentID = "";
603  int m_currentIdx = 0;
604  };
607 
608 
614 
619  const std::regex m_regexNamedRelationCheck = std::regex("^[a-zA-Z]*$");
620 
623  };
624 
627 } // namespace Belle2
Encapsulates DataStoreContents, but allows transparently switching between different versions ('DataS...
Definition: DataStore.h:570
const std::string & currentID() const
returns ID of current DataStore.
Definition: DataStore.h:592
std::string m_currentID
currently active DataStore ID.
Definition: DataStore.h:602
void createNewDataStoreID(const std::string &id)
creates new datastore with given id, copying the registered objects/arrays from the current one.
Definition: DataStore.cc:788
std::vector< DataStoreContents > m_entries
wrapped DataStoreContents.
Definition: DataStore.h:600
void switchID(const std::string &id)
switch to DataStore with given ID.
Definition: DataStore.cc:887
std::map< std::string, int > m_idToIndexMap
Maps DataStore ID to index in m_entries.
Definition: DataStore.h:601
int m_currentIdx
index of currently active DataStore.
Definition: DataStore.h:603
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: DataStore.cc:846
const StoreEntryMap & operator[](int durability) const
Get StoreEntry map for given durability (and current DataStore ID).
Definition: DataStore.h:580
void clear()
same as calling reset() for all durabilities + all non-default datastore IDs are removed.
Definition: DataStore.cc:897
void reset(EDurability durability)
Frees memory occupied by data store items and removes all objects from the map.
Definition: DataStore.cc:910
void invalidateData(EDurability durability)
Clears all registered StoreEntry objects of a specified durability, invalidating all objects.
Definition: DataStore.cc:919
void copyEntriesTo(const std::string &id, const std::vector< std::string > &entrylist_event={})
copy entries (not contents) of current DataStore to the DataStore with given ID.
Definition: DataStore.cc:798
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
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:149
StoreEntryMap::const_iterator StoreEntryConstIter
const_iterator for a StoreEntry map.
Definition: DataStore.h:89
std::array< StoreEntryMap, c_NDurabilityTypes > DataStoreContents
StoreEntry maps for each durability.
Definition: DataStore.h:90
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.
Definition: DataStore.h:388
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'.
Definition: DataStore.cc:638
bool getInitializeActive() const
Are we currently initializing modules?
Definition: DataStore.h:495
void createNewDataStoreID(const std::string &id)
creates new datastore with given id, copying the registered objects/arrays from the current one.
Definition: DataStore.cc:750
static std::string defaultArrayName(const std::string &classname)
Return the default storage name for an given class name.
Definition: DataStore.h:143
DependencyMap & getDependencyMap()
Return map of depedencies between modules.
Definition: DataStore.h:517
bool findStoreEntry(const TObject *object, StoreEntry *&entry, int &index)
Find an object in an array in the data store.
Definition: DataStore.cc:396
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
static std::string defaultObjectName()
Return the default storage name for an object of the given type.
Definition: DataStore.h:127
bool checkType(const StoreEntry &entry, const StoreAccessorBase &accessor) const
Check whether the given entry and the requested class match.
Definition: DataStore.cc:168
bool optionalRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, std::string const &namedRelation)
Register the given relation as an optional input.
Definition: DataStore.cc:737
EStoreFlags
Flags describing behaviours of objects etc.
Definition: DataStore.h:69
@ c_WriteOut
Object/array should be saved by output modules.
Definition: DataStore.h:70
@ 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
void switchID(const std::string &id)
switch to DataStore with given ID.
Definition: DataStore.cc:760
StoreEntryMap::iterator StoreEntryIter
Iterator for a StoreEntry map.
Definition: DataStore.h:88
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:619
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 wi...
Definition: DataStore.cc:114
static std::string objectName(const std::string &name)
Return the storage name for an object of the given type and name.
Definition: DataStore.h:137
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.
Definition: DataStore.cc:490
bool registerRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, EStoreFlags storeFlags, const std::string &namedRelation)
Register a relation in the DataStore map.
Definition: DataStore.cc:242
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:103
StoreEntryMap & getStoreEntryMap(EDurability durability)
Get a reference to the object/array map.
Definition: DataStore.h:326
DataStore()
Hidden constructor, as it is a singleton.
Definition: DataStore.cc:59
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.
Definition: DataStore.h:413
DependencyMap * m_dependencyMap
Collect information about the dependencies between modules.
Definition: DataStore.h:622
~DataStore()
Destructor.
Definition: DataStore.cc:63
static std::string defaultArrayName()
Return the default storage name for an array of the given type.
Definition: DataStore.h:157
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
ESearchSide
Which side of relations should be returned?
Definition: DataStore.h:76
@ c_BothSides
Combination of c_FromSide and c_ToSide.
Definition: DataStore.h:79
@ c_FromSide
Return relations/objects pointed from (to a given object).
Definition: DataStore.h:77
@ c_ToSide
Return relations/objects pointed to (from a given object).
Definition: DataStore.h:78
static void updateRelationsObjectCache(StoreEntry &entry)
For an array containing RelationsObjects, update index and entry cache for entire contents.
Definition: DataStore.cc:385
static const int c_NDurabilityTypes
Number of Durability Types.
Definition: DataStore.h:63
std::vector< std::string > getListOfObjects(const TClass *objClass, EDurability durability) const
Returns a list of names of StoreObjPtr-objects whose class is (or inherits from) objClass.
Definition: DataStore.cc:669
static std::string defaultRelationName()
Return the default storage name for a relation between the given types.
Definition: DataStore.h:173
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: DataStore.cc:776
bool requireInput(const StoreAccessorBase &accessor)
Produce ERROR message if no entry of the given type is registered in the DataStore.
Definition: DataStore.cc:693
EDurability
Durability types.
Definition: DataStore.h:58
@ c_Persistent
Object is available during entire execution time.
Definition: DataStore.h:60
@ 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
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 tha...
Definition: DataStore.cc:720
std::string currentID() const
returns ID of current DataStore.
Definition: DataStore.cc:755
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.
Definition: DataStore.h:433
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:52
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.
Definition: DataStore.cc:269
void setInitializeActive(bool active)
Setter for m_initializeActive.
Definition: DataStore.cc:92
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
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:72
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:292
void invalidateData(EDurability durability)
Clears all registered StoreEntry objects of a specified durability, invalidating all objects.
Definition: DataStore.cc:686
static std::string arrayName(const std::string &name)
Return the storage name for an object of the given type and name.
Definition: DataStore.h:167
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
void copyEntriesTo(const std::string &id, const std::vector< std::string > &entrylist_event={})
copy entries (not contents) of current DataStore to the DataStore with given ID.
Definition: DataStore.cc:771
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
bool createObject(TObject *object, bool replace, const StoreAccessorBase &accessor)
Create a new object/array in the DataStore or add an existing one.
Definition: DataStore.cc:314
TObject ** getObject(const StoreAccessorBase &accessor)
Get a pointer to a pointer of an object in the DataStore.
Definition: DataStore.cc:304
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:162
bool m_initializeActive
True if modules are currently being initialized.
Definition: DataStore.h:613
DataStore & operator=(const DataStore &)=delete
no assignment operator
bool registerEntry(const std::string &name, EDurability durability, TClass *objClass, bool array, EStoreFlags storeFlags)
Register an entry in the DataStore map.
Definition: DataStore.cc:188
bool optionalInput(const StoreAccessorBase &accessor)
Register the given object/array as an optional input.
Definition: DataStore.cc:710
DataStore(const DataStore &)=delete
no copy constructor
SwitchableDataStoreContents m_storeEntryMap
Maps (name, durability) key to StoreEntry objects.
Definition: DataStore.h:606
void replaceData(const StoreAccessorBase &from, const StoreAccessorBase &to)
For two StoreAccessors of same type, move all data in 'from' into 'to', discarding previous contents ...
Definition: DataStore.cc:341
std::map< std::string, StoreEntry > StoreEntryMap
Map for StoreEntries.
Definition: DataStore.h:87
Collect information about the dependencies between modules.
Definition: DependencyMap.h:29
base class for RelationVector<T>
Class for type safe access to objects that are referred to in relations.
Base class for StoreObjPtr and StoreArray for easier common treatment.
ADD_BITMASK_OPERATORS(DataStore::EStoreFlags)
Add bitmask operators to DataStore::EStoreFlags.
Abstract base class for different kinds of events.
Struct for relations.
Definition: RelationEntry.h:24
TObject * object
Pointer to the object.
Definition: RelationEntry.h:32
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:22