Belle II Software  release-06-01-15
TreeTraversal.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/findlets/base/Findlet.h>
11 
12 #include <tracking/trackFindingCDC/utilities/Relation.h>
13 #include <tracking/trackFindingCDC/utilities/Range.h>
14 
15 #include <framework/logging/Logger.h>
16 
17 #include <vector>
18 #include <string>
19 
20 namespace Belle2 {
25  class ModuleParamList;
26  namespace TrackFindingCDC {
50  template <class AStateRejecter, class AState, class AResult = std::vector<const AState*> >
51  class TreeTraversal : public Findlet<const AState* const, const Relation<AState>, AResult> {
52  private:
55 
56  public:
59  : Super()
60  {
62  }
63 
65  void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix) final {
66  m_stateRejecter.exposeParameters(moduleParamList, prefix);
67  }
68 
75  void apply(const std::vector<const AState*>& seededStates,
76  const std::vector<Relation<AState>>& stateRelations,
77  std::vector<AResult>& results) override;
78 
79  private:
81  void traverseTree(std::vector<const AState*>& path,
82  const std::vector<Relation<AState>>& stateRelations,
83  std::vector<AResult>& results);
84 
85  private:
87  AStateRejecter m_stateRejecter;
88  };
89 
90  template <class AStateRejecter, class AState, class AResult>
92  const std::vector<const AState*>& seededStates,
93  const std::vector<Relation<AState>>& stateRelations,
94  std::vector<AResult>& results)
95  {
96  B2ASSERT("Expected relation to be sorted",
97  std::is_sorted(stateRelations.begin(), stateRelations.end()));
98 
99  std::vector<const AState*> path;
100  for (const AState* state : seededStates) {
101  B2DEBUG(50, "Starting with new seed...");
102  path.push_back(state);
103  traverseTree(path, stateRelations, results);
104  path.pop_back();
105  B2DEBUG(50, "... finished with seed");
106  }
107  assert(path.empty());
108  }
109 
110  template <class AStateRejecter, class AState, class AResult>
112  std::vector<const AState*>& path,
113  const std::vector<Relation<AState>>& stateRelations,
114  std::vector<AResult>& results)
115  {
116  // Implement only graph traversal logic and leave the extrapolation and selection to the
117  // rejecter.
118  const AState* currentState = path.back();
119  auto continuations =
120  std::equal_range(stateRelations.begin(), stateRelations.end(), currentState);
121 
122  std::vector<AState*> childStates;
123  for (const Relation<AState>& continuation : asRange(continuations)) {
124  AState* childState = continuation.getTo();
125  childStates.push_back(childState);
126  }
127 
128  // Do everything with child states, linking, extrapolation, teaching, discarding, what have
129  // you.
130  const std::vector<const AState*>& constPath = path;
131  m_stateRejecter.apply(constPath, childStates);
132 
133  if (childStates.empty()) {
134  B2DEBUG(50, "Terminating this route, as there are no possible child states.");
135  results.emplace_back(path);
136  return;
137  }
138 
139  // Traverse the tree from each new state on
140  B2DEBUG(50, "Having found " << childStates.size() << " child states.");
141  for (const AState* childState : childStates) {
142  if (std::count(path.begin(), path.end(), childState)) {
143  // Cycle detected -- is this the best handling?
144  // Other options: Raise and exception and bail out of this seed
145  continue;
146  }
147  path.push_back(childState);
148  traverseTree(path, stateRelations, results);
149  path.pop_back();
150  }
151  }
152  }
154 }
The Module parameter list class.
void addProcessingSignalListener(ProcessingSignalListener *psl)
Register a processing signal listener to be notified.
Interface for a minimal algorithm part that wants to expose some parameters to a module.
Definition: Findlet.h:26
Type for two related objects.
Definition: Relation.h:21
General implementation of a tree search algorithm using a given classes as state and results and one ...
Definition: TreeTraversal.h:51
void traverseTree(std::vector< const AState * > &path, const std::vector< Relation< AState >> &stateRelations, std::vector< AResult > &results)
Implementation of the traverseTree function.
void apply(const std::vector< const AState * > &seededStates, const std::vector< Relation< AState >> &stateRelations, std::vector< AResult > &results) override
Main function of this findlet: traverse a tree starting from a given seed states.
Definition: TreeTraversal.h:91
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters of the subfindlet.
Definition: TreeTraversal.h:65
AStateRejecter m_stateRejecter
State rejecter to decide which available continuations should be traversed next.
Definition: TreeTraversal.h:87
TreeTraversal()
Construct this findlet and add the subfindlet as listener.
Definition: TreeTraversal.h:58
Abstract base class for different kinds of events.