Belle II Software  release-08-01-10
RelationsObject.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 
9 #pragma once
10 
11 #include <framework/datastore/DataStore.h>
12 #include <framework/datastore/RelationVector.h>
13 #include <framework/datastore/RelationEntry.h>
14 
15 #include <TObject.h>
16 
17 #include <string>
18 
19 class TClonesArray;
20 
21 namespace Belle2 {
27  namespace _RelationsInterfaceImpl {
29  std::string htmlToPlainText(const std::string& html);
30  };
31 
100  template <class BASE> class RelationsInterface: public BASE {
102  static_assert(std::is_same<TObject, BASE>::value,
103  "Using RelationsInterface<BASE> is no longer allowed. Please use RelationsObject as a base class.");
104  public:
108 
110  template<class ...Args> explicit RelationsInterface(Args&& ... params):
111  BASE(std::forward<Args>(params)...),
112  m_cacheDataStoreEntry(nullptr), m_cacheArrayIndex(-1) { }
113 
119  RelationsInterface(const RelationsInterface& relationsInterface):
120  BASE(relationsInterface),
121  m_cacheDataStoreEntry(nullptr), m_cacheArrayIndex(-1) { }
122 
128  RelationsInterface& operator=(const RelationsInterface& relationsInterface)
129  {
130  if (this != &relationsInterface)
131  this->BASE::operator=(relationsInterface);
132  return *this;
133  }
134 
135 
142  void addRelationTo(const RelationsInterface<BASE>* object, float weight = 1.0, const std::string& namedRelation = "") const
143  {
144  if (object)
146  object, object->m_cacheDataStoreEntry, object->m_cacheArrayIndex, weight, namedRelation);
147  }
148 
155  void addRelationTo(const TObject* object, float weight = 1.0, const std::string& namedRelation = "") const
156  {
157  StoreEntry* toEntry = nullptr;
158  int toIndex = -1;
159  DataStore::Instance().addRelation(this, m_cacheDataStoreEntry, m_cacheArrayIndex, object, toEntry, toIndex, weight, namedRelation);
160  }
161 
171  {
172  if (!sourceObj)
173  return;
174  auto fromRels = sourceObj->getRelationsFrom<RelationsInterface<BASE>>("ALL");
175  for (unsigned int iRel = 0; iRel < fromRels.size(); iRel++) {
176  fromRels.object(iRel)->addRelationTo(this, fromRels.weight(iRel));
177  }
178 
179  auto toRels = sourceObj->getRelationsTo<RelationsInterface<BASE>>("ALL");
180  for (unsigned int iRel = 0; iRel < toRels.size(); iRel++) {
181  this->addRelationTo(toRels.object(iRel), toRels.weight(iRel));
182  }
183  }
184 
185  //===========================================================================
186  //These return a vector of relations:
187 
197  template <class TO> RelationVector<TO> getRelationsTo(const std::string& name = "", const std::string& namedRelation = "") const
198  {
200  m_cacheArrayIndex, TO::Class(), name, namedRelation));
201  }
202 
212  template <class FROM> RelationVector<FROM> getRelationsFrom(const std::string& name = "",
213  const std::string& namedRelation = "") const
214  {
216  m_cacheArrayIndex, FROM::Class(), name, namedRelation));
217  }
218 
230  template <class T> RelationVector<T> getRelationsWith(const std::string& name = "", const std::string& namedRelation = "") const
231  {
233  m_cacheArrayIndex, T::Class(), name, namedRelation));
234  }
235 
236  //===========================================================================
237  //These return only the first related object
238 
248  template <class TO> TO* getRelatedTo(const std::string& name = "", const std::string& namedRelation = "") const
249  {
251  TO::Class(), name, namedRelation).object);
252  }
253 
263  template <class FROM> FROM* getRelatedFrom(const std::string& name = "", const std::string& namedRelation = "") const
264  {
266  m_cacheArrayIndex, FROM::Class(), name, namedRelation).object);
267  }
268 
278  template <class T> T* getRelated(const std::string& name = "", const std::string& namedRelation = "") const
279  {
281  T::Class(), name, namedRelation).object);
282  }
283 
284 
285  //===========================================================================
286  //These return an object/weight pair
287 
297  template <class TO> std::pair<TO*, float> getRelatedToWithWeight(const std::string& name = "",
298  const std::string& namedRelation = "") const
299  {
301  TO::Class(), name, namedRelation);
302  return std::make_pair(static_cast<TO*>(entry.object), entry.weight);
303  }
304 
314  template <class FROM> std::pair<FROM*, float> getRelatedFromWithWeight(const std::string& name = "",
315  const std::string& namedRelation = "") const
316  {
318  FROM::Class(), name, namedRelation);
319  return std::make_pair(static_cast<FROM*>(entry.object), entry.weight);
320  }
321 
331  template <class T> std::pair<T*, float> getRelatedWithWeight(const std::string& name = "",
332  const std::string& namedRelation = "") const
333  {
335  T::Class(), name, namedRelation);
336  return std::make_pair(static_cast<T*>(entry.object), entry.weight);
337  }
338 
339 
340  //===========================================================================
341  // Information about this object (not strictly relation-related)
342 
344  virtual std::string getName() const { return ""; }
345 
362  virtual std::string getInfoHTML() const { return ""; }
363 
370  std::string getInfo() const
371  {
373  }
374 
375 
377  std::string getArrayName() const
378  {
381  }
382 
383 
385  int getArrayIndex() const
386  {
388  return m_cacheArrayIndex;
389  }
390 
391 
392 #if defined(__CINT__) || defined(__ROOTCLING__) || defined(R__DICTIONARY_FILENAME)
393 
398  RelationVector<TObject> getRelationsTo(const std::string& name) const
399  {
400  return getRelationsTo<TObject>(name);
401  }
402  RelationVector<TObject> getRelationsFrom(const std::string& name) const
403  {
404  return getRelationsFrom<TObject>(name);
405  }
406  RelationVector<TObject> getRelationsWith(const std::string& name) const
407  {
408  return getRelationsWith<TObject>(name);
409  }
410  TObject* getRelatedTo(const std::string& name, const std::string& namedRelation = "") const { return getRelatedTo<TObject>(name, namedRelation); }
411  TObject* getRelatedFrom(const std::string& name, const std::string& namedRelation = "") const { return getRelatedFrom<TObject>(name, namedRelation); }
412  TObject* getRelated(const std::string& name, const std::string& namedRelation = "") const { return getRelated<TObject>(name, namedRelation); }
414 #endif
415  protected:
416 
418  TClonesArray* getArrayPointer() const
419  {
422  return nullptr;
424  }
425 
426  private:
427 
430 
432  mutable int m_cacheArrayIndex;
433 
434  friend class DataStore;
435 
436  //version 0 to disable streaming
438  };
439 
443 }
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
bool findStoreEntry(const TObject *object, StoreEntry *&entry, int &index)
Find an object in an array in the data store.
Definition: DataStore.cc:398
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:492
@ 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 DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
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:597
Class for type safe access to objects that are referred to in relations.
Defines interface for accessing relations of objects in StoreArray.
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
RelationsInterface(const RelationsInterface &relationsInterface)
Copy constructor.
std::pair< T *, float > getRelatedWithWeight(const std::string &name="", const std::string &namedRelation="") const
Get first related object & weight of relation pointing from/to an array.
TClonesArray * getArrayPointer() const
Returns the pointer to the raw DataStore array holding this object (protected since these arrays are ...
std::string getArrayName() const
Get name of array this object is stored in, or "" if not found.
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
RelationsInterface & operator=(const RelationsInterface &relationsInterface)
Assignment operator.
RelationVector< FROM > getRelationsFrom(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from another store array to this object.
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.
void addRelationTo(const TObject *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (no caching, can be quite slow).
virtual std::string getInfoHTML() const
Return a short summary of this object's contents in HTML format.
virtual std::string getName() const
Return a short name that describes this object, e.g.
std::pair< FROM *, float > getRelatedFromWithWeight(const std::string &name="", const std::string &namedRelation="") const
Get first related object & weight of relation pointing from an array.
ClassDef(RelationsInterface, 0)
defines interface for accessing relations of objects in StoreArray.
DataStore::StoreEntry * m_cacheDataStoreEntry
Cache of the data store entry to which this object belongs.
RelationsInterface(Args &&... params)
Constructor, forwards all arguments to BASE constructor.
std::pair< TO *, float > getRelatedToWithWeight(const std::string &name="", const std::string &namedRelation="") const
Get first related object & weight of relation pointing to an array.
int m_cacheArrayIndex
Cache of the index in the TClonesArray to which this object belongs.
FROM * getRelatedFrom(const std::string &name="", const std::string &namedRelation="") const
Get the object from which this object has a relation.
T * getRelated(const std::string &name="", const std::string &namedRelation="") const
Get the object to or from which this object has a relation.
std::string getInfo() const
Return a short summary of this object's contents in raw text format.
RelationVector< T > getRelationsWith(const std::string &name="", const std::string &namedRelation="") const
Get the relations between this object and another store array.
void copyRelations(const RelationsInterface< BASE > *sourceObj)
Copies all relations of sourceObj (pointing from or to sourceObj) to this object (including weights).
TO * getRelatedTo(const std::string &name="", const std::string &namedRelation="") const
Get the object to which this object has a relation.
RelationsInterface()
template class cannot be removed without breaking ROOT I/O, so just disable it.
RelationsInterface< TObject > RelationsObject
Provides interface for getting/adding relations to objects in StoreArrays.
std::string htmlToPlainText(const std::string &html)
See RelationsObject::getInfo()
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
float weight
Weight of the relation.
Definition: RelationEntry.h:33
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:22
TClonesArray * getPtrAsArray() const
Return ptr cast to TClonesArray.
Definition: StoreEntry.cc:83
std::string name
Name of the entry.
Definition: StoreEntry.h:53