Belle II Software  release-08-01-10
SingleMatchSelector.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/WeightComperator.h>
13 
14 #include <tracking/trackFindingCDC/utilities/WeightedRelation.h>
15 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
16 
17 #include <framework/core/ModuleParamList.h>
18 #include <framework/logging/Logger.h>
19 
20 namespace Belle2 {
25  namespace TrackFindingCDC {
43  template <class ACollectorItem, class ACollectionItem, class AComparer = std::less<const ACollectionItem*>>
45  public Findlet<WeightedRelation<ACollectorItem, const ACollectionItem>> {
46  public:
49 
52 
54  void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix) override
55  {
56  Super::exposeParameters(moduleParamList, prefix);
57 
58  moduleParamList->addParameter(prefixed(prefix, "useOnlySingleBestCandidate"), m_param_useOnlySingleBestCandidate,
59  "Use only the found candidate, if it is the only one. Otherwise, use the best one.",
61  }
62 
64  void apply(std::vector<WeightedRelationItem>& weightedRelations) override
65  {
66  B2ASSERT("The relations need to be sorted for this selector!",
67  std::is_sorted(weightedRelations.begin(), weightedRelations.end()));
68 
69  // Build a map from collectionItem -> matched collectionItems with weight
70  std::map<const ACollectionItem*, std::vector<WeightedRelationItem>, AComparer> collectionItemToMatchedMap;
71 
72  for (const WeightedRelationItem& relation : weightedRelations) {
73  collectionItemToMatchedMap[relation.getTo()].push_back(relation);
74  }
75 
76  // Clear all relations and start filling in only the ones which are valid
77  weightedRelations.clear();
78 
79  for (const auto& collectionItemToMatches : collectionItemToMatchedMap) {
80  const auto& matches = collectionItemToMatches.second;
81 
82  const auto& bestMatch = std::min_element(matches.begin(), matches.end(), GreaterWeight());
83 
84  const bool addBestMatch = matches.size() == 1 or not m_param_useOnlySingleBestCandidate;
85 
86  if (addBestMatch) {
87  weightedRelations.push_back(*bestMatch);
88  }
89  }
90 
91  std::sort(weightedRelations.begin(), weightedRelations.end());
92  }
93 
95  void setUseOnlySingleBestCandidate(bool useOnlySingleBestCandidate)
96  {
97  m_param_useOnlySingleBestCandidate = useOnlySingleBestCandidate;
98  }
99 
100  private:
106  };
107  }
109 }
The Module parameter list class.
Interface for a minimal algorithm part that wants to expose some parameters to a module.
Definition: Findlet.h:26
virtual void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix)
Forward prefixed parameters of this findlet to the module parameter list.
Definition: Findlet.h:69
Selector to remove all relations in the list, which share the same collection item - except one in ca...
void setUseOnlySingleBestCandidate(bool useOnlySingleBestCandidate)
Set the UseOnlySingleBestCandidate parameter (mostly for tests).
void apply(std::vector< WeightedRelationItem > &weightedRelations) override
Main function of this class doing the relation selection.
bool m_param_useOnlySingleBestCandidate
Whether to output only those relations, where the relation from collectio to collector items is injec...
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) override
Expose the useOnlySingleBestCandidate parameter to the module.
Type for two related objects with a weight.
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Abstract base class for different kinds of events.
Functor factory turning a binary functor and two functors into a new functor which executes the binar...
Definition: Functional.h:127