Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:03

0001 #ifndef ECALDETID_ECALCONTAINER_H
0002 #define ECALDETID_ECALCONTAINER_H
0003 
0004 #include "DataFormats/DetId/interface/DetId.h"
0005 #include <vector>
0006 #include <utility>
0007 #include <algorithm>
0008 
0009 // #include <iostream>
0010 
0011 /* a generic container for ecal items
0012  * provides access by hashedIndex and by DetId...
0013  */
0014 
0015 template <typename DetId, typename T>
0016 class EcalContainer {
0017 public:
0018   typedef EcalContainer<DetId, T> self;
0019   typedef T Item;
0020   typedef Item value_type;
0021   typedef typename std::vector<Item> Items;
0022   typedef typename std::vector<Item>::const_iterator const_iterator;
0023   typedef typename std::vector<Item>::iterator iterator;
0024 
0025   EcalContainer() { checkAndResize(); }
0026 
0027   void clear() {
0028     m_items.clear();
0029     checkAndResize();
0030   }
0031 
0032   void insert(std::pair<uint32_t, Item> const& a) { (*this)[a.first] = a.second; }
0033 
0034   inline const Item& item(size_t hashid) const { return m_items[hashid]; }
0035 
0036   inline const Items& items() const { return m_items; }
0037 
0038   inline Item& operator[](uint32_t rawId) {
0039     checkAndResize();
0040     static Item dummy;
0041     DetId id(rawId);
0042     if (!isValidId(id))
0043       return dummy;
0044     return m_items[id.hashedIndex()];
0045   }
0046 
0047   void checkAndResize() {
0048     if (m_items.empty()) {
0049       //            std::cout << "resizing to " << DetId::kSizeForDenseIndexing << std::endl;
0050       m_items.resize(DetId::kSizeForDenseIndexing);
0051     }
0052   }
0053 
0054   void checkAndResize(size_t priv_size) {
0055     // this method allows to resize the vector to a specific size forcing a specific value
0056     if (m_items.empty()) {
0057       //            std::cout << "resizing to " << priv_size << std::endl;
0058       m_items.resize(priv_size);
0059     }
0060   }
0061 
0062   inline Item const& operator[](uint32_t rawId) const {
0063     //                        if (m_items.size()==0) {
0064     //    std::cout << "resizing to " << DetId::kSizeForDenseIndexing << std::endl;
0065     //              m_items.resize((size_t) DetId::kSizeForDenseIndexing);
0066     //      }
0067     DetId id(rawId);
0068     if (!isValidId(id))
0069       return dummy_item();
0070     return m_items[id.hashedIndex()];
0071   }
0072 
0073   inline const_iterator find(uint32_t rawId) const {
0074     DetId ib(rawId);
0075     if (!isValidId(ib))
0076       return m_items.end();
0077     return m_items.begin() + ib.hashedIndex();
0078   }
0079 
0080   inline const_iterator begin() const { return m_items.begin(); }
0081 
0082   inline const_iterator end() const { return m_items.end(); }
0083 
0084   inline size_t size() const { return m_items.size(); }
0085 
0086   void setItems(const std::vector<Item>& items) { m_items = items; }
0087 
0088 private:
0089   //Cint can't parse the new C++11 initialization syntax
0090 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
0091 
0092   static const Item& dummy_item() {
0093     static const Item s_dummy{};
0094     return s_dummy;
0095   }
0096 #else
0097   static const Item& dummy_item();
0098 #endif
0099   // not protected on EB <--> EE swap -- FIXME?
0100   inline bool isValidId(const DetId id) const { return id.det() == ::DetId::Ecal; };
0101 
0102   std::vector<Item> m_items;
0103 };
0104 
0105 #endif  // ECALCONTAINER