Back to home page

Project CMSSW displayed by LXR

 
 

    


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 // This collection allows lookup of items by HcalDetId.
0016 // If the given HcalDetId is not explicitly listed in the
0017 // lookup table, default item is returned.
0018 //
0019 // Just like HcalItemColl, this collection works with pointers
0020 // and references only, so it can be used with the inheritance
0021 // scenarios. Note that the ownership of objects is shared with
0022 // the collection provided in the constructor. The default item
0023 // is owned by this collection. Its ownership will also become
0024 // shared if a copy of this collection is made.
0025 //
0026 template <typename Item>
0027 class HcalItemCollById : public AbsHcalAlgoData {
0028 public:
0029   typedef Item value_type;
0030 
0031   // Dummy default constructor. To be used only for deserialization.
0032   inline HcalItemCollById() : transformCode_(HcalDetIdTransform::N_TRANSFORMS) {}
0033 
0034   // Normal constructor
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     // Check that the lookup table is valid for this application
0041     if (lookup_.hasDuplicateIds())
0042       throw cms::Exception(
0043           "In HcalItemCollById constructor:"
0044           " invalid lookup table");
0045 
0046     // Check that the lookup table is consistent with the size
0047     // of the collection
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   // Modifier for the default item
0060   inline void setDefault(std::unique_ptr<Item> f) { default_ = std::shared_ptr<Item>(f.release()); }
0061 
0062   // Size of the internal collection, not counting the default
0063   inline std::size_t size() const { return coll_.size(); }
0064 
0065   // The following method will return nullptr if there is no default
0066   inline const Item* getDefault() const { return default_.get(); }
0067 
0068   // Look up the index into the collection by detector id
0069   inline unsigned getIndex(const HcalDetId& id) const {
0070     return lookup_.find(HcalDetIdTransform::transform(id, transformCode_));
0071   }
0072 
0073   // Get an item by its index in the collection. If the index
0074   // is out of range, the default item is returned. If the
0075   // index is out of range and there is no default, nullptr
0076   // is returned.
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   // Convenience function for getting what we need by id.
0085   // This method can return nullptr.
0086   inline const Item* get(const HcalDetId& id) const { return getByIndex(getIndex(id)); }
0087 
0088   // The following method will throw an exception if the id is not
0089   // in the lookup table and, in addition, there is no default
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     // The default may or may not be there
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 /* version */) {
0127     ar & coll_ & lookup_ & default_ & transformCode_;
0128   }
0129 };
0130 
0131 // boost serialization version number for this template
0132 namespace boost {
0133   namespace serialization {
0134     template <typename Item>
0135     struct version<HcalItemCollById<Item> > {
0136       BOOST_STATIC_CONSTANT(int, value = 1);
0137     };
0138   }  // namespace serialization
0139 }  // namespace boost
0140 
0141 #endif  // CondFormats_HcalObjects_HcalItemCollById_h