CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
median.h
Go to the documentation of this file.
1 #ifndef CommonTools_Statistics_median_h
2 #define CommonTools_Statistics_median_h
3 
4 #include <vector>
5 #include <algorithm>
6 #include <cmath>
7 //#include "CommonDet/DetUtilities/interface/DetExceptions.h"
8 
9 //using namespace std;
10 
15 template <class T>
16 T median(std::vector<T> values) {
17  switch (values.size()) {
18  case 0:
19  // throw LogicError ("Median of empty vector");
20  case 1:
21  return values[0];
22  case 2:
23  return (values[0] + values[1]) / 2.;
24  };
25  T ret;
26  const int size = values.size();
27  const int half = (int)ceil(size / 2.);
28  partial_sort(values.begin(), values.begin() + half + 1, values.end());
29  if (size & 1) {
30  ret = values[half - 1];
31  } else {
32  ret = (values[half - 1] + values[half]) / 2.;
33  };
34  return ret;
35 }
36 #endif
constexpr int32_t ceil(float num)
tuple ret
prodAgent to be discontinued
T median(std::vector< T > values)
Definition: median.h:16
long double T
tuple size
Write out results.