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