Belle II Software  release-08-01-10
MultipassCellularPathFinder.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 <tracking/trackFindingCDC/ca/CellularAutomaton.h>
11 #include <tracking/trackFindingCDC/ca/CellularPathFollower.h>
12 
13 #include <tracking/trackFindingCDC/ca/Path.h>
14 #include <tracking/trackFindingCDC/ca/CellHolder.h>
15 
16 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
17 
18 #include <framework/core/ModuleParamList.h>
19 #include <framework/logging/Logger.h>
20 
21 #include <vector>
22 
23 namespace Belle2 {
28  namespace TrackFindingCDC {
35  template <class ACellHolder>
37  public:
40  {
41  // Experiment in how to specify the requirements of the template parameters
42  // Somewhat incomplete
43  static_assert_isCellHolder<ACellHolder>();
44  }
45 
46  public:
48  void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
49  {
50  moduleParamList->addParameter(prefixed(prefix, "caMode"),
52  "Mode for the cellular automaton application"
53  "* * 'normal' normal path search for high value paths"
54  "* * 'cells' make path for each individual cell for debugging"
55  "* * 'relations' make path for each individual relation for debugging",
57 
58  moduleParamList->addParameter(prefixed(prefix, "minState"),
60  "The minimal accumulated state to follow",
62 
63  moduleParamList->addParameter(prefixed(prefix, "minPathLength"),
65  "The minimal path length to that is written to output",
67 
68  }
69 
71  void apply(const std::vector<ACellHolder*>& cellHolders,
72  const std::vector<WeightedRelation<ACellHolder>>& cellHolderRelations,
73  std::vector<Path<ACellHolder> >& paths)
74  {
75  B2ASSERT("Expected the relations to be sorted",
76  std::is_sorted(cellHolderRelations.begin(), cellHolderRelations.end()));
77 
78  // Forward all cells as paths
79  if (m_param_caMode == "cells") {
80  for (ACellHolder* cellHolder : cellHolders) {
81  paths.push_back({cellHolder});
82  }
83  return;
84  }
85 
86  // Forward all relations as paths
87  if (m_param_caMode == "relations") {
88  for (const WeightedRelation<ACellHolder>& cellHolderRelation : cellHolderRelations) {
89  paths.push_back({cellHolderRelation.getFrom(), cellHolderRelation.getTo()});
90  }
91  return;
92  }
93 
94  // Everything else is just normal
95  if (m_param_caMode != "normal") {
96  B2WARNING("Unrecognised caMode parameter value " << m_param_caMode);
97  m_param_caMode = "normal";
98  }
99 
100  // Multiple passes of the cellular automaton. One path is created
101  // at a time denying all knots it picked up, applying the
102  // cellular automaton again and so on. No best candidate
103  // analysis needed
104  for (ACellHolder* cellHolder : cellHolders) {
105  cellHolder->unsetAndForwardMaskedFlag();
106  }
107 
108  B2DEBUG(25, "Apply multipass cellular automat");
109  do {
110  m_cellularAutomaton.applyTo(cellHolders, cellHolderRelations);
111 
112  auto lessStartCellState = [this](ACellHolder * lhs, ACellHolder * rhs) {
113  const AutomatonCell& lhsCell = lhs->getAutomatonCell();
114  const AutomatonCell& rhsCell = rhs->getAutomatonCell();
115 
116  // Cells with state lower than the minimal cell state are one lowest category
117  if (rhsCell.getCellState() < m_param_minState) return false;
118  if (lhsCell.getCellState() < m_param_minState) return true;
119 
120  return (std::make_tuple(lhsCell.hasPriorityPathFlag(),
121  lhsCell.hasStartFlag(),
122  lhsCell.getCellState()) <
123  std::make_tuple(rhsCell.hasPriorityPathFlag(),
124  rhsCell.hasStartFlag(),
125  rhsCell.getCellState()));
126  };
127 
128  auto itStartCellHolder =
129  std::max_element(cellHolders.begin(), cellHolders.end(), lessStartCellState);
130  if (itStartCellHolder == cellHolders.end()) break;
131  else if (not(*itStartCellHolder)->getAutomatonCell().hasStartFlag()) break;
132  else if ((*itStartCellHolder)->getAutomatonCell().getCellState() < m_param_minState) break;
133 
134  const ACellHolder* highestCellHolder = *itStartCellHolder;
135 
136  Path<ACellHolder> newPath = m_cellularPathFollower.followSingle(highestCellHolder,
137  cellHolderRelations,
139 
140  if (newPath.empty()) break;
141 
142  // Block the used items
143  for (ACellHolder* cellHolderPtr : newPath) {
144  cellHolderPtr->setAndForwardMaskedFlag();
145  }
146 
147  // Block the items that have already used components
148  for (ACellHolder* cellHolder : cellHolders) {
149  cellHolder->receiveMaskedFlag();
150  }
151 
152  if (static_cast<int>(newPath.size()) >= m_param_minPathLength) {
153  paths.push_back(std::move(newPath));
154  }
155 
156  } while (true);
157  }
158 
159  private:
167  std::string m_param_caMode{"normal"};
168 
170  Weight m_param_minState = -INFINITY;
171 
174 
177 
180  };
181  }
183 }
The Module parameter list class.
Cell used by the cellular automata.
Definition: AutomatonCell.h:29
bool hasStartFlag() const
Gets the current state of the start marker flag.
Weight getCellState() const
Getter for the cell state.
Definition: AutomatonCell.h:96
bool hasPriorityPathFlag() const
Gets the current state of the priority path marker flag.
Implements the weighted cellular automaton algorithm.
Implements to pick up of the highest value path in neighborhood Following high value paths can be don...
Class to combine the run of the cellular automaton and the repeated path extraction.
CellularAutomaton< ACellHolder > m_cellularAutomaton
The cellular automaton to be used.
int m_param_minPathLength
The minimal path length to write to output.
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix)
Expose the parameters to a module.
Weight m_param_minState
The minimal accumulated state of the paths to follow.
void apply(const std::vector< ACellHolder * > &cellHolders, const std::vector< WeightedRelation< ACellHolder >> &cellHolderRelations, std::vector< Path< ACellHolder > > &paths)
Applies the cellular automaton to the collection and its relations.
CellularPathFollower< ACellHolder > m_cellularPathFollower
The path follower used to extract the path from the graph processed by the cellular automaton.
MultipassCellularPathFinder()
Default constructor also checking the validity of the template arguments.
std::string m_param_caMode
Mode for the cellular automaton application.
Type for two related objects with a weight.
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Abstract base class for different kinds of events.