CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/DQM/SiStripCommissioningSources/src/Averages.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripCommissioningSources/interface/Averages.h"
00002 #include <iostream>
00003 #include <math.h>
00004 #include <algorithm>
00005 
00006 using namespace std;
00007 
00008 // ----------------------------------------------------------------------------
00009 // 
00010 Averages::Averages() 
00011   : n_(0),
00012     s_(0.),
00013     x_(0.),
00014     xx_(0.),
00015     median_(),
00016     mode_()
00017 {;}
00018 
00019 // ----------------------------------------------------------------------------
00020 // 
00021 void Averages::add( const uint32_t& x,
00022                     const uint32_t& e ) {
00023   mode_[x]++;
00024   add( static_cast<float>(x),
00025        static_cast<float>(e) );
00026 }
00027 // ----------------------------------------------------------------------------
00028 // 
00029 void Averages::add( const uint32_t& x ) {
00030   mode_[x]++;
00031   add( static_cast<float>(x), -1. );
00032 }
00033 
00034 // ----------------------------------------------------------------------------
00035 // 
00036 void Averages::add( const float& x,
00037                     const float& e ) {
00038   n_++;
00039   if ( e > 0. ) { 
00040     float wt = 1. / sqrt(e); 
00041     s_ += wt;
00042     x_ += x*wt;
00043     xx_ += x*x*wt;
00044   } else {
00045     s_++;
00046     x_ += x;
00047     xx_ += x*x;
00048   }
00049   median_.push_back(x);
00050 }
00051 
00052 // ----------------------------------------------------------------------------
00053 // 
00054 void Averages::add( const float& x ) { 
00055   add( x, -1. );
00056 }
00057 
00058 // ----------------------------------------------------------------------------
00059 // 
00060 void Averages::calc( Params& params ) {
00061   params.num_ = n_;
00062   if ( s_ > 0. ) { 
00063     float m = x_/s_;
00064     float t = xx_/s_ - m*m;
00065     if ( t > 0. ) { t = sqrt(t); } 
00066     else { t = 0.; }
00067     params.mean_ = m;
00068     params.rms_ = t;
00069     params.weight_ = s_;
00070   }
00071   if ( !median_.empty() ) {
00072     sort( median_.begin(), median_.end() );
00073     uint16_t index = median_.size()%2 ? median_.size()/2 : median_.size()/2-1;
00074     params.median_ = median_[index];
00075     params.max_ = median_.back();
00076     params.min_ = median_.front();
00077   }
00078   if ( !mode_.empty() ) {
00079     uint32_t max = 0;
00080     std::map<uint32_t,uint32_t>::const_iterator imap = mode_.begin();
00081     for ( ; imap != mode_.end(); imap++ ) {
00082       if ( imap->second > max ) { 
00083         max = imap->second;
00084         params.mode_ = imap->first;
00085       }
00086     }    
00087   }
00088   
00089 }