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