forked from sPHENIX-Collaboration/acts
/
BinnedSPGroup.hpp
276 lines (243 loc) · 9.24 KB
/
BinnedSPGroup.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// This file is part of the Acts project.
//
// Copyright (C) 2019 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include "Acts/Seeding/BinFinder.hpp"
#include "Acts/Seeding/InternalSeed.hpp"
#include "Acts/Seeding/Seed.hpp"
#include "Acts/Seeding/SeedfinderConfig.hpp"
#include "Acts/Seeding/SpacePointGrid.hpp"
#include <memory>
#include <vector>
namespace Acts {
/// Iterates over the elements of all bins given
/// by the indices parameter in the given SpacePointGrid.
/// Fullfills the forward iterator.
template <typename external_spacepoint_t>
class NeighborhoodIterator {
public:
using sp_it_t = typename std::vector<std::unique_ptr<
const InternalSpacePoint<external_spacepoint_t>>>::const_iterator;
NeighborhoodIterator() = delete;
NeighborhoodIterator(std::vector<size_t> indices,
const SpacePointGrid<external_spacepoint_t>* spgrid) {
m_grid = spgrid;
m_indices = indices;
m_curInd = 0;
if (m_indices.size() > m_curInd) {
m_curIt = std::begin(spgrid->at(m_indices[m_curInd]));
m_binEnd = std::end(spgrid->at(m_indices[m_curInd]));
}
}
NeighborhoodIterator(std::vector<size_t> indices,
const SpacePointGrid<external_spacepoint_t>* spgrid,
size_t curInd, sp_it_t curIt) {
m_grid = spgrid;
m_indices = indices;
m_curInd = curInd;
m_curIt = curIt;
if (m_indices.size() > m_curInd) {
m_binEnd = std::end(spgrid->at(m_indices[m_curInd]));
}
}
static NeighborhoodIterator<external_spacepoint_t> begin(
std::vector<size_t> indices,
const SpacePointGrid<external_spacepoint_t>* spgrid) {
auto nIt = NeighborhoodIterator<external_spacepoint_t>(indices, spgrid);
// advance until first non-empty bin or last bin
if (nIt.m_curIt == nIt.m_binEnd) {
++nIt;
}
return nIt;
}
NeighborhoodIterator(
const NeighborhoodIterator<external_spacepoint_t>& other) {
m_grid = other.m_grid;
m_indices = other.m_indices;
m_curInd = other.m_curInd;
m_curIt = other.m_curIt;
m_binEnd = other.m_binEnd;
}
void operator++() {
// if iterator of current Bin not yet at end, increase
if (m_curIt != m_binEnd) {
m_curIt++;
// return only if end of current bin still not reached
if (m_curIt != m_binEnd) {
return;
}
}
// increase bin index m_curInd until you find non-empty bin
// or until m_curInd >= m_indices.size()-1
while (m_curIt == m_binEnd && m_indices.size() - 1 > m_curInd) {
m_curInd++;
m_curIt = std::begin(m_grid->at(m_indices[m_curInd]));
m_binEnd = std::end(m_grid->at(m_indices[m_curInd]));
}
}
const InternalSpacePoint<external_spacepoint_t>* operator*() {
return (*m_curIt).get();
}
bool operator!=(const NeighborhoodIterator<external_spacepoint_t>& other) {
return m_curIt != other.m_curIt || m_curInd != other.m_curInd;
}
// iterators within current bin
sp_it_t m_curIt;
sp_it_t m_binEnd;
// number of bins
std::vector<size_t> m_indices;
// current bin
size_t m_curInd;
const Acts::SpacePointGrid<external_spacepoint_t>* m_grid;
};
///@class Neighborhood Used to access iterators to access a group of bins
/// returned by a BinFinder.
/// Fulfills the range_expression interface
template <typename external_spacepoint_t>
class Neighborhood {
public:
Neighborhood() = delete;
Neighborhood(std::vector<size_t> indices,
const SpacePointGrid<external_spacepoint_t>* spgrid) {
m_indices = indices;
m_spgrid = spgrid;
}
NeighborhoodIterator<external_spacepoint_t> begin() {
return NeighborhoodIterator<external_spacepoint_t>::begin(m_indices,
m_spgrid);
}
NeighborhoodIterator<external_spacepoint_t> end() {
return NeighborhoodIterator<external_spacepoint_t>(
m_indices, m_spgrid, m_indices.size() - 1,
std::end(m_spgrid->at(m_indices.back())));
}
private:
std::vector<size_t> m_indices;
const SpacePointGrid<external_spacepoint_t>* m_spgrid;
};
///@class BinnedSPGroupIterator Allows to iterate over all groups of bins
/// a provided BinFinder can generate for each bin of a provided SPGrid
template <typename external_spacepoint_t>
class BinnedSPGroupIterator {
public:
BinnedSPGroupIterator& operator++() {
if (zIndex < phiZbins[1]) {
zIndex++;
} else {
zIndex = 1;
phiIndex++;
}
// set current & neighbor bins only if bin indices valid
if (phiIndex <= phiZbins[0] && zIndex <= phiZbins[1]) {
currentBin =
std::vector<size_t>{grid->globalBinFromLocalBins({phiIndex, zIndex})};
bottomBinIndices = m_bottomBinFinder->findBins(phiIndex, zIndex, grid);
topBinIndices = m_topBinFinder->findBins(phiIndex, zIndex, grid);
outputIndex++;
return *this;
}
phiIndex = phiZbins[0];
zIndex = phiZbins[1] + 1;
return *this;
}
bool operator==(const BinnedSPGroupIterator& otherState) {
return (zIndex == otherState.zIndex && phiIndex == otherState.phiIndex);
}
bool operator!=(const BinnedSPGroupIterator& otherState) {
return !(this->operator==(otherState));
}
Neighborhood<external_spacepoint_t> middle() {
return Neighborhood<external_spacepoint_t>(currentBin, grid);
}
Neighborhood<external_spacepoint_t> bottom() {
return Neighborhood<external_spacepoint_t>(bottomBinIndices, grid);
}
Neighborhood<external_spacepoint_t> top() {
return Neighborhood<external_spacepoint_t>(topBinIndices, grid);
}
BinnedSPGroupIterator(const SpacePointGrid<external_spacepoint_t>* spgrid,
BinFinder<external_spacepoint_t>* botBinFinder,
BinFinder<external_spacepoint_t>* tBinFinder)
: currentBin({spgrid->globalBinFromLocalBins({1, 1})}) {
grid = spgrid;
m_bottomBinFinder = botBinFinder;
m_topBinFinder = tBinFinder;
phiZbins = grid->numLocalBins();
phiIndex = 1;
zIndex = 1;
outputIndex = 0;
bottomBinIndices = m_bottomBinFinder->findBins(phiIndex, zIndex, grid);
topBinIndices = m_topBinFinder->findBins(phiIndex, zIndex, grid);
}
BinnedSPGroupIterator(const SpacePointGrid<external_spacepoint_t>* spgrid,
BinFinder<external_spacepoint_t>* botBinFinder,
BinFinder<external_spacepoint_t>* tBinFinder,
size_t phiInd, size_t zInd)
: currentBin({spgrid->globalBinFromLocalBins({phiInd, zInd})}) {
m_bottomBinFinder = botBinFinder;
m_topBinFinder = tBinFinder;
grid = spgrid;
phiIndex = phiInd;
zIndex = zInd;
phiZbins = grid->numLocalBins();
outputIndex = (phiInd - 1) * phiZbins[1] + zInd - 1;
if (phiIndex <= phiZbins[0] && zIndex <= phiZbins[1]) {
bottomBinIndices = m_bottomBinFinder->findBins(phiIndex, zIndex, grid);
topBinIndices = m_topBinFinder->findBins(phiIndex, zIndex, grid);
}
}
private:
// middle spacepoint bin
std::vector<size_t> currentBin;
std::vector<size_t> bottomBinIndices;
std::vector<size_t> topBinIndices;
const SpacePointGrid<external_spacepoint_t>* grid;
size_t phiIndex = 1;
size_t zIndex = 1;
size_t outputIndex = 0;
std::array<long unsigned int, 2ul> phiZbins;
BinFinder<external_spacepoint_t>* m_bottomBinFinder;
BinFinder<external_spacepoint_t>* m_topBinFinder;
};
///@class BinnedSPGroup Provides access to begin and end BinnedSPGroupIterator
/// for given BinFinders and SpacePointGrid.
/// Fulfills the range_expression interface.
template <typename external_spacepoint_t>
class BinnedSPGroup {
public:
BinnedSPGroup() = delete;
template <typename spacepoint_iterator_t>
BinnedSPGroup<external_spacepoint_t>(
spacepoint_iterator_t spBegin, spacepoint_iterator_t spEnd,
std::function<Acts::Vector2D(const external_spacepoint_t&, float, float,
float)>
covTool,
std::shared_ptr<Acts::BinFinder<external_spacepoint_t>> botBinFinder,
std::shared_ptr<Acts::BinFinder<external_spacepoint_t>> tBinFinder,
std::unique_ptr<SpacePointGrid<external_spacepoint_t>> grid,
SeedfinderConfig<external_spacepoint_t>& config);
size_t size() { return m_binnedSP.size(); }
BinnedSPGroupIterator<external_spacepoint_t> begin() {
return BinnedSPGroupIterator<external_spacepoint_t>(
m_binnedSP.get(), m_bottomBinFinder.get(), m_topBinFinder.get());
}
BinnedSPGroupIterator<external_spacepoint_t> end() {
auto phiZbins = m_binnedSP->numLocalBins();
return BinnedSPGroupIterator<external_spacepoint_t>(
m_binnedSP.get(), m_bottomBinFinder.get(), m_topBinFinder.get(),
phiZbins[0], phiZbins[1] + 1);
}
private:
// grid with ownership of all InternalSpacePoint
std::unique_ptr<Acts::SpacePointGrid<external_spacepoint_t>> m_binnedSP;
// BinFinder must return std::vector<Acts::Seeding::Bin> with content of
// each bin sorted in r (ascending)
std::shared_ptr<BinFinder<external_spacepoint_t>> m_topBinFinder;
std::shared_ptr<BinFinder<external_spacepoint_t>> m_bottomBinFinder;
};
} // namespace Acts
#include "Acts/Seeding/BinnedSPGroup.ipp"