ATLAS Offline Software
PufitMultiGrid.icc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 #ifndef TRIGEFMISSINGET_PUFITMULTIGRID_ICC
6 #define TRIGEFMISSINGET_PUFITMULTIGRID_ICC
7 
8 namespace HLT
9 {
10  namespace MET
11  {
12  namespace detail
13  {
14  /// Initialise an array (thank you stack overflow https://stackoverflow.com/a/41259045/8434292)
15  template <typename T, std::size_t... Is>
16  constexpr std::array<T, sizeof...(Is)>
17  make_array(const T &value, std::index_sequence<Is...>)
18  {
19  return {{(static_cast<void>(Is), value)...}};
20  }
21  } // namespace detail
22 
23  template <std::size_t N>
24  PufitMultiGrid<N>::Tower::Tower(PufitMultiGrid *parent, std::size_t index)
25  : PeriodicGridBase::Tower(index),
26  m_parent(parent)
27  {
28  }
29 
30  template <std::size_t N>
31  typename PufitMultiGrid<N>::Tower &PufitMultiGrid<N>::Tower::operator=(const Tower &other)
32  {
33  applyToAll(&PufitGrid::Tower::operator=, other); // cppcheck-suppress [syntaxError] (bug: https://trac.cppcheck.net/ticket/10080)
34  return *this;
35  }
36 
37  template <std::size_t N>
38  double PufitMultiGrid<N>::Tower::ex(std::size_t type) const
39  {
40  return sumOver(type, &PufitGrid::Tower::ex);
41  }
42 
43  template <std::size_t N>
44  double PufitMultiGrid<N>::Tower::ey(std::size_t type) const
45  {
46  return sumOver(type, &PufitGrid::Tower::ey);
47  }
48 
49  template <std::size_t N>
50  double PufitMultiGrid<N>::Tower::ez(std::size_t type) const
51  {
52  return sumOver(type, &PufitGrid::Tower::ez);
53  }
54 
55  template <std::size_t N>
56  double PufitMultiGrid<N>::Tower::sumEt(std::size_t type) const
57  {
58  return sumOver(type, &PufitGrid::Tower::sumEt);
59  }
60 
61  template <std::size_t N>
62  double PufitMultiGrid<N>::Tower::sumE(std::size_t type) const
63  {
64  return sumOver(type, &PufitGrid::Tower::sumE);
65  }
66 
67  template <std::size_t N>
68  double PufitMultiGrid<N>::Tower::phi(std::size_t type) const
69  {
70  return kinematics(type).phi();
71  }
72 
73  template <std::size_t N>
74  double PufitMultiGrid<N>::Tower::eta(std::size_t type) const
75  {
76  return kinematics(type).eta();
77  }
78 
79  template <std::size_t N>
80  bool PufitMultiGrid<N>::Tower::masked() const { return m_mask; }
81 
82  template <std::size_t N>
83  void PufitMultiGrid<N>::Tower::mask(bool value)
84  {
85  m_mask = value;
86  for (std::size_t ii = 0; ii < N; ++ii)
87  subTower(ii).mask(value);
88  }
89 
90  template <std::size_t N>
91  const PufitMultiGrid<N> *PufitMultiGrid<N>::Tower::grid() const { return m_parent; }
92 
93  template <std::size_t N>
94  SignedKinematics PufitMultiGrid<N>::Tower::kinematics(std::size_t type) const
95  {
96  return sumOver(type, &PufitGrid::Tower::kinematics);
97  }
98 
99  template <std::size_t N>
100  PufitMultiGrid<N>::Tower::operator SignedKinematics() const { return kinematics(); }
101 
102  template <std::size_t N>
103  typename PufitMultiGrid<N>::Tower &PufitMultiGrid<N>::Tower::operator+=(const Tower &other)
104  {
105  applyToAll(&PufitGrid::Tower::operator+=, other);
106  return *this;
107  }
108 
109  template <std::size_t N>
110  typename PufitMultiGrid<N>::Tower &PufitMultiGrid<N>::Tower::operator-=(const Tower &other)
111  {
112  applyToAll(&PufitGrid::Tower::operator-=, other);
113  return *this;
114  }
115 
116  template <std::size_t N>
117  PufitMultiGrid<N>::PufitMultiGrid(
118  double maxEta,
119  std::size_t nEtaTowers,
120  std::size_t nPhiTowers,
121  bool displaceEta,
122  bool displacePhi)
123  : PufitMultiGrid(
124  GridParameters{maxEta, nEtaTowers, nPhiTowers, displaceEta, displacePhi})
125  {
126  }
127 
128  template <std::size_t N>
129  PufitMultiGrid<N>::PufitMultiGrid(const GridParameters &parameters)
130  : PeriodicGridBase(parameters),
131  m_grids(detail::make_array(PufitGrid(parameters), std::make_index_sequence<N>()))
132  {
133  m_towers.reserve(nTowers());
134  for (std::size_t index = 0; index < nTowers(); ++index)
135  m_towers.emplace_back(this, index);
136  }
137 
138  template <std::size_t N>
139  PufitMultiGrid<N>::PufitMultiGrid(const PufitMultiGrid &other)
140  : PufitMultiGrid(other.parameters())
141  {
142  *this = other;
143  }
144 
145  template <std::size_t N>
146  PufitMultiGrid<N> &PufitMultiGrid<N>::operator=(const PufitMultiGrid &other)
147  {
148  if (parameters() != other.parameters())
149  throw std::invalid_argument("Grid parameters do not match");
150  std::copy(other.begin(), other.end(), m_towers.begin());
151  std::copy(other.m_grids.begin(), other.m_grids.end(), m_grids.begin());
152  return *this;
153  }
154 
155  template <std::size_t N>
156  void PufitMultiGrid<N>::reset()
157  {
158  std::fill(begin(), end(), Tower(nullptr, -1));
159  for (PufitGrid &grid : m_grids)
160  grid.reset();
161  }
162  template <std::size_t N>
163  typename PufitMultiGrid<N>::Tower &PufitMultiGrid<N>::operator[](
164  const std::pair<std::size_t, std::size_t> &indices)
165  {
166  return operator[](globalIndex(indices.first, indices.second));
167  }
168  template <std::size_t N>
169  const typename PufitMultiGrid<N>::Tower &PufitMultiGrid<N>::operator[](
170  const std::pair<std::size_t, std::size_t> &indices) const
171  {
172  return operator[](globalIndex(indices.first, indices.second));
173  }
174 
175  template <std::size_t N>
176  typename PufitMultiGrid<N>::Tower &PufitMultiGrid<N>::operator[](std::size_t index)
177  {
178  return m_towers.at(index);
179  }
180  template <std::size_t N>
181  const typename PufitMultiGrid<N>::Tower &PufitMultiGrid<N>::operator[](std::size_t index) const
182  {
183  return m_towers.at(index);
184  }
185 
186  template <std::size_t N>
187  typename std::vector<typename PufitMultiGrid<N>::Tower>::iterator PufitMultiGrid<N>::begin()
188  {
189  return m_towers.begin();
190  }
191  template <std::size_t N>
192  typename std::vector<typename PufitMultiGrid<N>::Tower>::const_iterator PufitMultiGrid<N>::begin() const
193  {
194  return m_towers.begin();
195  }
196  template <std::size_t N>
197  typename std::vector<typename PufitMultiGrid<N>::Tower>::iterator PufitMultiGrid<N>::end()
198  {
199  return m_towers.end();
200  }
201  template <std::size_t N>
202  typename std::vector<typename PufitMultiGrid<N>::Tower>::const_iterator PufitMultiGrid<N>::end() const
203  {
204  return m_towers.end();
205  }
206 
207  template <std::size_t N>
208  PufitGrid PufitMultiGrid<N>::get(std::size_t type) const
209  {
210  if (isPow2(type))
211  return m_grids[intLog2(type)];
212  PufitGrid val(parameters());
213  for (std::size_t ii = 0; ii < N; ++ii)
214  if (1 << ii & type)
215  val += m_grids[ii];
216  return val;
217  }
218 
219  template <typename Grid>
220  PufitMultiGridSet<Grid>::PufitMultiGridSet(
221  double maxEta, std::size_t nEta, std::size_t nPhi)
222  : grids({Grid(maxEta, nEta, nPhi, false, false),
223  Grid(maxEta, nEta, nPhi, true, false),
224  Grid(maxEta, nEta, nPhi, false, true),
225  Grid(maxEta, nEta, nPhi, true, true)})
226  {
227  }
228 
229  template <typename Grid>
230  template <std::size_t I>
231  PufitMultiGridSet<Grid>::Element<I>::Element(PufitMultiGridSet &parent)
232  : parent(parent) {}
233 
234  template <typename Grid>
235  template <std::size_t I>
236  typename PufitMultiGridSet<Grid>::template Element<I>&
237  PufitMultiGridSet<Grid>::Element<I>::operator+=(
238  const SignedKinematics &kin)
239  {
240  for (Grid &grid : parent.grids)
241  grid.template get<I>() += kin;
242  return *this;
243  }
244 
245  template <typename Grid>
246  template <std::size_t I>
247  typename PufitMultiGridSet<Grid>::template Element<I>&
248  PufitMultiGridSet<Grid>::Element<I>::operator-=(
249  const SignedKinematics &kin)
250  {
251  for (Grid &grid : parent.grids)
252  grid.template get<I>() -= kin;
253  return *this;
254  }
255 
256  template <typename Grid>
257  template <std::size_t I, typename, typename>
258  typename PufitMultiGridSet<Grid>::template Element<I>
259  PufitMultiGridSet<Grid>::get()
260  {
261  return Element<I>(*this);
262  }
263 
264  template <typename Grid>
265  PufitGridSet PufitMultiGridSet<Grid>::get(std::size_t type) const
266  {
267  PufitGridSet gridSet(grids[0].maxEta, grids[0].nEta, grids[0].nPhi);
268  for (std::size_t ii = 0; ii < 4; ++ii)
269  gridSet.grids[ii] = grids[ii].get(type);
270  return gridSet;
271  }
272 
273  template <std::size_t N>
274  PufitMultiGrid<N> &PufitMultiGrid<N>::operator+=(const PufitMultiGrid &other)
275  {
276  if (parameters() != other.parameters())
277  throw std::invalid_argument("Grid parameters do not match");
278  auto itr = m_grids.begin();
279  auto otherItr = other.m_grids.begin();
280  for (; itr != end(); ++itr, ++otherItr)
281  *itr += *otherItr;
282  return *this;
283  }
284 
285  template <std::size_t N>
286  PufitMultiGrid<N> &PufitMultiGrid<N>::operator-=(const PufitMultiGrid &other)
287  {
288  if (parameters() != other.parameters())
289  throw std::invalid_argument("Grid parameters do not match");
290  auto itr = m_grids.begin();
291  auto otherItr = other.m_grids.begin();
292  for (; itr != end(); ++itr, ++otherItr)
293  *itr -= *otherItr;
294  return *this;
295  }
296 
297  template <std::size_t N>
298  PufitMultiGrid<N> operator+(const PufitMultiGrid<N> &lhs, const PufitMultiGrid<N> &rhs)
299  {
300  PufitMultiGrid<N> ret(lhs);
301  ret += rhs;
302  return ret;
303  }
304 
305  template <std::size_t N>
306  PufitMultiGrid<N> operator-(const PufitMultiGrid<N> &lhs, const PufitMultiGrid<N> &rhs)
307  {
308  PufitMultiGrid<N> ret(lhs);
309  ret -= rhs;
310  return ret;
311  }
312 
313  } // namespace MET
314 } // namespace HLT
315 
316 #endif //> !TRIGEFMISSINGET_PUFITMULTIGRID_ICC