CMS 3D CMS Logo

EcalContainer.h
Go to the documentation of this file.
1 #ifndef ECALDETID_ECALCONTAINER_H
2 #define ECALDETID_ECALCONTAINER_H
3 
5 #include <vector>
6 #include <utility>
7 #include <algorithm>
8 
9 // #include <iostream>
10 
11 
12 
13 /* a generic container for ecal items
14  * provides access by hashedIndex and by DetId...
15  */
16 
17 template<typename DetId, typename T>
19 
20  public:
21 
23  typedef T Item;
24  typedef Item value_type;
25  typedef typename std::vector<Item> Items;
26  typedef typename std::vector<Item>::const_iterator const_iterator;
27  typedef typename std::vector<Item>::iterator iterator;
28 
29 
31 
32  void clear() {
33  m_items.clear();
35  }
36 
37  void insert(std::pair<uint32_t, Item> const &a) {
38  (*this)[a.first] = a.second;
39  }
40 
41  inline const Item & item(size_t hashid) const {
42  return m_items[hashid];
43  }
44 
45  inline const Items & items() const {
46  return m_items;
47  }
48 
49  inline Item & operator[](uint32_t rawId) {
51  static Item dummy;
52  DetId id(rawId);
53  if ( !isValidId(id) ) return dummy;
54  return m_items[id.hashedIndex()];
55  }
56 
57 
58  void checkAndResize() {
59  if (m_items.empty()) {
60  // std::cout << "resizing to " << DetId::kSizeForDenseIndexing << std::endl;
61  m_items.resize(DetId::kSizeForDenseIndexing);
62  }
63  }
64 
65 
66  void checkAndResize( size_t priv_size ) {
67  // this method allows to resize the vector to a specific size forcing a specific value
68  if (m_items.empty()) {
69  // std::cout << "resizing to " << priv_size << std::endl;
70  m_items.resize(priv_size);
71  }
72  }
73 
74  inline Item const & operator[](uint32_t rawId) const {
75  // if (m_items.size()==0) {
76  // std::cout << "resizing to " << DetId::kSizeForDenseIndexing << std::endl;
77  // m_items.resize((size_t) DetId::kSizeForDenseIndexing);
78  // }
79  DetId id(rawId);
80  if ( !isValidId(id) ) return dummy_item();
81  return m_items[id.hashedIndex()];
82  }
83 
84  inline const_iterator find(uint32_t rawId) const {
85  DetId ib(rawId);
86  if ( !isValidId(ib) ) return m_items.end();
87  return m_items.begin() + ib.hashedIndex();
88  }
89 
90  inline const_iterator begin() const {
91  return m_items.begin();
92  }
93 
94  inline const_iterator end() const {
95  return m_items.end();
96  }
97 
98  inline size_t size() const {
99  return m_items.size();
100  }
101 
102  void setItems(const std::vector<Item> & items) {
103  m_items = items;
104  }
105  private:
106 
107  //Cint can't parse the new C++11 initialization syntax
108 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
109 
110  static const Item& dummy_item() {
111  static const Item s_dummy{};
112  return s_dummy;
113  }
114 #else
115  static const Item& dummy_item();
116 #endif
117  // not protected on EB <--> EE swap -- FIXME?
118  inline bool isValidId(const DetId id) const {
119  return id.det() == ::DetId::Ecal;
120  };
121 
122  std::vector<Item> m_items;
123 
124 };
125 
126 
127 
128 #endif // ECALCONTAINER
const_iterator begin() const
Definition: EcalContainer.h:90
const Item & item(size_t hashid) const
Definition: EcalContainer.h:41
void setItems(const std::vector< Item > &items)
std::vector< Item >::iterator iterator
Definition: EcalContainer.h:27
const Items & items() const
Definition: EcalContainer.h:45
const_iterator end() const
Definition: EcalContainer.h:94
std::vector< Item > m_items
std::vector< Item >::const_iterator const_iterator
Definition: EcalContainer.h:26
const_iterator find(uint32_t rawId) const
Definition: EcalContainer.h:84
Definition: DetId.h:18
Item & operator[](uint32_t rawId)
Definition: EcalContainer.h:49
void checkAndResize()
Definition: EcalContainer.h:58
static const Item & dummy_item()
void checkAndResize(size_t priv_size)
Definition: EcalContainer.h:66
Item const & operator[](uint32_t rawId) const
Definition: EcalContainer.h:74
void insert(std::pair< uint32_t, Item > const &a)
Definition: EcalContainer.h:37
double a
Definition: hdecay.h:121
bool isValidId(const DetId id) const
std::vector< Item > Items
Definition: EcalContainer.h:25
size_t size() const
Definition: EcalContainer.h:98
long double T
ib
Definition: cuy.py:662