ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
dnn.cxx
Go to the documentation of this file.
1 //
2 // dnn.cpp
3 // Functions for computing the characteristic length dnn
4 //
5 // Author: Jens Wilberg, Lukas Aymans, Christoph Dalitz
6 // Date: 2018-08-30
7 // License: see ../LICENSE
8 //
9 
10 #include "dnn.h"
11 
12 #include "pointcloud.h" // for PointCloud
13 
14 #include <algorithm>
15 #include <cstddef> // for size_t
16 #include <memory> // for allocator_traits<>::value_type
17 #include <numeric>
18 #include <vector>
19 
20 #include "kdtree/kdtree.hpp"
21 
22 //-------------------------------------------------------------------
23 // Compute mean squared distances.
24 // the distances is computed for every point in *cloud* to its *k*
25 // nearest neighbours. The distances are returned in *msd*.
26 //-------------------------------------------------------------------
27 void compute_mean_square_distance(const PointCloud &cloud, std::vector<double> &msd, int k)
28 {
29  // compute mean square distances for every point to its k nearest neighbours
30  Kdtree::KdNodeVector nodes, result;
31  double sum;
32 
33  // build kdtree
34  for (size_t i = 0; i < cloud.size(); ++i) {
35  nodes.push_back(cloud[i].as_vector());
36  }
37  Kdtree::KdTree kdtree(&nodes);
38 
39  k++; // k must be one higher because the first point found by the kdtree is
40  // the point itself
41 
42  for (size_t i = 0; i < cloud.size(); ++i) {
43  // compute
44  std::vector<double> squared_distances;
45  kdtree.k_nearest_neighbors(cloud[i].as_vector(), k, &result, &squared_distances);
46 
47  squared_distances.erase(squared_distances.begin()); // The first value
48  // must be deleted
49  // because it is
50  // the distance
51  // with the point
52  // itself
53 
54  sum = std::accumulate(squared_distances.begin(), squared_distances.end(), 0.0);
55  msd.push_back(sum / squared_distances.size());
56  }
57 }
58 
59 //-------------------------------------------------------------------
60 // Compute first quartile of the mean squared distance of all points
61 // in *cloud*
62 //-------------------------------------------------------------------
63 double first_quartile(const PointCloud &cloud)
64 {
65  std::vector<double> msd;
67  const double q1 = msd.size() / 4;
68  std::nth_element(msd.begin(), msd.begin() + q1, msd.end());
69  return msd[q1];
70 }
Kdtree::KdNodeVector
std::vector< KdNode > KdNodeVector
Definition: kdtree.hpp:32
msd
Definition: msd.cxx:12
dnn.h
first_quartile
double first_quartile(const PointCloud &cloud)
Definition: dnn.cxx:63
Kdtree::KdTree
Definition: kdtree.hpp:68
Kdtree::KdTree::k_nearest_neighbors
void k_nearest_neighbors(const CoordPoint &point, size_t k, KdNodeVector *result, std::vector< double > *distances, KdNodePredicate *pred=NULL)
Definition: kdtree.cxx:295
compute_mean_square_distance
void compute_mean_square_distance(const PointCloud &cloud, std::vector< double > &msd, int k)
Definition: dnn.cxx:27
pointcloud.h
PointCloud
Definition: pointcloud.h:60
kdtree.hpp