CMS 3D CMS Logo

Quantile.h
Go to the documentation of this file.
1 #ifndef QUANTILE_H
2 #define QUANTILE_H
3 
4 #include "TH1.h"
5 #include <vector>
6 #include <iostream>
7 
8 struct Quantile {
9  typedef std::pair<double, double> pair;
10  typedef std::vector<pair> array;
11 
12  pair operator()(const double frac) const { return fromHead(frac); }
13  pair operator[](const double frac) const { return fromTail(frac); }
14 
15  Quantile(const TH1* h) : N(1 + h->GetNbinsX()), Total(h->Integral(0, N)) {
16  for (int i = 0; i < N; i++) {
17  const double H = h->GetBinContent(i) + (!head.empty() ? head.back().second : 0);
18  const double T = h->GetBinContent(N - i) + (!tail.empty() ? tail.back().second : 0);
19  if (H)
20  head.push_back(pair(h->GetBinWidth(i) + h->GetBinLowEdge(i), H));
21  if (T)
22  tail.push_back(pair(h->GetBinLowEdge(N - i), T));
23  }
24  }
25 
26  pair fromHead(const double frac) const { return calculateQ(frac, true); }
27  pair fromTail(const double frac) const { return calculateQ(frac, false); }
28 
29 private:
30  pair calculateQ(const double frac, const bool fromHead) const {
31  const double f = frac < 0.5 ? frac : 1 - frac;
32  array::const_iterator begin(((frac < 0.5) == fromHead) ? head.begin() : tail.begin()),
33  end(((frac < 0.5) == fromHead) ? head.end() : tail.end()), bin(begin);
34 
35  while (bin->second < f * Total)
36  bin++;
37  //dk if( bin==begin ) return pair(sqrt(-1),0);
38  if (bin == begin)
39  return pair(-1, 0);
40 
41  array::const_iterator binNext(next(bin, end)), binPrev(prev(bin, begin)), binPPrev(prev(binPrev, begin));
42 
43  const double DX(binNext->first - binPPrev->first), DY((binNext->second - binPPrev->second) / Total),
44 
45  dX(bin->first - binPrev->first), dY((bin->second - binPrev->second) / Total),
46 
47  avgX((bin->first + binPrev->first) / 2), avgY((bin->second + binPrev->second) / (2 * Total)),
48 
49  x_q(avgX + dX / dY * (f - avgY)), xerr_q(std::max(fabs(DX / DY), fabs(dX / dY)) * sqrt(f * (1 - f) / Total));
50 
51  return pair(x_q, xerr_q);
52  }
53 
54  template <class T>
55  T prev(T bin, T begin) const {
56  T binPrev = bin;
57  while (binPrev > begin && (binPrev - 1)->second == (bin - 1)->second)
58  binPrev--;
59  return binPrev;
60  }
61 
62  template <class T>
63  T next(T bin, T end) const {
64  T binNext = bin;
65  while (binNext < end - 1 && (++binNext)->second == bin->second)
66  ;
67  return binNext;
68  }
69 
70  const int N;
71  const double Total;
73 };
74 #endif
T next(T bin, T end) const
Definition: Quantile.h:63
pair operator()(const double frac) const
Definition: Quantile.h:12
U second(std::pair< T, U > const &p)
pair fromTail(const double frac) const
Definition: Quantile.h:27
Quantile(const TH1 *h)
Definition: Quantile.h:15
T sqrt(T t)
Definition: SSEVec.h:23
array head
Definition: Quantile.h:72
double f[11][100]
T prev(T bin, T begin) const
Definition: Quantile.h:55
const double Total
Definition: Quantile.h:71
pair fromHead(const double frac) const
Definition: Quantile.h:26
float dX(const MatchPair &match)
pair operator[](const double frac) const
Definition: Quantile.h:13
array tail
Definition: Quantile.h:72
const int N
Definition: Quantile.h:70
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
long double T
pair calculateQ(const double frac, const bool fromHead) const
Definition: Quantile.h:30
float dY(const MatchPair &match)
std::pair< double, double > pair
Definition: Quantile.h:9
std::vector< pair > array
Definition: Quantile.h:10