CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/DQM/SiStripCommissioningAnalysis/src/Utility.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripCommissioningAnalysis/src/Utility.h"
00002 #include <algorithm>
00003 
00004 // ----------------------------------------------------------------------------
00005 // 
00006 sistrip::LinearFit::LinearFit() 
00007   : x_(),
00008     y_(),
00009     e_(),
00010     ss_(0.),
00011     sx_(0.),
00012     sy_(0.) 
00013 {;}
00014 
00015 // ----------------------------------------------------------------------------
00016 // 
00017 void sistrip::LinearFit::add( const float& x,
00018                               const float& y ) {
00019   float e = 1.; // default
00020   x_.push_back(x);
00021   y_.push_back(y);
00022   e_.push_back(e);
00023   float wt = 1. / sqrt(e); 
00024   ss_ += wt;
00025   sx_ += x*wt;
00026   sy_ += y*wt;
00027 }
00028 
00029 // ----------------------------------------------------------------------------
00030 // 
00031 void sistrip::LinearFit::add( const float& x,
00032                               const float& y,
00033                               const float& e ) {
00034   if ( e > 0. ) { 
00035     x_.push_back(x);
00036     y_.push_back(y);
00037     e_.push_back(e);
00038     float wt = 1. / sqrt(e); 
00039     ss_ += wt;
00040     sx_ += x*wt;
00041     sy_ += y*wt;
00042   } 
00043 }
00044 
00045 // ----------------------------------------------------------------------------
00046 // 
00047 void sistrip::LinearFit::fit( Params& params ) {
00048 
00049   float s2 = 0.;
00050   float b = 0;
00051   for ( uint16_t i = 0; i < x_.size(); i++ ) {
00052     float t = ( x_[i] - sx_/ss_ ) / e_[i]; 
00053     s2 += t*t;
00054     b += t * y_[i] / e_[i];
00055   }
00056   
00057   // Set parameters
00058   params.n_ = x_.size();
00059   params.b_ = b / s2;
00060   params.a_ = ( sy_ - sx_ * params.b_ ) / ss_;
00061   params.erra_ = sqrt( ( 1. + (sx_*sx_) / (ss_*s2) ) / ss_ );
00062   params.errb_ = sqrt( 1. / s2 );
00063   
00064   /*
00065     params.chi2_ = 0.;
00066     *q=1.0;
00067     if (mwt == 0) {
00068     for (i=1;i<=ndata;i++)
00069     *chi2 += SQR(y[i]-(*a)-(*b)*x[i]);
00070     sigdat=sqrt((*chi2)/(ndata-2));
00071     *sigb *= sigdat;
00072     */    
00073   
00074 }
00075 
00076 // ----------------------------------------------------------------------------
00077 // 
00078 sistrip::MeanAndStdDev::MeanAndStdDev() 
00079   : s_(0.),
00080     x_(0.),
00081     xx_(0.),
00082     vec_()
00083 {;}
00084 
00085 // ----------------------------------------------------------------------------
00086 // 
00087 void sistrip::MeanAndStdDev::add( const float& x,
00088                          const float& e ) {
00089   if ( e > 0. ) { 
00090     float wt = 1. / sqrt(e); 
00091     s_ += wt;
00092     x_ += x*wt;
00093     xx_ += x*x*wt;
00094   } else {
00095     s_++;
00096     x_ += x;
00097     xx_ += x*x;
00098   }
00099   vec_.push_back(x);
00100 }
00101 
00102 // ----------------------------------------------------------------------------
00103 // 
00104 void sistrip::MeanAndStdDev::fit( Params& params ) {
00105   if ( s_ > 0. ) { 
00106     float m = x_/s_;
00107     float t = xx_/s_ - m*m;
00108     if ( t > 0. ) { t = sqrt(t); } 
00109     else { t = 0.; }
00110     params.mean_ = m;
00111     params.rms_  = t;
00112   }
00113   if ( !vec_.empty() ) {
00114     sort( vec_.begin(), vec_.end() );
00115     uint16_t index = vec_.size()%2 ? vec_.size()/2 : vec_.size()/2-1;
00116     params.median_ = vec_[index];
00117   }      
00118 }