File indexing completed on 2024-09-07 04:35:39
0001 #ifndef CondFormats_HcalObjects_HcalItemCollById_h
0002 #define CondFormats_HcalObjects_HcalItemCollById_h
0003
0004 #include <cstdint>
0005
0006 #include "FWCore/Utilities/interface/Exception.h"
0007
0008 #include "CondFormats/HcalObjects/interface/HcalItemColl.h"
0009 #include "CondFormats/HcalObjects/interface/HcalIndexLookup.h"
0010 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0011 #include "CondFormats/HcalObjects/interface/HcalDetIdTransform.h"
0012 #include "CondFormats/HcalObjects/interface/AbsHcalAlgoData.h"
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 template <typename Item>
0027 class HcalItemCollById : public AbsHcalAlgoData {
0028 public:
0029 typedef Item value_type;
0030
0031
0032 inline HcalItemCollById() : transformCode_(HcalDetIdTransform::N_TRANSFORMS) {}
0033
0034
0035 HcalItemCollById(const HcalItemColl<Item>& coll,
0036 const HcalIndexLookup& indexLookupTable,
0037 const unsigned detIdTransformCode,
0038 std::unique_ptr<Item> defaultItem)
0039 : coll_(coll), lookup_(indexLookupTable), default_(defaultItem.release()), transformCode_(detIdTransformCode) {
0040
0041 if (lookup_.hasDuplicateIds())
0042 throw cms::Exception(
0043 "In HcalItemCollById constructor:"
0044 " invalid lookup table");
0045
0046
0047
0048 const unsigned maxIndex = lookup_.largestIndex();
0049 if (maxIndex != HcalIndexLookup::InvalidIndex && maxIndex >= coll_.size())
0050 throw cms::Exception(
0051 "In HcalItemCollById constructor:"
0052 " collection and lookup table are inconsistent");
0053
0054 HcalDetIdTransform::validateCode(transformCode_);
0055 }
0056
0057 inline ~HcalItemCollById() override {}
0058
0059
0060 inline void setDefault(std::unique_ptr<Item> f) { default_ = std::shared_ptr<Item>(f.release()); }
0061
0062
0063 inline std::size_t size() const { return coll_.size(); }
0064
0065
0066 inline const Item* getDefault() const { return default_.get(); }
0067
0068
0069 inline unsigned getIndex(const HcalDetId& id) const {
0070 return lookup_.find(HcalDetIdTransform::transform(id, transformCode_));
0071 }
0072
0073
0074
0075
0076
0077 inline const Item* getByIndex(const unsigned index) const {
0078 if (index < coll_.size())
0079 return coll_.get(index);
0080 else
0081 return default_.get();
0082 }
0083
0084
0085
0086 inline const Item* get(const HcalDetId& id) const { return getByIndex(getIndex(id)); }
0087
0088
0089
0090 inline const Item& at(const HcalDetId& id) const {
0091 const Item* ptr = getByIndex(getIndex(id));
0092 if (ptr == nullptr)
0093 throw cms::Exception("In HcalItemCollById::at: invalid detector id");
0094 return *ptr;
0095 }
0096
0097 protected:
0098 bool isEqual(const AbsHcalAlgoData& other) const override {
0099 const HcalItemCollById& r = static_cast<const HcalItemCollById&>(other);
0100 if (coll_ != r.coll_)
0101 return false;
0102 if (lookup_ != r.lookup_)
0103 return false;
0104 if (transformCode_ != r.transformCode_)
0105 return false;
0106
0107 const bool ld = default_.get();
0108 const bool rd = r.default_.get();
0109 if (ld != rd)
0110 return false;
0111 if (ld)
0112 if (!(*default_ == *r.default_))
0113 return false;
0114 return true;
0115 }
0116
0117 private:
0118 HcalItemColl<Item> coll_;
0119 HcalIndexLookup lookup_;
0120 std::shared_ptr<Item> default_;
0121 uint32_t transformCode_;
0122
0123 friend class boost::serialization::access;
0124
0125 template <class Archive>
0126 inline void serialize(Archive& ar, unsigned ) {
0127 ar & coll_ & lookup_ & default_ & transformCode_;
0128 }
0129 };
0130
0131
0132 namespace boost {
0133 namespace serialization {
0134 template <typename Item>
0135 struct version<HcalItemCollById<Item> > {
0136 BOOST_STATIC_CONSTANT(int, value = 1);
0137 };
0138 }
0139 }
0140
0141 #endif