Belle II Software development
TreeTraversal.test.cc
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
9#include <gtest/gtest.h>
10#include <tracking/trackFindingCDC/findlets/generic/TreeTraversal.h>
11#include <tracking/trackFindingCDC/findlets/generic/WeightedTreeTraversal.h>
12
13#include <tracking/trackFindingCDC/findlets/base/Findlet.h>
14
15#include <tracking/trackFindingCDC/numerics/Weight.h>
16#include <tracking/trackFindingCDC/numerics/WithWeight.h>
17
18#include <tracking/trackFindingCDC/utilities/Functional.h>
19#include <tracking/trackFindingCDC/utilities/Algorithms.h>
20
21#include <vector>
22
23using namespace Belle2;
24using namespace TrackFindingCDC;
25
26namespace {
27 using State = int;
28 using Result = std::vector<const int*>;
29
30 class AcceptAll : public Findlet<const int* const, int*> {
31 public:
32 void apply(const std::vector<const int*>& currentPath __attribute__((unused)),
33 std::vector<int*>& nextStates __attribute__((unused))) override
34 {
35 // Do not filter states at all.
36 }
37 };
38
39 class AcceptHighWeight : public Findlet<const int* const, WithWeight<int*> > {
40 public:
41 void apply(const std::vector<const int*>& currentPath __attribute__((unused)),
42 std::vector<WithWeight<int*>>& nextStates __attribute__((unused)))
43 {
44 // Remove states with low weight
45 erase_remove_if(nextStates, GetWeight() < 0.5);
46 }
47 };
48}
49
50
51TEST(TrackFindingCDCTest, Findlet_generic_TreeTraversal)
52{
54
55 // Prepare states
56 std::vector<int> states({1, 2, 3});
57
58 // Prepare relations
59 std::vector<Relation<int> > stateRelations;
60 stateRelations.push_back({&states[1], &states[2]});
61 stateRelations.push_back({&states[0], &states[2]});
62 stateRelations.push_back({&states[0], &states[1]});
63 std::sort(stateRelations.begin(), stateRelations.end());
64
65 // Select some seeds
66 std::vector<const int*> seedStates;
67 seedStates.push_back(&states[0]);
68
69 // Find the paths
70 std::vector<std::vector<const int*>> results;
71 testTreeTraversal.apply(seedStates, stateRelations, results);
72
73 ASSERT_EQ(2, results.size());
74
75 ASSERT_EQ(3, results[0].size());
76 EXPECT_EQ(1, *results[0][0]);
77 EXPECT_EQ(2, *results[0][1]);
78 EXPECT_EQ(3, *results[0][2]);
79
80 ASSERT_EQ(2, results[1].size());
81 EXPECT_EQ(1, *results[1][0]);
82 EXPECT_EQ(3, *results[1][1]);
83}
84
85TEST(TrackFindingCDCTest, Findlet_generic_WeightedTreeTraversal)
86{
87 WeightedTreeTraversal<AcceptHighWeight, State> testWeightedTreeTraversal;
88
89 // Prepare states
90 std::vector<int> states({1, 2, 3});
91
92 // Prepare relations
93 std::vector<WeightedRelation<int> > stateRelations;
94 stateRelations.push_back({&states[1], 0.1, &states[2]});
95 stateRelations.push_back({&states[0], 1, &states[2]});
96 stateRelations.push_back({&states[0], 1, &states[1]});
97 std::sort(stateRelations.begin(), stateRelations.end());
98
99 // Select some seeds
100 std::vector<const int*> seedStates;
101 seedStates.push_back(&states[0]);
102
103 // Find the paths
104 std::vector<std::vector<const int*>> results;
105 testWeightedTreeTraversal.apply(seedStates, stateRelations, results);
106
107 ASSERT_EQ(2, results.size());
108
109 ASSERT_EQ(2, results[0].size());
110 EXPECT_EQ(1, *results[0][0]);
111 EXPECT_EQ(2, *results[0][1]);
112
113 ASSERT_EQ(2, results[1].size());
114 EXPECT_EQ(1, *results[1][0]);
115 EXPECT_EQ(3, *results[1][1]);
116}
Interface for a minimal algorithm part that wants to expose some parameters to a module.
Definition: Findlet.h:26
virtual void apply(ToVector< AIOTypes > &... ioVectors)=0
Main function executing the algorithm.
General implementation of a tree search algorithm using a given classes as state and results and one ...
Definition: TreeTraversal.h:51
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:92
General implementation of a tree search algorithm using a given classes as state and results and one ...
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.
A mixin class to attach a weight to an object.
Definition: WithWeight.h:24
Abstract base class for different kinds of events.
Generic functor to get the weight from an object.
Definition: Weight.h:23