CMS 3D CMS Logo

HcalItemArrayCollById.h
Go to the documentation of this file.
1 #ifndef CondFormats_HcalObjects_HcalItemArrayCollById_h
2 #define CondFormats_HcalObjects_HcalItemArrayCollById_h
3 
4 #include <cstdint>
5 
7 
13 
14 //
15 // This collection allows lookup of arrays 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 HcalItemArrayColl, 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 array
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, unsigned N>
28 public:
29  typedef Item value_type;
31  static constexpr unsigned arraySize() { return N; }
32 
33  // Dummy constructor. To be used for deserialization only.
35 
36  // Normal constructor
38  const HcalIndexLookup& indexLookupTable,
39  const unsigned detIdTransformCode,
40  InputArray& defaultFunctors)
41  : coll_(coll), lookup_(indexLookupTable), transformCode_(detIdTransformCode) {
42  // Check that the lookup table is valid for this application
44  throw cms::Exception(
45  "In HcalItemArrayCollById constructor:"
46  " invalid lookup table");
47 
48  // Check that the lookup table is consistent with the size
49  // of the collection
50  const unsigned maxIndex = lookup_.largestIndex();
51  if (maxIndex != HcalIndexLookup::InvalidIndex && maxIndex >= coll_.size())
52  throw cms::Exception(
53  "In HcalItemArrayCollById constructor:"
54  " collection and lookup table are inconsistent");
55 
57 
58  // Take care of the default array
59  setDefault(defaultFunctors);
60  }
61 
62  inline virtual ~HcalItemArrayCollById() {}
63 
64  // Modifier for the default array of items
65  inline void setDefault(InputArray& arr) {
66  for (unsigned i = 0; i < N; ++i)
67  default_[i] = std::shared_ptr<Item>(arr[i].release());
68  }
69 
70  // Size of the internal collection, not counting the default
71  inline std::size_t size() const { return coll_.size(); }
72 
73  // Look up the index into the collection by detector id
74  inline unsigned getIndex(const HcalDetId& id) const {
76  }
77 
78  // Item lookup by its index and array index. If item lookup
79  // by index fails and the array index is not out of bounds,
80  // default item is returned.
81  inline const Item* getByIndex(const unsigned itemIndex, const unsigned arrayIndex) const {
82  const Item* f = coll_.get(itemIndex, arrayIndex);
83  if (f == nullptr && arrayIndex < N)
84  f = default_[arrayIndex].get();
85  return f;
86  }
87 
88  // The following method will return nullptr if
89  // there is no corresponding default
90  inline const Item* getDefault(const unsigned arrayIndex) const {
91  if (arrayIndex < N)
92  return default_[arrayIndex].get();
93  else
94  return nullptr;
95  }
96 
97  // Convenience function for getting what we need by id.
98  // Note that, if you are simply cycling over array indices,
99  // it will be more efficient to retrieve the item index
100  // first and then use "getByIndex" method.
101  inline const Item* get(const HcalDetId& id, const unsigned arrayIndex) const {
102  return getByIndex(getIndex(id), arrayIndex);
103  }
104 
105  // Similar comment applies here if you are just cycling over array indices
106  inline const Item& at(const HcalDetId& id, const unsigned arrayIndex) const {
107  const Item* f = getByIndex(getIndex(id), arrayIndex);
108  if (f == nullptr)
109  throw cms::Exception("In HcalItemArrayCollById::at: invalid detector id");
110  return *f;
111  }
112 
113 protected:
114  virtual bool isEqual(const AbsHcalAlgoData& other) const override {
115  const HcalItemArrayCollById& r = static_cast<const HcalItemArrayCollById&>(other);
116  if (coll_ != r.coll_)
117  return false;
118  if (lookup_ != r.lookup_)
119  return false;
121  return false;
122  for (unsigned j = 0; j < N; ++j) {
123  // The default may or may not be there
124  const bool ld = default_[j].get();
125  const bool rd = r.default_[j].get();
126  if (ld != rd)
127  return false;
128  if (ld)
129  if (!(*default_[j] == *r.default_[j]))
130  return false;
131  }
132  return true;
133  }
134 
135 private:
136  typedef boost::array<std::shared_ptr<Item>, N> StoredArray;
137 
140  StoredArray default_;
141  uint32_t transformCode_;
142 
144 
145  template <class Archive>
146  inline void serialize(Archive& ar, unsigned /* version */) {
147  ar& coll_& lookup_& default_& transformCode_;
148  }
149 };
150 
151 // boost serialization version number for this template
152 namespace boost {
153  namespace serialization {
154  template <typename Item, unsigned N>
155  struct version<HcalItemArrayCollById<Item, N> > {
156  BOOST_STATIC_CONSTANT(int, value = 1);
157  };
158  } // namespace serialization
159 } // namespace boost
160 
161 #endif // CondFormats_HcalObjects_HcalItemArrayCollById_h
friend class boost::serialization::access
Definition: CLHEP.h:16
virtual bool isEqual(const AbsHcalAlgoData &other) const override
HcalItemArrayColl< Item, N >::InputArray InputArray
boost::array< std::shared_ptr< Item >, N > StoredArray
std::size_t size() const
unsigned largestIndex() const
void validateCode(unsigned transformCode)
double f[11][100]
Definition: value.py:1
HcalItemArrayColl< Item, N > coll_
JetCorrectorParametersCollection coll
Definition: classes.h:10
#define N
Definition: blowfish.cc:9
unsigned getIndex(const HcalDetId &id) const
unsigned find(unsigned detId) const
void setDefault(InputArray &arr)
static const unsigned InvalidIndex
std::array< std::unique_ptr< Item >, N > InputArray
HcalItemArrayCollById(const HcalItemArrayColl< Item, N > &coll, const HcalIndexLookup &indexLookupTable, const unsigned detIdTransformCode, InputArray &defaultFunctors)
void serialize(Archive &ar, unsigned)
static unsigned arraySize()
#define constexpr
const Item * getDefault(const unsigned arrayIndex) const
const Item * getByIndex(const unsigned itemIndex, const unsigned arrayIndex) const
const Item & at(const HcalDetId &id, const unsigned arrayIndex) const
unsigned transform(const HcalDetId &id, unsigned transformCode)