Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:39:59

0001 #ifndef DataFormats_SiStripCluster_SiStripApproximateClusterCollection_h
0002 #define DataFormats_SiStripCluster_SiStripApproximateClusterCollection_h
0003 
0004 #include <vector>
0005 
0006 #include "DataFormats/SiStripCluster/interface/SiStripApproximateCluster.h"
0007 
0008 /**
0009  * This class provides a minimal interface that resembles
0010  * edmNew::DetSetVector, but is crafted such that we are comfortable
0011  * to provide an infinite backwards compatibility guarantee for it
0012  * (like all RAW data). Any modifications need to be made with care.
0013  * Please consult core software group if in doubt.
0014 **/
0015 class SiStripApproximateClusterCollection {
0016 public:
0017   // Helper classes to make creation and iteration easier
0018   class Filler {
0019   public:
0020     void push_back(SiStripApproximateCluster const& cluster) { clusters_.push_back(cluster); }
0021 
0022   private:
0023     friend SiStripApproximateClusterCollection;
0024     Filler(std::vector<SiStripApproximateCluster>& clusters) : clusters_(clusters) {}
0025 
0026     std::vector<SiStripApproximateCluster>& clusters_;
0027   };
0028 
0029   class const_iterator;
0030   class DetSet {
0031   public:
0032     using const_iterator = std::vector<SiStripApproximateCluster>::const_iterator;
0033 
0034     unsigned int id() const { return coll_->detIds_[detIndex_]; }
0035 
0036     const_iterator begin() const { return coll_->clusters_.begin() + clusBegin_; }
0037     const_iterator cbegin() const { return begin(); }
0038     const_iterator end() const { return coll_->clusters_.begin() + clusEnd_; }
0039     const_iterator cend() const { return end(); }
0040 
0041   private:
0042     friend SiStripApproximateClusterCollection::const_iterator;
0043     DetSet(SiStripApproximateClusterCollection const* coll, unsigned int detIndex)
0044         : coll_(coll),
0045           detIndex_(detIndex),
0046           clusBegin_(coll_->beginIndices_[detIndex]),
0047           clusEnd_(detIndex == coll_->beginIndices_.size() - 1 ? coll->clusters_.size()
0048                                                                : coll_->beginIndices_[detIndex + 1]) {}
0049 
0050     SiStripApproximateClusterCollection const* const coll_;
0051     unsigned int const detIndex_;
0052     unsigned int const clusBegin_;
0053     unsigned int const clusEnd_;
0054   };
0055 
0056   class const_iterator {
0057   public:
0058     DetSet operator*() const { return DetSet(coll_, index_); }
0059 
0060     const_iterator& operator++() {
0061       ++index_;
0062       if (index_ == coll_->detIds_.size()) {
0063         *this = const_iterator();
0064       }
0065       return *this;
0066     }
0067 
0068     const_iterator operator++(int) {
0069       const_iterator clone = *this;
0070       ++(*this);
0071       return clone;
0072     }
0073 
0074     bool operator==(const_iterator const& other) const { return coll_ == other.coll_ and index_ == other.index_; }
0075     bool operator!=(const_iterator const& other) const { return not operator==(other); }
0076 
0077   private:
0078     friend SiStripApproximateClusterCollection;
0079     // default-constructed object acts as the sentinel
0080     const_iterator() = default;
0081     const_iterator(SiStripApproximateClusterCollection const* coll) : coll_(coll) {}
0082 
0083     SiStripApproximateClusterCollection const* coll_ = nullptr;
0084     unsigned int index_ = 0;
0085   };
0086 
0087   // Actual public interface
0088   SiStripApproximateClusterCollection() = default;
0089 
0090   void reserve(std::size_t dets, std::size_t clusters);
0091   Filler beginDet(unsigned int detId);
0092 
0093   const_iterator begin() const { return clusters_.empty() ? end() : const_iterator(this); }
0094   const_iterator cbegin() const { return begin(); }
0095   const_iterator end() const { return const_iterator(); }
0096   const_iterator cend() const { return end(); }
0097 
0098 private:
0099   // The detIds_ and beginIndices_ have one element for each Det. An
0100   // element of beginIndices_ points to the first cluster of the Det
0101   // in clusters_.
0102   std::vector<unsigned int> detIds_;  // DetId for the Det
0103   std::vector<unsigned int> beginIndices_;
0104   std::vector<SiStripApproximateCluster> clusters_;
0105 };
0106 
0107 #endif