Macros
PointIsolationAlg_test.cc File Reference

Unit tests for PointIsolationAlg. More...

#include "larexamples/Algorithms/RemoveIsolatedSpacePoints/PointIsolationAlg.h"
#include "cetlib/pow.h"
#include "boost/test/unit_test.hpp"
#include <array>
#include <algorithm>
#include <numeric>

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   ( PointIsolationAlg_test )
 

Functions

void PointIsolationTest1 ()
 Low-multiplicity unit test. More...
 
template<typename T >
auto CreateStarOfPoints (unsigned int nShells, T distance=T(1)) -> decltype(auto)
 Creates a "star" disposition of points. More...
 
void PointIsolationTest2 (unsigned int levels)
 Tests various isolation radii on a star-distributed set of points. More...
 
 BOOST_AUTO_TEST_CASE (PointIsolationAlgTest)
 
 BOOST_AUTO_TEST_CASE (PointIsolationAlgVerificationTest)
 

Detailed Description

Unit tests for PointIsolationAlg.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
May 27, 2016
See also
PointIsolationAlg.h

This test sets up point distributions with known isolation features, runs the algorithm with various isolation radius settings and verifies that the results are as expected.

The test is run with no arguments.

Two tests are run:

See the documentation of the two functions for more information.

Definition in file PointIsolationAlg_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( PointIsolationAlg_test )

Definition at line 29 of file PointIsolationAlg_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( PointIsolationAlgTest  )

Definition at line 255 of file PointIsolationAlg_test.cc.

255  {
257 } // PointIsolationAlgTest()
void PointIsolationTest1()
Low-multiplicity unit test.
BOOST_AUTO_TEST_CASE ( PointIsolationAlgVerificationTest  )

Definition at line 260 of file PointIsolationAlg_test.cc.

260  {
262 } // PointIsolationAlgVerificationTest()
void PointIsolationTest2(unsigned int levels)
Tests various isolation radii on a star-distributed set of points.
template<typename T >
auto CreateStarOfPoints ( unsigned int  nShells,
distance = T(1) 
) -> decltype(auto)

Creates a "star" disposition of points.

