Belle II Software  release-05-02-19
RelationsObject.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2012-2014 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Kuhr, Christian Pulvermacher *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/datastore/DataStore.h>
14 #include <framework/datastore/RelationVector.h>
15 #include <framework/datastore/RelationEntry.h>
16 
17 #include <TObject.h>
18 
19 #include <string>
20 
21 class TClonesArray;
22 
23 namespace Belle2 {
29  namespace _RelationsInterfaceImpl {
31  std::string htmlToPlainText(const std::string& html);
32  };
33 
102  template <class BASE> class RelationsInterface: public BASE {
104  static_assert(std::is_same<TObject, BASE>::value,
105  "Using RelationsInterface<BASE> is no longer allowed. Please use RelationsObject as a base class.");
106  public:
110 
112  template<class ...Args> explicit RelationsInterface(Args&& ... params):
113  BASE(std::forward<Args>(params)...),
115 
121  RelationsInterface(const RelationsInterface& relationsInterface):
122  BASE(relationsInterface),
124 
130  RelationsInterface& operator=(const RelationsInterface& relationsInterface)
131  {
132  if (this != &relationsInterface)
133  this->BASE::operator=(relationsInterface);
134  return *this;
135  }
136 
137 
144  void addRelationTo(const RelationsInterface<BASE>* object, float weight = 1.0, const std::string& namedRelation = "") const
145  {
146  if (object)
148  object, object->m_cacheDataStoreEntry, object->m_cacheArrayIndex, weight, namedRelation);
149  }
150 
157  void addRelationTo(const TObject* object, float weight = 1.0, const std::string& namedRelation = "") const
158  {
159  StoreEntry* toEntry = nullptr;
160  int toIndex = -1;
161  DataStore::Instance().addRelation(this, m_cacheDataStoreEntry, m_cacheArrayIndex, object, toEntry, toIndex, weight, namedRelation);
162  }
163 
173  {
174  if (!sourceObj)
175  return;
176  auto fromRels = sourceObj->getRelationsFrom<RelationsInterface<BASE>>("ALL");
177  for (unsigned int iRel = 0; iRel < fromRels.size(); iRel++) {
178  fromRels.object(iRel)->addRelationTo(this, fromRels.weight(iRel));
179  }
180 
181  auto toRels = sourceObj->getRelationsTo<RelationsInterface<BASE>>("ALL");
182  for (unsigned int iRel = 0; iRel < toRels.size(); iRel++) {
183  this->addRelationTo(toRels.object(iRel), toRels.weight(iRel));
184  }
185  }
186 
187  //===========================================================================
188  //These return a vector of relations:
189 
199  template <class TO> RelationVector<TO> getRelationsTo(const std::string& name = "", const std::string& namedRelation = "") const
200  {
202  m_cacheArrayIndex, TO::Class(), name, namedRelation));
203  }
204 
214  template <class FROM> RelationVector<FROM> getRelationsFrom(const std::string& name = "",
215  const std::string& namedRelation = "") const
216  {
218  m_cacheArrayIndex, FROM::Class(), name, namedRelation));
219  }
220 
232  template <class T> RelationVector<T> getRelationsWith(const std::string& name = "", const std::string& namedRelation = "") const
233  {
235  m_cacheArrayIndex, T::Class(), name, namedRelation));
236  }
237 
238  //===========================================================================
239  //These return only the first related object
240 
250  template <class TO> TO* getRelatedTo(const std::string& name = "", const std::string& namedRelation = "") const
251  {
253  TO::Class(), name, namedRelation).object);
254  }
255 
265  template <class FROM> FROM* getRelatedFrom(const std::string& name = "", const std::string& namedRelation = "") const
266  {
268  m_cacheArrayIndex, FROM::Class(), name, namedRelation).object);
269  }
270 
280  template <class T> T* getRelated(const std::string& name = "", const std::string& namedRelation = "") const
281  {
283  T::Class(), name, namedRelation).object);
284  }
285 
286 
287  //===========================================================================
288  //These return an object/weight pair
289 
299  template <class TO> std::pair<TO*, float> getRelatedToWithWeight(const std::string& name = "",
300  const std::string& namedRelation = "") const
301  {
303  TO::Class(), name, namedRelation);
304  return std::make_pair(static_cast<TO*>(entry.object), entry.weight);
305  }
306 
316  template <class FROM> std::pair<FROM*, float> getRelatedFromWithWeight(const std::string& name = "",
317  const std::string& namedRelation = "") const
318  {
320  FROM::Class(), name, namedRelation);
321  return std::make_pair(static_cast<FROM*>(entry.object), entry.weight);
322  }
323 
333  template <class T> std::pair<T*, float> getRelatedWithWeight(const std::string& name = "",
334  const std::string& namedRelation = "") const
335  {
337  T::Class(), name, namedRelation);
338  return std::make_pair(static_cast<T*>(entry.object), entry.weight);
339  }
340 
341 
342  //===========================================================================
343  // Information about this object (not strictly relation-related)
344 
346  virtual std::string getName() const { return ""; }
347 
364  virtual std::string getInfoHTML() const { return ""; }
365 
372  std::string getInfo() const
373  {
375  }
376 
377 
379  std::string getArrayName() const
380  {
383  }
384 
385 
387  int getArrayIndex() const
388  {
390  return m_cacheArrayIndex;
391  }
392 
393 
394 #if defined(__CINT__) || defined(__ROOTCLING__) || defined(R__DICTIONARY_FILENAME)
395 
400  RelationVector<TObject> getRelationsTo(const std::string& name) const
401  {
402  return getRelationsTo<TObject>(name);
403  }
404  RelationVector<TObject> getRelationsFrom(const std::string& name) const
405  {
406  return getRelationsFrom<TObject>(name);
407  }
408  RelationVector<TObject> getRelationsWith(const std::string& name) const
409  {
410  return getRelationsWith<TObject>(name);
411  }
412  TObject* getRelatedTo(const std::string& name, const std::string& namedRelation = "") const { return getRelatedTo<TObject>(name, namedRelation); }
413  TObject* getRelatedFrom(const std::string& name, const std::string& namedRelation = "") const { return getRelatedFrom<TObject>(name, namedRelation); }
414  TObject* getRelated(const std::string& name, const std::string& namedRelation = "") const { return getRelated<TObject>(name, namedRelation); }
416 #endif
417  protected:
418 
420  TClonesArray* getArrayPointer() const
421  {
424  return nullptr;
426  }
427 
428  private:
429 
432 
434  mutable int m_cacheArrayIndex;
435 
436  friend class DataStore;
437 
438  //version 0 to disable streaming
440  };
441 
445 }
Belle2::DataStore::c_BothSides
@ c_BothSides
Combination of c_FromSide and c_ToSide.
Definition: DataStore.h:81
Belle2::RelationsInterface::m_cacheDataStoreEntry
DataStore::StoreEntry * m_cacheDataStoreEntry
Cache of the data store entry to which this object belongs.
Definition: RelationsObject.h:431
Belle2::RelationsInterface::getRelatedWithWeight
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.
Definition: RelationsObject.h:333
Belle2::RelationsInterface::getArrayPointer
TClonesArray * getArrayPointer() const
Returns the pointer to the raw DataStore array holding this object (protected since these arrays are ...
Definition: RelationsObject.h:420
Belle2::RelationsInterface::getRelationsWith
RelationVector< T > getRelationsWith(const std::string &name="", const std::string &namedRelation="") const
Get the relations between this object and another store array.
Definition: RelationsObject.h:232
Belle2::RelationsInterface::getInfo
std::string getInfo() const
Return a short summary of this object's contents in raw text format.
Definition: RelationsObject.h:372
Belle2::DataStore::Instance
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
Belle2::RelationsInterface::operator=
RelationsInterface & operator=(const RelationsInterface &relationsInterface)
Assignment operator.
Definition: RelationsObject.h:130
Belle2::RelationsInterface::RelationsInterface
RelationsInterface(Args &&... params)
Constructor, forwards all arguments to BASE constructor.
Definition: RelationsObject.h:112
Belle2::DataStore::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.
Definition: DataStore.cc:492
Belle2::StoreEntry
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:15
Belle2::RelationsInterface::ClassDef
ClassDef(RelationsInterface, 0)
defines interface for accessing relations of objects in StoreArray.
Belle2::RelationsInterface::getArrayName
std::string getArrayName() const
Get name of array this object is stored in, or "" if not found.
Definition: RelationsObject.h:379
Belle2::RelationsInterface::addRelationTo
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).
Definition: RelationsObject.h:144
Belle2::RelationsInterface::getRelated
T * getRelated(const std::string &name="", const std::string &namedRelation="") const
Get the object to or from which this object has a relation.
Definition: RelationsObject.h:280
Belle2::RelationsInterface::getRelatedTo
TO * getRelatedTo(const std::string &name="", const std::string &namedRelation="") const
Get the object to which this object has a relation.
Definition: RelationsObject.h:250
Belle2::RelationEntry::weight
float weight
Weight of the relation.
Definition: RelationEntry.h:35
Belle2::RelationsInterface::RelationsInterface
RelationsInterface(const RelationsInterface &relationsInterface)
Copy constructor.
Definition: RelationsObject.h:121
Belle2::RelationsInterface::getRelationsTo
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.
Definition: RelationsObject.h:199
Belle2::StoreEntry::name
std::string name
Name of the entry.
Definition: StoreEntry.h:46
Belle2::RelationEntry
Struct for relations.
Definition: RelationEntry.h:26
Belle2::RelationsInterface::getRelatedToWithWeight
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.
Definition: RelationsObject.h:299
Belle2::RelationVector
Class for type safe access to objects that are referred to in relations.
Definition: DataStore.h:38
Belle2::RelationEntry::object
TObject * object
Pointer to the object.
Definition: RelationEntry.h:34
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RelationsInterface::getName
virtual std::string getName() const
Return a short name that describes this object, e.g.
Definition: RelationsObject.h:346
Belle2::RelationsInterface::getRelatedFromWithWeight
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.
Definition: RelationsObject.h:316
Belle2::RelationsInterface::getRelationsFrom
RelationVector< FROM > getRelationsFrom(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from another store array to this object.
Definition: RelationsObject.h:214
Belle2::DataStore::c_ToSide
@ c_ToSide
Return relations/objects pointed to (from a given object).
Definition: DataStore.h:80
Belle2::RelationsInterface::copyRelations
void copyRelations(const RelationsInterface< BASE > *sourceObj)
Copies all relations of sourceObj (pointing from or to sourceObj) to this object (including weights).
Definition: RelationsObject.h:172
Belle2::RelationsInterface::addRelationTo
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).
Definition: RelationsObject.h:157
Belle2::RelationsInterface::m_cacheArrayIndex
int m_cacheArrayIndex
Cache of the index in the TClonesArray to which this object belongs.
Definition: RelationsObject.h:434
Belle2::RelationsInterface::getArrayIndex
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
Definition: RelationsObject.h:387
Belle2::DataStore::findStoreEntry
bool findStoreEntry(const TObject *object, StoreEntry *&entry, int &index)
Find an object in an array in the data store.
Definition: DataStore.cc:398
Belle2::StoreEntry::getPtrAsArray
TClonesArray * getPtrAsArray() const
Return ptr cast to TClonesArray.
Definition: StoreEntry.cc:76
Belle2::RelationsObject
RelationsInterface< TObject > RelationsObject
Provides interface for getting/adding relations to objects in StoreArrays.
Definition: RelationsObject.h:443
Belle2::RelationsInterface::getInfoHTML
virtual std::string getInfoHTML() const
Return a short summary of this object's contents in HTML format.
Definition: RelationsObject.h:364
Belle2::DataStore::getRelationWith
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:598
Belle2::DataStore
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:53
Belle2::RelationsInterface
Defines interface for accessing relations of objects in StoreArray.
Definition: RelationsObject.h:102
Belle2::_RelationsInterfaceImpl::htmlToPlainText
std::string htmlToPlainText(const std::string &html)
See RelationsObject::getInfo()
Definition: RelationsObject.cc:17
Belle2::RelationsInterface::getRelatedFrom
FROM * getRelatedFrom(const std::string &name="", const std::string &namedRelation="") const
Get the object from which this object has a relation.
Definition: RelationsObject.h:265
Belle2::DataStore::c_FromSide
@ c_FromSide
Return relations/objects pointed from (to a given object).
Definition: DataStore.h:79
Belle2::RelationsInterface::RelationsInterface
RelationsInterface()
template class cannot be removed without breaking ROOT I/O, so just disable it.
Definition: RelationsObject.h:109