CMS 3D CMS Logo

PHGCSimAccumulator.h
Go to the documentation of this file.
1 #ifndef DataFormats_HGCDigi_PHGCSimAccumulator_h
2 #define DataFormats_HGCDigi_PHGCSimAccumulator_h
3 
5 
6 #include <vector>
7 #include <cassert>
8 
10 public:
11  // These two structs are public only because of dictionary generation
12  class DetIdSize {
13  public:
14  // Use top 27 bits to hold the details of the DetId
15  constexpr static unsigned detIdOffset = 5;
16  constexpr static unsigned detIdMask = 0x7ffffff;
17  // Use the last 5 bits to index 2x15 elements
18  constexpr static unsigned sizeOffset = 27;
19  constexpr static unsigned sizeMask = 0x1f;
20  // With this arrangement increasing the size component is faster
21 
23  DetIdSize(unsigned int detId): detIdSize_(detId << detIdOffset) {}
24 
25  void increaseSize() {
26  assert(size()+1 < 1<<detIdOffset);
27  ++detIdSize_;
28  }
29 
30  unsigned int detIdDetails() const { return detIdSize_ >> detIdOffset; }
31  unsigned int size() const { return detIdSize_ & sizeMask; }
32 
33  private:
34  unsigned int detIdSize_;
35  };
36  class Data {
37  public:
38  constexpr static unsigned energyOffset = 15;
39  constexpr static unsigned energyMask = 0x1;
40  constexpr static unsigned sampleOffset = 11;
41  constexpr static unsigned sampleMask = 0xf;
42  constexpr static unsigned dataOffset = 0;
43  constexpr static unsigned dataMask = 0x7ff;
44 
45  Data(): data_(0) {}
46  Data(unsigned short ei, unsigned short si, unsigned short d):
47  data_((ei << energyOffset) | (si << sampleOffset) | d)
48  {}
49 
50  unsigned int energyIndex() const { return data_ >> energyOffset; }
51  unsigned int sampleIndex() const { return (data_ >> sampleOffset) & sampleMask; }
52  unsigned int data() const { return data_ & dataMask; }
53 
54  private:
55  unsigned short data_;
56  };
57 
58  PHGCSimAccumulator() = default;
59  PHGCSimAccumulator(unsigned int detId): detSubdetId_(detId >> DetId::kSubdetOffset) {}
60  ~PHGCSimAccumulator() = default;
61 
62  void reserve(size_t size) {
63  detIdSize_.reserve(size);
64  data_.reserve(size);
65  }
66 
67  void shrink_to_fit() {
68  detIdSize_.shrink_to_fit();
69  data_.shrink_to_fit();
70  }
71 
79  void emplace_back(unsigned int detId, unsigned short energyIndex, unsigned short sampleIndex, unsigned short data) {
80  assert( (detId >> DetId::kSubdetOffset) == detSubdetId_ );
81 
82  if(detIdSize_.empty() || detIdSize_.back().detIdDetails() != (detId & DetIdSize::detIdMask)) {
83  detIdSize_.emplace_back(detId);
84  }
85  data_.emplace_back(energyIndex, sampleIndex, data);
86  detIdSize_.back().increaseSize();
87  }
88 
89  class TmpElem {
90  public:
91  TmpElem(unsigned int detId, Data data): detId_(detId), data_(data) {}
92 
93  unsigned int detId() const { return detId_; }
94  unsigned short energyIndex() const { return data_.energyIndex(); }
95  unsigned short sampleIndex() const { return data_.sampleIndex(); }
96  unsigned short data() const { return data_.data(); }
97  private:
98  unsigned int detId_;
100  };
101 
103  public:
104  // begin
106  acc_(acc), iDet_(0), iData_(0),
107  endData_(acc->detIdSize_.empty() ? 0 : acc->detIdSize_.front().size())
108  {}
109 
110  // end
111  const_iterator(const PHGCSimAccumulator *acc, unsigned int detSize, unsigned int dataSize):
112  acc_(acc), iDet_(detSize), iData_(dataSize), endData_(0)
113  {}
114 
115  bool operator==(const const_iterator& other) const {
116  return iDet_ == other.iDet_ && iData_ == other.iData_;
117  }
118  bool operator!=(const const_iterator& other) const {
119  return !operator==(other);
120  }
122  ++iData_;
123  if(iData_ == endData_) {
124  ++iDet_;
125  endData_ += (iDet_ == acc_->detIdSize_.size()) ? 0 : acc_->detIdSize_[iDet_].size();
126  }
127  return *this;
128  }
130  auto tmp = *this;
131  ++(*this);
132  return tmp;
133  }
135  return TmpElem((acc_->detSubdetId_ << DetId::kSubdetOffset) | acc_->detIdSize_[iDet_].detIdDetails(),
136  acc_->data_[iData_]);
137  }
138 
139  private:
141  unsigned int iDet_;
142  unsigned int iData_;
143  unsigned int endData_;
144  };
145 
146  TmpElem back() const {
147  return TmpElem((detSubdetId_ << DetId::kSubdetOffset) | detIdSize_.back().detIdDetails(),
148  data_.back());
149  }
150 
151  const_iterator cbegin() const { return const_iterator(this); }
152  const_iterator begin() const { return cbegin(); }
153  const_iterator cend() const { return const_iterator(this, detIdSize_.size(), data_.size()); }
154  const_iterator end() const { return cend(); }
155 
156 private:
157  std::vector<DetIdSize> detIdSize_;
158  std::vector<Data> data_;
159  unsigned short detSubdetId_ = 0;
160 };
161 
162 #endif
unsigned short data() const
unsigned int data() const
const PHGCSimAccumulator * acc_
void emplace_back(unsigned int detId, unsigned short energyIndex, unsigned short sampleIndex, unsigned short data)
std::vector< Data > data_
static constexpr unsigned detIdOffset
PHGCSimAccumulator(unsigned int detId)
static const int kSubdetOffset
Definition: DetId.h:23
bool operator!=(const const_iterator &other) const
#define constexpr
static constexpr unsigned detIdMask
const_iterator cend() const
const_iterator begin() const
unsigned int energyIndex() const
unsigned int detIdDetails() const
TmpElem(unsigned int detId, Data data)
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
~PHGCSimAccumulator()=default
PHGCSimAccumulator()=default
unsigned short detSubdetId_
unsigned int sampleIndex() const
unsigned short sampleIndex() const
const_iterator(const PHGCSimAccumulator *acc)
Definition: DetId.h:18
void reserve(size_t size)
unsigned short energyIndex() const
std::vector< DetIdSize > detIdSize_
static constexpr unsigned sizeMask
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
TmpElem back() const
const_iterator cbegin() const
bool operator==(const const_iterator &other) const
unsigned int detId() const
const_iterator(const PHGCSimAccumulator *acc, unsigned int detSize, unsigned int dataSize)
Data(unsigned short ei, unsigned short si, unsigned short d)
const_iterator end() const
static constexpr unsigned sizeOffset