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