LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
cluster::DBScan3DAlg Class Reference

#include "DBScan3DAlg.h"

Public Member Functions

 DBScan3DAlg (fhicl::ParameterSet const &pset)
 
void init (const std::vector< art::Ptr< recob::SpacePoint >> &sps, art::FindManyP< recob::Hit > &hitFromSp)
 
void dbscan ()
 

Public Attributes

std::vector< point_tpoints
 

Private Member Functions

node_tcreate_node (unsigned int index)
 
int append_at_end (unsigned int index, epsilon_neighbours_t *en)
 
epsilon_neighbours_tget_epsilon_neighbours (unsigned int index)
 
void destroy_epsilon_neighbours (epsilon_neighbours_t *en)
 
int expand (unsigned int index, unsigned int cluster_id)
 
int spread (unsigned int index, epsilon_neighbours_t *seeds, unsigned int cluster_id)
 
float dist (point_t *a, point_t *b) const
 

Private Attributes

double epsilon
 
unsigned int minpts
 
double badchannelweight
 
unsigned int neighbors
 
std::map< geo::WireID, int > badchannelmap
 

Detailed Description

Definition at line 78 of file DBScan3DAlg.h.

Constructor & Destructor Documentation

cluster::DBScan3DAlg::DBScan3DAlg ( fhicl::ParameterSet const &  pset)

Definition at line 17 of file DBScan3DAlg.cxx.

References epsilon.

18  : epsilon(pset.get<float>("epsilon"))
19  , minpts(pset.get<unsigned int>("minpts"))
20  , badchannelweight(pset.get<double>("badchannelweight"))
21  , neighbors(pset.get<unsigned int>("neighbors"))
22 {
23  // square epsilon to eliminate the use of sqrt later on
24  epsilon *= epsilon;
25 }
unsigned int minpts
Definition: DBScan3DAlg.h:90
unsigned int neighbors
Definition: DBScan3DAlg.h:92

Member Function Documentation

int cluster::DBScan3DAlg::append_at_end ( unsigned int  index,
epsilon_neighbours_t en 
)
private

Definition at line 79 of file DBScan3DAlg.cxx.

References create_node(), FAILURE, epsilon_neighbours_s::head, n, node_s::next, epsilon_neighbours_s::num_members, SUCCESS, and epsilon_neighbours_s::tail.

Referenced by get_epsilon_neighbours(), and spread().

80 {
81  node_t* n = create_node(index);
82  if (n == nullptr) {
83  free(en);
84  return FAILURE;
85  }
86  if (en->head == nullptr) {
87  en->head = n;
88  en->tail = n;
89  }
90  else {
91  en->tail->next = n;
92  en->tail = n;
93  }
94  ++(en->num_members);
95  return SUCCESS;
96 }
node_t * next
Definition: DBScan3DAlg.h:66
unsigned int num_members
Definition: DBScan3DAlg.h:71
node_t * create_node(unsigned int index)
Definition: DBScan3DAlg.cxx:67
#define FAILURE
Definition: DBScan3DAlg.h:40
Char_t n[5]
#define SUCCESS
Definition: DBScan3DAlg.h:39
node_t * cluster::DBScan3DAlg::create_node ( unsigned int  index)
private

Definition at line 67 of file DBScan3DAlg.cxx.

References node_s::index, n, and node_s::next.

Referenced by append_at_end().

68 {
69  node_t* n = (node_t*)calloc(1, sizeof(node_t));
70  if (n == nullptr)
71  perror("Failed to allocate node.");
72  else {
73  n->index = index;
74  n->next = nullptr;
75  }
76  return n;
77 }
unsigned int index
Definition: DBScan3DAlg.h:65
node_t * next
Definition: DBScan3DAlg.h:66
Char_t n[5]
void cluster::DBScan3DAlg::dbscan ( )

Definition at line 133 of file DBScan3DAlg.cxx.

References CORE_POINT, expand(), points, and UNCLASSIFIED.

Referenced by cluster::DBCluster3D::produce().

