Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-04 04:04:14

0001 #ifndef CondFormats_HGCalObjects_interface_HGCalDenseIndexerBase_h
0002 #define CondFormats_HGCalObjects_interface_HGCalDenseIndexerBase_h
0003 
0004 #include "CondFormats/Serialization/interface/Serializable.h"
0005 #include "FWCore/Utilities/interface/Exception.h"
0006 #include <array>
0007 #include <numeric>
0008 
0009 /**
0010    @short this is a simple class that takes care of building a dense index for a set of categories
0011    the maximum number of items expected in each category is encoded in the IndexRanges_t
0012    the class is templated for the number of categories to use
0013  */
0014 class HGCalDenseIndexerBase {
0015 public:
0016   HGCalDenseIndexerBase() : HGCalDenseIndexerBase(0) {}
0017 
0018   HGCalDenseIndexerBase(int n) : n_(n), maxIdx_(0), vmax_(n, 0) {}
0019 
0020   HGCalDenseIndexerBase(std::vector<uint32_t> const &o) : n_(o.size()) { updateRanges(o); }
0021 
0022   void updateRanges(std::vector<uint32_t> const &o) {
0023     check(o.size());
0024     vmax_ = o;
0025     maxIdx_ = std::accumulate(vmax_.begin(), vmax_.end(), 1, std::multiplies<uint32_t>());
0026   }
0027 
0028   uint32_t denseIndex(std::vector<uint32_t> v) const {
0029     uint32_t rtn = v[0];
0030     for (size_t i = 1; i < n_; i++)
0031       rtn = rtn * vmax_[i] + v[i];
0032     return rtn;
0033   }
0034 
0035   std::vector<uint32_t> unpackDenseIndex(uint32_t rtn) const {
0036     std::vector<uint32_t> codes(n_, 0);
0037 
0038     const auto rend = vmax_.rend();
0039     for (auto rit = vmax_.rbegin(); rit != rend; ++rit) {
0040       size_t i = rend - rit - 1;
0041       codes[i] = rtn % (*rit);
0042       rtn = rtn / (*rit);
0043     }
0044 
0045     return codes;
0046   }
0047 
0048   uint32_t getMaxIndex() const { return maxIdx_; }
0049 
0050   ~HGCalDenseIndexerBase() = default;
0051 
0052 private:
0053   void check(size_t osize) const {
0054     if (osize != n_)
0055       throw cms::Exception("ValueError") << " unable to update indexer max values. Expected " << n_ << " received "
0056                                          << osize;
0057   }
0058 
0059   uint32_t n_;
0060   uint32_t maxIdx_;
0061   std::vector<uint32_t> vmax_;
0062 
0063   COND_SERIALIZABLE;
0064 };
0065 
0066 #endif