CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Averages.cc
Go to the documentation of this file.
2 #include <iostream>
3 #include <cmath>
4 #include <algorithm>
5 
6 using namespace std;
7 
8 // ----------------------------------------------------------------------------
9 //
10 Averages::Averages() : n_(0), s_(0.), x_(0.), xx_(0.), median_(), mode_() { ; }
11 
12 // ----------------------------------------------------------------------------
13 //
14 void Averages::add(const uint32_t& x, const uint32_t& e) {
15  mode_[x]++;
16  add(static_cast<float>(x), static_cast<float>(e));
17 }
18 // ----------------------------------------------------------------------------
19 //
20 void Averages::add(const uint32_t& x) {
21  mode_[x]++;
22  add(static_cast<float>(x), -1.);
23 }
24 
25 // ----------------------------------------------------------------------------
26 //
27 void Averages::add(const float& x, const float& e) {
28  n_++;
29  if (e > 0.) {
30  float wt = 1. / sqrt(e);
31  s_ += wt;
32  x_ += x * wt;
33  xx_ += x * x * wt;
34  } else {
35  s_++;
36  x_ += x;
37  xx_ += x * x;
38  }
39  median_.push_back(x);
40 }
41 
42 // ----------------------------------------------------------------------------
43 //
44 void Averages::add(const float& x) { add(x, -1.); }
45 
46 // ----------------------------------------------------------------------------
47 //
49  params.num_ = n_;
50  if (s_ > 0.) {
51  float m = x_ / s_;
52  float t = xx_ / s_ - m * m;
53  if (t > 0.) {
54  t = sqrt(t);
55  } else {
56  t = 0.;
57  }
58  params.mean_ = m;
59  params.rms_ = t;
60  params.weight_ = s_;
61  }
62  if (!median_.empty()) {
63  sort(median_.begin(), median_.end());
64  uint16_t index = median_.size() % 2 ? median_.size() / 2 : median_.size() / 2 - 1;
65  params.median_ = median_[index];
66  params.max_ = median_.back();
67  params.min_ = median_.front();
68  }
69  if (!mode_.empty()) {
70  uint32_t max = 0;
71  std::map<uint32_t, uint32_t>::const_iterator imap = mode_.begin();
72  for (; imap != mode_.end(); imap++) {
73  if (imap->second > max) {
74  max = imap->second;
75  params.mode_ = imap->first;
76  }
77  }
78  }
79 }
T sqrt(T t)
Definition: SSEVec.h:19
void add(const float &value, const float &weight)
uint16_t const *__restrict__ x
Definition: gpuClustering.h:43
void calc(Params &)
Definition: Averages.cc:48
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
uint32_t num_
Definition: Averages.h:24
Averages()
Definition: Averages.cc:10