ATLAS Offline Software
IdentifierToHash.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_IDENTIFIERTOHASH_H
6 #define MUONCALIBIDENTIFIER_IDENTIFIERTOHASH_H
7 
8 /***************************************************************************
9  * Identifier utility
10  * -----------------------------------------
11  *
12  * Author : Martin Woudstra
13  * Creation Date: 28 April 2004
14  * Last Update : 27 April 2005
15  ***************************************************************************/
16 
18 #include "GaudiKernel/MsgStream.h"
20 
21 #include <iostream>
22 #include <sstream>
23 #include <string>
24 #include <limits.h>
25 
38 template <class T>
40 public:
41  //
42  // nested types
43  //
47  enum { NFIELDS = T::NFIELDS, NMAX=UINT_MAX };
49  typedef typename T::IdentifierType IdType;
51  typedef typename T::HashType HashType;
55  static HashType defaultHashValue() {
56  return T::defaultHash();
57  }
61  template < unsigned int N, class K >
63  public:
64  static const typename K::ValueType& getConstRef( const K& obj, const T& indices ) {
65  int idx = indices[N-1];
66 #ifdef IDENTIFIERTOHASH_DEBUG
67  MsgStream log(Athena::getMessageSvc(),"RecursiveIndexCall");
68  log<<MSG::DEBUG<<"[" << idx << "]"<<endmsg;
69 #endif
71  }
72  static typename K::ValueType& getRef( K& obj, const T& indices ) {
73  int idx = indices[N-1];
74 #ifdef IDENTIFIERTOHASH_DEBUG
75  log<<MSG::DEBUG<<"{" << idx << "}"<<endmsg;
76 #endif
78  }
79  };
80 
81  // specialise for N=1 to end recursion
82  template < class K >
83  class RecursiveIndexCall<1,K> {
84  public:
85  static const typename K::ValueType& getConstRef( const K& obj, const T& indices ) {
86  int idx = indices[0];
87 #ifdef IDENTIFIERTOHASH_DEBUG
88  MsgStream log(Athena::getMessageSvc(),"RecursiveIndexCall");
89  log<<MSG::DEBUG<<"[" << idx << "]"<<endmsg;
90 #endif
91  return obj[ idx ];
92  }
93  static typename K::ValueType& getRef( K& obj, const T& indices ) {
94  int idx = indices[0];
95 #ifdef IDENTIFIERTOHASH_DEBUG
96  log<<MSG::DEBUG<<"{" << idx << "}"<<endmsg;
97 #endif
98  return obj[ idx ];
99  }
100  };
101 
102  // zero is invalid. Make empty class.
103  template < class K >
104  class RecursiveIndexCall<0,K> {
105  };
109  template < unsigned int N, class K>
110  class RecursiveRangeCheck {
111  public:
112  static bool isInRange( const K& obj, const T& indices ) {
113  int idx = indices[N-1];
114  bool bOK = obj.isInRange( idx );
115 #ifdef IDENTIFIERTOHASH_DEBUG
116  MsgStream log(Athena::getMessageSvc(),"RecursiveRangeCheck");
117  log<<MSG::DEBUG<<"(" << idx << ")"<<endmsg;
118  if ( !bOK ) {
119  log<<MSG::DEBUG<<" CHECK: " << idx << " Out of range (" << obj.minIndex() << "," << obj.maxIndex() << ")"<<endmsg;
120  }
121 #endif
123  }
124 
125  };
127  template <class K>
128  class RecursiveRangeCheck<1,K> {
129  public:
130  static bool isInRange( const K& obj, const T& indices ) {
131  int idx = indices[0];
132  bool bOK = obj.isInRange( idx );
133 #ifdef IDENTIFIERTOHASH_DEBUG
134  MsgStream log(Athena::getMessageSvc(),"RecursiveRangeCheck");
135  log<<MSG::DEBUG<<"(" << idx << ")"<<endmsg;
136  if ( !bOK ) {
137  log<<MSG::DEBUG<<" CHECK: Out of range (" << obj.minIndex() << "," << obj.maxIndex() << ")"<<endmsg;
138  }else {
139  log<<MSG::DEBUG<<" CHECK: Range OK"<<endmsg;
140  }
141 #endif
142  return bOK;
143  }
144  };
146  template <class K>
147  class RecursiveRangeCheck<0,K> {
148  // empty class
149  };
150  //
151  // Member functions
152  //
156  explicit IdentifierToHash( const T& idFields );
158  void clear();
160  unsigned int size() const;
162  unsigned int totalSize() const;
166  HashType addEntry( const IdType& id );
171  bool addEntry( const IdType& id, const HashType& aHash );
173  HashType getHash( const IdType& id ) const;
175  void dumpOneEntry( const IdType& id, std::ostream& os ) const;
177  std::string dumpOneEntryToString( const IdType& id ) const;
179  void dump( std::ostream& os = std::cout ) const;
181  std::string dumpToString() const;
182 private:
183  mutable T m_idFields;
184  unsigned int m_entries;
186 };
187 
188 template <class T>
190  : m_entries(0)
191 {}
192 
193 template <class T>
194 inline IdentifierToHash<T>::IdentifierToHash( const T& idFields )
195  : m_idFields( idFields ), m_entries(0)
196 {}
197 
198 template <class T>
199 bool
201  const typename IdentifierToHash<T>::HashType& aHash ) {
202 #ifdef IDENTIFIERTOHASH_DEBUG
203  MsgStream log(Athena::getMessageSvc(),"IdentifierToHash<T>");
204  log<<MSG::DEBUG<<"IdentifierToHash<T>::addEntry(0x" << std::hex << id << std::dec << "," << aHash << ")"<<endmsg;
205 #endif
206  if ( !T::isValid( id ) ) {
207 #ifdef IDENTIFIERTOHASH_DEBUG
208  log<<MSG::DEBUG<<": id is invalid. Nothing added."<<endmsg;
209 #endif
210  return false;
211  }
212  m_idFields.setAll( id );
213 #ifdef
214  log<<MSG::DEBUG<<"::IdToHashTable"<<endmsg;
215 #endif
216 
217  HashType& hashRef =
218  RecursiveIndexCall< NFIELDS, ArrayType >::getRef( m_data, m_idFields );
219  // only add hash if entry is not yet taken
220  if ( hashRef != defaultHashValue() ) {
221 #ifdef IDENTIFIERTOHASH_DEBUG
222  log<<MSG::WARNING<<"id already taken by hash=" << hashRef << ". Nothing added."<<endmsg;
223 #endif
224  return false;
225  } else {
226  // set the hash to the indicated value
227  hashRef = aHash;
228 #ifdef IDENTIFIERTOHASH_DEBUG
229  log<<MSG::DEBUG<<" ADDED."<<endmsg;
230 #endif
231  }
232 
233  ++m_entries;
234  return true;
235 }
236 
237 template <class T>
240  HashType aHash = m_entries;
241  if ( !addEntry( id, aHash ) ) return defaultHashValue();
242  return aHash;
243 }
244 
245 template <class T>
247 IdentifierToHash<T>::getHash( const typename IdentifierToHash<T>::IdType& id ) const {
248  if ( !T::isValid( id ) ) return defaultHashValue();
249  m_idFields.setAll( id );
250  // check that fields are in range
252  return defaultHashValue();
253  }
255 }
256 
257 template <class T>
258 inline void IdentifierToHash<T>::clear() {
259  m_entries = 0;
260  m_data.clear();
261 }
262 
263 template <class T>
264 inline unsigned int IdentifierToHash<T>::size() const {
265  return m_entries;
266 }
267 
268 
269 template <class T>
270 inline unsigned int IdentifierToHash<T>::totalSize() const {
271  return m_data.totalSize();
272 }
273 
274 template <class T>
275 inline void IdentifierToHash<T>::dumpOneEntry( const typename IdentifierToHash<T>::IdType& id,
276  std::ostream& os ) const {
277  m_idFields.setAll( id );
278  m_data.dumpOneEntry( m_idFields, os );
279 }
280 
281 template <class T>
282 inline std::string IdentifierToHash<T>::dumpOneEntryToString( const typename IdentifierToHash<T>::IdType& id ) const {
283  m_idFields.setAll( id );
284  return m_data.dumpOneEntryToString( m_idFields );
285 }
286 
287 template <class T>
288 inline void IdentifierToHash<T>::dump( std::ostream& os ) const {
289  m_data.dump( os );
290 }
291 
292 template <class T>
293 inline std::string IdentifierToHash<T>::dumpToString() const {
294  return m_data.dumpToString();
295 }
296 
297 
298 #endif // MUONCALIBIDENTIFIER_IDENTIFIERTOHASH_H
IdentifierToHash::dumpOneEntry
void dumpOneEntry(const IdType &id, std::ostream &os) const
Dump one entry for given id to os.
Definition: IdentifierToHash.h:274
IdentifierToHash::getHash
HashType getHash(const IdType &id) const
Get hash from 0 to size()-1.
Definition: IdentifierToHash.h:246
IdentifierToHash::m_idFields
T m_idFields
Definition: IdentifierToHash.h:190
IdentifierToHash
The IdentifierToHash table.
Definition: IdentifierToHash.h:39
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
IdentifierToHash::defaultHashValue
static HashType defaultHashValue()
Helper function to get a properly initialised hash.
Definition: IdentifierToHash.h:62
m_data
std::vector< T > m_data
Definition: TrackTruthMatchingBaseAlg.cxx:660
Trk::indices
std::pair< long int, long int > indices
Definition: AlSymMatBase.h:24
IdentifierToHash::NFIELDS
@ NFIELDS
Definition: IdentifierToHash.h:61
IdentifierToHash::IdType
T::IdentifierType IdType
define type IdType
Definition: IdentifierToHash.h:56
IdentifierToHash::RecursiveIndexCall::getConstRef
static const K::ValueType & getConstRef(const K &obj, const T &indices)
Definition: IdentifierToHash.h:71
JetTiledMap::N
@ N
Definition: TiledEtaPhiMap.h:44
isValid
bool isValid(const T &p)
Definition: AtlasPID.h:214
IdentifierToHash::dumpToString
std::string dumpToString() const
Dump complete table into a string.
Definition: IdentifierToHash.h:292
IdentifierToHash::NMAX
@ NMAX
Definition: IdentifierToHash.h:61
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
IdentifierToHash::clear
void clear()
Clear the hashtable.
Definition: IdentifierToHash.h:257
IdentifierToHash::RecursiveRangeCheck::isInRange
static bool isInRange(const K &obj, const T &indices)
Definition: IdentifierToHash.h:119
IdentifierToHash::dumpOneEntryToString
std::string dumpOneEntryToString(const IdType &id) const
Dump one entry for given id to a string.
Definition: IdentifierToHash.h:281
IdentifierToHash::RecursiveIndexCall::getRef
static K::ValueType & getRef(K &obj, const T &indices)
Definition: IdentifierToHash.h:79
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
IdentifierToHash::RecursiveRangeCheck
Helper class for one-go recursive range check on all indices.
Definition: IdentifierToHash.h:117
MultiDimArray.h
IdentifierToHash::RecursiveIndexCall
Helper class for IdentifierToHash to automate recursive index call.
Definition: IdentifierToHash.h:69
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
IdentifierToHash::totalSize
unsigned int totalSize() const
Total number of hashes in the table.
Definition: IdentifierToHash.h:269
IdentifierToHash::size
unsigned int size() const
Number of valid hashes in the table.
Definition: IdentifierToHash.h:263
IdentifierToHash::dump
void dump(std::ostream &os=std::cout) const
Dump complete table to output stream.
Definition: IdentifierToHash.h:287
IdentifierToHash::m_entries
unsigned int m_entries
Definition: IdentifierToHash.h:191
DEBUG
#define DEBUG
Definition: page_access.h:11
IdentifierToHash::addEntry
HashType addEntry(const IdType &id)
Add an identifier to the table, and return the hash value belonging to it.
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
IdentifierToHash::HashType
T::HashType HashType
define type HashType
Definition: IdentifierToHash.h:58
IdentifierToHash::m_data
ArrayType m_data
Definition: IdentifierToHash.h:192
python.PyAthena.obj
obj
Definition: PyAthena.py:135
MultiDimArray< T, NFIELDS >
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
IdentifierToHash::ArrayType
MultiDimArray< T, NFIELDS > ArrayType
define type ArrayType
Definition: IdentifierToHash.h:60
IdentifierToHash::IdentifierToHash
IdentifierToHash()
Default constructor makes empty hash table and creates default idFields object.
Definition: IdentifierToHash.h:189