ICluster.h
Go to the documentation of this file.
1 /** A "cluster" (as defined here) is a graph connecting shared points
2  * to instances of a specific set of types of WCT data classes related
3  * to WC imaging. See node_t below for the supported types.
4  */
5 
6 #ifndef WIRECELL_ICLUSTER
7 #define WIRECELL_ICLUSTER
8 
10 #include "WireCellIface/IWire.h"
11 #include "WireCellIface/IBlob.h"
12 #include "WireCellIface/ISlice.h"
14 
16 
17 #include <boost/graph/graph_traits.hpp>
18 #include <boost/graph/adjacency_list.hpp>
19 #include <boost/graph/connected_components.hpp>
20 
21 #include <variant>
22 
23 namespace WireCell {
24 
25  /// The vertex property.
26  typedef std::variant<
27  size_t,
34 
35  struct cluster_node_t {
37 
38  cluster_node_t() : ptr() {
39  }
40  cluster_node_t(const cluster_ptr_t& p) : ptr(p) {}
41  cluster_node_t(const IChannel::pointer& p) : ptr(p) {}
42  cluster_node_t(const IWire::pointer& p) : ptr(p) {}
43  cluster_node_t(const IBlob::pointer& p) : ptr(p) {}
44  cluster_node_t(const ISlice::pointer& p) : ptr(p) {}
46 
47  cluster_node_t(const cluster_node_t& other) : ptr(other.ptr) {}
48 
49  // Helper: return a letter code for the type of the ptr or \0.
50  char code() const {
51  auto ind=ptr.index();
52  if (ind == std::variant_npos) { return 0; }
53  return "0cwbsm"[ind];
54  }
55  bool operator==(const cluster_node_t &other) const {
56  return ptr == other.ptr;
57  }
59  ptr = other.ptr;
60  return *this;
61  }
62 
63  };
64 }
65 
66 namespace std {
67  template<>
68  struct hash<WireCell::cluster_node_t> {
69  std::size_t operator()(const WireCell::cluster_node_t& n) const {
70  size_t h = 0;
71  switch (n.ptr.index()) {
72  case 0: h=std::get<0>(n.ptr); break;
73  case 1: h=(std::size_t)std::get<1>(n.ptr).get(); break;
74  case 2: h=(std::size_t)std::get<2>(n.ptr).get(); break;
75  case 3: h=(std::size_t)std::get<3>(n.ptr).get(); break;
76  case 4: h=(std::size_t)std::get<4>(n.ptr).get(); break;
77  case 5: h=(std::size_t)std::get<5>(n.ptr).get(); break;
78  }
79  return h;
80  }
81  };
82 }
83 
84 namespace WireCell {
85 
86 
87  typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, cluster_node_t> cluster_graph_t;
88  typedef boost::graph_traits<cluster_graph_t>::vertex_descriptor cluster_vertex_t;
89  typedef boost::graph_traits<cluster_graph_t>::edge_descriptor cluster_edge_t;
90  typedef boost::graph_traits<cluster_graph_t>::vertex_iterator cluster_vertex_iter_t;
91 
92 
93  class ICluster : public IData<ICluster> {
94  public:
95  virtual ~ICluster();
96 
97  /// Return an identifying number.
98  virtual int ident() const = 0;
99 
100  // Access the graph.
101  virtual const cluster_graph_t& graph() const = 0;
102 
103  };
104 
106 
107  template<typename Type>
108  std::vector<Type> oftype(const cluster_indexed_graph_t& g) {
109  std::vector<Type> ret;
110  for (const auto& v : boost::make_iterator_range(boost::vertices(g.graph()))) {
111  const auto& vp = g.graph()[v];
112  if (std::holds_alternative<Type>(vp.ptr)) {
113  ret.push_back(std::get<Type>(vp.ptr));
114  }
115  }
116  return ret;
117  }
118 
119  template<typename Type>
120  std::vector<Type> neighbors_oftype(const cluster_indexed_graph_t& g, const cluster_node_t& n) {
121  std::vector<Type> ret;
122  for (const auto& vp : g.neighbors(n)) {
123  if (std::holds_alternative<Type>(vp.ptr)) {
124  ret.push_back(std::get<Type>(vp.ptr));
125  }
126  }
127  return ret;
128  }
129 
130 
131 }
132 
133 
134 #endif
std::shared_ptr< const IChannel > pointer
Definition: IData.h:19
IndexedGraph< cluster_node_t > cluster_indexed_graph_t
Definition: ICluster.h:105
cluster_node_t(const IBlob::pointer &p)
Definition: ICluster.h:43
cluster_node_t(const IChannel::pointer &p)
Definition: ICluster.h:41
char code() const
Definition: ICluster.h:50
std::vector< Type > oftype(const cluster_indexed_graph_t &g)
Definition: ICluster.h:108
std::vector< vertex_t > neighbors(vertex_t obj) const
Definition: IndexedGraph.h:69
cluster_node_t(const IChannel::shared_vector &p)
Definition: ICluster.h:45
const graph_t & graph() const
Definition: IndexedGraph.h:141
def graph(desc, maker=maker)
Definition: apa.py:294
cluster_node_t(const IWire::pointer &p)
Definition: ICluster.h:42
STL namespace.
std::size_t operator()(const WireCell::cluster_node_t &n) const
Definition: ICluster.h:69
static const double g
Definition: Units.h:145
bool operator==(const cluster_node_t &other) const
Definition: ICluster.h:55
boost::adjacency_list< boost::setS, boost::vecS, boost::undirectedS, cluster_node_t > cluster_graph_t
Definition: ICluster.h:87
boost::graph_traits< cluster_graph_t >::edge_descriptor cluster_edge_t
Definition: ICluster.h:89
cluster_node_t & operator=(const cluster_node_t &other)
Definition: ICluster.h:58
cluster_node_t(const cluster_node_t &other)
Definition: ICluster.h:47
cluster_node_t(const ISlice::pointer &p)
Definition: ICluster.h:44
Definition: Main.h:22
p
Definition: test.py:223
cluster_node_t(const cluster_ptr_t &p)
Definition: ICluster.h:40
boost::graph_traits< cluster_graph_t >::vertex_descriptor cluster_vertex_t
Definition: ICluster.h:88
boost::graph_traits< cluster_graph_t >::vertex_iterator cluster_vertex_iter_t
Definition: ICluster.h:90
std::vector< Type > neighbors_oftype(const cluster_indexed_graph_t &g, const cluster_node_t &n)
Definition: ICluster.h:120
cluster_ptr_t ptr
Definition: ICluster.h:36
std::size_t n
Definition: format.h:3399
std::shared_ptr< const vector > shared_vector
Definition: IData.h:22
h
training ###############################
Definition: train_cnn.py:186
std::variant< size_t, IChannel::pointer, IWire::pointer, IBlob::pointer, ISlice::pointer, IChannel::shared_vector > cluster_ptr_t
The vertex property.
Definition: ICluster.h:33