Belle II Software light-2406-ragdoll
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).
 
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.
 
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.
 
void inheritRelationsFrom (const StoreArray< T > &array, MoreArguments... moreArgs)
 Inherit relations pointing from Other to objects selected into this subset.
 
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 Particle *)> &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

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

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.
 
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 22 of file ParticleSubset.h.

Member Function Documentation

◆ copyRelationsToSelf()

void copyRelationsToSelf
protectedinherited

Copy any set -> set relations between selected objects.

Definition at line 334 of file SelectSubset.h.

391 {
392 for (const StoredClass& subsetObject1 : *m_subset) {
393 //TODO: change relation direction to set -> subset?
394 const StoredClass* setObject1 = subsetObject1.template getRelatedFrom<StoredClass>(m_set->getName());
395
396 if (setObject1 != nullptr) {
397 //get all objects in original set related to setObject1
398 const RelationVector<StoredClass>& relations = setObject1->template getRelationsTo<StoredClass>(m_set->getName());
399 for (unsigned int iRel = 0; iRel < relations.size(); iRel++) {
400 const StoredClass* setObject2 = relations.object(iRel);
401 const double weight = relations.weight(iRel);
402 //if setObject2 was selected into subset, inherit relation
403 const StoredClass* subsetObject2 = setObject2->template getRelatedTo<StoredClass>(m_subset->getName());
404 if (subsetObject2) {
405 subsetObject1.addRelationTo(subsetObject2, weight);
406 }
407 }
408 }
409 }
410 }
StoreArray< Particle > * m_set
The array we use as input.
Definition: SelectSubset.h:342
const std::string & getName() const
Return name under which the object is saved in the DataStore.

◆ copySetWithRelations()

std::map< int, int > copySetWithRelations ( std::function< bool(const Particle *)>  f)
protectedinherited

Selects the elements, fill the subset and copies all the relations in which the set is involved.

Definition at line 331 of file SelectSubset.h.

352 {
353 std::map<int, int> oldToNew;
354 for (const StoredClass& setObject : *m_set) {
355 if (!f(&setObject))
356 continue;
357
358 oldToNew[setObject.getArrayIndex()] = m_subset->getEntries();
359 const StoredClass* subsetObject = m_subset->appendNew(setObject);
360 if (!m_reduceExistingSet)
361 setObject.addRelationTo(subsetObject);
362 }
363
364
365 //TODO this is the slow bit, can probably be improved by directly dealing with indices
366 for (const auto& oldToNewPair : oldToNew) {
367 const StoredClass* setObject = (*m_set)[oldToNewPair.first];
368 const StoredClass* subsetObject = (*m_subset)[oldToNewPair.second];
369
370 for (std::string fromArray : m_inheritFromArrays) {
371 const RelationVector<RelationsObject>& relations = setObject->template getRelationsFrom<RelationsObject>(fromArray);
372 for (unsigned int iRel = 0; iRel < relations.size(); iRel++) {
373 relations.object(iRel)->addRelationTo(subsetObject, relations.weight(iRel));
374 }
375 }
376 for (std::string toArray : m_inheritToArrays) {
377 const RelationVector<RelationsObject>& relations = setObject->template getRelationsTo<RelationsObject>(toArray);
378 for (unsigned int iRel = 0; iRel < relations.size(); iRel++) {
379 subsetObject->addRelationTo(relations.object(iRel), relations.weight(iRel));
380 }
381 }
382 }
383
384 return oldToNew;
385 }
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).
StoreArray< Particle > * m_subset
The array we create.
Definition: SelectSubset.h:344
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:246
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216

◆ fixParticleLists()

void fixParticleLists ( const std::map< int, int > &  oldToNewMap)
protected

fix contents of particlelists by updating indices or removing entries.

Definition at line 69 of file ParticleSubset.cc.

70{
72 for (const auto& entry : entryMap) {
73 if (!entry.second.ptr or !entry.second.object->InheritsFrom(ParticleList::Class()))
74 continue;
75
76 auto* list = static_cast<ParticleList*>(entry.second.ptr);
77 if (list->getParticleCollectionName() == m_set->getName()) {
78 fixVector(list->m_scList, oldToNewMap);
79 fixVector(list->m_fsList, oldToNewMap);
80 }
81 }
82}
StoreEntryMap & getStoreEntryMap(EDurability durability)
Get a reference to the object/array map.
Definition: DataStore.h:325
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:59
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
ParticleList is a container class that stores a collection of Particle objects.
Definition: ParticleList.h:140
static void fixVector(std::vector< int > &vec, const std::map< int, int > &oldToNewMap)
replace entries in vec via oldToNewMap, removing those not found.

◆ fixParticles()

void fixParticles ( const std::map< int, int > &  oldToNewMap)
protected

fix daughter indices, reset m_arrayPointer

Definition at line 56 of file ParticleSubset.cc.

57{
58 TClonesArray* arrayPtr = m_set->getPtr();
59 for (Particle& p : *m_set) {
60 unsigned int n = p.m_daughterIndices.size();
61 for (unsigned int i = 0; i < n; i++) {
62 p.m_daughterIndices[i] = oldToNewMap.at(p.m_daughterIndices[i]);
63 }
64
65 p.m_arrayPointer = arrayPtr;
66 }
67}
Class to store reconstructed particles.
Definition: Particle.h:75
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
Definition: StoreArray.h:311

◆ fixVector()

void fixVector ( std::vector< int > &  vec,
const std::map< int, int > &  oldToNewMap 
)
staticprotected

replace entries in vec via oldToNewMap, removing those not found.

Definition at line 84 of file ParticleSubset.cc.

85{
86 const std::vector<int> oldList(vec);
87 vec.clear();
88 for (const int idx : oldList) {
89 const auto& it = oldToNewMap.find(idx);
90 if (it != oldToNewMap.end())
91 vec.push_back(it->second);
92 }
93}

◆ 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 ( )
inlineoverridevirtualinherited

Get accessor for original set.

Implements SelectSubsetBase.

Definition at line 325 of file SelectSubset.h.

325{ return m_set; }

◆ getSubSet()

StoreAccessorBase * getSubSet ( )
inlineoverridevirtualinherited

Get accessor for reduced set.

Implements SelectSubsetBase.

Definition at line 327 of file SelectSubset.h.

327{ return m_subset; }

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

302 {
303 auto arrays = DataStore::Instance().getListOfRelatedArrays(*m_set);
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 }
void inheritRelationsTo(const StoreArray< T > &array, MoreArguments... moreArgs)
Inherit relations pointing from objects selected into this subset to Other.
Definition: SelectSubset.h:275
void inheritRelationsFrom(const StoreArray< T > &array, MoreArguments... moreArgs)
Inherit relations pointing from Other to objects selected into this subset.
Definition: SelectSubset.h:250
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 ( )
inlineprotectedinherited

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 
)
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 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
258 DataStore::EStoreFlags flags = m_subsetFlags;
259 if (m_subset->notWrittenOut() or array.notWrittenOut())
260 flags |= DataStore::c_DontWriteOut;
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 }
DataStore::EStoreFlags m_subsetFlags
Flags used for m_subset.
Definition: SelectSubset.h:346
bool notWrittenOut() const
Returns true if this object/array should not be saved by output modules.

◆ inheritRelationsTo() [1/2]

void inheritRelationsTo ( )
inlineprotectedinherited

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 
)
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 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
283 DataStore::EStoreFlags flags = m_subsetFlags;
284 if (m_subset->notWrittenOut() or array.notWrittenOut())
285 flags |= DataStore::c_DontWriteOut;
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< 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 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
242 set.registerRelationTo(*m_subset, m_subset->getDurability(), storeFlags);
243 }

◆ 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 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
void inheritAllRelations()
Automatically inherit all relations to or from the original set (if registered when calling this func...
Definition: SelectSubset.h:301
void registerSubset(const StoreArray< Particle > &set, DataStore::EStoreFlags storeFlags=DataStore::c_ErrorIfAlreadyRegistered)
Remove all non-selected objects from set.
Definition: SelectSubset.h:215

◆ 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 28 of file ParticleSubset.cc.

29{
30 std::unordered_set<int> indicesToKeep;
31 for (const auto& l : listNames) {
33 if (!list)
34 continue;
35
36 if (list->getParticleCollectionName() == m_set->getName()) {
37 const int n = list->getListSize();
38 for (int i = 0; i < n; i++) {
39 const Particle* p = list->getParticle(i);
40 keepParticle(p, &indicesToKeep);
41 }
42 } else {
43 B2ERROR("ParticleList " << l << " uses Particle array '" << list->getParticleCollectionName() <<
44 "', but ParticleSubset uses different array '" << m_set->getName() << "'!");
45 }
46 }
47
48 //remove everything not in indicesToKeep
49 auto selector = [indicesToKeep](const Particle * p) -> bool {
50 int idx = p->getArrayIndex();
51 return indicesToKeep.count(idx) == 1;
52 };
53 select(selector);
54}
void select(std::function< bool(const Particle *)> f)
select Particles for which f returns true, discard others
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96

◆ select() [1/2]

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

415 {
417
418 if (m_inheritToSelf) {
420 }
421
422 if (m_reduceExistingSet) {
424 }
425 }
void swapSetsAndDestroyOriginal()
Swap set and subset (+relations), and keep only the reduced set.
Definition: SelectSubset.cc:14
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.
Definition: SelectSubset.h:351
void copyRelationsToSelf()
Copy any set -> set relations between selected objects.
Definition: SelectSubset.h:390

◆ select() [2/2]

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

select Particles for which f returns true, discard others

Definition at line 31 of file ParticleSubset.h.

32 {
33 const std::map<int, int>& oldToNewMap = copySetWithRelations(f);
34
35 if (m_inheritToSelf) {
37 }
38
41 }
42
43 fixParticles(oldToNewMap);
44
45 fixParticleLists(oldToNewMap);
46 }
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.

◆ 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{
17 StoreAccessorBase* subset = getSubSet();
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.

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<Particle >* m_set
protectedinherited

The array we use as input.

Definition at line 342 of file SelectSubset.h.

◆ m_subset

StoreArray<Particle >* m_subset
protectedinherited

The array we create.

Definition at line 344 of file SelectSubset.h.

◆ m_subsetFlags

DataStore::EStoreFlags m_subsetFlags
protectedinherited

Flags used for m_subset.

Definition at line 346 of file SelectSubset.h.


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