12 #include <tracking/ckf/general/findlets/TreeSearcher.dcl.h>
14 #include <framework/logging/Logger.h>
16 #include <tracking/trackFindingCDC/utilities/Range.h>
17 #include <tracking/trackFindingCDC/utilities/Algorithms.h>
18 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
20 #include <framework/core/ModuleParamList.templateDetails.h>
27 template <
class AState,
class AStateRejecter,
class AResult>
30 Super::addProcessingSignalListener(&m_stateRejecter);
33 template <
class AState,
class AStateRejecter,
class AResult>
34 void TreeSearcher<AState, AStateRejecter, AResult>::exposeParameters(ModuleParamList* moduleParamList,
const std::string& prefix)
36 m_stateRejecter.exposeParameters(moduleParamList, prefix);
38 moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix,
"endEarly"), m_param_endEarly,
39 "Make it possible to have all subresults in the end result vector.",
43 template <
class AState,
class AStateRejecter,
class AResult>
45 std::vector<AState>& hitStates,
47 std::vector<AResult>& results)
49 B2ASSERT(
"Expected relation to be sorted",
50 std::is_sorted(relations.begin(), relations.end()));
53 const std::vector<AState*>& statePointers = TrackFindingCDC::as_pointers<AState>(hitStates);
54 m_automaton.applyTo(statePointers, relations);
56 std::vector<TrackFindingCDC::WithWeight<const AState*>> path;
57 for (
const AState& state : seededStates) {
58 B2DEBUG(50,
"Starting with new seed...");
60 path.emplace_back(&state, 0);
61 traverseTree(path, relations, results);
63 B2ASSERT(
"Something went wrong during the path traversal", path.empty());
65 B2DEBUG(50,
"... finished with seed");
69 template <
class AState,
class AStateRejecter,
class AResult>
70 void TreeSearcher<AState, AStateRejecter, AResult>::traverseTree(std::vector<TrackFindingCDC::WithWeight<const AState*>>& path,
71 const std::vector<TrackFindingCDC::WeightedRelation<AState>>& relations,
72 std::vector<AResult>& results)
74 if (m_param_endEarly) {
76 results.emplace_back(path);
81 const AState* currentState = path.back();
83 TrackFindingCDC::asRange(std::equal_range(relations.begin(), relations.end(), currentState));
85 std::vector<TrackFindingCDC::WithWeight<AState*>> childStates;
87 AState* childState = continuation.getTo();
88 TrackFindingCDC::Weight weight = continuation.getWeight();
91 if (std::count(path.begin(), path.end(), childState)) {
94 B2FATAL(
"Cycle detected!");
98 childStates.emplace_back(childState, weight);
103 const std::vector<TrackFindingCDC::WithWeight<const AState*>>& constPath = path;
104 m_stateRejecter.apply(constPath, childStates);
106 if (childStates.empty()) {
107 B2DEBUG(50,
"Terminating this route, as there are no possible child states.");
108 if (not m_param_endEarly) {
109 results.emplace_back(path);
115 const auto stateLess = [](
const auto & lhs,
const auto & rhs) {
116 return lhs->getAutomatonCell().getCellState() < rhs->getAutomatonCell().getCellState();
118 std::sort(childStates.begin(), childStates.end(), stateLess);
120 B2DEBUG(50,
"Having found " << childStates.size() <<
" child states.");
121 for (
const TrackFindingCDC::WithWeight<AState*>& childState : childStates) {
122 path.emplace_back(childState, childState.getWeight());
123 traverseTree(path, relations, results);