Belle II Software  release-05-01-25
BestMatchSelector.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/Algorithms.h>
18 
19 #include <algorithm>
20 #include <vector>
21 
22 namespace Belle2 {
27  namespace TrackFindingCDC {
44  template <class ACollectorItem, class ACollectionItem>
45  class BestMatchSelector :
46  public Findlet<WeightedRelation<ACollectorItem, const ACollectionItem>> {
47  public:
49  using WeightedRelationItem = WeightedRelation<ACollectorItem, const ACollectionItem>;
50 
52  using Super = Findlet<WeightedRelation<ACollectorItem, const ACollectionItem>>;
53 
55  void apply(std::vector<WeightedRelationItem>& weightedRelations) override
56  {
57  std::vector<WeightedRelationItem> selectedWeightedRelations;
58 
59  // Do until there are no more relations left:
60  // (1) find the relation with the maximum weight
61  // (2) store this and delete all relations, which share a "To" or a "From" item with this maximal relation
62  // (3) repeat
63  while (not weightedRelations.empty()) {
64  // std:min_element is strange... this actually returns the element with the largest weight
65  const auto maxElement = *(std::min_element(weightedRelations.begin(), weightedRelations.end(), GreaterWeight()));
66  selectedWeightedRelations.push_back(maxElement);
67 
68  const auto itemSharesFromOrTo = [&maxElement](const WeightedRelationItem & item) {
69  return item.getFrom() == maxElement.getFrom() or item.getTo() == maxElement.getTo();
70  };
71 
72  erase_remove_if(weightedRelations, itemSharesFromOrTo);
73  }
74 
75  std::sort(selectedWeightedRelations.begin(), selectedWeightedRelations.end());
76  weightedRelations.swap(selectedWeightedRelations);
77  }
78  };
79  }
81 }
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::BestMatchSelector::apply
void apply(std::vector< WeightedRelationItem > &weightedRelations) override
Main function of this class doing the relation selection.
Definition: BestMatchSelector.h:63
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::BestMatchSelector::WeightedRelationItem
WeightedRelation< ACollectorItem, const ACollectionItem > WeightedRelationItem
Shortcut class name for a weighted relation between a collector item and a collection item.
Definition: BestMatchSelector.h:57
Belle2::TrackFindingCDC::BestMatchSelector::Super
Findlet< WeightedRelation< ACollectorItem, const ACollectionItem > > Super
The parent class.
Definition: BestMatchSelector.h:60
Belle2::TrackFindingCDC::WeightedRelation
Type for two related objects with a weight.
Definition: CDCSegment2D.h:36