Belle II Software  release-05-02-19
WeightedTreeTraversal.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/numerics/WithWeight.h>
15 #include <tracking/trackFindingCDC/utilities/WeightedRelation.h>
16 #include <tracking/trackFindingCDC/utilities/Range.h>
17 
18 #include <framework/logging/Logger.h>
19 
20 #include <vector>
21 #include <string>
22 
23 namespace Belle2 {
28  class ModuleParamList;
29  namespace TrackFindingCDC {
53  template <class AStateRejecter, class AState, class AResult = std::vector<const AState*>>
54  class WeightedTreeTraversal
55  : public Findlet<const AState* const, const WeightedRelation<AState>, AResult> {
56  private:
58  using Super = Findlet<const AState* const, const WeightedRelation<AState>, AResult>;
59 
60  public:
63  : Super()
64  {
66  }
67 
69  void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix) final {
70  m_stateRejecter.exposeParameters(moduleParamList, prefix);
71  }
72 
79  void apply(const std::vector<const AState*>& seededStates,
80  const std::vector<WeightedRelation<AState>>& stateRelations,
81  std::vector<AResult>& results) override;
82 
83  private:
85  void traverseTree(std::vector<const AState*>& path,
86  const std::vector<WeightedRelation<AState>>& stateRelations,
87  std::vector<AResult>& results);
88 
89  private:
91  AStateRejecter m_stateRejecter;
92  };
93 
94  template <class AStateRejecter, class AState, class AResult>
96  const std::vector<const AState*>& seededStates,
97  const std::vector<WeightedRelation<AState>>& stateRelations,
98  std::vector<AResult>& results)
99  {
100  std::vector<const AState*> path;
101  for (const AState* state : seededStates) {
102  B2DEBUG(50, "Starting with new seed...");
103  path.push_back(state);
104  traverseTree(path, stateRelations, results);
105  path.pop_back();
106  B2DEBUG(50, "... finished with seed");
107  }
108  assert(path.empty());
109  }
110 
111  template <class AStateRejecter, class AState, class AResult>
113  std::vector<const AState*>& path,
114  const std::vector<WeightedRelation<AState>>& stateRelations,
115  std::vector<AResult>& results)
116  {
117  // Implement only graph traversal logic and leave the extrapolation and selection to the
118  // rejecter.
119  const AState* currentState = path.back();
120  auto continuations =
121  std::equal_range(stateRelations.begin(), stateRelations.end(), currentState);
122 
123  std::vector<WithWeight<AState*>> childStates;
124  for (const WeightedRelation<AState>& continuation : asRange(continuations)) {
125  AState* childState = continuation.getTo();
126  Weight weight = continuation.getWeight();
127  childStates.push_back({childState, weight});
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 (WithWeight<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::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
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::WeightedTreeTraversal::exposeParameters
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters of the subfindlet.
Definition: WeightedTreeTraversal.h:77
Belle2::TrackFindingCDC::WeightedTreeTraversal::WeightedTreeTraversal
WeightedTreeTraversal()
Construct this findlet and add the subfindlet as listener.
Definition: WeightedTreeTraversal.h:70
Belle2::TrackFindingCDC::WeightedTreeTraversal::Super
Findlet< const AState *const, const WeightedRelation< AState >, AResult > Super
Parent class.
Definition: WeightedTreeTraversal.h:66
Belle2::TrackFindingCDC::WeightedTreeTraversal::apply
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.
Definition: WeightedTreeTraversal.h:103
Belle2::TrackFindingCDC::WeightedRelation
Type for two related objects with a weight.
Definition: CDCSegment2D.h:36
Belle2::TrackFindingCDC::WeightedTreeTraversal::m_stateRejecter
AStateRejecter m_stateRejecter
State rejecter to decide which available continuations should be traversed next.
Definition: WeightedTreeTraversal.h:99
Belle2::TrackFindingCDC::WeightedTreeTraversal::traverseTree
void traverseTree(std::vector< const AState * > &path, const std::vector< WeightedRelation< AState >> &stateRelations, std::vector< AResult > &results)
Implementation of the traverseTree function.
Definition: WeightedTreeTraversal.h:120
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46