134 {
135  unsigned int i, cluster_id = 0;
136  for (i = 0; i < points.size(); ++i) {
137  if (points[i].cluster_id == UNCLASSIFIED) {
138  if (expand(i, cluster_id) == CORE_POINT) ++cluster_id;
139  }
140  }
141 }
#define UNCLASSIFIED
Definition: DBScan3DAlg.h:33
int expand(unsigned int index, unsigned int cluster_id)
#define CORE_POINT
Definition: DBScan3DAlg.h:36
std::vector< point_t > points
Definition: DBScan3DAlg.h:82
void cluster::DBScan3DAlg::destroy_epsilon_neighbours ( epsilon_neighbours_t en)
private

Definition at line 120 of file DBScan3DAlg.cxx.

References epsilon_neighbours_s::head, and node_s::next.

Referenced by expand(), get_epsilon_neighbours(), and spread().

121 {
122  if (en) {
123  node_t *t, *h = en->head;
124  while (h) {
125  t = h->next;
126  free(h);
127  h = t;
128  }
129  free(en);
130  }
131 }
node_t * next
Definition: DBScan3DAlg.h:66
float cluster::DBScan3DAlg::dist ( point_t a,
point_t b 
) const
private

Definition at line 199 of file DBScan3DAlg.cxx.

References badchannelweight, f, point_s::nbadchannels, point_s::sp, and recob::SpacePoint::XYZ().

Referenced by get_epsilon_neighbours().

200 {
201  Double32_t const* a_xyz = a->sp->XYZ();
202  Double32_t const* b_xyz = b->sp->XYZ();
203  auto const nbadchannels = a->nbadchannels + b->nbadchannels;
204  float const dx = a_xyz[0] - b_xyz[0];
205  float const dy = a_xyz[1] - b_xyz[1];
206  float const dz = a_xyz[2] - b_xyz[2];
207  float const dist = cet::sum_of_squares(dx, dy, dz) - cet::square(nbadchannels * badchannelweight);
208  // Do not return a distance smaller than 0.
209  return std::max(dist, 0.f);
210 }
art::Ptr< recob::SpacePoint > sp
Definition: DBScan3DAlg.h:58
TFile f
Definition: plotHisto.C:6
float dist(point_t *a, point_t *b) const
const Double32_t * XYZ() const
Definition: SpacePoint.h:78
unsigned int nbadchannels
Definition: DBScan3DAlg.h:59
int cluster::DBScan3DAlg::expand ( unsigned int  index,
unsigned int  cluster_id 
)
private

Definition at line 143 of file DBScan3DAlg.cxx.

References CORE_POINT, destroy_epsilon_neighbours(), FAILURE, get_epsilon_neighbours(), epsilon_neighbours_s::head, node_s::index, minpts, node_s::next, NOISE, NOT_CORE_POINT, epsilon_neighbours_s::num_members, points, tca::seeds, and spread().

Referenced by dbscan().

144 {
145  int return_value = NOT_CORE_POINT;
147  if (seeds == nullptr) return FAILURE;
148 
149  if (seeds->num_members < minpts)
150  points[index].cluster_id = NOISE;
151  else {
152  points[index].cluster_id = cluster_id;
153  node_t* h = seeds->head;
154  while (h) {
155  points[h->index].cluster_id = cluster_id;
156  h = h->next;
157  }
158 
159  h = seeds->head;
160  while (h) {
161  spread(h->index, seeds, cluster_id);
162  h = h->next;
163  }
164 
165  return_value = CORE_POINT;
166  }
168  return return_value;
169 }
unsigned int minpts
Definition: DBScan3DAlg.h:90
unsigned int index
Definition: DBScan3DAlg.h:65
node_t * next
Definition: DBScan3DAlg.h:66
epsilon_neighbours_t * get_epsilon_neighbours(unsigned int index)
Definition: DBScan3DAlg.cxx:98
unsigned int num_members
Definition: DBScan3DAlg.h:71
#define NOT_CORE_POINT
Definition: DBScan3DAlg.h:37
#define CORE_POINT
Definition: DBScan3DAlg.h:36
std::vector< TrajPoint > seeds
Definition: DataStructs.cxx:14
#define FAILURE
Definition: DBScan3DAlg.h:40
std::vector< point_t > points
Definition: DBScan3DAlg.h:82
int spread(unsigned int index, epsilon_neighbours_t *seeds, unsigned int cluster_id)
#define NOISE
Definition: DBScan3DAlg.h:34
void destroy_epsilon_neighbours(epsilon_neighbours_t *en)
epsilon_neighbours_t * cluster::DBScan3DAlg::get_epsilon_neighbours ( unsigned int  index)
private

