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 {
29 public:
30  typedef Item value_type;
31 
32  // Dummy default constructor. To be used only for deserialization.
35 
36  // Normal constructor
38  const HcalIndexLookup& indexLookupTable,
39  const unsigned detIdTransformCode,
40  std::unique_ptr<Item> defaultItem)
41  : coll_(coll),
42  lookup_(indexLookupTable),
43  default_(defaultItem.release()),
44  transformCode_(detIdTransformCode)
45  {
46  // Check that the lookup table is valid for this application
48  throw cms::Exception("In HcalItemCollById constructor:"
49  " invalid lookup table");
50 
51  // Check that the lookup table is consistent with the size
52  // of the collection
53  const unsigned maxIndex = lookup_.largestIndex();
54  if (maxIndex != HcalIndexLookup::InvalidIndex &&
55  maxIndex >= coll_.size())
56  throw cms::Exception("In HcalItemCollById constructor:"
57  " collection and lookup table are inconsistent");
58 
60  }
61 
62  inline ~HcalItemCollById() override {}
63 
64  // Modifier for the default item
65  inline void setDefault(std::unique_ptr<Item> f)
66  {default_ = boost::shared_ptr<Item>(f.release());}
67 
68  // Size of the internal collection, not counting the default
69  inline std::size_t size() const {return coll_.size();}
70 
71  // The following method will return nullptr if there is no default
72  inline const Item* getDefault() const {return default_.get();}
73 
74  // Look up the index into the collection by detector id
75  inline unsigned getIndex(const HcalDetId& id) const
77 
78  // Get an item by its index in the collection. If the index
79  // is out of range, the default item is returned. If the
80  // index is out of range and there is no default, nullptr
81  // is returned.
82  inline const Item* getByIndex(const unsigned index) const
83  {
84  if (index < coll_.size()) return coll_.get(index);
85  else return default_.get();
86  }
87 
88  // Convenience function for getting what we need by id.
89  // This method can return nullptr.
90  inline const Item* get(const HcalDetId& id) const
91  {return getByIndex(getIndex(id));}
92 
93  // The following method will throw an exception if the id is not
94  // in the lookup table and, in addition, there is no default
95  inline const Item& at(const HcalDetId& id) const
96  {
97  const Item* ptr = getByIndex(getIndex(id));
98  if (ptr == nullptr) throw cms::Exception(
99  "In HcalItemCollById::at: invalid detector id");
100  return *ptr;
101  }
102 
103 protected:
104  bool isEqual(const AbsHcalAlgoData& other) const override
105  {
106  const HcalItemCollById& r =
107  static_cast<const HcalItemCollById&>(other);
108  if (coll_ != r.coll_)
109  return false;
110  if (lookup_ != r.lookup_)
111  return false;
113  return false;
114  // The default may or may not be there
115  const bool ld = default_.get();
116  const bool rd = r.default_.get();
117  if (ld != rd)
118  return false;
119  if (ld)
120  if (!(*default_ == *r.default_))
121  return false;
122  return true;
123  }
124 
125 private:
128  boost::shared_ptr<Item> default_;
129  uint32_t transformCode_;
130 
131  friend class boost::serialization::access;
132 
133  template<class Archive>
134  inline void serialize(Archive & ar, unsigned /* version */)
135  {
136  ar & coll_ & lookup_ & default_ & transformCode_;
137  }
138 };
139 
140 // boost serialization version number for this template
141 namespace boost {
142  namespace serialization {
143  template<typename Item>
144  struct version<HcalItemCollById<Item> >
145  {
146  BOOST_STATIC_CONSTANT(int, value = 1);
147  };
148  }
149 }
150 
151 #endif // CondFormats_HcalObjects_HcalItemCollById_h
unsigned getIndex(const HcalDetId &id) const
Definition: CLHEP.h:16
HcalItemColl< Item > coll_
std::size_t size() const
HcalIndexLookup lookup_
void serialize(Archive &ar, unsigned)
boost::shared_ptr< Item > default_
unsigned largestIndex() const
const Item * getByIndex(const unsigned index) const
const Item * getDefault() const
const Item & at(const HcalDetId &id) 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
bool isEqual(const AbsHcalAlgoData &other) const override
JetCorrectorParametersCollection coll
Definition: classes.h:10
unsigned find(unsigned detId) const
void setDefault(std::unique_ptr< Item > f)
static const unsigned InvalidIndex
~HcalItemCollById() override
unsigned transform(const HcalDetId &id, unsigned transformCode)