ATLAS Offline Software
FPGATrackSimGenScanArray.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 #ifndef FPGATrackSimGenScanArray_H
3 #define FPGATrackSimGenScanArray_H
4 
32 #include <vector>
33 #include <utility>
34 
35 template <typename T>
37 {
38 public:
39  // constructors
41  FPGATrackSimGenScanArray(const std::vector<unsigned int> &dims, const T &initval)
42  {
43  setsize(dims, initval);
44  }
45 
46  // resize the array (need to provide reference to initialize elements to)
47  void setsize(const std::vector<unsigned int> &dims, const T &initval)
48  {
49  m_dims = dims;
50 
51  // Caclulate the number of entries and a "step"-size needed for translating
52  // N-dim indices into a 1-d index
53  m_entries = 1;
54  for (auto &dim : dims)
55  {
56  m_step.push_back(m_entries);
57  m_entries *= dim;
58  }
59 
60  // Allocate 1-d array
61  m_data.resize(m_entries, initval);
62  }
63 
64  // full size of the array (i.e. product of the size in each dimension)
65  unsigned int size() const { return m_entries; }
66 
67  // access to internal 1-d std::vector as a const so that you can
68  // use various std::count and std::for_each on the elements
69  // note you can change the vector because it's const, but you can
70  // change the elements
71  const std::vector<T>& flatdata() const {return m_data;}
72 
73  // get the array with the size in each dimension of the
74  const std::vector<unsigned int> &dims() const { return m_dims; }
75 
76  // look up content by idx specified as a std::vector<unsigned int>
77  const T &operator[](const std::vector<unsigned int> &idx) const
78  {
79  if(idx.size() != m_step.size()) {
80  throw std::runtime_error("FPGATrackSimGenScanArray: index size does not match array dimensions");
81  }
82 
83  // Translate N-dim index to 1-d index
84  unsigned int offset = 0;
85  for (unsigned int i = 0; i < m_step.size(); i++)
86  {
87  if (idx.at(i) >= m_dims.at(i))
88  {
89  throw std::runtime_error("FPGATrackSimGenScanArray: index out of range i=" + std::to_string(i) +
90  " idx[i]=" + std::to_string(idx.at(i)) +
91  " dims[i]=" + std::to_string(m_dims.at(i)) + "\n");
92  }
93  offset += idx.at(i) * m_step.at(i);
94  }
95 
96  // Return data from 1-d vector
97  return m_data.at(offset);
98  }
99 
100  // convient to also be able to use a signed integer vector
101  const T &operator[](const std::vector<int> &idx) const
102  {
103  std::vector<unsigned int> idx_unsigned;
104  for (auto &d : idx)
105  idx_unsigned.push_back(d);
106  return (*this)[idx_unsigned];
107  }
108 
109  // make non-const version of the operator[]
110  // just call the const version and cast away the constness
111  // of the return value
112  T& operator[](const std::vector<int> &idx) {
113  T& retv ATLAS_THREAD_SAFE = const_cast<T&>(std::as_const(*this)[idx]);
114  return retv;
115  }
116  T& operator[](const std::vector<unsigned> &idx) {
117  T &retv ATLAS_THREAD_SAFE = const_cast<T &>(std::as_const(*this)[idx]);
118  return retv;
119  }
120 
121  // Iterator implemenation
122  struct Iterator
123  {
125  using iterator_category = std::forward_iterator_tag;
127  using difference_type = void;
130 
131  // Construtctor
132  Iterator(const std::vector<unsigned int> &idx,
134  : m_idx(idx), m_itrdata(itrdata) {
135  if (m_idx.size() != m_itrdata.dims().size()) {
136  throw std::runtime_error(
137  "FPGATrackSimGenScanArray::Interator array size mismatch in "
138  "constructor");
139  }
140  }
141 
142  Iterator &operator*() { return *this; }
143  Iterator *operator->() { return this; }
144 
145  // Prefix increment
147  {
148  ++m_idx.at(0);
149  for (unsigned int i = 0; i < m_idx.size() - 1; i++)
150  {
151  if (m_idx.at(i) >= m_itrdata.dims().at(i))
152  {
153  ++m_idx.at(i + 1);
154  m_idx.at(i) = 0;
155  }
156  }
157  return *this;
158  }
159 
160  const std::vector<unsigned int> &idx() const { return m_idx; }
161  T &data() { return m_itrdata[m_idx]; }
162 
163  // Postfix increment
165  {
166  Iterator tmp = *this;
167  ++(*this);
168  return tmp;
169  }
170 
171  friend bool operator==(const Iterator &a, const Iterator &b) { return a.m_idx == b.m_idx; };
172  friend bool operator!=(const Iterator &a, const Iterator &b)
173  {
174  return a.m_idx != b.m_idx;
175  };
176 
177  private:
178  // current state of the iterator
179  std::vector<unsigned int> m_idx;
180 
181  // reference back to the array being iterated over
183  };
184 
185  // ussual std iterator meanings of begin, end, and size
186  Iterator begin() { return Iterator(std::vector<unsigned int>(m_dims.size(), 0), *this); }
188  {
189  std::vector<unsigned int> retv;
190  for (auto &d : m_dims)
191  retv.push_back(d - 1);
192  return ++Iterator(retv, *this);
193  }
194 
195 private:
196  unsigned int m_entries{};
197  std::vector<unsigned int> m_dims{};
198  std::vector<unsigned int> m_step{};
199  std::vector<T> m_data{};
200 };
201 
202 #endif // FPGATrackSimGenScanArray_H
FPGATrackSimGenScanArray::dims
const std::vector< unsigned int > & dims() const
Definition: FPGATrackSimGenScanArray.h:74
FPGATrackSimGenScanArray
Definition: FPGATrackSimGenScanArray.h:37
FPGATrackSimGenScanArray::m_step
std::vector< unsigned int > m_step
Definition: FPGATrackSimGenScanArray.h:198
yodamerge_tmp.dim
dim
Definition: yodamerge_tmp.py:239
FPGATrackSimGenScanArray::Iterator::difference_type
void difference_type
Definition: FPGATrackSimGenScanArray.h:127
FPGATrackSimGenScanArray::Iterator::operator->
Iterator * operator->()
Definition: FPGATrackSimGenScanArray.h:143
FPGATrackSimGenScanArray::Iterator::Iterator
Iterator(const std::vector< unsigned int > &idx, FPGATrackSimGenScanArray< T > &itrdata)
Definition: FPGATrackSimGenScanArray.h:132
hist_file_dump.d
d
Definition: hist_file_dump.py:137
FPGATrackSimGenScanArray::Iterator::m_idx
std::vector< unsigned int > m_idx
Definition: FPGATrackSimGenScanArray.h:175
FPGATrackSimGenScanArray::size
unsigned int size() const
Definition: FPGATrackSimGenScanArray.h:65
FPGATrackSimGenScanArray::operator[]
const T & operator[](const std::vector< int > &idx) const
Definition: FPGATrackSimGenScanArray.h:101
FPGATrackSimGenScanArray::Iterator::operator*
Iterator & operator*()
Definition: FPGATrackSimGenScanArray.h:142
FPGATrackSimGenScanArray::Iterator::idx
const std::vector< unsigned int > & idx() const
Definition: FPGATrackSimGenScanArray.h:160
FPGATrackSimGenScanArray::Iterator::data
T & data()
Definition: FPGATrackSimGenScanArray.h:161
FPGATrackSimGenScanArray::Iterator::operator++
Iterator operator++(int)
Definition: FPGATrackSimGenScanArray.h:164
FPGATrackSimGenScanArray::m_entries
unsigned int m_entries
Definition: FPGATrackSimGenScanArray.h:196
lumiFormat.i
int i
Definition: lumiFormat.py:85
FPGATrackSimGenScanArray::operator[]
T & operator[](const std::vector< unsigned > &idx)
Definition: FPGATrackSimGenScanArray.h:116
FPGATrackSimGenScanArray::operator[]
T & operator[](const std::vector< int > &idx)
Definition: FPGATrackSimGenScanArray.h:112
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
FPGATrackSimGenScanArray::Iterator::m_itrdata
FPGATrackSimGenScanArray< T > & m_itrdata
Definition: FPGATrackSimGenScanArray.h:182
FPGATrackSimGenScanArray::m_dims
std::vector< unsigned int > m_dims
Definition: FPGATrackSimGenScanArray.h:197
FPGATrackSimGenScanArray::begin
Iterator begin()
Definition: FPGATrackSimGenScanArray.h:186
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
FPGATrackSimGenScanArray::flatdata
const std::vector< T > & flatdata() const
Definition: FPGATrackSimGenScanArray.h:71
FPGATrackSimGenScanArray::Iterator::operator==
friend bool operator==(const Iterator &a, const Iterator &b)
Definition: FPGATrackSimGenScanArray.h:171
FPGATrackSimGenScanArray::Iterator::operator++
Iterator & operator++()
Definition: FPGATrackSimGenScanArray.h:146
a
TList * a
Definition: liststreamerinfos.cxx:10
FPGATrackSimGenScanArray::Iterator
Definition: FPGATrackSimGenScanArray.h:123
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
FPGATrackSimGenScanArray::m_data
std::vector< T > m_data
Definition: FPGATrackSimGenScanArray.h:199
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
FPGATrackSimGenScanArray::FPGATrackSimGenScanArray
FPGATrackSimGenScanArray()=default
FPGATrackSimGenScanArray::end
Iterator end()
Definition: FPGATrackSimGenScanArray.h:187
FPGATrackSimGenScanArray::setsize
void setsize(const std::vector< unsigned int > &dims, const T &initval)
Definition: FPGATrackSimGenScanArray.h:47
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
FPGATrackSimGenScanArray::Iterator::operator!=
friend bool operator!=(const Iterator &a, const Iterator &b)
Definition: FPGATrackSimGenScanArray.h:172
FPGATrackSimGenScanArray::FPGATrackSimGenScanArray
FPGATrackSimGenScanArray(const std::vector< unsigned int > &dims, const T &initval)
Definition: FPGATrackSimGenScanArray.h:41
FPGATrackSimGenScanArray::Iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: FPGATrackSimGenScanArray.h:125
FPGATrackSimGenScanArray::operator[]
const T & operator[](const std::vector< unsigned int > &idx) const
Definition: FPGATrackSimGenScanArray.h:77