CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/DataFormats/EcalDetId/interface/EcalContainer.h

Go to the documentation of this file.
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                    
00030                 EcalContainer() {checkAndResize();}
00031 
00032                 void insert(std::pair<uint32_t, Item> const &a) {
00033                         (*this)[a.first] = a.second;
00034                 }
00035 
00036                 inline const Item & item(size_t hashid) const {
00037                         return m_items[hashid];
00038                 }
00039 
00040                 inline const Items & items() const {
00041                         return m_items;
00042                 }
00043 
00044                 inline Item & operator[](uint32_t rawId) {
00045                   checkAndResize();
00046                   static Item dummy;
00047                   DetId id(rawId);
00048                   if ( !isValidId(id) ) return dummy;
00049                   return m_items[id.hashedIndex()];
00050                 }
00051 
00052 
00053                 void checkAndResize() {
00054                   if (m_items.size()==0) {
00055                     //              std::cout << "resizing to " << DetId::kSizeForDenseIndexing << std::endl;
00056                     m_items.resize(DetId::kSizeForDenseIndexing);
00057                   }
00058                 }
00059 
00060 
00061                 void checkAndResize( size_t priv_size ) {
00062                   // this method allows to resize the vector to a specific size forcing a specific value
00063                   if (m_items.size()==0) {
00064                     //              std::cout << "resizing to " << priv_size << std::endl;
00065                     m_items.resize(priv_size);
00066                   }
00067                 }
00068 
00069                 inline Item const & operator[](uint32_t rawId) const {
00070                   //                        if (m_items.size()==0) {
00071                   //      std::cout << "resizing to " << DetId::kSizeForDenseIndexing << std::endl;
00072                   //              m_items.resize((size_t) DetId::kSizeForDenseIndexing);
00073                   //      }
00074                         static Item dummy;
00075                         DetId id(rawId);
00076                         if ( !isValidId(id) ) return dummy;
00077                         return m_items[id.hashedIndex()];
00078                 }
00079 
00080                 inline const_iterator find(uint32_t rawId) const {
00081                         DetId ib(rawId);
00082                         if ( !isValidId(ib) ) return m_items.end();
00083                         return m_items.begin() + ib.hashedIndex();
00084                 }
00085 
00086                 inline const_iterator begin() const {
00087                         return m_items.begin();
00088                 }
00089 
00090                 inline const_iterator end() const {
00091                         return m_items.end();
00092                 }
00093 
00094                 inline size_t size() const {
00095                         return m_items.size();
00096                 }
00097 
00098         private:
00099 
00100                 // not protected on EB <--> EE swap -- FIXME?
00101                 inline bool isValidId(const DetId id) const {
00102                         return id.det() == ::DetId::Ecal;
00103                 };
00104 
00105                 std::vector<Item> m_items;
00106 
00107 };
00108 
00109 
00110 
00111 #endif // ECALCONTAINER