Template Parameters
Ttype of coordinate being used
Parameters
nShellsnumber of points on each ray of the star (origin excluded)
distancedistance from the origin of the farthest point
Returns
a collection of points (each a std::array<T, 3>

Points are aligned on a semi-axis, sparser and sparser as they go away from origin. nShell is the number of points beside the origin on each semi-axis. Origin is always included. A sequence is generated for each of the semi-axes (x, y, and z, two directions each). Example for nShells = 5, showing only one axis (that is, two semi-axes):

1 o o o o o O o o o o o

(points are marked with o, with O being the origin)

The order of the points in the set is:

Definition at line 135 of file PointIsolationAlg_test.cc.

137 {
138 
139  using Coord_t = T;
140  using Point_t = std::array<Coord_t, 3U>;
141 
142  std::vector<Point_t> points;
143  points.reserve(1 + 1 * nShells);
144 
145  // fill shell by shell
146  while (nShells-- > 0) {
147  points.push_back({{ distance, 0., 0. }});
148  points.push_back({{ -distance, 0., 0. }});
149  points.push_back({{ 0., distance, 0. }});
150  points.push_back({{ 0., -distance, 0. }});
151  points.push_back({{ 0., 0., distance }});
152  points.push_back({{ 0., 0., -distance }});
153  distance /= 2;
154  } // while
155 
156  // add the origin
157  points.push_back({{ 0., 0., 0. }});
158 
159  return points;
160 } // CreateStarOfPoints()
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >> Point_t
enum geo::coordinates Coord_t
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
void PointIsolationTest1 ( )

Low-multiplicity unit test.

This tests exercises simple point dispositions: a single point, two points, three points at different distance, and two "pairs" of points. The isolation radius of the test is fixed.

This test uses coordinate type float.

Definition at line 54 of file PointIsolationAlg_test.cc.

54  {
55 
56  using Coord_t = float;
57  using PointIsolationAlg_t = lar::example::PointIsolationAlg<Coord_t>;
58 
59  using Point_t = std::array<Coord_t, 3U>;
60 
61  PointIsolationAlg_t::Configuration_t config;
62  config.radius2 = cet::square(1.);
63  config.rangeX = { -2., +2. };
64  config.rangeY = { -2., +2. };
65  config.rangeZ = { -2., +2. };
66 
67  std::vector<Point_t> points;
68  std::vector<size_t> result, expected;
69 
70  PointIsolationAlg_t algo(config);
71 
72  // first test: a single point
73  points.push_back({{ +1., +1., +1. }});
74  expected.clear();
75 
76  result = algo.removeIsolatedPoints(points);
77  std::sort(result.begin(), result.end());
78  BOOST_TEST(result == expected, boost::test_tools::per_element());
79 
80 
81  // second test: two far points
82  points.push_back({{ -1., -1., -1. }});
83  expected.clear();
84 
85  result = algo.removeIsolatedPoints(points);
86  std::sort(result.begin(), result.end());
87  BOOST_TEST(result == expected, boost::test_tools::per_element());
88 
89 
90  // third test: two close points, another isolated
91  points.push_back({{ +0.5, +1.0, +1.0 }});
92  expected.insert(expected.end(), { 0U, 2U });
93  std::sort(expected.begin(), expected.end());
94 
95  result = algo.removeIsolatedPoints(points);
96  std::sort(result.begin(), result.end());
97  BOOST_TEST(result == expected, boost::test_tools::per_element());
98 
99 
100  // fourth test: two close points, another two also close
101  points.push_back({{ -0.5, -1.0, -1.0 }});
102  expected.insert(expected.end(), { 1U, 3U });
103  std::sort(expected.begin(), expected.end());
104 
105  result = algo.removeIsolatedPoints(points);
106  std::sort(result.begin(), result.end());
107  BOOST_TEST(result == expected, boost::test_tools::per_element());
108 
109 } // PointIsolationTest1()
Algorithm to detect isolated space points.
static QCString result
const char expected[]
Definition: Exception_t.cc:22
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >> Point_t
enum geo::coordinates Coord_t
constexpr T square(T x)
Definition: pow.h:21
static Config * config
Definition: config.cpp:1054
void PointIsolationTest2 ( unsigned int  levels)

Tests various isolation radii on a star-distributed set of points.

Parameters
levelsnumber of shells in the star (0 is only its centre)

The test uses a star-distributed set of points, as produced by CreateStarOfPoints(). This distribution has the characteristic that all the points farther than the isolation radius from the origin are indeed isolated. This makes the prediction of the number of isolated points easier.

This test uses coordinate type double.

Definition at line 177 of file PointIsolationAlg_test.cc.

177  {
178 
179  using Coord_t = double;
180  using PointIsolationAlg_t = lar::example::PointIsolationAlg<Coord_t>;
181 
182  using Point_t = std::array<Coord_t, 3U>;
183 
184  //
185  // prepare the input
186  //
187  constexpr Coord_t starRadius = 1.;
188  std::vector<Point_t> points = CreateStarOfPoints<Coord_t>(levels, starRadius);
189 
190  //
191  // prepare the algorithm
192  //
193  PointIsolationAlg_t::Configuration_t config;
194  config.radius2 = cet::square(1.);
195  config.rangeX = { -2., +2. };
196  config.rangeY = { -2., +2. };
197  config.rangeZ = { -2., +2. };
198  PointIsolationAlg_t algo(config);
199 
200  //
201  // check every level
202  //
203  constexpr unsigned int nSemiDirections = 6;
204  // small step (smaller than smallest distance between shells):
205  const Coord_t epsilonStep = starRadius / (2 << levels);
206  double baseRadius = starRadius; // starting radius
207 
208  // with the wider isolation radius, we expect all the points to be
209  // non-isolated; the most isolated points are at the beginning of the list
210  size_t const maxExpectedPoints = 1 + levels * nSemiDirections;
211  assert(maxExpectedPoints == points.size());
212 
213  std::vector<size_t> expectedBase(maxExpectedPoints);
214  std::iota(expectedBase.begin(), expectedBase.end(), 0U);
215 
216  // check radii that fall in between all the shells;
217  // the first radius is a bit more than half the star radius, and it is
218  // expected to include all points; the next, a bit more than a fourth of the
219  // star radius, will define the 6 points on the outer shell as isolated;
220  // and so forth.
221  // Level N has a radius that includes N shells plus the origin.
222  // Level 0 has a radius so small that it includes only the origin.
223  unsigned int level = levels;
224  do {
225 
226  // compute and set up a proper isolation radius for this level
227  baseRadius /= 2.;
228  config.radius2 = cet::square(baseRadius + epsilonStep);
229  algo.reconfigure(config);
230 
231  BOOST_TEST_MESSAGE
232  ("[" << level <<"] testing with radius " << (baseRadius + epsilonStep));
233 
234  // we expect to progressively have less and less non-isolated points...
235  unsigned int const nExpected = (level > 1)? (1 + level * nSemiDirections): 0;
236  // ... and we expect those points to be the first ones in the collection
237  std::vector<size_t> expected
238  (expectedBase.end() - nExpected, expectedBase.end());
239 
240  std::vector<size_t> result = algo.removeIsolatedPoints(points);
241  BOOST_TEST(result.size() == expected.size());
242 
243  std::sort(result.begin(), result.end());
244  std::sort(expected.begin(), expected.end());
245  BOOST_TEST(result == expected, boost::test_tools::per_element());
246 
247  } while (--level > 0);
248 
249 } // PointIsolationTest2()
Algorithm to detect isolated space points.
static QCString result
const char expected[]
Definition: Exception_t.cc:22
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >> Point_t
enum geo::coordinates Coord_t
constexpr T square(T x)
Definition: pow.h:21
static Config * config
Definition: config.cpp:1054