Belle II Software  release-05-01-25
ParticleSubset Class Reference

Specialised SelectSubset<Particle> that also fixes daughter indices and all ParticleLists. More...

#include <ParticleSubset.h>

Inheritance diagram for ParticleSubset:
Collaboration diagram for ParticleSubset:

Public Member Functions

void removeParticlesNotInLists (const std::vector< std::string > &listNames)
 Removes all Particles that are not in one of the given ParticleLists (or daughters of Particles in the lists). More...
 
void select (std::function< bool(const Particle *)> f)
 select Particles for which f returns true, discard others
 
void registerSubset (const StoreArray< Particle > &set, DataStore::EStoreFlags storeFlags=DataStore::c_ErrorIfAlreadyRegistered)
 Remove all non-selected objects from set. More...
 
void registerSubset (const StoreArray< Particle > &set, const std::string &subsetName, DataStore::EStoreFlags storeFlags=DataStore::c_ErrorIfAlreadyRegistered)
 Register the StoreArray<StoredClass> that will contain the subset of selected elements. More...
 
void inheritRelationsFrom (const StoreArray< T > &array, MoreArguments... moreArgs)
 Inherit relations pointing from Other to objects selected into this subset. More...
 
void inheritRelationsTo (const StoreArray< T > &array, MoreArguments... moreArgs)
 Inherit relations pointing from objects selected into this subset to Other. More...
 
void inheritAllRelations ()
 Automatically inherit all relations to or from the original set (if registered when calling this function). More...
 
void select (const std::function< bool(const Particle *)> &f)
 This method is the actual worker. More...
 
StoreAccessorBasegetSet () override
 Get accessor for original set.
 
StoreAccessorBasegetSubSet () override
 Get accessor for reduced set.
 
std::vector< std::string > getInheritFromArrays () const
 Get list of arrays we inherit relations from.
 
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?
 

Protected Member Functions

void fixParticles (const std::map< int, int > &oldToNewMap)
 fix daughter indices, reset m_arrayPointer
 
void fixParticleLists (const std::map< int, int > &oldToNewMap)
 fix contents of particlelists by updating indices or removing entries.
 
void inheritRelationsFrom ()
 Empty method to stop the recursion of the variadic template. More...
 
void inheritRelationsTo ()
 Empty method to stop the recursion of the variadic template. More...
 
