ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
clusterizer.hpp
Go to the documentation of this file.
1 #ifndef H_CLUSTERIZER
2 #define H_CLUSTERIZER
3 
4 #include <list>
5 #include <set>
6 #include <vector>
7 #include <ostream>
8 #include <random>
9 
10 #include "TH2D.h"
11 #include "TGraph.h"
12 
13 namespace Clustering{
14  class Point{
15  public:
16  Point() = default;
17  Point(float x, float y, float w);
18 
19  float x_, y_, w_;
20 
21  bool operator<(const Point &other) const;
22  };
23 
24  float WeightedDistance(const Point &a, const Point &b);
25 
26  class Node : public Point{
27  public:
28  Node(float x, float y, float z);
29  Node(const Point &p);
30 
31  bool operator<(const Node &other) const;
32 
34  std::list<Node>::iterator neighbor_;
35  std::vector<std::list<Node>::iterator> neighbor_of_;
36  };
37 
38  class Clusterizer{
39  public:
40  explicit Clusterizer(const TH2D &hist_template,
41  long max_points = -1);
42 
43  void AddPoint(float x, float y, float w);
44 
45  void SetPoints(const std::vector<Point> &points);
46  void SetPoints(const TH2D &h);
47 
48  TH2D GetHistogram(double luminosity) const;
49  TGraph GetGraph(double luminosity, bool keep_in_frame = true) const;
50 
51  private:
53  bool hist_mode_;
54  TH2D hist_;
55  std::vector<Point> orig_points_;
56  mutable std::list<Node> nodes_;
57  mutable std::vector<Point> final_points_;
58  mutable float clustered_lumi_;
59 
60  static std::mt19937_64 prng_;
61  static std::uniform_real_distribution<float> urd_;
62 
63  void InsertPoint(float x, float y, float w) const;
64  void InsertPoint(const Point &p) const;
65  void RemovePoint(std::list<Node>::iterator node) const;
66 
67  std::list<Node>::iterator NearestNeighbors() const;
68 
69  static void Link(std::list<Node>::iterator node,
70  std::list<Node>::iterator neighbor,
71  float dist);
72 
73  void EmptyHistogram();
74  void ConvertToHist();
75 
76  void Cluster(double luminosity) const;
77  void SetupNodes(double luminosity) const;
78  void MergeNodes() const;
79  void MergeNodes(std::list<Node>::iterator a,
80  std::list<Node>::iterator b) const;
81  void SplitNode() const;
82  };
83 }
84 
85 std::ostream & operator<<(std::ostream &stream, const Clustering::Point &p);
86 std::ostream & operator<<(std::ostream &stream, const Clustering::Node &n);
87 
88 #endif
float WeightedDistance(const Point &a, const Point &b)
Definition: clusterizer.cpp:34
bool operator<(const Point &other) const
Definition: clusterizer.cpp:30
std::vector< Point > orig_points_
Definition: clusterizer.hpp:55
std::ostream & operator<<(std::ostream &stream, const Clustering::Point &p)
std::list< Node > nodes_
Definition: clusterizer.hpp:56
static std::mt19937_64 prng_
Definition: clusterizer.hpp:60
std::vector< Point > final_points_
Definition: clusterizer.hpp:57
std::list< Node >::iterator neighbor_
Definition: clusterizer.hpp:34
static std::uniform_real_distribution< float > urd_
Definition: clusterizer.hpp:61
std::vector< std::list< Node >::iterator > neighbor_of_
Definition: clusterizer.hpp:35