12 #ifndef LAREXAMPLES_ALGORITHMS_REMOVEISOLATEDSPACEPOINTS_POINTISOLATIONALG_H
13 #define LAREXAMPLES_ALGORITHMS_REMOVEISOLATEDSPACEPOINTS_POINTISOLATIONALG_H
19 #include "cetlib/pow.h"
126 template <
typename Coord =
double>
190 template <
typename Po
intIter>
192 (PointIter
begin, PointIter
end)
const;
201 template <
typename Cont>
219 template <
typename Po
intIter>
221 (PointIter
begin, PointIter
end)
const;
236 {
return radius / std::sqrt(3.); }
246 template <
typename Po
intIter>
249 template <
typename Po
intIter>
257 template <
typename Po
intIter = std::array<
double, 3> const*>
263 (
Indexer_t const& indexer,
unsigned int neighExtent)
const;
266 template <
typename Po
intIter>
273 template <
typename Po
intIter>
282 template <
typename Po
int>
308 template <
typename Coord>
309 template <
typename Po
intIter>
314 std::vector<size_t> nonIsolated;
316 Coord_t const R = std::sqrt(config.radius2);
327 Coord_t cellSize = computeCellSize<PointIter>();
328 assert(cellSize > 0);
330 { config.rangeX, cellSize },
331 { config.rangeY, cellSize },
332 { config.rangeZ, cellSize }
336 bool const cellContainedInIsolationSphere
337 = (cellSize <= maximumOptimalCellSize(R));
346 unsigned int const neighExtent = (int) std::ceil(R / cellSize);
348 = buildNeighborhood(partition.indexManager(), neighExtent);
353 if (!cellContainedInIsolationSphere)
354 neighList.insert(neighList.begin(), { 0, 0, 0 });
359 partition.
fill(begin, end);
364 size_t const nCells = partition.indexManager().size();
366 auto const& cellPoints = partition[cellIndex];
372 if (cellContainedInIsolationSphere && (cellPoints.size() > 1)) {
373 for (
auto const& pointPtr: cellPoints)
382 for (
auto const pointPtr: cellPoints) {
390 if (!isPointIsolatedWithinNeighborhood
391 (partition, cellIndex, *pointPtr, neighList)
405 template <
typename Coord>
409 std::vector<std::string> errors;
415 errors.push_back(
"invalid x range " + rangeString(config.
rangeX));
418 errors.push_back(
"invalid y range " + rangeString(config.
rangeY));
421 errors.push_back(
"invalid z range " + rangeString(config.
rangeZ));
424 if (errors.empty())
return;
430 for (
auto const&
error: errors) message +=
"\n * " +
error;
431 throw std::runtime_error(message);
437 template <
typename Coord>
438 template <
typename PointIter >
446 Coord_t cellSize = maximumOptimalCellSize(R);
454 if (config.
maxMemory == 0)
return cellSize;
463 size_t const nCells = partition[0] * partition[1] * partition[2];
464 if (nCells <= 1)
break;
479 template <
typename Coord>
482 (
Indexer_t const& indexer,
unsigned int neighExtent)
const
484 unsigned int const neighSize = 1 + 2 * neighExtent;
486 neighList.reserve(neighSize * neighSize * neighSize - 1);
498 CellDimIndex_t
const ext = neighExtent;
500 CellID_t center{{ 0, 0, 0 }}, cellID;
501 for (CellDimIndex_t ixOfs = -ext; ixOfs <= ext; ++ixOfs) {
503 for (CellDimIndex_t iyOfs = -ext; iyOfs <= ext; ++iyOfs) {
505 for (CellDimIndex_t izOfs = -ext; izOfs <= ext; ++izOfs) {
506 if ((ixOfs == 0) && (iyOfs == 0) && (izOfs == 0))
continue;
509 neighList.push_back(indexer.
offset(center, cellID));
520 template <
typename Coord>
521 template <
typename Po
intIter>
528 for (
auto const& otherPointPtr: otherPoints) {
530 if (closeEnough(point, *otherPointPtr) && (&point != &*otherPointPtr))
540 template <
typename Coord>
541 template <
typename Po
intIter>
558 if (!partition.
has(cellIndex + neighOfs))
continue;
559 auto const& neighCellPoints = partition[cellIndex + neighOfs];
561 if (!isPointIsolatedFrom<PointIter>(point, neighCellPoints))
return false;
571 template <
typename Coord>
572 template <
typename Po
intIter>
575 (PointIter begin, PointIter end)
const
581 std::vector<size_t> nonIsolated;
584 for (
auto it = begin; it !=
end; ++it, ++i) {
586 for (
auto ioth = begin; ioth !=
end; ++ioth) {
587 if (it == ioth)
continue;
589 if (closeEnough(*it, *ioth)) {
590 nonIsolated.push_back(i);
603 template <
typename Coord>
604 template <
typename Po
int>
608 return cet::sum_of_squares(
617 template <
typename Coord>
625 #endif // LAREXAMPLES_ALGORITHMS_REMOVEISOLATEDSPACEPOINTS_POINTISOLATIONALG_H
Algorithm to detect isolated space points.
A container of points sorted in cells.
std::ptrdiff_t CellIndexOffset_t
type of difference between indices
CellIndexOffset_t CellDimIndex_t
type of difference between indices along a dimension
static std::string rangeString(Range_t range)
Helper function. Returns a string "(\<from\> to \<to\>)"
Coord Coord_t
Type of coordinate.
Coord_t radius2
square of isolation radius [cm^2]
size_t maxMemory
grid smaller than this number of bytes (100 MiB)
typename IndexManager_t::LinIndex_t CellIndex_t
type of index for direct access to the cell
Range_t rangeY
range in Y of the covered volume
auto cbegin(FixedBins< T, C > const &) noexcept
static std::string rangeString(Coord_t from, Coord_t to)
Helper function. Returns a string "(\<from\> to \<to\>)"
bool closeEnough(Point const &A, Point const &B) const
Returns whether A and B are close enough to be considered non-isolated.
NeighAddresses_t buildNeighborhood(Indexer_t const &indexer, unsigned int neighExtent) const
Returns a list of cell offsets for the neighbourhood of given radius.
Class to organise data into a 3D grid.
Range_t rangeX
range in X of the covered volume
auto cend(FixedBins< T, C > const &) noexcept
bool has(CellIndexOffset_t ofs) const
Returns whether there is a cell with the specified index (signed!)
Coord_t upper
upper boundary
std::array< CellDimIndex_t, dims()> CellID_t
type of cell coordinate (x, y, z)
GridContainerIndicesBase3D<> GridContainer3DIndices
Index manager for a container of data arranged on a 3D grid.
std::array< size_t, 3 > diceVolume(CoordRangeCells< Coord > const &rangeX, CoordRangeCells< Coord > const &rangeY, CoordRangeCells< Coord > const &rangeZ)
Returns the dimensions of a grid diced with the specified size.
Coord_t lower
lower boundary
auto extractPositionY(Point const &point)
std::vector< Indexer_t::CellIndexOffset_t > NeighAddresses_t
type of neighbourhood cell offsets
bool valid() const
Returns whether the range is valid (empty is also valid)
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
std::vector< size_t > bruteRemoveIsolatedPoints(PointIter begin, PointIter end) const
Brute-force reference algorithm.
std::tuple< double, double, const reco::ClusterHit3D * > Point
Definitions used by the VoronoiDiagram algorithm.
decltype(*PointIter()) Point_t
PointIsolationAlg(Configuration_t const &first_config)
Constructor with configuration validation.
Type containing all configuration parameters of the algorithm.
auto end(FixedBins< T, C > const &) noexcept
Configuration_t & configuration() const
typename Grid_t::Cell_t Cell_t
type of cell
Index manager for a container of data arranged on a >=3-dim grid.
CellIndexOffset_t offset(CellID_t const &origin, CellID_t const &cellID) const
Returns the difference in index of cellID respect to origin.
static void validateConfiguration(Configuration_t const &config)
auto begin(FixedBins< T, C > const &) noexcept
std::vector< size_t > removeIsolatedPoints(Cont const &points) const
Returns the set of points that are not isolated.
std::string to_string(WindowPattern const &pattern)
Range_t rangeZ
range in Z of the covered volume
auto extractPositionX(Point const &point)
bool isPointIsolatedFrom(Point_t< PointIter > const &point, typename Partition_t< PointIter >::Cell_t const &otherPoints) const
Returns whether a point is isolated with respect to all the others.
Configuration_t config
all configuration data
std::vector< size_t > removeIsolatedPoints(PointIter begin, PointIter end) const
Returns the set of points that are not isolated.
bool isPointIsolatedWithinNeighborhood(Partition_t< PointIter > const &partition, Indexer_t::CellIndex_t cellIndex, Point_t< PointIter > const &point, NeighAddresses_t const &neighList) const
Returns whether a point is isolated in the specified neighbourhood.
void reconfigure(Configuration_t const &new_config)
static Coord_t maximumOptimalCellSize(Coord_t radius)
Returns the maximum optimal cell size when using a isolation radius.
Coord_t computeCellSize() const
Computes the cell size to be used.
auto extractPositionZ(Point const &point)
void fill(PointIter begin, PointIter end)