Belle II Software development
SelectSubset< StoredClass > Class Template Reference

Class to create a subset of a given StoreArray together with the relations with other StoreArrays. More...

#include <SelectSubset.h>

Inheritance diagram for SelectSubset< StoredClass >:
SelectSubsetBase

Public Member Functions

 SelectSubset ()
 Constructor.
 
 ~SelectSubset ()
 Destructor.
 
void registerSubset (const StoreArray< StoredClass > &set, DataStore::EStoreFlags storeFlags=DataStore::c_ErrorIfAlreadyRegistered)
 Remove all non-selected objects from 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.
 
template<class T , class ... MoreArguments>
void inheritRelationsFrom (const StoreArray< T > &array, MoreArguments... moreArgs)
 Inherit relations pointing from Other to objects selected into this subset.
 
template<class T , class ... MoreArguments>
void inheritRelationsTo (const StoreArray< T > &array, MoreArguments... moreArgs)
 Inherit relations pointing from objects selected into this subset to Other.
 
void inheritAllRelations ()
 Automatically inherit all relations to or from the original set (if registered when calling this function).
 
void select (const std::function< bool(const StoredClass *)> &f)
 This method is the actual worker.
 
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?
 
void swapSetsAndDestroyOriginal ()
 Swap set and subset (+relations), and keep only the reduced set.
 

Protected Member Functions

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.
 
void inheritRelationsFrom ()
 Empty method to stop the recursion of the variadic template.
 
void inheritRelationsTo ()
 Empty method to stop the recursion of the variadic template.
 

Protected Attributes

StoreArray< StoredClass > * m_set = nullptr
 The array we use as input.
 
StoreArray< StoredClass > * m_subset = nullptr
 The array we create.
 
DataStore::EStoreFlags m_subsetFlags = DataStore::c_WriteOut
 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.
 
bool m_reduceExistingSet = false
 If true, non-selected candidates are removed from m_set, m_subset only exists temporarily.
 

Detailed Description

template<typename StoredClass>
class Belle2::SelectSubset< StoredClass >

Class to create a subset of a given StoreArray together with the relations with other StoreArrays.

The class SelectSubset selects a subset of objects contained in a given StoreArray creating at the same time a set of relations with objects contained in other StoreArrays that are the natural restrictions on the subset of the relations "from" or "to" the original one.

Creating a subset

Assuming you have a StoreArray called 'particles' that contains objects of type Particle you can use SelectSubset to select particles with a given feature and put them in another StoreArray called, as an example,'oddParticles'.

Instantiation

First you need to add a SelectSubset to your module as a member variable:

Initialization

In the initialize method of your module you have to initialize the SelectSubset

StoreArray< Particle > set( "particles" );
set.isRequired(); // or isOptional(). The choice is up to you.
m_selector.registerSubset( set, "oddParticles");
void registerSubset(const StoreArray< StoredClass > &set, DataStore::EStoreFlags storeFlags=DataStore::c_ErrorIfAlreadyRegistered)
Remove all non-selected objects from set.
Definition: SelectSubset.h:215

The SelectSubset class will take care of creating the new StoreArray<Particle>, register it into the datastore with name "oddParticles" same durability and same persistent attributes of the original one.

Inheritance of relations can also be configured here, see "Relations" section below.

Selection

To create the subset you have to specify the selection criterium. You can do that in two possible ways.

via C++ function

You can define your C++ function and then use it. E.g:

bool
MySelectionFunction( const Particle * particle){
// this function tells if the particle is odd
return ( particle->UniqueId() % 2 ) == 1 ;
}
Class to store reconstructed particles.
Definition: Particle.h:75

Then in the event method of your module:

m_selector.select( MySelectionFunction );
void select(const std::function< bool(const StoredClass *)> &f)
This method is the actual worker.
Definition: SelectSubset.h:414

via C++ lambda function

You can specify a lambda expression as parameter of the select method. E.g.: in the event method of your code:

m_selector.select( []( const Particle * particle )
{ return ( particle->UniqueId() % 2 ) == 1 ; } );

with the advantage of an easy capture of module parameters. E.g. to count the number of rejected particles:

int rejected(0);
m_selector.select( [& rejected]( const Particle * particle )
{
if ( ( particle->UniqueId() % 2 ) == 1 )
return true;
rejected ++;
return false;
});
B2INFO("The selector rejected " << rejected << " particles." );

Relations

By default the class SelectSubset produces a one to one relation from the set to the subset by which you can interpret all the relations from and to the original set. E.g. The original StoreArray<Particle> is in relation To the StoreArray<MCParticle>. You can use the relation from the set to the subset and then from the set to the MCParticles. This can be quite tedious, so you can ask SelectSubset to produce the natural restrictions of the relations from and to the original set.

Automatic inheritance

If you want your subset to have the same relations as for the original set, you can simply use

