CMS 3D CMS Logo

HcalItemCollById.h
Go to the documentation of this file.
1 #ifndef CondFormats_HcalObjects_HcalItemCollById_h
2 #define CondFormats_HcalObjects_HcalItemCollById_h
3 
4 #include <cstdint>
5 
7 
13 
14 //
15 // This collection allows lookup of items by HcalDetId.
16 // If the given HcalDetId is not explicitly listed in the
17 // lookup table, default item is returned.
18 //
19 // Just like HcalItemColl, this collection works with pointers
20 // and references only, so it can be used with the inheritance
21 // scenarios. Note that the ownership of objects is shared with
22 // the collection provided in the constructor. The default item
23 // is owned by this collection. Its ownership will also become
24 // shared if a copy of this collection is made.
25 //
26 template <typename Item>
28 public:
29  typedef Item value_type;
30 
31  // Dummy default constructor. To be used only for deserialization.
33 
34  // Normal constructor
36  const HcalIndexLookup& indexLookupTable,
37  const unsigned detIdTransformCode,
38  std::unique_ptr<Item> defaultItem)
39  : coll_(coll), lookup_(indexLookupTable), default_(defaultItem.release()), transformCode_(detIdTransformCode) {
40  // Check that the lookup table is valid for this application
42  throw cms::Exception(
43  "In HcalItemCollById constructor:"
44  " invalid lookup table");
45 
46  // Check that the lookup table is consistent with the size
47  // of the collection
48  const unsigned maxIndex = lookup_.largestIndex();
50  throw cms::Exception(
51  "In HcalItemCollById constructor:"
52  " collection and lookup table are inconsistent");
53 
55  }
56 
57  inline ~HcalItemCollById() override {}
58 
59  // Modifier for the default item
60  inline void setDefault(std::unique_ptr<Item> f) { default_ = std::shared_ptr<Item>(f.release()); }
61 
62  // Size of the internal collection, not counting the default
63  inline std::size_t size() const { return coll_.size(); }
64 
65  // The following method will return nullptr if there is no default
66  inline const Item* getDefault() const { return default_.get(); }
67 
68  // Look up the index into the collection by detector id
69  inline unsigned getIndex(const HcalDetId& id) const {
71  }
72 
73  // Get an item by its index in the collection. If the index
74  // is out of range, the default item is returned. If the
75  // index is out of range and there is no default, nullptr
76  // is returned.
77  inline const Item* getByIndex(const unsigned index) const {
78  if (index < coll_.size())
79  return coll_.get(index);
80  else
81  return default_.get();
82  }
83 
84  // Convenience function for getting what we need by id.
85  // This method can return nullptr.
86  inline const Item* get(const HcalDetId& id) const { return getByIndex(getIndex(id)); }
87 
88  // The following method will throw an exception if the id is not
89  // in the lookup table and, in addition, there is no default
90  inline const Item& at(const HcalDetId& id) const {
91  const Item* ptr = getByIndex(getIndex(id));
92  if (ptr == nullptr)
93  throw cms::Exception("In HcalItemCollById::at: invalid detector id");
94  return *ptr;
95  }
96 
97 protected:
98  bool isEqual(const AbsHcalAlgoData& other) const override {
99  const HcalItemCollById& r = static_cast<const HcalItemCollById&>(other);
100  if (coll_ != r.coll_)
101  return false;
102  if (lookup_ != r.lookup_)
103  return false;
104  if (transformCode_ != r.transformCode_)
105  return false;
106  // The default may or may not be there
107  const bool ld = default_.get();
108  const bool rd = r.default_.get();
109  if (ld != rd)
110  return false;
111  if (ld)
112  if (!(*default_ == *r.default_))
113  return false;
114  return true;
115  }
116 
117 private:
120  std::shared_ptr<Item> default_;
121  uint32_t transformCode_;
122 
124 
125  template <class Archive>
126  inline void serialize(Archive& ar, unsigned /* version */) {
128  }
129 };
130 
131 // boost serialization version number for this template
132 namespace boost {
133  namespace serialization {
134  template <typename Item>
135  struct version<HcalItemCollById<Item> > {
136  BOOST_STATIC_CONSTANT(int, value = 1);
137  };
138  } // namespace serialization
139 } // namespace boost
140 
141 #endif // CondFormats_HcalObjects_HcalItemCollById_h
unsigned largestIndex() const
Definition: CLHEP.h:16
HcalItemColl< Item > coll_
bool isEqual(const AbsHcalAlgoData &other) const override
HcalIndexLookup lookup_
std::size_t size() const
void serialize(Archive &ar, unsigned)
unsigned getIndex(const HcalDetId &id) const
const Item * getDefault() const
HcalItemCollById(const HcalItemColl< Item > &coll, const HcalIndexLookup &indexLookupTable, const unsigned detIdTransformCode, std::unique_ptr< Item > defaultItem)
void validateCode(unsigned transformCode)
double f[11][100]
Definition: value.py:1
const Item & at(const HcalDetId &id) const
std::shared_ptr< Item > default_
const Item * getByIndex(const unsigned index) const
unsigned find(unsigned detId) const
void setDefault(std::unique_ptr< Item > f)
friend class boost::serialization::access
static const unsigned InvalidIndex
~HcalItemCollById() override
unsigned transform(const HcalDetId &id, unsigned transformCode)