Belle II Software  release-05-02-19
NodeFamilyDefiner.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jonas Wagner *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/logging/Logger.h>
13 
14 namespace Belle2 {
35  template<class ContainerType, class NodeType, class NeighbourContainerType>
36  class NodeFamilyDefiner {
37  public:
38 
43  short defineFamilies(ContainerType& aNetwork)
44  {
45  short currentFamily = 0;
46  for (NodeType* aNode : aNetwork) {
47  if (aNode->getFamily() != -1) {
48  continue;
49  }
50 
51  aNode->setFamily(currentFamily);
52 
53  NeighbourContainerType& innerNeighbours = aNode->getInnerNodes();
54  NeighbourContainerType& outerNeighbours = aNode->getOuterNodes();
55  NeighbourContainerType neighbours;
56  neighbours.reserve(innerNeighbours.size() + outerNeighbours.size());
57  neighbours.insert(neighbours.end(), innerNeighbours.begin(), innerNeighbours.end());
58  neighbours.insert(neighbours.end(), outerNeighbours.begin(), outerNeighbours.end());
59 
60  while (neighbours.size() != 0) {
61  neighbours = markNodes(currentFamily, neighbours);
62  }
63  currentFamily++;
64  }
65  return currentFamily;
66  }
67 
68  private:
70  NeighbourContainerType markNodes(short family, NeighbourContainerType& neighbours)
71  {
72  NeighbourContainerType newNeighbours;
73  for (auto& neighbour : neighbours) {
74  // If node was already touched continue;
75  if (neighbour->getFamily() != -1) {
76  short tmpFamily = neighbour->getFamily();
77  if (tmpFamily != family) {
78  B2FATAL("Node already assigned to different family: " << family << ", " << tmpFamily);
79  } else {
80  continue;
81  }
82  }
83  neighbour->setFamily(family);
84  NeighbourContainerType& innerNeighbours = neighbour->getInnerNodes();
85  NeighbourContainerType& outerNeighbours = neighbour->getOuterNodes();
86  newNeighbours.reserve(innerNeighbours.size() + outerNeighbours.size());
87  newNeighbours.insert(newNeighbours.end(), innerNeighbours.begin(), innerNeighbours.end());
88  newNeighbours.insert(newNeighbours.end(), outerNeighbours.begin(), outerNeighbours.end());
89  }
90  return newNeighbours;
91  }
92  };
94 }
Belle2::NodeFamilyDefiner::markNodes
NeighbourContainerType markNodes(short family, NeighbourContainerType &neighbours)
Assign family to all connected nodes and return their neighbours.
Definition: NodeFamilyDefiner.h:78
Belle2::NodeFamilyDefiner::defineFamilies
short defineFamilies(ContainerType &aNetwork)
Assign a common family identifier to all Nodes in the network that are connected.
Definition: NodeFamilyDefiner.h:51
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19