ATLAS Offline Software
IdentifierHashTable.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef MUONCALIBIDENTIFIER_IDENTIFIERHASHTABLE_H
6 #define MUONCALIBIDENTIFIER_IDENTIFIERHASHTABLE_H
7 
8 /***************************************************************************
9  * Identifier utility
10  * -----------------------------------------
11  *
12  * Author : Martin Woudstra
13  * Creation Date: 28 April 2004
14  * Last Update : 07 May 2004
15  ***************************************************************************/
16 
18 #include "GaudiKernel/MsgStream.h"
20 
21 #include <iostream>
22 #include <sstream>
23 #include <limits.h>
24 
36 template <class T>
38 public:
39  //
40  // nested types
41  //
43  typedef typename T::IdentifierType IdType;
45  typedef typename T::HashType HashType;
49  }
50  //
51  // Member functions
52  //
56  explicit IdentifierHashTable( const T& idFields );
58  void clear();
60  unsigned int size() const;
62  bool checkValidity() const;
65  HashType addEntry( const IdType& id );
67  HashType getHash( const IdType& id ) const;
69  IdType getIdentifier( const HashType& hash ) const;
71  void dump( std::ostream& os = std::cout ) const;
73  std::string dumpToString() const;
74 private:
76  std::vector<IdType> m_hashToId;
77 };
78 
79 template <class T>
81 {}
82 
83 template <class T>
84 inline IdentifierHashTable<T>::IdentifierHashTable( const T& idFields )
85  : m_idToHash(idFields)
86 {}
87 
88 template <class T>
91 #ifdef IDENTIFIERHASHTABLE_DEBUG
92  MsgStream log(Athena::getMessageSvc(),"IdentifierHashTable");
93  log<<MSG::DEBUG<<"IdentifierHashTable<T>::addEntry(0x" << std::hex << id << std::dec << ")"<<endmsg;
94 #endif
95  if ( !T::isValid( id ) ) {
96 #ifdef IDENTIFIERHASHTABLE_DEBUG
97  log<<MSG::DEBUG<<": id is invalid. Nothing added."<<endmsg;
98 #endif
99  return defaultHashValue();
100  }
101 #ifdef IDENTIFIERHASHTABLE_DEBUG
102  log<<MSG::DEBUG<<"::IdToHashTable"<<endmsg;
103 #endif
104  HashType theHash( m_hashToId.size() );
105  if ( !m_idToHash.addEntry( id, theHash ) ) {
106 #ifdef IDENTIFIERHASHTABLE_DEBUG
107  log<<MSG::DEBUG<<" WARNING: could not add to table"<<endmsg;
108 #endif
109  return defaultHashValue();
110  } else {
111  m_hashToId.push_back( id );
112 #ifdef IDENTIFIERHASHTABLE_DEBUG
113  log<<MSG::DEBUG<<" ADDED at hash=" << theHash<<endmsg;
114 #endif
115  }
116 
117  return theHash;
118 }
119 
120 template <class T>
121 inline typename IdentifierHashTable<T>::HashType
123  return m_idToHash.getHash( id );
124 }
125 
126 template <class T>
127 inline typename IdentifierHashTable<T>::IdType
129  unsigned int idx = hash;
130  if ( idx >= m_hashToId.size() ) return IdType();
131  return m_hashToId[ idx ];
132 }
133 
134 template <class T>
135 inline void IdentifierHashTable<T>::clear() {
136  m_idToHash.clear();
137  m_hashToId.clear();
138 }
139 
140 template <class T>
141 inline unsigned int IdentifierHashTable<T>::size() const {
142  return m_hashToId.size();
143 }
144 
145 template <class T>
147  // check that both tables are same size
148  unsigned int idToHashSize = m_idToHash.size();
149  if ( idToHashSize != m_hashToId.size() ) {
150  MsgStream log(Athena::getMessageSvc(),"IdentifierHashTable");
151  log<<MSG::WARNING<<"IdentifierHashTable<T>::checkValidity() idToHash size (" << idToHashSize << ") not equal to hashToId size (" << m_hashToId.size() << ")"<<endmsg;
152  return false;
153  }
154  // check that table is non-empty
155  if ( !m_hashToId.size() ) {
156  MsgStream log(Athena::getMessageSvc(),"IdentifierHashTable");
157  log<<MSG::WARNING<<"IdentifierHashTable<T>::checkValidity() Table is empty."<<endmsg;
158  return false;
159  }
160 #ifdef IDENTIFIERHASHTABLE_DEBUG
161  MsgStream log(Athena::getMessageSvc(),"IdentifierHashTable");
162 #endif
163  // check that tables are each other's inverse
164  unsigned int nErrors = 0;
165  for ( unsigned int i = 0; i < m_hashToId.size(); ++i ) {
166  HashType tryHash(i);
167  IdType id = getIdentifier( tryHash );
168  HashType gotHash = getHash( id );
169  if ( gotHash != tryHash ) {
170 #ifndef IDENTIFIERHASHTABLE_DEBUG
171  MsgStream log(Athena::getMessageSvc(),"IdentifierHashTable");
172 #endif
173  log<<MSG::WARNING<<"IdentifierHashTable<T>::checkValidity() getIdentifier(" << tryHash << ")=0x" << std::hex << id << std::dec << " whereas getHash(" << std::hex << id << std::dec << ")=" << gotHash<<endmsg;
174  ++nErrors;
175  } else {
176 #ifdef IDENTIFIERHASHTABLE_DEBUG
177  log<<MSG::DEBUG<<"hash=" << i << " <--> id=0x" << std::hex << id << std::dec << ": OK" <<endmsg;
178 #endif
179  }
180  }
181 #ifdef IDENTIFIERHASHTABLE_DEBUG
182  if ( nErrors ) {
183  log<<MSG::WARNING<<"IdentifierHashTable<T>::checkValidity() table contains " << nErrors << " errors." << endmsg;
184  } else {
185  log<<MSG::DEBUG<<"IdentifierHashTable<T>::checkValidity(): table OK" << endmsg;
186  }
187 #endif
188 
189  if ( nErrors ) return false;
190 
191  // if we get here, everything is OK!
192  return true;
193 }
194 
195 template <class T>
196 void IdentifierHashTable<T>::dump( std::ostream& os ) const {
197  int n = size();
198  for ( int i = 0; i < n; ++i ) {
199  HashType iHash(i);
200  IdType id = getIdentifier( iHash );
201  os << "ID=0x" << std::hex << id << std::dec << " fields";
202  os << m_idToHash.dumpOneEntryToString( id ) << "\n";
203  }
204  int nTotal = m_idToHash.totalSize();
205  os << "IdentifierHashTable has " << n << " valid entries and "
206  << nTotal - n << " wasted entries" << std::endl;
207 }
208 
209 template <class T>
210 inline std::string IdentifierHashTable<T>::dumpToString() const {
211  std::ostringstream oss;
212  dump( oss );
213  return oss.str();
214 }
215 
216 #endif // MUONCALIBIDENTIFIER_IDENTIFIERHASHTABLE_H
IdentifierToHash
The IdentifierToHash table.
Definition: IdentifierToHash.h:39
IdentifierHashTable::IdentifierHashTable
IdentifierHashTable(const T &idFields)
Make empty hash table initialised with idFields object.
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
IdentifierHashTable::getHash
HashType getHash(const IdType &id) const
Get hash from 0 to size()-1.
IdentifierToHash::defaultHashValue
static HashType defaultHashValue()
Helper function to get a properly initialised hash.
Definition: IdentifierToHash.h:62
IdentifierHashTable::HashType
T::HashType HashType
define the type HashType
Definition: IdentifierHashTable.h:45
isValid
bool isValid(const T &p)
Definition: AtlasPID.h:214
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
IdentifierHashTable::size
unsigned int size() const
Number of hashes in the table.
IdentifierToHash.h
IdentifierHashTable::clear
void clear()
Clear the hashtable.
lumiFormat.i
int i
Definition: lumiFormat.py:92
IdentifierHashTable::checkValidity
bool checkValidity() const
Check that the table is internally consistent.
beamspotman.n
n
Definition: beamspotman.py:731
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
IdentifierHashTable::getIdentifier
IdType getIdentifier(const HashType &hash) const
Get identifier from hash.
IdentifierHashTable::addEntry
HashType addEntry(const IdType &id)
Add an identifier to the table.
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
IdentifierHashTable::IdentifierHashTable
IdentifierHashTable()
Default constructor makes empty hash table with default idFields object.
IdentifierHashTable::m_idToHash
IdentifierToHash< T > m_idToHash
Definition: IdentifierHashTable.h:75
IdentifierHashTable::dump
void dump(std::ostream &os=std::cout) const
Dump complete table to output stream.
IdentifierHashTable
Definition: IdentifierHashTable.h:37
IdentifierHashTable::m_hashToId
std::vector< IdType > m_hashToId
Definition: IdentifierHashTable.h:76
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
IdentifierHashTable::IdType
T::IdentifierType IdType
define the type IdType
Definition: IdentifierHashTable.h:43
DEBUG
#define DEBUG
Definition: page_access.h:11
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
IdentifierHashTable::dumpToString
std::string dumpToString() const
Dump complete table into a string.
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
IdentifierHashTable::defaultHashValue
static HashType defaultHashValue()
Helper function to get a properly initialised hash.
Definition: IdentifierHashTable.h:47