CMS 3D CMS Logo

GraphMap.h
Go to the documentation of this file.
1 #ifndef RecoEcal_EgammaCoreTools_GraphMap_h
2 #define RecoEcal_EgammaCoreTools_GraphMap_h
3 
4 #include <vector>
5 #include <array>
6 #include <map>
7 #include <algorithm>
8 
9 /*
10  * Class handling a sparse graph of clusters.
11  *
12  * Author: D. Valsecchi
13  * Date: 08-02-2022
14  */
15 
16 namespace reco {
17 
18  class GraphMap {
19  public:
20  GraphMap(uint nNodes);
21 
23 
24  void addNode(const uint index, const NodeCategory category);
25  void addNodes(const std::vector<uint> &indices, const std::vector<NodeCategory> &categories);
26  void addEdge(const uint i, const uint j);
27  void setAdjMatrix(const uint i, const uint j, const float score);
28  void setAdjMatrixSym(const uint i, const uint j, const float score);
29  void printGraphMap();
30 
31  //Getters
32  const std::vector<uint> &getOutEdges(const uint i) const;
33  const std::vector<uint> &getInEdges(const uint i) const;
34  uint getAdjMatrix(const uint i, const uint j) const;
35  std::vector<float> getAdjMatrixRow(const uint i) const;
36  std::vector<float> getAdjMatrixCol(const uint j) const;
37 
39  Cascade, // Starting from the highest energy seed, collect all the nodes.
40  // Other seeds collected by higher energy seeds are ignored
41  CollectAndMerge, // First, for each simple node keep only the edge with the highest score.
42  // Then collect all the simple nodes around the other seeds.
43  // Edges between the seeds nodes are ignored.
44  // Finally, starting from the first seed, look for linked secondary seeds
45  // and if they pass the threshold, merge their noded.
46  SeedsFirst, // Like strategy D, but after solving the edges between the seeds,
47  // the simple nodes edges are cleaned to keep only the highest score link.
48  // Then proceed as strategy B.
49  CascadeHighest // First, for each simple node keep only the edge with the highest score.
50  // Then proceed as strategy A, from the first seed node cascading to the others.
51  // Secondary seeds linked are absorbed and ignored in the next iteration:
52  // this implies that nodes connected to these seed are lost.
53  };
54 
55  // Output of the collection [{seed, [list of clusters]}]
56  typedef std::vector<std::pair<uint, std::vector<uint>>> GraphOutput;
57  typedef std::map<uint, std::vector<uint>> GraphOutputMap;
58  // Apply the collection algorithms
60  const GraphOutput &getGraphOutput() { return graphOutput_; };
61 
62  private:
63  uint nNodes_;
64  // Map with list of indices of nodes for each category
65  std::map<NodeCategory, std::vector<uint>> nodesCategories_;
66  // Count of nodes for each category
67  std::map<uint, uint> nodesCount_;
68  // Incoming edges, one list for each node (no distinction between type)
69  std::vector<std::vector<uint>> edgesIn_;
70  // Outcoming edges, one list for each node
71  std::vector<std::vector<uint>> edgesOut_;
72  // Adjacency matrix (i,j) --> score
73  // Rows are interpreted as OUT edges
74  // Columns are interpreted as IN edges
75  std::map<std::pair<uint, uint>, float> adjMatrix_;
76 
77  // Store for the graph collection result
79 
80  // Functions for the collection strategies
81  void collectCascading(float threshold);
83  // Return both the output graph with only seedss and a GraphOutputMap
84  // of the collected simple nodes from each seed.
85  std::pair<GraphOutput, GraphOutputMap> collectSeparately(float threshold);
86  void mergeSubGraphs(float threshold, GraphOutput seedsGraph, GraphOutputMap nodesGraphMap);
88  };
89 
90 } // namespace reco
91 
92 #endif
const GraphOutput & getGraphOutput()
Definition: GraphMap.h:60
void mergeSubGraphs(float threshold, GraphOutput seedsGraph, GraphOutputMap nodesGraphMap)
Definition: GraphMap.cc:242
uint getAdjMatrix(const uint i, const uint j) const
Definition: GraphMap.cc:45
std::map< uint, uint > nodesCount_
Definition: GraphMap.h:67
void addNode(const uint index, const NodeCategory category)
Definition: GraphMap.cc:15
void resolveSuperNodesEdges(float threshold)
Definition: GraphMap.cc:281
const std::vector< uint > & getInEdges(const uint i) const
Definition: GraphMap.cc:43
void addNodes(const std::vector< uint > &indices, const std::vector< NodeCategory > &categories)
Definition: GraphMap.cc:20
const std::vector< uint > & getOutEdges(const uint i) const
Definition: GraphMap.cc:41
void collectNodes(GraphMap::CollectionStrategy strategy, float threshold)
Definition: GraphMap.cc:97
std::vector< std::pair< uint, std::vector< uint > > > GraphOutput
Definition: GraphMap.h:56
void setAdjMatrix(const uint i, const uint j, const float score)
Definition: GraphMap.cc:34
std::vector< float > getAdjMatrixCol(const uint j) const
Definition: GraphMap.cc:55
std::vector< float > getAdjMatrixRow(const uint i) const
Definition: GraphMap.cc:47
std::map< NodeCategory, std::vector< uint > > nodesCategories_
Definition: GraphMap.h:65
std::vector< std::vector< uint > > edgesOut_
Definition: GraphMap.h:71
void assignHighestScoreEdge()
Definition: GraphMap.cc:172
void printGraphMap()
Definition: GraphMap.cc:65
std::map< std::pair< uint, uint >, float > adjMatrix_
Definition: GraphMap.h:75
void addEdge(const uint i, const uint j)
Definition: GraphMap.cc:26
std::map< uint, std::vector< uint > > GraphOutputMap
Definition: GraphMap.h:57
void setAdjMatrixSym(const uint i, const uint j, const float score)
Definition: GraphMap.cc:36
fixed size matrix
uint nNodes_
Definition: GraphMap.h:60
GraphMap(uint nNodes)
Definition: GraphMap.cc:9
void collectCascading(float threshold)
Definition: GraphMap.cc:134
std::pair< GraphOutput, GraphOutputMap > collectSeparately(float threshold)
Definition: GraphMap.cc:198
std::vector< std::vector< uint > > edgesIn_
Definition: GraphMap.h:69
GraphOutput graphOutput_
Definition: GraphMap.h:78