12#include <framework/datastore/StoreArray.h>
13#include <framework/datastore/RelationVector.h>
14#include <framework/datastore/RelationsObject.h>
15#include <framework/logging/Logger.h>
192 template <
typename StoredClass >
194 static_assert(std::is_base_of<RelationsObject, StoredClass>::value,
195 "SelectSubset<T> only works with classes T inheriting from RelationsObject.");
233 B2FATAL(
"SelectSubset::registerSubset() can only be called once!");
249 template<
class T,
class ... MoreArguments >
274 template<
class T,
class ... MoreArguments >
305 for (std::string arrayName : arrays) {
322 void select(
const std::function<
bool (
const StoredClass*)>& f);
349 template <
class StoredClass>
353 std::map<int, int> oldToNew;
354 for (
const StoredClass& setObject : *m_set) {
358 oldToNew[setObject.getArrayIndex()] = m_subset->getEntries();
359 const StoredClass* subsetObject = m_subset->appendNew(setObject);
360 if (!m_reduceExistingSet)
361 setObject.addRelationTo(subsetObject);
366 for (
const auto& oldToNewPair : oldToNew) {
367 const StoredClass* setObject = (*m_set)[oldToNewPair.first];
368 const StoredClass* subsetObject = (*m_subset)[oldToNewPair.second];
370 for (std::string fromArray : m_inheritFromArrays) {
372 for (
unsigned int iRel = 0; iRel < relations.
size(); iRel++) {
373 relations.
object(iRel)->addRelationTo(subsetObject, relations.
weight(iRel));
376 for (std::string toArray : m_inheritToArrays) {
378 for (
unsigned int iRel = 0; iRel < relations.
size(); iRel++) {
379 subsetObject->addRelationTo(relations.
object(iRel), relations.
weight(iRel));
388 template <
class StoredClass>
392 for (
const StoredClass& subsetObject1 : *m_subset) {
394 const StoredClass* setObject1 = subsetObject1.template getRelatedFrom<StoredClass>(m_set->getName());
396 if (setObject1 !=
nullptr) {
399 for (
unsigned int iRel = 0; iRel < relations.
size(); iRel++) {
400 const StoredClass* setObject2 = relations.
object(iRel);
401 const double weight = relations.
weight(iRel);
403 const StoredClass* subsetObject2 = setObject2->template getRelatedTo<StoredClass>(m_subset->getName());
405 subsetObject1.addRelationTo(subsetObject2, weight);
412 template <
class StoredClass>
416 copySetWithRelations(f);
418 if (m_inheritToSelf) {
419 copyRelationsToSelf();
422 if (m_reduceExistingSet) {
423 swapSetsAndDestroyOriginal();
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'.
EStoreFlags
Flags describing behaviours of objects etc.
@ c_WriteOut
Object/array should be saved by output modules.
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
static DataStore & Instance()
Instance of singleton Store.
Class for type safe access to objects that are referred to in relations.
T * object(int index) const
Get object with index.
size_t size() const
Get number of relations.
float weight(int index) const
Get weight with index.
Type-independent implementation details of SelectSubset.
std::vector< std::string > m_inheritFromArrays
array names we inherit relations from.
virtual StoreAccessorBase * getSubSet()=0
Get accessor for reduced set.
bool m_inheritToSelf
If true, relations from set objects to set objects are copied.
bool m_reduceExistingSet
If true, non-selected candidates are removed from m_set, m_subset only exists temporarily.
std::vector< std::string > getInheritToArrays() const
Get list of arrays we inherit relations to.
bool getInheritToSelf() const
Do we inherit relations from original set to itself?
void swapSetsAndDestroyOriginal()
Swap set and subset (+relations), and keep only the reduced set.
virtual StoreAccessorBase * getSet()=0
Get accessor for original set.
std::vector< std::string > getInheritFromArrays() const
Get list of arrays we inherit relations from.
std::vector< std::string > m_inheritToArrays
array names we inherit relations to.
Class to create a subset of a given StoreArray together with the relations with other StoreArrays.
void inheritRelationsTo(const StoreArray< T > &array, MoreArguments... moreArgs)
Inherit relations pointing from objects selected into this subset to Other.
StoreArray< StoredClass > * m_set
The array we use as input.
void inheritRelationsTo()
Empty method to stop the recursion of the variadic template.
~SelectSubset()
Destructor.
StoreArray< StoredClass > * m_subset
The array we create.
DataStore::EStoreFlags m_subsetFlags
Flags used for m_subset.
void inheritRelationsFrom(const StoreArray< T > &array, MoreArguments... moreArgs)
Inherit relations pointing from Other to objects selected into this subset.
void inheritAllRelations()
Automatically inherit all relations to or from the original set (if registered when calling this func...
StoreAccessorBase * getSet() override
Get accessor for original set.
void registerSubset(const StoreArray< StoredClass > &set, const std::string &subsetName, DataStore::EStoreFlags storeFlags=DataStore::c_ErrorIfAlreadyRegistered)
Register the StoreArray<StoredClass> that will contain the subset of selected elements.
void inheritRelationsFrom()
Empty method to stop the recursion of the variadic template.
SelectSubset()
Constructor.
StoreAccessorBase * getSubSet() override
Get accessor for reduced set.
void registerSubset(const StoreArray< StoredClass > &set, DataStore::EStoreFlags storeFlags=DataStore::c_ErrorIfAlreadyRegistered)
Remove all non-selected objects from set.
Base class for StoreObjPtr and StoreArray for easier common treatment.
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.
bool notWrittenOut() const
Returns true if this object/array should not be saved by output modules.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
bool optionalRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Tell the data store about a relation that we could make use of.
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
void select(const std::function< bool(const StoredClass *)> &f)
This method is the actual worker.
std::map< int, int > copySetWithRelations(std::function< bool(const StoredClass *)> f)
Selects the elements, fill the subset and copies all the relations in which the set is involved.
void copyRelationsToSelf()
Copy any set -> set relations between selected objects.
Abstract base class for different kinds of events.