 |
Belle II Software
release-05-02-19
|
14 #include <framework/datastore/StoreArray.h>
15 #include <framework/datastore/RelationVector.h>
16 #include <framework/datastore/RelationsObject.h>
17 #include <framework/logging/Logger.h>
22 #include <type_traits>
31 class SelectSubsetBase {
194 template <
typename StoredClass >
196 static_assert(std::is_base_of<RelationsObject, StoredClass>::value,
197 "SelectSubset<T> only works with classes T inheriting from RelationsObject.");
235 B2FATAL(
"SelectSubset::registerSubset() can only be called once!");
241 m_subset->registerInDataStore(storeFlags);
251 template<
class T,
class ... MoreArguments >
254 if (array.getName() ==
m_set->getName()) {
258 const_cast<StoreArray<T>&
>(array).isRequired();
261 if (
m_subset->notWrittenOut() or array.notWrittenOut())
265 if (array.getName() !=
m_subset->getName())
276 template<
class T,
class ... MoreArguments >
279 if (array.getName() ==
m_set->getName()) {
283 const_cast<StoreArray<T>&
>(array).isRequired();
286 if (
m_subset->notWrittenOut() or array.notWrittenOut())
290 if (array.getName() !=
m_subset->getName())
307 for (std::string arrayName : arrays) {
308 StoreArray<TObject> array(arrayName,
m_set->getDurability());
312 if (array.optionalRelationTo(*
m_set,
m_set->getDurability()))
324 void select(
const std::function<
bool (
const StoredClass*)>& f);
327 StoreAccessorBase*
getSet()
override {
return m_set; }
344 StoreArray<StoredClass>*
m_set =
nullptr;
346 StoreArray<StoredClass>*
m_subset =
nullptr;
351 template <
class StoredClass>
355 std::map<int, int> oldToNew;
356 for (
const StoredClass& setObject : *m_set) {
360 oldToNew[setObject.getArrayIndex()] = m_subset->getEntries();
361 const StoredClass* subsetObject = m_subset->appendNew(setObject);
362 if (!m_reduceExistingSet)
363 setObject.addRelationTo(subsetObject);
368 for (
const auto& oldToNewPair : oldToNew) {
369 const StoredClass* setObject = (*m_set)[oldToNewPair.first];
370 const StoredClass* subsetObject = (*m_subset)[oldToNewPair.second];
372 for (std::string fromArray : m_inheritFromArrays) {
374 for (
unsigned int iRel = 0; iRel < relations.
size(); iRel++) {
378 for (std::string toArray : m_inheritToArrays) {
379 const RelationVector<RelationsObject>& relations = setObject->template getRelationsTo<RelationsObject>(toArray);
380 for (
unsigned int iRel = 0; iRel < relations.size(); iRel++) {
381 subsetObject->addRelationTo(relations.object(iRel), relations.weight(iRel));
390 template <
class StoredClass>
394 for (
const StoredClass& subsetObject1 : *m_subset) {
396 const StoredClass* setObject1 = subsetObject1.template getRelatedFrom<StoredClass>(m_set->getName());
399 const RelationVector<StoredClass>& relations = setObject1->template getRelationsTo<StoredClass>(m_set->getName());
400 for (
unsigned int iRel = 0; iRel < relations.size(); iRel++) {
401 const StoredClass* setObject2 = relations.object(iRel);
402 const double weight = relations.weight(iRel);
405 const StoredClass* subsetObject2 = setObject2->template getRelatedTo<StoredClass>(m_subset->getName());
407 subsetObject1.addRelationTo(subsetObject2, weight);
413 template <
class StoredClass>
417 copySetWithRelations(f);
419 if (m_inheritToSelf) {
420 copyRelationsToSelf();
423 if (m_reduceExistingSet) {
424 swapSetsAndDestroyOriginal();
size_t size() const
Get number of relations.
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.
void copyRelationsToSelf()
Copy any set -> set relations between selected objects.
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 registerSubset(const StoreArray< StoredClass > &set, DataStore::EStoreFlags storeFlags=DataStore::c_ErrorIfAlreadyRegistered)
Remove all non-selected objects from set.
Type-independent implementation details of SelectSubset.
static DataStore & Instance()
Instance of singleton Store.
Class to create a subset of a given StoreArray together with the relations with other StoreArrays.
EStoreFlags
Flags describing behaviours of objects etc.
void select(const std::function< bool(const StoredClass *)> &f)
This method is the actual worker.
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'.
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).
Base class for StoreObjPtr and StoreArray for easier common treatment.
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
StoreAccessorBase * getSubSet() override
Get accessor for reduced set.
void inheritRelationsTo()
Empty method to stop the recursion of the variadic template.
@ c_WriteOut
Object/array should be saved by output modules.
SelectSubset()
Constructor.
Class for type safe access to objects that are referred to in relations.
Abstract base class for different kinds of events.
bool getInheritToSelf() const
Do we inherit relations from original set to itself?
std::vector< std::string > m_inheritToArrays
array names we inherit relations to.
virtual StoreAccessorBase * getSet()=0
Get accessor for original set.
bool m_inheritToSelf
If true, relations from set objects to set objects are copied.
T * object(int index) const
Get object with index.
bool m_reduceExistingSet
If true, non-selected candidates are removed from m_set, m_subset only exists temporarily.
StoreArray< StoredClass > * m_subset
The array we create.
void inheritRelationsFrom()
Empty method to stop the recursion of the variadic template.
std::vector< std::string > getInheritToArrays() const
Get list of arrays we inherit relations to.
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
~SelectSubset()
Destructor.
float weight(int index) const
Get weight with index.
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.
virtual StoreAccessorBase * getSubSet()=0
Get accessor for reduced set.
StoreAccessorBase * getSet() override
Get accessor for original set.
void swapSetsAndDestroyOriginal()
Swap set and subset (+relations), and keep only the reduced set.
std::vector< std::string > m_inheritFromArrays
array names we inherit relations from.
std::vector< std::string > getInheritFromArrays() const
Get list of arrays we inherit relations from.
StoreArray< StoredClass > * m_set
The array we use as input.
DataStore::EStoreFlags m_subsetFlags
Flags used for m_subset.
void inheritAllRelations()
Automatically inherit all relations to or from the original set (if registered when calling this func...