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
class-composition.H
H
Definition: class-composition.py:31
Quantile::array
std::vector< pair > array
Definition: Quantile.h:10
mps_fire.i
i
Definition: mps_fire.py:428
Quantile::fromHead
pair fromHead(const double frac) const
Definition: Quantile.h:26
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
Quantile::fromTail
pair fromTail(const double frac) const
Definition: Quantile.h:27
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
Quantile::operator[]
pair operator[](const double frac) const
Definition: Quantile.h:13
DivergingColor.frac
float frac
Definition: DivergingColor.py:175
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
mps_fire.end
end
Definition: mps_fire.py:242
Quantile::tail
array tail
Definition: Quantile.h:72
h
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
Quantile::head
array head
Definition: Quantile.h:72
Quantile::N
const int N
Definition: Quantile.h:70
Quantile::pair
std::pair< double, double > pair
Definition: Quantile.h:9
newFWLiteAna.bin
bin
Definition: newFWLiteAna.py:161
T
long double T
Definition: Basic3DVectorLD.h:48
Quantile::Total
const double Total
Definition: Quantile.h:71
Quantile::Quantile
Quantile(const TH1 *h)
Definition: Quantile.h:15
Quantile::next
T next(T bin, T end) const
Definition: Quantile.h:63
Quantile::prev
T prev(T bin, T begin) const
Definition: Quantile.h:55
Quantile::operator()
pair operator()(const double frac) const
Definition: Quantile.h:12
Quantile::calculateQ
pair calculateQ(const double frac, const bool fromHead) const
Definition: Quantile.h:30
Quantile
Definition: Quantile.h:8