Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-04 22:27:33

0001 #ifndef DataFormats_HGCalDigis_PHGCSimAccumulator_h
0002 #define DataFormats_HGCalDigis_PHGCSimAccumulator_h
0003 
0004 #include "DataFormats/DetId/interface/DetId.h"
0005 #include <iostream>
0006 #include <vector>
0007 
0008 class PHGCSimAccumulator {
0009 public:
0010   class DetIdSize {
0011   public:
0012     DetIdSize() {}
0013     DetIdSize(unsigned int detId) : detId_(detId) {}
0014     void increaseSize() { ++size_; }
0015     unsigned int detId() const { return detId_; }
0016     unsigned int size() const { return size_; }
0017 
0018   private:
0019     unsigned int detId_ = 0;
0020     unsigned char size_ = 0;
0021   };
0022 
0023   class SimHitCollection {
0024   public:
0025     constexpr static unsigned energyOffset = 15;
0026     constexpr static unsigned energyMask = 0x1;
0027     constexpr static unsigned sampleOffset = 11;
0028     constexpr static unsigned sampleMask = 0xf;
0029     constexpr static unsigned dataOffset = 0;
0030     constexpr static unsigned dataMask = 0x7ff;
0031 
0032     SimHitCollection() {}
0033     SimHitCollection(unsigned int nhits) : nhits_(nhits) {}
0034     SimHitCollection(const unsigned short si,
0035                      const std::vector<unsigned short>& accCharge,
0036                      const std::vector<unsigned short>& time)
0037         : nhits_(accCharge.size()) {
0038       chargeArray_.reserve(nhits_);
0039       timeArray_.reserve(nhits_);
0040       for (size_t i = 0; i < nhits_; ++i) {
0041         unsigned short ei = 0;
0042         unsigned short d = accCharge[i];
0043         unsigned short data = ((ei << energyOffset) | (si << sampleOffset) | d);
0044         chargeArray_.emplace_back(data);
0045       }
0046       for (size_t i = 0; i < nhits_; ++i) {
0047         unsigned short ei = 1;
0048         unsigned short d = time[i];
0049         unsigned short data = ((ei << energyOffset) | (si << sampleOffset) | d);
0050         timeArray_.emplace_back(data);
0051       }
0052     }
0053     SimHitCollection(const SimHitCollection& simhitcollection) = default;
0054     unsigned int nhits() const { return nhits_; }
0055     unsigned int sampleIndex() const { return (chargeArray_[0] >> sampleOffset) & sampleMask; }
0056     const std::vector<unsigned short>& chargeArray() const { return chargeArray_; }
0057     const std::vector<unsigned short>& timeArray() const { return timeArray_; }
0058 
0059   private:
0060     unsigned int nhits_;
0061     std::vector<unsigned short> chargeArray_;
0062     std::vector<unsigned short> timeArray_;
0063   };
0064 
0065   PHGCSimAccumulator() = default;
0066   ~PHGCSimAccumulator() = default;
0067 
0068   void reserve(size_t size) {
0069     detIdSize_.reserve(size);
0070     simhitCollection_.reserve(size);
0071   }
0072   void shrink_to_fit() {
0073     detIdSize_.shrink_to_fit();
0074     simhitCollection_.shrink_to_fit();
0075   }
0076 
0077   void emplace_back(unsigned int detId,
0078                     unsigned short sampleIndex,
0079                     const std::vector<unsigned short>& accCharge,
0080                     const std::vector<unsigned short>& timing) {
0081     if (detIdSize_.empty() || detIdSize_.back().detId() != detId) {
0082       detIdSize_.emplace_back(detId);
0083     }
0084     simhitCollection_.emplace_back(sampleIndex, accCharge, timing);
0085     detIdSize_.back().increaseSize();
0086   }
0087 
0088   class TmpElem {
0089   public:
0090     TmpElem(const unsigned int detId, const SimHitCollection& simhitCollection)
0091         : detId_(detId), simhitcollection_(simhitCollection) {}
0092 
0093     unsigned int detId() const { return detId_; }
0094     unsigned short sampleIndex() const { return simhitcollection_.sampleIndex(); }
0095     unsigned int nhits() const { return simhitcollection_.nhits(); }
0096     const std::vector<unsigned short> chargeArray() const { return simhitcollection_.chargeArray(); }
0097     const std::vector<unsigned short> timeArray() const { return simhitcollection_.timeArray(); }
0098 
0099   private:
0100     unsigned int detId_;
0101     SimHitCollection simhitcollection_;
0102   };
0103 
0104   class const_iterator {
0105   public:
0106     // begin
0107     const_iterator(const PHGCSimAccumulator* ncc)
0108         : ncc_(ncc), iDet_(0), iData_(0), endData_(ncc->detIdSize_.empty() ? 0 : ncc->detIdSize_.front().size()) {}
0109     // end
0110     const_iterator(const PHGCSimAccumulator* ncc, unsigned int detSize, unsigned int dataSize)
0111         : ncc_(ncc), iDet_(detSize), iData_(dataSize), endData_(0) {}
0112 
0113     bool operator==(const const_iterator& other) const { return iDet_ == other.iDet_ && iData_ == other.iData_; }
0114     bool operator!=(const const_iterator& other) const { return !operator==(other); }
0115     const_iterator& operator++() {
0116       ++iData_;
0117       if (iData_ == endData_) {
0118         ++iDet_;
0119         endData_ += (iDet_ == ncc_->detIdSize_.size()) ? 0 : ncc_->detIdSize_[iDet_].size();
0120       }
0121       return *this;
0122     }
0123     const_iterator operator++(int) {
0124       auto tmp = *this;
0125       ++(*this);
0126       return tmp;
0127     }
0128 
0129     TmpElem operator*() { return TmpElem(ncc_->detIdSize_[iDet_].detId(), ncc_->simhitCollection_[iData_]); }
0130 
0131   private:
0132     const PHGCSimAccumulator* ncc_;
0133     unsigned int iDet_;
0134     unsigned int iData_;
0135     unsigned int endData_;
0136   };
0137 
0138   TmpElem back() const { return TmpElem(detIdSize_.back().detId(), simhitCollection_.back()); }
0139 
0140   const_iterator cbegin() const { return const_iterator(this); }
0141   const_iterator begin() const { return cbegin(); }
0142   const_iterator cend() const { return const_iterator(this, detIdSize_.size(), simhitCollection_.size()); }
0143   const_iterator end() const { return cend(); }
0144 
0145 private:
0146   std::vector<DetIdSize> detIdSize_;
0147   std::vector<SimHitCollection> simhitCollection_;
0148 };
0149 
0150 #endif