std::map< int, int > copySetWithRelations (std::function< bool(const Particle *)> 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.
 
void swapSetsAndDestroyOriginal ()
 Swap set and subset (+relations), and keep only the reduced set. More...
 

Static Protected Member Functions

static void fixVector (std::vector< int > &vec, const std::map< int, int > &oldToNewMap)
 replace entries in vec via oldToNewMap, removing those not found.
 

Protected Attributes

StoreArray< Particle > * m_set
 The array we use as input.
 
StoreArray< Particle > * m_subset
 The array we create.
 
DataStore::EStoreFlags m_subsetFlags
 Flags used for m_subset.
 
std::vector< std::string > m_inheritFromArrays
 array names we inherit relations from.
 
std::vector< std::string > m_inheritToArrays
 array names we inherit relations to.
 
bool m_inheritToSelf = false
 If true, relations from set objects to set objects are copied. More...
 
bool m_reduceExistingSet = false
 If true, non-selected candidates are removed from m_set, m_subset only exists temporarily.
 

Detailed Description

Specialised SelectSubset<Particle> that also fixes daughter indices and all ParticleLists.

Relations are already handled in SelectSubset.

Can be used like SelectSubset via select(), or by calling removeParticlesNotInLists() instead.

Definition at line 15 of file ParticleSubset.h.

Member Function Documentation

◆ inheritAllRelations()

void inheritAllRelations
inlineinherited

Automatically inherit all relations to or from the original set (if registered when calling this function).

Equivalent to calling inheritRelationsFrom()/To() for all related arrays.

Note: Do not combine with inheritRelationsFrom() and inheritRelationsTo().

Definition at line 311 of file SelectSubset.h.

◆ inheritRelationsFrom() [1/2]

void inheritRelationsFrom
inlineprotectedinherited

Empty method to stop the recursion of the variadic template.


Definition at line 347 of file SelectSubset.h.

◆ inheritRelationsFrom() [2/2]

void inheritRelationsFrom ( const StoreArray< T > &  array,
MoreArguments...  moreArgs 
)
inlineinherited

Inherit relations pointing from Other to objects selected into this subset.

You can specify an unlimited number of arrays as arguments to this function.

Definition at line 260 of file SelectSubset.h.

◆ inheritRelationsTo() [1/2]

void inheritRelationsTo
inlineprotectedinherited

Empty method to stop the recursion of the variadic template.


Definition at line 349 of file SelectSubset.h.

◆ inheritRelationsTo() [2/2]

void inheritRelationsTo ( const StoreArray< T > &  array,
MoreArguments...  moreArgs 
)
inlineinherited

Inherit relations pointing from objects selected into this subset to Other.

You can specify an unlimited number of arrays as arguments to this function.

Definition at line 285 of file SelectSubset.h.

◆ registerSubset() [1/2]

void registerSubset ( const StoreArray< Particle > &  set,
const std::string &  subsetName,
DataStore::EStoreFlags  storeFlags = DataStore::c_ErrorIfAlreadyRegistered 
)
inlineinherited

Register the StoreArray<StoredClass> that will contain the subset of selected elements.

Parameters
setThe StoreArray<StoredClass> from which the elements will be selected
subsetNameThe name of the StoreArray<StoredClass> that will contain the selected elements
storeFlagsORed combination of DataStore::EStoreFlags.

Definition at line 239 of file SelectSubset.h.

◆ registerSubset() [2/2]

void registerSubset ( const StoreArray< Particle > &  set,
DataStore::EStoreFlags  storeFlags = DataStore::c_ErrorIfAlreadyRegistered 
)
inlineinherited

Remove all non-selected objects from set.

All relations registered so far are retained. TODO: consider moving this into StoreArray itself

Parameters
setThe StoreArray<StoredClass> from which to retain only selected elements
storeFlagsflags used for temporary arrays and relations. Should be changed from the default if you want multiple instances. c_DontWriteOut is always used.

Definition at line 225 of file SelectSubset.h.

◆ removeParticlesNotInLists()

void removeParticlesNotInLists ( const std::vector< std::string > &  listNames)

Removes all Particles that are not in one of the given ParticleLists (or daughters of Particles in the lists).

Removal is done immediately, there is no need to call select() afterwards.

Definition at line 21 of file ParticleSubset.cc.

22 {
23  std::unordered_set<int> indicesToKeep;
24  for (const auto& l : listNames) {
26  if (!list)
27  continue;
28 
29  if (list->getParticleCollectionName() == m_set->getName()) {
30  const int n = list->getListSize();
31  for (int i = 0; i < n; i++) {
32  const Particle* p = list->getParticle(i);
33  keepParticle(p, &indicesToKeep);
34  }
35  } else {
36  B2ERROR("ParticleList " << l << " uses Particle array '" << list->getParticleCollectionName() <<
37  "', but ParticleSubset uses different array '" << m_set->getName() << "'!");
38  }
39  }
40 
41  //remove everything not in indicesToKeep
42  auto selector = [indicesToKeep](const Particle * p) -> bool {
43  int idx = p->getArrayIndex();
44  return indicesToKeep.count(idx) == 1;
45  };
46  select(selector);
47 }

◆ select()

void select ( const std::function< bool(const Particle *)> &  f)
inherited

This method is the actual worker.

It selects the elements, fill the subset and all the relations in which the subset is involved.

Parameters
fthe pointer to the function (or a nameless lambda expression) returning true for the elements to be selected and false for the others.

Definition at line 423 of file SelectSubset.h.

◆ swapSetsAndDestroyOriginal()

void swapSetsAndDestroyOriginal ( )
protectedinherited

Swap set and subset (+relations), and keep only the reduced set.

Subset and associated relations will be empty afterwards.

Definition at line 7 of file SelectSubset.cc.

8 {
9  StoreAccessorBase* set = getSet();
11 
12  //replace set with subset
13  DataStore::Instance().replaceData(*subset, *set);
14 
15  //swap relations
16  for (const std::string& fromArray : m_inheritFromArrays) {
17  RelationArray setRel(DataStore::relationName(fromArray, set->getName()));
18  RelationArray subsetRel(DataStore::relationName(fromArray, subset->getName()));
19  DataStore::Instance().replaceData(subsetRel, setRel);
20  }
21  for (const std::string& toArray : m_inheritToArrays) {
22  RelationArray setRel(DataStore::relationName(set->getName(), toArray));
23  RelationArray subsetRel(DataStore::relationName(subset->getName(), toArray));
24  DataStore::Instance().replaceData(subsetRel, setRel);
25  }
26 }

Member Data Documentation

◆ m_inheritToSelf

bool m_inheritToSelf = false
protectedinherited

If true, relations from set objects to set objects are copied.

(if both objects are selected!).

Definition at line 68 of file SelectSubset.h.


The documentation for this class was generated from the following files:
Belle2::RelationArray
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:72
Belle2::DataStore::Instance
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
Belle2::StoreAccessorBase::getName
const std::string & getName() const
Return name under which the object is saved in the DataStore.
Definition: StoreAccessorBase.h:130
Belle2::DataStore::replaceData
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:343
Belle2::StoreAccessorBase
Base class for StoreObjPtr and StoreArray for easier common treatment.
Definition: StoreAccessorBase.h:29
Cosim::subset
std::bitset< max - min+1 > subset(std::bitset< nbits > set)
extract a subset of bitstring, like substring.
Definition: Cosim.h:113
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::SelectSubsetBase::m_inheritToArrays
std::vector< std::string > m_inheritToArrays
array names we inherit relations to.
Definition: SelectSubset.h:66
Belle2::SelectSubsetBase::getSet
virtual StoreAccessorBase * getSet()=0
Get accessor for original set.
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::DataStore::relationName
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:182
Belle2::ParticleSubset::select
void select(std::function< bool(const Particle *)> f)
select Particles for which f returns true, discard others
Definition: ParticleSubset.h:24
Belle2::SelectSubsetBase::getSubSet
virtual StoreAccessorBase * getSubSet()=0
Get accessor for reduced set.
Belle2::SelectSubsetBase::m_inheritFromArrays
std::vector< std::string > m_inheritFromArrays
array names we inherit relations from.
Definition: SelectSubset.h:64
Belle2::SelectSubset< Particle >::m_set
StoreArray< Particle > * m_set
The array we use as input.
Definition: SelectSubset.h:352