Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:35

0001 #include "DQM/SiStripCommissioningSources/interface/Averages.h"
0002 #include <iostream>
0003 #include <cmath>
0004 #include <algorithm>
0005 
0006 using namespace std;
0007 
0008 // ----------------------------------------------------------------------------
0009 //
0010 Averages::Averages() : n_(0), s_(0.), x_(0.), xx_(0.), median_(), mode_() { ; }
0011 
0012 // ----------------------------------------------------------------------------
0013 //
0014 void Averages::add(const uint32_t& x, const uint32_t& e) {
0015   mode_[x]++;
0016   add(static_cast<float>(x), static_cast<float>(e));
0017 }
0018 // ----------------------------------------------------------------------------
0019 //
0020 void Averages::add(const uint32_t& x) {
0021   mode_[x]++;
0022   add(static_cast<float>(x), -1.);
0023 }
0024 
0025 // ----------------------------------------------------------------------------
0026 //
0027 void Averages::add(const float& x, const float& e) {
0028   n_++;
0029   if (e > 0.) {
0030     float wt = 1. / sqrt(e);
0031     s_ += wt;
0032     x_ += x * wt;
0033     xx_ += x * x * wt;
0034   } else {
0035     s_++;
0036     x_ += x;
0037     xx_ += x * x;
0038   }
0039   median_.push_back(x);
0040 }
0041 
0042 // ----------------------------------------------------------------------------
0043 //
0044 void Averages::add(const float& x) { add(x, -1.); }
0045 
0046 // ----------------------------------------------------------------------------
0047 //
0048 void Averages::calc(Params& params) {
0049   params.num_ = n_;
0050   if (s_ > 0.) {
0051     float m = x_ / s_;
0052     float t = xx_ / s_ - m * m;
0053     if (t > 0.) {
0054       t = sqrt(t);
0055     } else {
0056       t = 0.;
0057     }
0058     params.mean_ = m;
0059     params.rms_ = t;
0060     params.weight_ = s_;
0061   }
0062   if (!median_.empty()) {
0063     sort(median_.begin(), median_.end());
0064     uint16_t index = median_.size() % 2 ? median_.size() / 2 : median_.size() / 2 - 1;
0065     params.median_ = median_[index];
0066     params.max_ = median_.back();
0067     params.min_ = median_.front();
0068   }
0069   if (!mode_.empty()) {
0070     uint32_t max = 0;
0071     std::map<uint32_t, uint32_t>::const_iterator imap = mode_.begin();
0072     for (; imap != mode_.end(); imap++) {
0073       if (imap->second > max) {
0074         max = imap->second;
0075         params.mode_ = imap->first;
0076       }
0077     }
0078   }
0079 }