Belle II Software  release-05-02-19
MultipassCellularPathFinder.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2014 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/ca/CellularAutomaton.h>
13 #include <tracking/trackFindingCDC/ca/CellularPathFollower.h>
14 
15 #include <tracking/trackFindingCDC/ca/Path.h>
16 #include <tracking/trackFindingCDC/ca/CellHolder.h>
17 
18 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
19 
20 #include <framework/core/ModuleParamList.h>
21 #include <framework/logging/Logger.h>
22 
23 #include <vector>
24 
25 namespace Belle2 {
30  namespace TrackFindingCDC {
37  template <class ACellHolder>
38  class MultipassCellularPathFinder {
39  public:
42  {
43  // Experiment in how to specify the requirements of the template parameters
44  // Somewhat incomplete
45  static_assert_isCellHolder<ACellHolder>();
46  }
47 
48  public:
50  void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
51  {
52  moduleParamList->addParameter(prefixed(prefix, "caMode"),
54  "Mode for the cellular automaton application"
55  "* * 'normal' normal path search for high value paths"
56  "* * 'cells' make path for each individual cell for debugging"
57  "* * 'relations' make path for each individual relation for debugging",
59 
60  moduleParamList->addParameter(prefixed(prefix, "minState"),
62  "The minimal accumulated state to follow",
64 
65  moduleParamList->addParameter(prefixed(prefix, "minPathLength"),
67  "The minimal path length to that is written to output",
69 
70  }
71 
73  void apply(const std::vector<ACellHolder*>& cellHolders,
74  const std::vector<WeightedRelation<ACellHolder>>& cellHolderRelations,
75  std::vector<Path<ACellHolder> >& paths)
76  {
77  B2ASSERT("Expected the relations to be sorted",
78  std::is_sorted(cellHolderRelations.begin(), cellHolderRelations.end()));
79 
80  // Forward all cells as paths
81  if (m_param_caMode == "cells") {
82  for (ACellHolder* cellHolder : cellHolders) {
83  paths.push_back({cellHolder});
84  }
85  return;
86  }
87 
88  // Forward all relations as paths
89  if (m_param_caMode == "relations") {
90  for (const WeightedRelation<ACellHolder>& cellHolderRelation : cellHolderRelations) {
91  paths.push_back({cellHolderRelation.getFrom(), cellHolderRelation.getTo()});
92  }
93  return;
94  }
95 
96  // Everything else is just normal
97  if (m_param_caMode != "normal") {
98  B2WARNING("Unrecognised caMode parameter value " << m_param_caMode);
99  m_param_caMode = "normal";
100  }
101 
102  // Multiple passes of the cellular automaton. One path is created
103  // at a time denying all knots it picked up, applying the
104  // cellular automaton again and so on. No best candidate
105  // analysis needed
106  for (ACellHolder* cellHolder : cellHolders) {
107  cellHolder->unsetAndForwardMaskedFlag();
108  }
109 
110  B2DEBUG(100, "Apply multipass cellular automat");
111  do {
112  m_cellularAutomaton.applyTo(cellHolders, cellHolderRelations);
113 
114  auto lessStartCellState = [this](ACellHolder * lhs, ACellHolder * rhs) {
115  AutomatonCell& lhsCell = lhs->getAutomatonCell();
116  AutomatonCell& rhsCell = rhs->getAutomatonCell();
117 
118  // Cells with state lower than the minimal cell state are one lowest category
119  if (rhsCell.getCellState() < m_param_minState) return false;
120  if (lhsCell.getCellState() < m_param_minState) return true;
121 
122  return (std::make_tuple(lhsCell.hasPriorityPathFlag(),
123  lhsCell.hasStartFlag(),
124  lhsCell.getCellState()) <
125  std::make_tuple(rhsCell.hasPriorityPathFlag(),
126  rhsCell.hasStartFlag(),
127  rhsCell.getCellState()));
128  };
129 
130  auto itStartCellHolder =
131  std::max_element(cellHolders.begin(), cellHolders.end(), lessStartCellState);
132  if (itStartCellHolder == cellHolders.end()) break;
133  else if (not(*itStartCellHolder)->getAutomatonCell().hasStartFlag()) break;
134  else if ((*itStartCellHolder)->getAutomatonCell().getCellState() < m_param_minState) break;
135 
136  const ACellHolder* highestCellHolder = *itStartCellHolder;
137 
138  Path<ACellHolder> newPath = m_cellularPathFollower.followSingle(highestCellHolder,
139  cellHolderRelations,
141 
142  if (newPath.empty()) break;
143 
144  // Block the used items
145  for (ACellHolder* cellHolderPtr : newPath) {
146  cellHolderPtr->setAndForwardMaskedFlag();
147  }
148 
149  // Block the items that have already used components
150  for (ACellHolder* cellHolder : cellHolders) {
151  cellHolder->receiveMaskedFlag();
152  }
153 
154  if (static_cast<int>(newPath.size()) >= m_param_minPathLength) {
155  paths.push_back(std::move(newPath));
156  }
157 
158  } while (true);
159  }
160 
161  private:
169  std::string m_param_caMode{"normal"};
170 
172  Weight m_param_minState = -INFINITY;
173 
175  int m_param_minPathLength = 0;
176 
178  CellularAutomaton<ACellHolder> m_cellularAutomaton;
179 
181  CellularPathFollower<ACellHolder> m_cellularPathFollower;
182  };
183  }
185 }
Belle2::TrackFindingCDC::MultipassCellularPathFinder::m_param_minState
Weight m_param_minState
The minimal accumulated state of the paths to follow.
Definition: MultipassCellularPathFinder.h:180
Belle2::TrackFindingCDC::MultipassCellularPathFinder::apply
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.
Definition: MultipassCellularPathFinder.h:81
Belle2::ModuleParamList::addParameter
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Definition: ModuleParamList.templateDetails.h:38
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::MultipassCellularPathFinder::exposeParameters
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix)
Expose the parameters to a module.
Definition: MultipassCellularPathFinder.h:58
Belle2::TrackFindingCDC::MultipassCellularPathFinder::MultipassCellularPathFinder
MultipassCellularPathFinder()
Default constructor also checking the validity of the template arguments.
Definition: MultipassCellularPathFinder.h:49
Belle2::TrackFindingCDC::MultipassCellularPathFinder::m_cellularPathFollower
CellularPathFollower< ACellHolder > m_cellularPathFollower
The path follower used to extract the path from the graph processed by the cellular automaton.
Definition: MultipassCellularPathFinder.h:189
Belle2::TrackFindingCDC::MultipassCellularPathFinder::m_param_caMode
std::string m_param_caMode
Mode for the cellular automaton application.
Definition: MultipassCellularPathFinder.h:177
Belle2::TrackFindingCDC::WeightedRelation
Type for two related objects with a weight.
Definition: CDCSegment2D.h:36
Belle2::TrackFindingCDC::MultipassCellularPathFinder::m_param_minPathLength
int m_param_minPathLength
The minimal path length to write to output.
Definition: MultipassCellularPathFinder.h:183
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::TrackFindingCDC::MultipassCellularPathFinder::m_cellularAutomaton
CellularAutomaton< ACellHolder > m_cellularAutomaton
The cellular automaton to be used.
Definition: MultipassCellularPathFinder.h:186