ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FsmwMode1dFinder.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FsmwMode1dFinder.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 #include <algorithm>
11 #include <cmath>
12 #include <limits>
14 
15 Acts::FsmwMode1dFinder::FsmwMode1dFinder(double firstFraction, double fraction)
16  : m_firstFraction(firstFraction), m_fraction(fraction) {}
17 
19  std::vector<std::pair<double, double>> inputVector) const {
20  if (inputVector.empty()) {
21  return VertexingError::EmptyInput;
22  }
23  if (inputVector.size() == 1) {
24  return inputVector.begin()->first;
25  }
26 
27  // first of all order the vector according to the double value
28 
29  std::sort(inputVector.begin(), inputVector.end(),
30  [](std::pair<double, double> a, std::pair<double, double> b) {
31  return a.first < b.first;
32  });
33 
34  // begin to consider a certain number of elements according to the fraction
35  auto begin = inputVector.begin();
36  auto end = inputVector.end();
37 
38  double overallweight(0.);
39  auto best_begin = begin;
40  auto best_end = end;
41 
42  double last_value = std::numeric_limits<double>::max();
43 
44  bool isthelast = false;
45 
46  int counter = 0;
47  double fraction = m_firstFraction;
48  while (!isthelast) {
49  counter += 1;
50  if (counter == 2) {
51  fraction = m_fraction;
52  }
53  int step = (int)std::floor(fraction * (end - begin + 1));
54  overallweight = 0.;
55  {
56  auto i = begin;
57  if (step > 0) {
58  auto j_end = i + step - 1;
59  for (auto j = i; j != j_end; j++) {
60  overallweight += j->second;
61  }
62  }
63  }
64  auto i_last = begin + step - 1;
65 
66  for (auto i = begin; i != (end - step + 1); ++i, ++i_last) {
67  // calculate the weight the interval should be divided into
68  overallweight += i_last->second;
69 
70  double new_value = ((i + step - 1)->first - i->first) / overallweight;
71  if (new_value < last_value) {
72  last_value = ((i + step - 1)->first - i->first) / overallweight;
73  best_begin = i;
74  best_end = i + step - 1;
75  }
76  overallweight -= i->second;
77  }
78 
79  // assign the new begin and end...
80  begin = best_begin;
81  end = best_end;
82  last_value = std::numeric_limits<double>::max();
83 
84  // Now it should have returned the value with smallest (x2-x1)/weight
85  if (best_end - best_begin <= 2) {
86  isthelast = true;
87  }
88  }
89 
90  if (best_end - best_begin == 2) {
91  auto medium = begin;
92  medium++;
93  return (begin->first * begin->second + medium->first * medium->second +
94  end->first * end->second) /
95  (begin->second + medium->second + end->second);
96  }
97 
98  return (begin->first * begin->second + end->first * end->second) /
99  (begin->second + end->second);
100 }