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 
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <limits>
16 
17 Acts::FsmwMode1dFinder::FsmwMode1dFinder(double firstFraction, double fraction)
18  : m_firstFraction(firstFraction), m_fraction(fraction) {}
19 
21  std::vector<std::pair<double, double>> inputVector) const {
22  if (inputVector.empty()) {
23  return VertexingError::EmptyInput;
24  }
25  if (inputVector.size() == 1) {
26  return inputVector.begin()->first;
27  }
28 
29  // first of all order the vector according to the double value
30 
31  std::sort(inputVector.begin(), inputVector.end(),
32  [](std::pair<double, double> a, std::pair<double, double> b) {
33  return a.first < b.first;
34  });
35 
36  // begin to consider a certain number of elements according to the fraction
37  auto begin = inputVector.begin();
38  auto end = inputVector.end();
39 
40  double overallweight(0.);
41  auto best_begin = begin;
42  auto best_end = end;
43 
44  double last_value = std::numeric_limits<double>::max();
45 
46  bool isthelast = false;
47 
48  int counter = 0;
49  double fraction = m_firstFraction;
50  while (!isthelast) {
51  counter += 1;
52  if (counter == 2) {
53  fraction = m_fraction;
54  }
55  int step = (int)std::floor(fraction * (end - begin + 1));
56  overallweight = 0.;
57  {
58  auto i = begin;
59  if (step > 0) {
60  auto j_end = i + step - 1;
61  for (auto j = i; j != j_end; j++) {
62  overallweight += j->second;
63  }
64  }
65  }
66  auto i_last = begin + step - 1;
67 
68  for (auto i = begin; i != (end - step + 1); ++i, ++i_last) {
69  // calculate the weight the interval should be divided into
70  overallweight += i_last->second;
71 
72  double new_value = ((i + step - 1)->first - i->first) / overallweight;
73  if (new_value < last_value) {
74  last_value = ((i + step - 1)->first - i->first) / overallweight;
75  best_begin = i;
76  best_end = i + step - 1;
77  }
78  overallweight -= i->second;
79  }
80 
81  // assign the new begin and end...
82  begin = best_begin;
83  end = best_end;
84  last_value = std::numeric_limits<double>::max();
85 
86  // Now it should have returned the value with smallest (x2-x1)/weight
87  if (best_end - best_begin <= 2) {
88  isthelast = true;
89  }
90  }
91 
92  if (best_end - best_begin == 2) {
93  auto medium = begin;
94  medium++;
95  return (begin->first * begin->second + medium->first * medium->second +
96  end->first * end->second) /
97  (begin->second + medium->second + end->second);
98  }
99 
100  return (begin->first * begin->second + end->first * end->second) /
101  (begin->second + end->second);
102 }