Belle II Software  release-05-02-19
WeightedRelation.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/numerics/Weight.h>
13 
14 #include <algorithm>
15 #include <utility>
16 #include <type_traits>
17 #include <cassert>
18 
19 namespace Belle2 {
24  namespace TrackFindingCDC {
25 
27  template <class AFrom, class ATo = AFrom>
28  class WeightedRelation : public std::pair<std::pair<AFrom*, Weight>, ATo*> {
29 
31  using Super = std::pair<std::pair<AFrom*, Weight>, ATo*>;
32 
33  public:
35  using From = AFrom;
36 
38  using To = ATo;
39 
40  public:
42  WeightedRelation() = default;
43 
45  WeightedRelation(From* from, Weight weight, To* to)
46  : Super( {from, weight}, to)
47  {}
48 
50  bool operator<(const WeightedRelation<From, To>& rhs) const
51  {
52  return (getFrom() < rhs.getFrom() or
53  (not(rhs.getFrom() < getFrom()) and
54  // highest weight first
55  (getWeight() > rhs.getWeight() or
56  (not(rhs.getWeight() > getWeight()) and
57  (getTo() < rhs.getTo())))));
58  }
59 
61  friend bool operator<(const std::pair<From*, Weight>& weightedPtr,
62  const WeightedRelation<From, To>& weightedRelation)
63  {
64  return (weightedPtr.first < weightedRelation.getFrom() or
65  (not(weightedRelation.getFrom() < weightedPtr.first) and
66  // highest weight first
67  (weightedPtr.second > weightedRelation.getWeight())));
68  }
69 
71  friend bool operator<(const WeightedRelation<From, To>& weightedRelation,
72  const std::pair<From*, Weight>& weightedPtr)
73  {
74  return (weightedRelation.getFrom() < weightedPtr.first or
75  (not(weightedPtr.first < weightedRelation.getFrom()) and
76  // highest weight first
77  (weightedRelation.getWeight() > weightedPtr.second)));
78  }
79 
81  friend bool operator<(const From* ptrFrom, const WeightedRelation<From, To>& weightedRelation)
82  {
83  return ptrFrom < weightedRelation.getFrom();
84  }
85 
87  friend bool operator<(const WeightedRelation<From, To>& weightedRelation, const From* ptrFrom)
88  {
89  return weightedRelation.getFrom() < ptrFrom;
90  }
91 
93  From* getFrom() const
94  {
95  return getWeightedFrom().first;
96  }
97 
99  Weight getWeight() const
100  {
101  return getWeightedFrom().second;
102  }
103 
105  void setWeight(Weight weight)
106  {
107  this->first.second = weight;
108  }
109 
111  const std::pair<From*, Weight>& getWeightedFrom() const
112  {
113  return this->first;
114  }
115 
117  To* getTo() const
118  {
119  return this->second;
120  }
121 
124  {
125  //return WeightedRelation<To, From>(getTo(), getWeight(), getFrom());
126  return {getTo(), getWeight(), getFrom()};
127  }
128  };
129 
131  template <class AFrom, class ATo = AFrom>
132  struct WeightedRelationUtil {
138  template <class AWeightedRelations>
139  static bool areSymmetric(const AWeightedRelations& weightedRelations)
140  {
141  static_assert(std::is_same<AFrom, ATo>::value, "Symmetric check requires some types in From and To");
142  assert(std::is_sorted(std::begin(weightedRelations), std::end(weightedRelations)));
143  auto reversedRelationExists =
144  [&weightedRelations](const WeightedRelation<AFrom, ATo>& weightedRelation) -> bool {
145  auto reversedRelations = std::equal_range(std::begin(weightedRelations),
146  std::end(weightedRelations),
147  weightedRelation.reversed());
148  return reversedRelations.first != reversedRelations.second;
149  };
150  return std::all_of(std::begin(weightedRelations),
151  std::end(weightedRelations),
152  reversedRelationExists);
153  }
154  };
155  }
157 }
Belle2::TrackFindingCDC::WeightedRelation::Super
std::pair< std::pair< AFrom *, Weight >, ATo * > Super
Type of the base class.
Definition: WeightedRelation.h:39
Belle2::TrackFindingCDC::WeightedRelation::From
AFrom From
Type of from which the relation originates.
Definition: WeightedRelation.h:43
Belle2::TrackFindingCDC::CDCSegmentPair
Class representing a pair of one reconstructed axial segement and one stereo segment in adjacent supe...
Definition: CDCSegmentPair.h:44
Belle2::TrackFindingCDC::WeightedRelationUtil
Utility structure with functions related to weighted relations.
Definition: WeightedRelation.h:140
Belle2::TrackFindingCDC::WeightedRelation::getWeightedFrom
const std::pair< From *, Weight > & getWeightedFrom() const
Getter for the pointer to the weighted from side object.
Definition: WeightedRelation.h:119
Belle2::TrackFindingCDC::WeightedRelation::getFrom
From * getFrom() const
Getter for the pointer to the from side object.
Definition: WeightedRelation.h:101
Belle2::TrackFindingCDC::WeightedRelation::To
ATo To
Type of to which the relation points.
Definition: WeightedRelation.h:46
Belle2::TrackFindingCDC::WeightedRelation::reversed
WeightedRelation< To, From > reversed() const
Make a relation in the opposite direciton with the same weight.
Definition: WeightedRelation.h:131
Belle2::TrackFindingCDC::WeightedRelation::getWeight
Weight getWeight() const
Getter for the weight.
Definition: WeightedRelation.h:107
Belle2::TrackFindingCDC::WeightedRelationUtil::areSymmetric
static bool areSymmetric(const AWeightedRelations &weightedRelations)
Checks for the symmetry of a range of weighted relations Explicitly checks for each weighted relation...
Definition: WeightedRelation.h:147
Belle2::TrackFindingCDC::WeightedRelation::operator<
bool operator<(const WeightedRelation< From, To > &rhs) const
Operator for ordering of relations.
Definition: WeightedRelation.h:58
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::WeightedRelation::WeightedRelation
WeightedRelation()=default
Default constructor.
Belle2::TrackFindingCDC::WeightedRelation::setWeight
void setWeight(Weight weight)
Setter for the weight.
Definition: WeightedRelation.h:113
Belle2::TrackFindingCDC::WeightedRelation
Type for two related objects with a weight.
Definition: CDCSegment2D.h:36
Belle2::TrackFindingCDC::WeightedRelation::getTo
To * getTo() const
Getter for the pointer to the to side object.
Definition: WeightedRelation.h:125