Belle II Software  release-08-01-10
WeightedTreeTraversal.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/numerics/WithWeight.h>
13 #include <tracking/trackFindingCDC/utilities/WeightedRelation.h>
14 #include <tracking/trackFindingCDC/utilities/Range.h>
15 
16 #include <framework/logging/Logger.h>
17 
18 #include <vector>
19 #include <string>
20 
21 namespace Belle2 {
26  class ModuleParamList;
27  namespace TrackFindingCDC {
51  template <class AStateRejecter, class AState, class AResult = std::vector<const AState*>>
53  : public Findlet<const AState* const, const WeightedRelation<AState>, AResult> {
54  private:
57 
58  public:
61  : Super()
62  {
64  }
65 
67  void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix) final
68  {
69  m_stateRejecter.exposeParameters(moduleParamList, prefix);
70  }
71 
78  void apply(const std::vector<const AState*>& seededStates,
79  const std::vector<WeightedRelation<AState>>& stateRelations,
80  std::vector<AResult>& results) override;
81 
82  private:
84  void traverseTree(std::vector<const AState*>& path,
85  const std::vector<WeightedRelation<AState>>& stateRelations,
86  std::vector<AResult>& results);
87 
88  private:
90  AStateRejecter m_stateRejecter;
91  };
92 
93  template <class AStateRejecter, class AState, class AResult>
95  const std::vector<const AState*>& seededStates,
96  const std::vector<WeightedRelation<AState>>& stateRelations,
97  std::vector<AResult>& results)
98  {
99  std::vector<const AState*> path;
100  for (const AState* state : seededStates) {
101  B2DEBUG(25, "Starting with new seed...");
102  path.push_back(state);
103  traverseTree(path, stateRelations, results);
104  path.pop_back();
105  B2DEBUG(25, "... 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<WeightedRelation<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<WithWeight<AState*>> childStates;
123  for (const WeightedRelation<AState>& continuation : asRange(continuations)) {
124  AState* childState = continuation.getTo();
125  Weight weight = continuation.getWeight();
126  childStates.push_back({childState, weight});
127  }
128 
129  // Do everything with child states, linking, extrapolation, teaching, discarding, what have
130  // you.
131  const std::vector<const AState*>& constPath = path;
132  m_stateRejecter.apply(constPath, childStates);
133 
134  if (childStates.empty()) {
135  B2DEBUG(25, "Terminating this route, as there are no possible child states.");
136  results.emplace_back(path);
137  return;
138  }
139 
140  // Traverse the tree from each new state on
141  B2DEBUG(25, "Having found " << childStates.size() << " child states.");
142  for (WithWeight<AState*> childState : childStates) {
143  if (std::count(path.begin(), path.end(), childState)) {
144  // Cycle detected -- is this the best handling?
145  // Other options: Raise and exception and bail out of this seed
146  continue;
147  }
148  path.push_back(childState);
149  traverseTree(path, stateRelations, results);
150  path.pop_back();
151  }
152  }
153  }
155 }
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 with a weight.
General implementation of a tree search algorithm using a given classes as state and results and one ...
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters of the subfindlet.
AStateRejecter m_stateRejecter
State rejecter to decide which available continuations should be traversed next.
void apply(const std::vector< const AState * > &seededStates, const std::vector< WeightedRelation< AState >> &stateRelations, std::vector< AResult > &results) override
Main function of this findlet: traverse a tree starting from a given seed states.
void traverseTree(std::vector< const AState * > &path, const std::vector< WeightedRelation< AState >> &stateRelations, std::vector< AResult > &results)
Implementation of the traverseTree function.
WeightedTreeTraversal()
Construct this findlet and add the subfindlet as listener.
A mixin class to attach a weight to an object.
Definition: WithWeight.h:24
Abstract base class for different kinds of events.