CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hsm_1d.h
Go to the documentation of this file.
1 #ifndef CommonTools_Statistics_hsm_1d_h
2 #define CommonTools_Statistics_hsm_1d_h
3 
5 
6 #include <vector>
7 #include <cmath>
8 #include <algorithm>
9 
10 template <class T>
11 std::pair<typename std::vector<T>::iterator, typename std::vector<T>::iterator> hsm_1d_returning_iterators(
12  std::vector<T>& values) {
13  sort(values.begin(), values.end());
14 
15  typename std::vector<T>::iterator begin = values.begin();
16  typename std::vector<T>::iterator end = values.end() - 1;
17 
18  while (true) {
19  const int size = (int)(end - begin);
20  // const int stepsize = size / 2;
21  int stepsize = static_cast<int>(floor(.4999999999 + size * .5));
22  if (stepsize == 0)
23  stepsize = 1;
24 
25  end = begin + stepsize;
26  T div = (T)fabs((T)(*end) - (T)(*begin));
27  typename std::vector<T>::iterator new_begin = begin;
28  typename std::vector<T>::iterator new_end = end;
29 
30  for (typename std::vector<T>::iterator i = (begin + 1); i != (begin + size - stepsize + 1); ++i) {
31  if (fabs((*i) - (*(i + stepsize))) < div) {
32  new_begin = i;
33  new_end = i + stepsize;
34  div = (T)((T)(*new_end) - (T)(*new_begin));
35  };
36  };
37  begin = new_begin;
38  end = new_end;
39  if (size < 4)
40  break;
41  };
42 
43  std::pair<typename std::vector<T>::iterator, typename std::vector<T>::iterator> ret(begin, end);
44  return ret;
45 }
46 
52 template <class T>
53 T hsm_1d(std::vector<T> values) {
54  switch (values.size()) {
55  case 0:
56  throw StatisticsException("Hsm of empty vector undefined");
57  case 1:
58  return (T) * (values.begin());
59  case 2:
60  return (T)((*(values.begin()) + *(values.end() - 1)) / 2);
61  };
62  std::pair<typename std::vector<T>::iterator, typename std::vector<T>::iterator> itors =
64  T ret = ((T)(*(itors.first)) + (T)(*(itors.second))) / (T)2.;
65  return ret;
66 }
67 
68 #endif
tuple ret
prodAgent to be discontinued
T hsm_1d(std::vector< T > values)
Definition: hsm_1d.h:53
std::pair< typename std::vector< T >::iterator, typename std::vector< T >::iterator > hsm_1d_returning_iterators(std::vector< T > &values)
Definition: hsm_1d.h:11
string end
Definition: dataset.py:937
long double T
tuple size
Write out results.