DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dbscan.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <map>
5#include <iostream>
6#include <algorithm> // For std::lower_bound
7#include <set>
8#include <list>
9
12
13namespace triggeralgs {
14namespace dbscan {
15//======================================================================
16// Find the eps-neighbours of hit q, assuming that the hits vector is sorted by
17// time
18int
19neighbours_sorted(const std::vector<Hit*>& hits, Hit& q, float eps, int minPts);
20
21//======================================================================
22struct Cluster
23{
24 Cluster(int index_)
25 : index{ index_ }
26 {}
27 // The index of this cluster
28 int index{ -1 };
29 // A cluster is kComplete if its hits are all kComplete, so no
30 // newly-arriving hit could be a neighbour of any hit in the
31 // cluster
33 // The latest time of any hit in the cluster
34 float latest_time{ 0 };
35 // The latest (largest time) "core" point in the cluster
37 // The hits in this cluster
39
40 // Add hit if it's a neighbour of a hit already in the
41 // cluster. Precondition: time of new_hit is >= the time of any
42 // hit in the cluster. Returns true if the hit was added
43 bool maybe_add_new_hit(Hit* new_hit, float eps, int minPts);
44
45 // Add the hit `h` to this cluster
46 void add_hit(Hit* h);
47
48 // Steal all of the hits from cluster `other` and merge them into
49 // this cluster
50 void steal_hits(Cluster& other);
51};
52
53//======================================================================
54//
55// Modified DBSCAN algorithm that takes one hit at a time, with the requirement
56// that the hits are passed in time order
58{
59public:
60 IncrementalDBSCAN(float eps, unsigned int minPts, size_t pool_size=100000)
61 : m_eps(eps)
62 , m_minPts(minPts)
63 , m_pool_begin(0)
64 , m_pool_end(0)
65 {
66 for(size_t i=0; i<pool_size; ++i){
67 m_hit_pool.emplace_back(0,0);
68 }
69 }
70
71 void add_primitive(const triggeralgs::TriggerPrimitive& prim, std::vector<Cluster>* completed_clusters=nullptr);
72
73 void add_point(float time, float channel, std::vector<Cluster>* completed_clusters=nullptr);
74
75 // Add a new hit. The hit time *must* be >= the time of all hits
76 // previously added
77 void add_hit(Hit* new_hit, std::vector<Cluster>* completed_clusters=nullptr);
78
79 void trim_hits();
80
81 std::vector<Hit*> get_hits() const { return m_hits; }
82
83 std::map<int, Cluster> get_clusters() const { return m_clusters; }
84
85 uint64_t get_first_prim_time() const { return m_first_prim_time; }
86
87private:
88 //======================================================================
89 //
90 // Starting from `seed_hit`, find all the reachable hits and add them
91 // to `cluster`
92 void cluster_reachable(Hit* seed_hit, Cluster& cluster);
93
94 float m_eps;
95 float m_minPts;
96 std::vector<Hit> m_hit_pool;
98 std::vector<Hit*> m_hits; // All the hits we've seen so far, in time order
99 float m_latest_time{ 0 }; // The latest time of a hit in the vector of hits
100 uint64_t m_first_prim_time{0};
101 std::map<int, Cluster>
102 m_clusters; // All of the currently-active (ie, kIncomplete) clusters
103};
104
105}
106}
107
108// Local Variables:
109// mode: c++
110// c-basic-offset: 4
111// c-file-style: "linux"
112// End:
void add_primitive(const triggeralgs::TriggerPrimitive &prim, std::vector< Cluster > *completed_clusters=nullptr)
Definition dbscan.cpp:137
std::vector< Hit * > get_hits() const
Definition dbscan.hpp:81
std::map< int, Cluster > get_clusters() const
Definition dbscan.hpp:83
IncrementalDBSCAN(float eps, unsigned int minPts, size_t pool_size=100000)
Definition dbscan.hpp:60
void add_point(float time, float channel, std::vector< Cluster > *completed_clusters=nullptr)
Definition dbscan.cpp:126
void cluster_reachable(Hit *seed_hit, Cluster &cluster)
Definition dbscan.cpp:95
void add_hit(Hit *new_hit, std::vector< Cluster > *completed_clusters=nullptr)
Definition dbscan.cpp:153
std::map< int, Cluster > m_clusters
Definition dbscan.hpp:102
int neighbours_sorted(const std::vector< Hit * > &hits, Hit &q, float eps, int minPts)
Definition dbscan.cpp:12
A single energy deposition on a TPC or PDS channel.
bool maybe_add_new_hit(Hit *new_hit, float eps, int minPts)
Definition dbscan.cpp:31
void steal_hits(Cluster &other)
Definition dbscan.cpp:76