File indexing completed on 2024-04-09 02:22:07
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 unsigned int nhits() const { return nhits_; }
0054 unsigned int sampleIndex() const { return (chargeArray_[0] >> sampleOffset) & sampleMask; }
0055 const std::vector<unsigned short>& chargeArray() const { return chargeArray_; }
0056 const std::vector<unsigned short>& timeArray() const { return timeArray_; }
0057
0058 private:
0059 unsigned int nhits_;
0060 std::vector<unsigned short> chargeArray_;
0061 std::vector<unsigned short> timeArray_;
0062 };
0063
0064 PHGCSimAccumulator() = default;
0065 ~PHGCSimAccumulator() = default;
0066
0067 void reserve(size_t size) {
0068 detIdSize_.reserve(size);
0069 simhitCollection_.reserve(size);
0070 }
0071 void shrink_to_fit() {
0072 detIdSize_.shrink_to_fit();
0073 simhitCollection_.shrink_to_fit();
0074 }
0075
0076 void emplace_back(unsigned int detId,
0077 unsigned short sampleIndex,
0078 const std::vector<unsigned short>& accCharge,
0079 const std::vector<unsigned short>& timing) {
0080 if (detIdSize_.empty() || detIdSize_.back().detId() != detId) {
0081 detIdSize_.emplace_back(detId);
0082 }
0083 simhitCollection_.emplace_back(sampleIndex, accCharge, timing);
0084 detIdSize_.back().increaseSize();
0085 }
0086
0087 class TmpElem {
0088 public:
0089 TmpElem(const unsigned int detId, const SimHitCollection& simhitCollection)
0090 : detId_(detId), simhitcollection_(simhitCollection) {}
0091
0092 unsigned int detId() const { return detId_; }
0093 unsigned short sampleIndex() const { return simhitcollection_.sampleIndex(); }
0094 unsigned int nhits() const { return simhitcollection_.nhits(); }
0095 const std::vector<unsigned short> chargeArray() const { return simhitcollection_.chargeArray(); }
0096 const std::vector<unsigned short> timeArray() const { return simhitcollection_.timeArray(); }
0097
0098 private:
0099 unsigned int detId_;
0100 SimHitCollection simhitcollection_;
0101 };
0102
0103 class const_iterator {
0104 public:
0105
0106 const_iterator(const PHGCSimAccumulator* ncc)
0107 : ncc_(ncc), iDet_(0), iData_(0), endData_(ncc->detIdSize_.empty() ? 0 : ncc->detIdSize_.front().size()) {}
0108
0109 const_iterator(const PHGCSimAccumulator* ncc, unsigned int detSize, unsigned int dataSize)
0110 : ncc_(ncc), iDet_(detSize), iData_(dataSize), endData_(0) {}
0111
0112 bool operator==(const const_iterator& other) const { return iDet_ == other.iDet_ && iData_ == other.iData_; }
0113 bool operator!=(const const_iterator& other) const { return !operator==(other); }
0114 const_iterator& operator++() {
0115 ++iData_;
0116 if (iData_ == endData_) {
0117 ++iDet_;
0118 endData_ += (iDet_ == ncc_->detIdSize_.size()) ? 0 : ncc_->detIdSize_[iDet_].size();
0119 }
0120 return *this;
0121 }
0122 const_iterator operator++(int) {
0123 auto tmp = *this;
0124 ++(*this);
0125 return tmp;
0126 }
0127
0128 TmpElem operator*() { return TmpElem(ncc_->detIdSize_[iDet_].detId(), ncc_->simhitCollection_[iData_]); }
0129
0130 private:
0131 const PHGCSimAccumulator* ncc_;
0132 unsigned int iDet_;
0133 unsigned int iData_;
0134 unsigned int endData_;
0135 };
0136
0137 TmpElem back() const { return TmpElem(detIdSize_.back().detId(), simhitCollection_.back()); }
0138
0139 const_iterator cbegin() const { return const_iterator(this); }
0140 const_iterator begin() const { return cbegin(); }
0141 const_iterator cend() const { return const_iterator(this, detIdSize_.size(), simhitCollection_.size()); }
0142 const_iterator end() const { return cend(); }
0143
0144 private:
0145 std::vector<DetIdSize> detIdSize_;
0146 std::vector<SimHitCollection> simhitCollection_;
0147 };
0148
0149 #endif