CMS 3D CMS Logo

OrbitCollection.h
Go to the documentation of this file.
1 #ifndef DataFormats_L1Scouting_OrbitCollection_h
2 #define DataFormats_L1Scouting_OrbitCollection_h
3 
7 
8 #include <cstdint>
9 #include <vector>
10 
11 template <class T>
13 public:
14  typedef typename std::vector<T>::iterator iterator;
15  typedef typename std::vector<T>::const_iterator const_iterator;
16  typedef T value_type;
18 
19  // Initialize the offset vector with 0s from 0 to 3565.
20  // BX range is [1,3564], an extra entry is needed for the offserts of the last BX
22  // Construct the flat orbit collection starting from an OrbitBuffer.
23  // The method fillAndClear will be used, meaning that, after copying the objects,
24  // orbitBuffer's vectors will be cleared.
25  OrbitCollection(std::vector<std::vector<T>>& orbitBuffer, unsigned nObjects = 0)
26  : bxOffsets_(orbitBufferSize_ + 1, 0), data_(nObjects) {
27  fillAndClear(orbitBuffer, nObjects);
28  }
29 
30  OrbitCollection(const OrbitCollection& other) = default;
34 
35  // Fill the orbit collection starting from a vector of vectors, one per BX.
36  // Objects are copied into a flat data vector, and a second vector is used to keep track
37  // of the starting index in the data vector for every BX.
38  // After the copy, the original input buffer is cleared.
39  // Input vector must be sorted with increasing BX and contain 3565 elements (BX in [1,3564])
40  void fillAndClear(std::vector<std::vector<T>>& orbitBuffer, unsigned nObjects = 0) {
41  if (orbitBuffer.size() != orbitBufferSize_)
42  throw cms::Exception("OrbitCollection::fillAndClear")
43  << "Trying to fill the collection by passing an orbit buffer with incorrect size. "
44  << "Passed " << orbitBuffer.size() << ", expected 3565";
45  data_.reserve(nObjects);
46  bxOffsets_[0] = 0;
47  unsigned bxIdx = 1;
48  for (auto& bxVec : orbitBuffer) {
49  // increase offset by the currect vec size
50  bxOffsets_[bxIdx] = bxOffsets_[bxIdx - 1] + bxVec.size();
51 
52  // if bxVec contains something, copy it into the data_ vector
53  // and clear original bxVec objects
54  if (bxVec.size() > 0) {
55  data_.insert(data_.end(), bxVec.begin(), bxVec.end());
56  bxVec.clear();
57  }
58 
59  // increment bx index
60  bxIdx++;
61  }
62  }
63 
64  // iterate over all elements contained in data
65  const_iterator begin() const { return data_.begin(); }
66  const_iterator end() const { return data_.end(); }
67 
68  // iterate over elements of a bx
70  if (bx >= orbitBufferSize_)
71  throw cms::Exception("OrbitCollection::bxIterator") << "Trying to access and object outside the orbit range. "
72  << " BX = " << bx;
73  if (getBxSize(bx) > 0) {
74  return edm::Span(data_.begin() + bxOffsets_[bx], data_.begin() + bxOffsets_[bx + 1]);
75  } else {
76  return edm::Span(end(), end());
77  }
78  }
79 
80  // get number of objects stored in a BX
81  int getBxSize(unsigned bx) const {
82  if (bx >= orbitBufferSize_) {
83  cms::Exception("OrbitCollection") << "Called getBxSize() of a bx out of the orbit range."
84  << " BX = " << bx;
85  return 0;
86  }
87  return bxOffsets_[bx + 1] - bxOffsets_[bx];
88  }
89 
90  // get i-th object from BX
91  const T& getBxObject(unsigned bx, unsigned i) const {
92  if (bx >= orbitBufferSize_)
93  throw cms::Exception("OrbitCollection::getBxObject") << "Trying to access and object outside the orbit range. "
94  << " BX = " << bx;
95  if (i >= getBxSize(bx))
96  throw cms::Exception("OrbitCollection::getBxObject")
97  << "Trying to get element " << i << " but for"
98  << " BX = " << bx << " there are " << getBxSize(bx) << " elements.";
99 
100  return data_[bxOffsets_[bx] + i];
101  }
102 
103  // get the list of non empty BXs
104  std::vector<unsigned> getFilledBxs() const {
105  std::vector<unsigned> filledBxVec;
106  if (!data_.empty()) {
107  for (unsigned bx = 0; bx < orbitBufferSize_; bx++) {
108  if ((bxOffsets_[bx + 1] - bxOffsets_[bx]) > 0)
109  filledBxVec.push_back(bx);
110  }
111  }
112  return filledBxVec;
113  }
114 
115  int size() const { return data_.size(); }
116 
117  T& operator[](std::size_t i) { return data_[i]; }
118  const T& operator[](std::size_t i) const { return data_[i]; }
119 
120  // used by ROOT storage
122 
123 private:
124  // store data vector and BX offsets as flat vectors.
125  // offset contains one entry per BX, indicating the starting index
126  // of the objects for that BX.
127  std::vector<unsigned> bxOffsets_;
129 
130  // there are 3564 BX in one orbtit [1,3564], one extra
131  // count added to keep first entry of the vector
132  static constexpr int orbitBufferSize_ = 3565;
133 };
134 
135 #endif // DataFormats_L1Scouting_OrbitCollection_h
std::vector< unsigned > getFilledBxs() const
const_iterator begin() const
T & operator[](std::size_t i)
int getBxSize(unsigned bx) const
const_iterator end() const
void fillAndClear(std::vector< std::vector< T >> &orbitBuffer, unsigned nObjects=0)
std::vector< T >::const_iterator const_iterator
std::vector< unsigned > bxOffsets_
#define CMS_CLASS_VERSION(_version_)
const T & getBxObject(unsigned bx, unsigned i) const
uint16_t size_type
const T & operator[](std::size_t i) const
int size() const
OrbitCollection & operator=(const OrbitCollection &other)=default
edm::Span< const_iterator > bxIterator(unsigned bx) const
OrbitCollection(std::vector< std::vector< T >> &orbitBuffer, unsigned nObjects=0)
std::vector< T >::size_type size_type
std::vector< T > data_
std::vector< T >::iterator iterator
static constexpr int orbitBufferSize_
Definition: Span.h:16
long double T