00001 #ifndef ECALDETID_ECALCONTAINER_H 00002 #define ECALDETID_ECALCONTAINER_H 00003 00004 #include "DataFormats/DetId/interface/DetId.h" 00005 #include <vector> 00006 #include <utility> 00007 #include <algorithm> 00008 00009 #include <iostream> 00010 00011 00012 00013 /* a generic container for ecal items 00014 * provides access by hashedIndex and by DetId... 00015 */ 00016 00017 template<typename DetId, typename T> 00018 class EcalContainer { 00019 00020 public: 00021 00022 typedef EcalContainer<DetId, T> self; 00023 typedef T Item; 00024 typedef Item value_type; 00025 typedef typename std::vector<Item> Items; 00026 typedef typename std::vector<Item>::const_iterator const_iterator; 00027 typedef typename std::vector<Item>::iterator iterator; 00028 00029 void insert(std::pair<uint32_t, Item> const &a) { 00030 (*this)[a.first] = a.second; 00031 } 00032 00033 inline const Item & item(size_t hashid) const { 00034 return m_items[hashid]; 00035 } 00036 00037 inline const Items & items() const { 00038 return m_items; 00039 } 00040 00041 inline Item & operator[](uint32_t rawId) { 00042 checkAndResize(); 00043 static Item dummy; 00044 DetId id(rawId); 00045 if ( !isValidId(id) ) return dummy; 00046 return m_items[id.hashedIndex()]; 00047 } 00048 00049 00050 void checkAndResize() { 00051 if (m_items.size()==0) { 00052 // std::cout << "resizing to " << DetId::kSizeForDenseIndexing << std::endl; 00053 m_items.resize(DetId::kSizeForDenseIndexing); 00054 } 00055 } 00056 00057 00058 void checkAndResize( size_t priv_size ) { 00059 // this method allows to resize the vector to a specific size forcing a specific value 00060 if (m_items.size()==0) { 00061 // std::cout << "resizing to " << priv_size << std::endl; 00062 m_items.resize(priv_size); 00063 } 00064 } 00065 00066 inline Item const & operator[](uint32_t rawId) const { 00067 // if (m_items.size()==0) { 00068 // std::cout << "resizing to " << DetId::kSizeForDenseIndexing << std::endl; 00069 // m_items.resize((size_t) DetId::kSizeForDenseIndexing); 00070 // } 00071 static Item dummy; 00072 DetId id(rawId); 00073 if ( !isValidId(id) ) return dummy; 00074 return m_items[id.hashedIndex()]; 00075 } 00076 00077 inline const_iterator find(uint32_t rawId) const { 00078 DetId ib(rawId); 00079 if ( !isValidId(ib) ) return m_items.end(); 00080 return m_items.begin() + ib.hashedIndex(); 00081 } 00082 00083 inline const_iterator begin() const { 00084 return m_items.begin(); 00085 } 00086 00087 inline const_iterator end() const { 00088 return m_items.end(); 00089 } 00090 00091 inline size_t size() const { 00092 return m_items.size(); 00093 } 00094 00095 private: 00096 00097 // not protected on EB <--> EE swap -- FIXME? 00098 inline bool isValidId(const DetId id) const { 00099 return id.det() == ::DetId::Ecal; 00100 }; 00101 00102 std::vector<Item> m_items; 00103 00104 }; 00105 00106 00107 00108 #endif // ECALCONTAINER