CMS 3D CMS Logo

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