Belle II Software  release-05-01-25
ParticleExtraInfoMap.cc
1 #include <analysis/dataobjects/ParticleExtraInfoMap.h>
2 
3 
4 using namespace Belle2;
5 
6 unsigned int ParticleExtraInfoMap::getIndex(unsigned int mapID, const std::string& name) const
7 {
8  const IndexMap& map = m_maps[mapID];
9 
10  auto it = map.find(name);
11  if (it == map.end())
12  return 0;
13  else
14  return it->second;
15 }
16 
17 unsigned int ParticleExtraInfoMap::getMapForNewVar(const std::string& name)
18 {
19  const unsigned int insertIndex = 1; //0 reserved
20  for (unsigned int iMap = 0; iMap < m_maps.size(); iMap++) {
21  const auto it = m_maps[iMap].find(name);
22  if (it != m_maps[iMap].end() and it->second == insertIndex)
23  return iMap;
24  }
25  //nothing found, add new map
26  IndexMap map;
27  map[name] = insertIndex;
28  m_maps.push_back(map);
29 
30  //return new index
31  return m_maps.size() - 1;
32 }
33 
34 unsigned int ParticleExtraInfoMap::getMapForNewVar(const std::string& name, unsigned int oldMapID, unsigned int insertIndex)
35 {
36  const IndexMap& oldMap = m_maps[oldMapID];
37 
38  //first check if old map can be reused
39  const unsigned int lastIndexInOldMap = oldMap.size(); //+1 because of reserved 0 index
40  if (lastIndexInOldMap + 1 == insertIndex) {
41  //we can make oldMap fit by adding one entry
42  m_maps[oldMapID][name] = insertIndex;
43  return oldMapID;
44  }
45  auto oldMapIter = oldMap.find(name);
46  if (oldMapIter != oldMap.end()) {
47  if (oldMapIter->second == insertIndex) {
48  return oldMapID; //nothing to do
49  }
50  }
51 
52  for (unsigned int iMap = 0; iMap < m_maps.size(); iMap++) {
53  const IndexMap& map = m_maps[iMap];
54  const auto it = map.find(name);
55  if (it != map.end() and it->second == insertIndex) {
56  //seems promising
57  if (isCompatible(oldMap, map, insertIndex)) {
58  //compatible with oldMap, can be used
59  return iMap;
60  }
61  }
62  }
63 
64  //nothing found, add new map (copy all entries prior to insertIndex from oldMap)
65  IndexMap map;
66  for (const auto& pair : oldMap) {
67  if (pair.second < insertIndex)
68  map[pair.first] = pair.second;
69  }
70  map[name] = insertIndex;
71  m_maps.push_back(map);
72 
73  //return new index
74  return m_maps.size() - 1;
75 }
76 
77 bool ParticleExtraInfoMap::isCompatible(const IndexMap& oldMap, const IndexMap& map, unsigned int insertIndex)
78 {
79  for (const auto& pair : oldMap) {
80  if (pair.second < insertIndex) {
81  const auto it = map.find(pair.first);
82  if (it == map.end() or it->second != pair.second) {
83  //mismatch
84  return false;
85  }
86  }
87  }
88  return true;
89 }
Belle2::ParticleExtraInfoMap::IndexMap
std::map< std::string, unsigned int > IndexMap
string -> index map.
Definition: ParticleExtraInfoMap.h:42
Belle2::ParticleExtraInfoMap::getMapForNewVar
unsigned int getMapForNewVar(const std::string &name)
Return map ID to a map that has 'name' as first entry.
Definition: ParticleExtraInfoMap.cc:17
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ParticleExtraInfoMap::m_maps
std::vector< IndexMap > m_maps
List of string -> index maps.
Definition: ParticleExtraInfoMap.h:71
Belle2::ParticleExtraInfoMap::getIndex
unsigned int getIndex(unsigned int mapID, const std::string &name) const
Find index for name in the given map, or return 0 if not found.
Definition: ParticleExtraInfoMap.cc:6
Belle2::ParticleExtraInfoMap::isCompatible
static bool isCompatible(const IndexMap &oldMap, const IndexMap &map, unsigned int insertIndex)
check if all entries in 'oldMap' prior to insertIndex are found in 'map' (with same idx).
Definition: ParticleExtraInfoMap.cc:77