Belle II Software  release-05-01-25
SingleMatchSelector.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
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/WeightComperator.h>
15 
16 #include <tracking/trackFindingCDC/utilities/WeightedRelation.h>
17 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
18 
19 #include <framework/core/ModuleParamList.h>
20 #include <framework/logging/Logger.h>
21 
22 namespace Belle2 {
27  namespace TrackFindingCDC {
45  template <class ACollectorItem, class ACollectionItem, class AComparer = std::less<const ACollectionItem*>>
46  class SingleMatchSelector :
47  public Findlet<WeightedRelation<ACollectorItem, const ACollectionItem>> {
48  public:
50  using WeightedRelationItem = WeightedRelation<ACollectorItem, const ACollectionItem>;
51 
53  using Super = Findlet<WeightedRelation<ACollectorItem, const ACollectionItem>>;
54 
56  void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix) override
57  {
58  Super::exposeParameters(moduleParamList, prefix);
59 
60  moduleParamList->addParameter(prefixed(prefix, "useOnlySingleBestCandidate"), m_param_useOnlySingleBestCandidate,
61  "Use only the found candidate, if it is the only one. Otherwise, use the best one.",
63  }
64 
66  void apply(std::vector<WeightedRelationItem>& weightedRelations) override
67  {
68  B2ASSERT("The relations need to be sorted for this selector!",
69  std::is_sorted(weightedRelations.begin(), weightedRelations.end()));
70 
71  // Build a map from collectionItem -> matched collectionItems with weight
72  std::map<const ACollectionItem*, std::vector<WeightedRelationItem>, AComparer> collectionItemToMatchedMap;
73 
74  for (const WeightedRelationItem& relation : weightedRelations) {
75  collectionItemToMatchedMap[relation.getTo()].push_back(relation);
76  }
77 
78  // Clear all relations and start filling in only the ones which are valid
79  weightedRelations.clear();
80 
81  for (const auto& collectionItemToMatches : collectionItemToMatchedMap) {
82  const auto& matches = collectionItemToMatches.second;
83 
84  const auto& bestMatch = std::min_element(matches.begin(), matches.end(), GreaterWeight());
85 
86  const bool addBestMatch = matches.size() == 1 or not m_param_useOnlySingleBestCandidate;
87 
88  if (addBestMatch) {
89  weightedRelations.push_back(*bestMatch);
90  }
91  }
92 
93  std::sort(weightedRelations.begin(), weightedRelations.end());
94  }
95 
97  void setUseOnlySingleBestCandidate(bool useOnlySingleBestCandidate)
98  {
99  m_param_useOnlySingleBestCandidate = useOnlySingleBestCandidate;
100  }
101 
102  private:
108  };
109  }
111 }
Belle2::TrackFindingCDC::BinaryJoin
Functor factory turning a binary functor and two functors into a new functor which executes the binar...
Definition: Functional.h:137
Belle2::TrackFindingCDC::SingleMatchSelector::exposeParameters
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) override
Expose the useOnlySingleBestCandidate parameter to the module.
Definition: SingleMatchSelector.h:64
Belle2::TrackFindingCDC::SingleMatchSelector::apply
void apply(std::vector< WeightedRelationItem > &weightedRelations) override
Main function of this class doing the relation selection.
Definition: SingleMatchSelector.h:74
Belle2::ModuleParamList::addParameter
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Definition: ModuleParamList.templateDetails.h:38
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::Findlet< WeightedRelation< ACollectorItem, const ACollectionItem > >::exposeParameters
virtual void exposeParameters(ModuleParamList *moduleParamList __attribute__((unused)), const std::string &prefix __attribute__((unused)))
Forward prefixed parameters of this findlet to the module parameter list.
Definition: Findlet.h:79
Belle2::TrackFindingCDC::WeightedRelation
Type for two related objects with a weight.
Definition: CDCSegment2D.h:36
Belle2::TrackFindingCDC::SingleMatchSelector::WeightedRelationItem
WeightedRelation< ACollectorItem, const ACollectionItem > WeightedRelationItem
Shortcut class name for a weighted relation between a collector item and a collection item.
Definition: SingleMatchSelector.h:58
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::TrackFindingCDC::SingleMatchSelector::Super
Findlet< WeightedRelation< ACollectorItem, const ACollectionItem > > Super
The parent class.
Definition: SingleMatchSelector.h:61
Belle2::TrackFindingCDC::SingleMatchSelector::m_param_useOnlySingleBestCandidate
bool m_param_useOnlySingleBestCandidate
Whether to output only those relations, where the relation from collectio to collector items is injec...
Definition: SingleMatchSelector.h:115
Belle2::TrackFindingCDC::SingleMatchSelector::setUseOnlySingleBestCandidate
void setUseOnlySingleBestCandidate(bool useOnlySingleBestCandidate)
Set the UseOnlySingleBestCandidate parameter (mostly for tests).
Definition: SingleMatchSelector.h:105