m_selector.inheritAllRelations();
void inheritAllRelations()
Automatically inherit all relations to or from the original set (if registered when calling this func...
Definition: SelectSubset.h:301

Manually specifying relations to inherit

Alternatively, you can specify the arrays you want to inherit from/to manually:

Relations to other StoreArrays

Assuming there is a relation from your original set to other arrays A and B, you can inherit these relations for all objects selected into your subset using:

m_selector.inheritRelationsTo(a, b);
//alternatively, you can also use multiple calls to the function
m_selector.inheritRelationsTo(a);
m_selector.inheritRelationsTo(b);
void inheritRelationsTo(const StoreArray< T > &array, MoreArguments... moreArgs)
Inherit relations pointing from objects selected into this subset to Other.
Definition: SelectSubset.h:275

Relations from other StoreArrays

Relations pointing from objects in other arrays to objects in the original set can also be inherited in a very similar way:

m_selector.inheritRelationsFrom(c, d);
//alternatively, you can also use multiple calls to the function
m_selector.inheritRelationsFrom(c);
m_selector.inheritRelationsFrom(d);
void inheritRelationsFrom(const StoreArray< T > &array, MoreArguments... moreArgs)
Inherit relations pointing from Other to objects selected into this subset.
Definition: SelectSubset.h:250

Relations from the StoreArray to itself

If there are relations from objects in the original set to other objects in the same array (e.g. Particles -> Particles), you can also inherit these by doing

m_selector.inheritRelationsFrom(set);
// or:
// m_selector.inheritRelationsTo(set);

Note that both objects related must pass the selection criteria, or there would be one missing partner in the relation.

Definition at line 193 of file SelectSubset.h.

Constructor & Destructor Documentation

◆ SelectSubset()

SelectSubset ( )
inline

Constructor.

Definition at line 198 of file SelectSubset.h.

198: SelectSubsetBase() {};

◆ ~SelectSubset()

~SelectSubset ( )
inline

Destructor.

Definition at line 201 of file SelectSubset.h.

202 {
203 delete m_set;
204 delete m_subset;
205 }
StoreArray< StoredClass > * m_set
The array we use as input.
Definition: SelectSubset.h:342
StoreArray< StoredClass > * m_subset
The array we create.
Definition: SelectSubset.h:344

Member Function Documentation

◆ getInheritFromArrays()

std::vector< std::string > getInheritFromArrays ( ) const
inlineinherited

Get list of arrays we inherit relations from.

Definition at line 32 of file SelectSubset.h.

32{ return m_inheritFromArrays; }
std::vector< std::string > m_inheritFromArrays
array names we inherit relations from.
Definition: SelectSubset.h:54

◆ getInheritToArrays()

std::vector< std::string > getInheritToArrays ( ) const
inlineinherited

Get list of arrays we inherit relations to.

Definition at line 34 of file SelectSubset.h.

34{ return m_inheritToArrays; }
std::vector< std::string > m_inheritToArrays
array names we inherit relations to.
Definition: SelectSubset.h:56

◆ getInheritToSelf()

bool getInheritToSelf ( ) const
inlineinherited

Do we inherit relations from original set to itself?

Definition at line 36 of file SelectSubset.h.

36{ return m_inheritToSelf; }
bool m_inheritToSelf
If true, relations from set objects to set objects are copied.
Definition: SelectSubset.h:58

◆ getSet()

StoreAccessorBase * getSet ( )
inlineoverridevirtual

Get accessor for original set.

Implements SelectSubsetBase.

Definition at line 325 of file SelectSubset.h.

325{ return m_set; }

◆ getSubSet()

StoreAccessorBase * getSubSet ( )
inlineoverridevirtual

Get accessor for reduced set.

Implements SelectSubsetBase.

Definition at line 327 of file SelectSubset.h.

327{ return m_subset; }

◆ inheritAllRelations()

void inheritAllRelations ( )
inline

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 301 of file SelectSubset.h.

302 {
304
305 for (std::string arrayName : arrays) {
306 StoreArray<TObject> array(arrayName, m_set->getDurability());
307 if (array == *m_subset)
308 continue; // from registerSubset(), ignore
309
310 if (array.optionalRelationTo(*m_set, m_set->getDurability()))
313 inheritRelationsTo(array);
314 }
315 }
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'.
Definition: DataStore.cc:640
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
void inheritRelationsTo()
Empty method to stop the recursion of the variadic template.
Definition: SelectSubset.h:339
void inheritRelationsFrom()
Empty method to stop the recursion of the variadic template.
Definition: SelectSubset.h:337
DataStore::EDurability getDurability() const
Return durability with which the object is saved 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.
Definition: StoreArray.h:172

◆ inheritRelationsFrom() [1/2]

void inheritRelationsFrom ( )
inlineprotected

Empty method to stop the recursion of the variadic template.


Definition at line 337 of file SelectSubset.h.

337{ }

◆ inheritRelationsFrom() [2/2]

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

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 250 of file SelectSubset.h.

251 {
252 if (array.getName() == m_set->getName()) {
253 m_inheritToSelf = true;
254 inheritRelationsFrom(*m_subset, moreArgs...);
255 } else {
256 const_cast<StoreArray<T>&>(array).isRequired();
257
259 if (m_subset->notWrittenOut() or array.notWrittenOut())
261 array.registerRelationTo(*m_subset, m_subset->getDurability(), flags);
262
263 if (array.getName() != m_subset->getName())
264 m_inheritFromArrays.push_back(array.getName());
265
266 inheritRelationsFrom(moreArgs ...);
267 }
268 }
EStoreFlags
Flags describing behaviours of objects etc.
Definition: DataStore.h:69
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition: DataStore.h:71
DataStore::EStoreFlags m_subsetFlags
Flags used for m_subset.
Definition: SelectSubset.h:346
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.

◆ inheritRelationsTo() [1/2]

void inheritRelationsTo ( )
inlineprotected

Empty method to stop the recursion of the variadic template.


Definition at line 339 of file SelectSubset.h.

339{ }

◆ inheritRelationsTo() [2/2]

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

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 275 of file SelectSubset.h.

276 {
277 if (array.getName() == m_set->getName()) {
278 m_inheritToSelf = true;
279 inheritRelationsTo(*m_subset, moreArgs...);
280 } else {
281 const_cast<StoreArray<T>&>(array).isRequired();
282
284 if (m_subset->notWrittenOut() or array.notWrittenOut())
287
288 if (array.getName() != m_subset->getName())
289 m_inheritToArrays.push_back(array.getName());
290
291 inheritRelationsTo(moreArgs ...);
292 }
293 }
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.
Definition: StoreArray.h:140

◆ registerSubset() [1/2]

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

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 229 of file SelectSubset.h.

231 {
232 if (m_set or m_subset) {
233 B2FATAL("SelectSubset::registerSubset() can only be called once!");
234 }
235
236 m_set = new StoreArray<StoredClass>(set);
237
238 m_subset = new StoreArray<StoredClass>(subsetName, m_set->getDurability());
239 m_subset->registerInDataStore(storeFlags);
240 m_subsetFlags = storeFlags;
241
243 }
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.

◆ registerSubset() [2/2]

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

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 215 of file SelectSubset.h.

217 {
218 m_reduceExistingSet = true;
219 registerSubset(set, set.getName() + "_tmpSubset", storeFlags | DataStore::c_DontWriteOut);
220
222 }
bool m_reduceExistingSet
If true, non-selected candidates are removed from m_set, m_subset only exists temporarily.
Definition: SelectSubset.h:60

◆ swapSetsAndDestroyOriginal()

void swapSetsAndDestroyOriginal ( )
inherited

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

Subset and associated relations will be empty afterwards.

Definition at line 14 of file SelectSubset.cc.

15{
18
19 //replace set with subset
20 DataStore::Instance().replaceData(*subset, *set);
21
22 //swap relations
23 for (const std::string& fromArray : m_inheritFromArrays) {
24 RelationArray setRel(DataStore::relationName(fromArray, set->getName()));
25 RelationArray subsetRel(DataStore::relationName(fromArray, subset->getName()));
26 DataStore::Instance().replaceData(subsetRel, setRel);
27 }
28 for (const std::string& toArray : m_inheritToArrays) {
29 RelationArray setRel(DataStore::relationName(set->getName(), toArray));
30 RelationArray subsetRel(DataStore::relationName(subset->getName(), toArray));
31 DataStore::Instance().replaceData(subsetRel, setRel);
32 }
33}
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:180
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
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
virtual StoreAccessorBase * getSubSet()=0
Get accessor for reduced set.
virtual StoreAccessorBase * getSet()=0
Get accessor for original set.
Base class for StoreObjPtr and StoreArray for easier common treatment.
std::bitset< max - min+1 > subset(std::bitset< nbits > set)
extract a subset of bitstring, like substring.
Definition: Cosim.h:120

Member Data Documentation

◆ m_inheritFromArrays

std::vector<std::string> m_inheritFromArrays
protectedinherited

array names we inherit relations from.

Definition at line 54 of file SelectSubset.h.

◆ m_inheritToArrays

std::vector<std::string> m_inheritToArrays
protectedinherited

array names we inherit relations to.

Definition at line 56 of file SelectSubset.h.

◆ 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 58 of file SelectSubset.h.

◆ m_reduceExistingSet

bool m_reduceExistingSet = false
protectedinherited

If true, non-selected candidates are removed from m_set, m_subset only exists temporarily.

Definition at line 60 of file SelectSubset.h.

◆ m_set

StoreArray<StoredClass>* m_set = nullptr
protected

The array we use as input.

Definition at line 342 of file SelectSubset.h.

◆ m_subset

StoreArray<StoredClass>* m_subset = nullptr
protected

The array we create.

Definition at line 344 of file SelectSubset.h.

◆ m_subsetFlags

DataStore::EStoreFlags m_subsetFlags = DataStore::c_WriteOut
protected

Flags used for m_subset.

Definition at line 346 of file SelectSubset.h.


The documentation for this class was generated from the following file: