Belle II Software  release-08-01-10
ParticleSubset.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include <analysis/utility/ParticleSubset.h>
9 
10 #include <analysis/dataobjects/ParticleList.h>
11 #include <framework/datastore/StoreObjPtr.h>
12 
13 #include <unordered_set>
14 
15 using namespace Belle2;
16 
17 namespace {
18  void keepParticle(const Particle* p, std::unordered_set<int>* indicesToKeep)
19  {
20  indicesToKeep->insert(p->getArrayIndex());
21  unsigned int n = p->getNDaughters();
22  for (unsigned int i = 0; i < n; i++) {
23  keepParticle(p->getDaughter(i), indicesToKeep);
24  }
25  }
26 }
27 
28 void ParticleSubset::removeParticlesNotInLists(const std::vector<std::string>& listNames)
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 }
55 
56 void ParticleSubset::fixParticles(const std::map<int, int>& oldToNewMap)
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 }
68 
69 void ParticleSubset::fixParticleLists(const std::map<int, int>& oldToNewMap)
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 }
83 
84 void ParticleSubset::fixVector(std::vector<int>& vec, const std::map<int, int>& oldToNewMap)
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 }
94 
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
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 th...
void select(std::function< bool(const Particle *)> f)
select Particles for which f returns true, discard others
static void fixVector(std::vector< int > &vec, const std::map< int, int > &oldToNewMap)
replace entries in vec via oldToNewMap, removing those not found.
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.
Class to store reconstructed particles.
Definition: Particle.h:75
StoreArray< Particle > * m_set
The array we use as input.
Definition: SelectSubset.h:342
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
Abstract base class for different kinds of events.