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