Definition at line 98 of file DBScan3DAlg.cxx.

References append_at_end(), destroy_epsilon_neighbours(), dist(), epsilon, FAILURE, and points.

Referenced by expand(), and spread().

99 {
101  if (en == nullptr) {
102  perror("Failed to allocate epsilon neighbours.");
103  return en;
104  }
105  for (unsigned int i = 0; i < points.size(); ++i) {
106  if (i == index) continue;
107  if (dist(&points[index], &points[i]) > epsilon)
108  continue;
109  else {
110  if (append_at_end(i, en) == FAILURE) {
112  en = nullptr;
113  break;
114  }
115  }
116  }
117  return en;
118 }
float dist(point_t *a, point_t *b) const
int append_at_end(unsigned int index, epsilon_neighbours_t *en)
Definition: DBScan3DAlg.cxx:79
#define FAILURE
Definition: DBScan3DAlg.h:40
std::vector< point_t > points
Definition: DBScan3DAlg.h:82
void destroy_epsilon_neighbours(epsilon_neighbours_t *en)
void cluster::DBScan3DAlg::init ( const std::vector< art::Ptr< recob::SpacePoint >> &  sps,
art::FindManyP< recob::Hit > &  hitFromSp 
)

Definition at line 28 of file DBScan3DAlg.cxx.

References lar::util::absDiff(), badchannelmap, point_s::cluster_id, Get, hits(), geo::Iterable< IterationPolicy, Transform >::Iterate(), point_s::nbadchannels, neighbors, geo::WireReadoutGeom::PlaneWireToChannel(), points, point_s::sp, and UNCLASSIFIED.

Referenced by cluster::DBCluster3D::produce().

30 {
31  if (badchannelmap.empty()) {
32  lariov::ChannelStatusProvider const& channelStatus =
34  geo::WireReadoutGeom const& wireReadoutGeom =
36  // build a map to count bad channels around each wire ID
37  for (auto& pid : wireReadoutGeom.Iterate<geo::PlaneID>()) {
38  for (auto& wid1 : wireReadoutGeom.Iterate<geo::WireID>(pid)) {
39  unsigned int nbadchs = 0;
40  for (auto& wid2 : wireReadoutGeom.Iterate<geo::WireID>(pid)) {
41  if (wid1 == wid2) continue;
42  if (lar::util::absDiff(wid1.Wire, wid2.Wire) < neighbors &&
43  !channelStatus.IsGood(wireReadoutGeom.PlaneWireToChannel(wid2)))
44  ++nbadchs;
45  }
46  badchannelmap[wid1] = nbadchs;
47  }
48  }
49  std::cout << "Done building bad channel map." << std::endl;
50  }
51 
52  points.clear();
53  for (auto& spt : sps) {
54  point_t point;
55  point.sp = spt;
56  point.cluster_id = UNCLASSIFIED;
57  // count bad channels
58  point.nbadchannels = 0;
59  auto& hits = hitFromSp.at(spt.key());
60  for (auto& hit : hits) {
61  point.nbadchannels += badchannelmap[hit->WireID()];
62  }
63  points.push_back(point);
64  }
65 }
std::map< geo::WireID, int > badchannelmap
Definition: DBScan3DAlg.h:93
art::Ptr< recob::SpacePoint > sp
Definition: DBScan3DAlg.h:58
The data type to uniquely identify a Plane.
Definition: geo_types.h:364
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
void hits()
Definition: readHits.C:15
int cluster_id
Definition: DBScan3DAlg.h:60
Interface for a class providing readout channel mapping to geometry.
#define UNCLASSIFIED
Definition: DBScan3DAlg.h:33
virtual raw::ChannelID_t PlaneWireToChannel(WireID const &wireID) const =0
Returns the channel ID a wire is connected to.
Detector simulation of raw signals on wires.
constexpr auto absDiff(A const &a, B const &b)
Returns the absolute value of the difference between two values.
Definition: NumericUtils.h:69
range_type< T > Iterate() const
Definition: Iterable.h:121
unsigned int neighbors
Definition: DBScan3DAlg.h:92
std::vector< point_t > points
Definition: DBScan3DAlg.h:82
unsigned int nbadchannels
Definition: DBScan3DAlg.h:59
int cluster::DBScan3DAlg::spread ( unsigned int  index,
epsilon_neighbours_t seeds,
unsigned int  cluster_id 
)
private

