Belle II Software light-2406-ragdoll
ParticleExtraInfoMap Class Reference

Internal class to store string -> index maps for extra info stored in Particle. More...

#include <ParticleExtraInfoMap.h>

Inheritance diagram for ParticleExtraInfoMap:
Collaboration diagram for ParticleExtraInfoMap:

Public Types

typedef std::map< std::string, unsigned int > IndexMap
 string -> index map.
 

Public Member Functions

const IndexMapgetMap (unsigned int mapID) const
 Return reference to map with given ID.
 
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.
 
unsigned int getMapForNewVar (const std::string &name)
 Return map ID to a map that has 'name' as first entry.
 
unsigned int getMapForNewVar (const std::string &name, unsigned int oldMapID, unsigned int insertIndex)
 Return map ID to a map that has 'name' at place 'insertIndex' and is compatible with previous map (in oldMapID)
 
unsigned int getNMaps () const
 How many maps do we use?
 

Private Member Functions

 ClassDef (ParticleExtraInfoMap, 1)
 Internal class to store string -> index maps for user-defined variables in Particle.
 

Static Private Member Functions

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).
 

Private Attributes

std::vector< IndexMapm_maps
 List of string -> index maps.
 

Detailed Description

Internal class to store string -> index maps for extra info stored in Particle.

An object of this class is used to store all strings used to identify the values added using Particle::addExtraInfo() and keeps Particles fairly light (only values and a map ID are stored directly). The necessary indirection is handled automatically by Particle::getExtraInfo().

Modules registering a StoreArray<Particle> should always register a StoreObjPtr<ParticleExtraInfoMap> to allow storing additional information.

Definition at line 29 of file ParticleExtraInfoMap.h.

Member Typedef Documentation

◆ IndexMap

typedef std::map<std::string, unsigned int> IndexMap

string -> index map.

Definition at line 32 of file ParticleExtraInfoMap.h.

Constructor & Destructor Documentation

◆ ParticleExtraInfoMap()

Definition at line 34 of file ParticleExtraInfoMap.h.

34: TObject() { }

Member Function Documentation

◆ 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 at line 14 of file ParticleExtraInfoMap.cc.

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}
std::vector< IndexMap > m_maps
List of string -> index maps.
std::map< std::string, unsigned int > IndexMap
string -> index map.

◆ getMap()

const IndexMap & getMap ( unsigned int  mapID) const
inline

Return reference to map with given ID.

Definition at line 37 of file ParticleExtraInfoMap.h.

37{ return m_maps[mapID]; }

◆ getMapForNewVar() [1/2]

unsigned int getMapForNewVar ( const std::string &  name)

Return map ID to a map that has 'name' as first entry.

Creates a new map if necessary.

Definition at line 25 of file ParticleExtraInfoMap.cc.

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}

◆ getMapForNewVar() [2/2]

unsigned int getMapForNewVar ( const std::string &  name,
unsigned int  oldMapID,
unsigned int  insertIndex 
)

Return map ID to a map that has 'name' at place 'insertIndex' and is compatible with previous map (in oldMapID)

Will reuse old map or another existing map if compatible, creates a new map if necessary.

Definition at line 42 of file ParticleExtraInfoMap.cc.

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}
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).

◆ getNMaps()

unsigned int getNMaps ( ) const
inline

How many maps do we use?

Definition at line 55 of file ParticleExtraInfoMap.h.

55{ return m_maps.size(); }

◆ isCompatible()

bool isCompatible ( const IndexMap oldMap,
const IndexMap map,
unsigned int  insertIndex 
)
staticprivate

check if all entries in 'oldMap' prior to insertIndex are found in 'map' (with same idx).

Definition at line 85 of file ParticleExtraInfoMap.cc.

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}

Member Data Documentation

◆ m_maps

std::vector<IndexMap> m_maps
private

List of string -> index maps.

Definition at line 61 of file ParticleExtraInfoMap.h.


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