Belle II Software  release-05-01-25
cellularAutomaton.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jakob Lettenbichler *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <array>
12 #include <iostream>
13 #include <gtest/gtest.h>
14 
15 #include <framework/logging/Logger.h>
16 
17 #include <tracking/trackFindingVXD/segmentNetwork/DirectedNode.h>
18 #include <tracking/trackFindingVXD/segmentNetwork/DirectedNodeNetworkContainer.h>
19 #include <tracking/trackFindingVXD/segmentNetwork/CACell.h>
20 #include <tracking/trackFindingVXD/algorithms/CellularAutomaton.h>
21 #include <tracking/trackFindingVXD/algorithms/CAValidator.h>
22 #include <tracking/trackFindingVXD/algorithms/PathCollectorRecursive.h>
23 #include <tracking/trackFindingVXD/algorithms/NodeCompatibilityCheckerPathCollector.h>
24 
25 
26 using namespace std;
27 using namespace Belle2;
28 
34 
35 
37  class CellularAutomatonTest : public ::testing::Test {
38  protected:
39 
40  };
41 
42 
46  TEST(CellularAutomatonTest, TestCAAndPathCollectorRecursiveUsingDirectedNodeNetworkInt)
47  {
48  // just some input for testing (same as in DirectedNodeNetwork-tests):
49  std::array<int, 5> intArray = { { 2, 5, 3, 4, 99} };
50  std::array<int, 5> intArray2 = { { 144, 121, 33, 35, 31415} }; // these entries are independent of the first intArray-entries
51  std::array<int, 5> intArray3 = { { 1440, 1210, 3, 33, 3141529} }; // entry 2 crosses intArray, entry 3 crosses intArray2
52  std::vector<int> onTheFlyCreatedInts; // the user has to take care about the lifetime of the objects to be linked in the network!
53  onTheFlyCreatedInts.reserve(4);
54 
55 
57  EXPECT_EQ(0, intNetwork.size());
58 
59  // filling network:
60  for (unsigned int index = 1 ; index < 5; index++) {
61  // correct order: outerEntry, innerEntry:
62  intNetwork.addNode(intArray.at(index - 1), intArray.at(index - 1));
63  intNetwork.addNode(intArray.at(index), intArray.at(index));
64 
65  intNetwork.linkNodes(intArray.at(index - 1), intArray.at(index));
66  }
67 
68  for (unsigned int index = 1 ; index < 5; index++) {
69  intNetwork.addNode(intArray2.at(index - 1), intArray2.at(index - 1));
70  intNetwork.addNode(intArray2.at(index), intArray2.at(index));
71 
72  intNetwork.linkNodes(intArray2.at(index - 1), intArray2.at(index));
73  }
74 
75  for (unsigned int index = 1 ; index < 5; index++) {
76  intNetwork.addNode(intArray3.at(index - 1), intArray3.at(index - 1));
77  intNetwork.addNode(intArray3.at(index), intArray3.at(index));
78 
79  intNetwork.linkNodes(intArray3.at(index - 1), intArray3.at(index));
80  }
81 
82  {
83  int oldOuterInt = 2;
84  onTheFlyCreatedInts.push_back(42);
85  int& newInnerInt = onTheFlyCreatedInts.back();
86  intNetwork.addNode(newInnerInt, newInnerInt);
87  intNetwork.linkNodes(newInnerInt, oldOuterInt);
88  }
89 
90  {
91  onTheFlyCreatedInts.push_back(23);
92  int& newOuterInt = onTheFlyCreatedInts.back();
93  int& existingInt = intArray.at(1); // neither an outer nor an inner end before.
94  intNetwork.addNode(newOuterInt, newOuterInt);
95  intNetwork.linkNodes(newOuterInt, existingInt);
96  }
97 
98  intNetwork.linkNodes(intArray.at(0), intArray.at(2));
99  intNetwork.addInnerToLastOuterNode(intArray.at(3));
100 
101  {
102  onTheFlyCreatedInts.push_back(31);
103  int& newInnerInt = onTheFlyCreatedInts.back();
104  intNetwork.addNode(newInnerInt, newInnerInt);
105  intNetwork.addInnerToLastOuterNode(newInnerInt);
106  }
107 
108  intNetwork.addOuterToLastInnerNode(intArray2.at(1));
109 
110  {
111  onTheFlyCreatedInts.push_back(66);
112  int& newOuterInt = onTheFlyCreatedInts.back();
113  intNetwork.addNode(newOuterInt, newOuterInt);
114  intNetwork.addOuterToLastInnerNode(newOuterInt);
115  }
116  // filling network - end.
117  EXPECT_EQ(17, intNetwork.size());
118  // uncomment following line if you want to stream a directed-graph-print of the network into "outputFile".gv
119  // DNN::printNetwork(intNetwork, "outputFile");
120 
121 
124 
125  int nRounds = cellularAutomaton.apply(intNetwork);
126  unsigned int nSeeds = cellularAutomaton.findSeeds(intNetwork);
127  EXPECT_EQ(8,
128  nRounds); // CA starts counting with 1, not with 0, the length of the paths is the number of Cells stored in it. the last round is an empty round.
129  EXPECT_EQ(13, nSeeds);
130 
131  typedef PathCollectorRecursive <
134  std::vector<DirectedNode<int, CACell>*>,
136 
137 
139  PathCollectorType pathCollector;
140 
141  std::vector< PathCollectorType::Path> paths;
142  pathCollector.findPaths(intNetwork, paths, 100000000);
143 
144  std::string out = PathCollectorType::printPaths(paths);
145  B2INFO(out);
146 
147  // there could be more paths than seeds: why?
148  // -> the pathCollectorRecursive based on the CA also adds alternative paths with the same length.
149  EXPECT_EQ(13, paths.size());
150  unsigned int longestPath = 0;
151  for (auto& aPath : paths) {
152  if (longestPath < aPath.size()) {
153  longestPath = aPath.size();
154  }
155  }
156 
157  EXPECT_EQ(7, longestPath); // TODO: fix
158  EXPECT_EQ(nRounds, longestPath +
159  1); // CA starts counting with 1, not with 0, the length of the paths is the number of Cells stored in it.
160 
161  // also collect subpaths
162  paths.clear();
163  bool test = pathCollector.findPaths(intNetwork, paths, 50, true);
164  EXPECT_EQ(44, paths.size()); // Out of the 13 paths one gets 31 subpaths -> 44 in total
165  EXPECT_EQ(true, test); // Should return true, as 44 paths do not exceed the given limit of 50
166 
167  // Checking if limit works
168  paths.clear();
169  test = pathCollector.findPaths(intNetwork, paths, 10);
170  EXPECT_EQ(false, test); // Should return false, as 13 paths exceed the given limit of 10
171  }
172 }
Belle2::CellularAutomaton::apply
int apply(ContainerType &aNetworkContainer) override final
actual algorithm of Cellular Automaton, returns number of rounds needed to finish or -1 if CA was abo...
Definition: CellularAutomaton.h:46
Belle2::CellularAutomaton::findSeeds
unsigned int findSeeds(ContainerType &aNetworkContainer, bool strictSeeding=false) override final
checks network given for seeds, returns number of seeds found (if strictSeeding is set to true,...
Definition: CellularAutomaton.h:114
Belle2::DirectedNodeNetwork::size
unsigned int size() const
Returns number of nodes to be found in the network.
Definition: DirectedNodeNetwork.h:225
Belle2::CellularAutomaton
The CellularAutomaton class This class serves as a functor for the algorithm itself.
Definition: CellularAutomaton.h:31
Belle2::DirectedNodeNetwork
Network of directed nodes of the type EntryType.
Definition: DirectedNodeNetwork.h:38
CellularAutomatonTests
These tests cover the functionality of the classes: DirectedNode, DirectedNodeNetwork.
Definition: cellularAutomaton.cc:33
Belle2::PathCollectorRecursive
Path finder for generic ContainerType.
Definition: PathCollectorRecursive.h:54
Belle2::DirectedNodeNetwork::addOuterToLastInnerNode
bool addOuterToLastInnerNode(NodeID outerNodeID)
to the last innerNode added, another outerNode will be attached
Definition: DirectedNodeNetwork.h:108
Belle2::DirectedNodeNetwork::linkNodes
bool linkNodes(NodeID outerNodeID, NodeID innerNodeID)
takes two entry IDs and weaves them into the network
Definition: DirectedNodeNetwork.h:132
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TEST
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Definition: utilityFunctions.cc:18
Belle2::NodeCompatibilityCheckerPathCollector
simple NodeCompatibilityChecker, which checks for compatible Neighboring states of passed nodes (does...
Definition: NodeCompatibilityCheckerPathCollector.h:29
Belle2::CAValidator
validation tool for CA algorithm
Definition: CAValidator.h:29
Belle2::DirectedNode
The Node-Class.
Definition: DirectedNode.h:41
Belle2::DirectedNodeNetwork::addNode
bool addNode(NodeID nodeID, EntryType &newEntry)
************************* PUBLIC MEMBER FUNCTIONS *************************
Definition: DirectedNodeNetwork.h:72
Belle2::DirectedNodeNetwork::addInnerToLastOuterNode
bool addInnerToLastOuterNode(NodeID innerNodeID)
to the last outerNode added, another innerNode will be attached
Definition: DirectedNodeNetwork.h:85
CellularAutomatonTests::CellularAutomatonTest
Test class demonstrating the behavior of The Cellular Automaton and the PathCollectorRecursive.
Definition: cellularAutomaton.cc:37