Belle II Software  release-05-01-25
ParticleSubset.cc
1 #include <analysis/utility/ParticleSubset.h>
2 
3 #include <analysis/dataobjects/ParticleList.h>
4 #include <framework/datastore/StoreObjPtr.h>
5 
6 #include <unordered_set>
7 
8 using namespace Belle2;
9 
10 namespace {
11  void keepParticle(const Particle* p, std::unordered_set<int>* indicesToKeep)
12  {
13  indicesToKeep->insert(p->getArrayIndex());
14  unsigned int n = p->getNDaughters();
15  for (unsigned int i = 0; i < n; i++) {
16  keepParticle(p->getDaughter(i), indicesToKeep);
17  }
18  }
19 }
20 
21 void ParticleSubset::removeParticlesNotInLists(const std::vector<std::string>& listNames)
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 }
48 
49 void ParticleSubset::fixParticles(const std::map<int, int>& oldToNewMap)
50 {
51  TClonesArray* arrayPtr = m_set->getPtr();
52  for (Particle& p : *m_set) {
53  unsigned int n = p.m_daughterIndices.size();
54  for (unsigned int i = 0; i < n; i++) {
55  p.m_daughterIndices[i] = oldToNewMap.at(p.m_daughterIndices[i]);
56  }
57 
58  p.m_arrayPointer = arrayPtr;
59  }
60 }
61 
62 void ParticleSubset::fixParticleLists(const std::map<int, int>& oldToNewMap)
63 {
65  for (const auto& entry : entryMap) {
66  if (!entry.second.ptr or !entry.second.object->InheritsFrom(ParticleList::Class()))
67  continue;
68 
69  auto* list = static_cast<ParticleList*>(entry.second.ptr);
70  if (list->getParticleCollectionName() == m_set->getName()) {
71  fixVector(list->m_scList, oldToNewMap);
72  fixVector(list->m_fsList, oldToNewMap);
73  }
74  }
75 }
76 
77 void ParticleSubset::fixVector(std::vector<int>& vec, const std::map<int, int>& oldToNewMap)
78 {
79  const std::vector<int> oldList(vec);
80  vec.clear();
81  for (const int idx : oldList) {
82  const auto& it = oldToNewMap.find(idx);
83  if (it != oldToNewMap.end())
84  vec.push_back(it->second);
85  }
86 }
87 
Belle2::ParticleList
ParticleList is a container class that stores a collection of Particle objects.
Definition: ParticleList.h:150
Belle2::DataStore::Instance
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
Belle2::ParticleSubset::fixParticleLists
void fixParticleLists(const std::map< int, int > &oldToNewMap)
fix contents of particlelists by updating indices or removing entries.
Definition: ParticleSubset.cc:62
Belle2::ParticleSubset::fixParticles
void fixParticles(const std::map< int, int > &oldToNewMap)
fix daughter indices, reset m_arrayPointer
Definition: ParticleSubset.cc:49
Belle2::DataStore::getStoreEntryMap
StoreEntryMap & getStoreEntryMap(EDurability durability)
Get a reference to the object/array map.
Definition: DataStore.h:321
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::StoreArray::getPtr
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
Definition: StoreArray.h:321
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
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::DataStore::c_Event
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:61
Belle2::SelectSubset< Particle >::m_set
StoreArray< Particle > * m_set
The array we use as input.
Definition: SelectSubset.h:352
Belle2::ParticleSubset::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 th...
Definition: ParticleSubset.cc:21
Belle2::ParticleSubset::fixVector
static void fixVector(std::vector< int > &vec, const std::map< int, int > &oldToNewMap)
replace entries in vec via oldToNewMap, removing those not found.
Definition: ParticleSubset.cc:77