Belle II Software  release-05-02-19
CellularAutomaton.h
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 #pragma once
11 
12 #include <tracking/trackFindingVXD/algorithms/TrackerAlgorithmBase.h>
13 
14 namespace Belle2 {
22  template<class ContainerType, class ValidatorType>
23  class CellularAutomaton final : public TrackerAlgorithmBase<ContainerType, ValidatorType> {
24  public:
26  using BaseClass = TrackerAlgorithmBase<ContainerType, ValidatorType>;
27 
28 
31 
32 
34  unsigned int stopInRound = BaseClass::m_validator.nMaxIterations + 2;
35 
37  // TODO: Check where the -1 is returned, for bad cases as described above.
38  int apply(ContainerType& aNetworkContainer) override final
39  {
45  unsigned int activeCells = 1, // is set 1 because of following while loop.
46  deadCells = 0,
47  caRound = 1,
48  goodNeighbours = 0,
49  highestCellState = 0;
50 
51  // each iteration of following while loop is one CA-time-step
52  while (activeCells != 0 and caRound < stopInRound) {
53  activeCells = 0;
54 
56  // compare cells with inner neighbours:
57  for (auto* aNode : aNetworkContainer) {
58  auto& currentCell = aNode->getMetaInfo();
59  if (currentCell.isActivated() == false) { continue; }
60  goodNeighbours = 0;
61 
62  for (auto* aNeighbour : aNode->getInnerNodes()) {
63  // skip if neighbour has not the same state (NOTE if one wants to improve the versatility of the code,
64  // this should actually become a member of the cell-class, which then can add some extra
65  // stuff like checking for loops.
66  if (currentCell != aNeighbour->getMetaInfo()) continue;
67 
68  goodNeighbours++;
69  }
70  if (goodNeighbours != 0) {
71  currentCell.setStateUpgrade(true);
72  activeCells++;
73  } else {
74  // WARNING setActivationState does provoke unintended behavior, since sometimes states can not be
75  // upgraded in one round, but can in the next round!
76  /*currentCell.setActivationState(false);*/
77  deadCells++;
78  }
79  }
80 
82  for (auto* aNode : aNetworkContainer) {
83  auto& currentCell = aNode->getMetaInfo();
84  if (currentCell.isActivated() == false or currentCell.isUpgradeAllowed() == false) { continue; }
85 
86  currentCell.setStateUpgrade(false);
87  currentCell.increaseState();
88  if (currentCell.getState() > highestCellState) { highestCellState = currentCell.getState(); }
89  }
90 
92  if (BaseClass::m_validator.isValidRound(caRound) == false) {
93  break;
94  }
95 
96  caRound++;
97  } // CA main-loop
98 
99  return caRound;
100  }
101 
102 
106  unsigned int findSeeds(ContainerType& aNetworkContainer, bool strictSeeding = false) override final
107  {
108  unsigned int nSeeds = 0;
109  for (auto* aNode : aNetworkContainer) {
110  if (strictSeeding && !(aNode->getOuterNodes().empty())) continue;
111 
112  if (BaseClass::m_validator.checkSeed(aNode->getMetaInfo()) == true) {
113  aNode->getMetaInfo().setSeed(true);
114  nSeeds++;
115  }
116  }
117  return nSeeds;
118  }
119  };
121 }
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::CellularAutomaton
CellularAutomaton()
constructor
Definition: CellularAutomaton.h:38
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
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::CellularAutomaton::BaseClass
TrackerAlgorithmBase< ContainerType, ValidatorType > BaseClass
typedef for the baseClass to get rid of the template arguments
Definition: CellularAutomaton.h:34
Belle2::TrackerAlgorithmBase::m_validator
ValidatorType m_validator
something which checks the quality of the test so far (will be applied by the apply-function
Definition: TrackerAlgorithmBase.h:33
Belle2::CellularAutomaton::stopInRound
unsigned int stopInRound
aborts CA after stopInRound iterations - mainly for debugging purposes:
Definition: CellularAutomaton.h:42