Definition at line 171 of file DBScan3DAlg.cxx.

References append_at_end(), point_s::cluster_id, d, destroy_epsilon_neighbours(), FAILURE, get_epsilon_neighbours(), epsilon_neighbours_s::head, node_s::index, minpts, n, node_s::next, NOISE, epsilon_neighbours_s::num_members, points, SUCCESS, and UNCLASSIFIED.

Referenced by expand().

174 {
176  if (spread == nullptr) return FAILURE;
177  if (spread->num_members >= minpts) {
178  node_t* n = spread->head;
179  point_t* d;
180  while (n) {
181  d = &points[n->index];
182  if (d->cluster_id == NOISE || d->cluster_id == UNCLASSIFIED) {
183  if (d->cluster_id == UNCLASSIFIED) {
184  if (append_at_end(n->index, seeds) == FAILURE) {
186  return FAILURE;
187  }
188  }
189  d->cluster_id = cluster_id;
190  }
191  n = n->next;
192  }
193  }
194 
196  return SUCCESS;
197 }
unsigned int minpts
Definition: DBScan3DAlg.h:90
unsigned int index
Definition: DBScan3DAlg.h:65
node_t * next
Definition: DBScan3DAlg.h:66
epsilon_neighbours_t * get_epsilon_neighbours(unsigned int index)
Definition: DBScan3DAlg.cxx:98
unsigned int num_members
Definition: DBScan3DAlg.h:71
int cluster_id
Definition: DBScan3DAlg.h:60
#define UNCLASSIFIED
Definition: DBScan3DAlg.h:33
Float_t d
Definition: plot.C:235
int append_at_end(unsigned int index, epsilon_neighbours_t *en)
Definition: DBScan3DAlg.cxx:79
#define FAILURE
Definition: DBScan3DAlg.h:40
Char_t n[5]
std::vector< point_t > points
Definition: DBScan3DAlg.h:82
int spread(unsigned int index, epsilon_neighbours_t *seeds, unsigned int cluster_id)
#define NOISE
Definition: DBScan3DAlg.h:34
void destroy_epsilon_neighbours(epsilon_neighbours_t *en)
#define SUCCESS
Definition: DBScan3DAlg.h:39

Member Data Documentation

std::map<geo::WireID, int> cluster::DBScan3DAlg::badchannelmap
private

Definition at line 93 of file DBScan3DAlg.h.

Referenced by init().

double cluster::DBScan3DAlg::badchannelweight
private

Definition at line 91 of file DBScan3DAlg.h.

Referenced by dist().

double cluster::DBScan3DAlg::epsilon
private

Definition at line 89 of file DBScan3DAlg.h.

Referenced by DBScan3DAlg(), and get_epsilon_neighbours().

unsigned int cluster::DBScan3DAlg::minpts
private

Definition at line 90 of file DBScan3DAlg.h.

Referenced by expand(), and spread().

unsigned int cluster::DBScan3DAlg::neighbors
private

Definition at line 92 of file DBScan3DAlg.h.

Referenced by init().

std::vector<point_t> cluster::DBScan3DAlg::points

The documentation for this class was generated from the following files: