CMS 3D CMS Logo

Averages.cc

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

Generated on Tue Jun 9 17:33:33 2009 for CMSSW by  doxygen 1.5.4