Belle II Software development
NodeFamilyDefiner.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <framework/logging/Logger.h>
11
12namespace Belle2 {
33 template<class ContainerType, class NodeType, class NeighbourContainerType>
35 public:
36
41 // cppcheck-suppress constParameter
42 short defineFamilies(ContainerType& aNetwork)
43 {
44 short currentFamily = 0;
45 for (NodeType* aNode : aNetwork) {
46 if (aNode->getFamily() != -1) {
47 continue;
48 }
49
50 aNode->setFamily(currentFamily);
51
52 NeighbourContainerType& innerNeighbours = aNode->getInnerNodes();
53 NeighbourContainerType& outerNeighbours = aNode->getOuterNodes();
54 NeighbourContainerType neighbours;
55 neighbours.reserve(innerNeighbours.size() + outerNeighbours.size());
56 neighbours.insert(neighbours.end(), innerNeighbours.begin(), innerNeighbours.end());
57 neighbours.insert(neighbours.end(), outerNeighbours.begin(), outerNeighbours.end());
58
59 while (neighbours.size() != 0) {
60 neighbours = markNodes(currentFamily, neighbours);
61 }
62 currentFamily++;
63 }
64 return currentFamily;
65 }
66
67 private:
69 NeighbourContainerType markNodes(short family, const NeighbourContainerType& neighbours)
70 {
71 NeighbourContainerType newNeighbours;
72 for (auto& neighbour : neighbours) {
73 // If node was already touched continue;
74 if (neighbour->getFamily() != -1) {
75 short tmpFamily = neighbour->getFamily();
76 if (tmpFamily != family) {
77 B2FATAL("Node already assigned to different family: " << family << ", " << tmpFamily);
78 } else {
79 continue;
80 }
81 }
82 neighbour->setFamily(family);
83 NeighbourContainerType& innerNeighbours = neighbour->getInnerNodes();
84 NeighbourContainerType& outerNeighbours = neighbour->getOuterNodes();
85 newNeighbours.reserve(innerNeighbours.size() + outerNeighbours.size());
86 newNeighbours.insert(newNeighbours.end(), innerNeighbours.begin(), innerNeighbours.end());
87 newNeighbours.insert(newNeighbours.end(), outerNeighbours.begin(), outerNeighbours.end());
88 }
89 return newNeighbours;
90 }
91 };
93}
This class assigns a common family identifier to all CACells in the network that are connected.
short defineFamilies(ContainerType &aNetwork)
Assign a common family identifier to all Nodes in the network that are connected.
NeighbourContainerType markNodes(short family, const NeighbourContainerType &neighbours)
Assign family to all connected nodes and return their neighbours.
NodeType
Enum of possible Nodes in parsing tree.
Abstract base class for different kinds of events.