CMS 3D CMS Logo

LASPeakFinder.cc

Go to the documentation of this file.
00001 
00002 #include "Alignment/LaserAlignment/src/LASPeakFinder.h"
00003 
00004 
00008 LASPeakFinder::LASPeakFinder() {
00009 }
00010 
00011 
00012 
00013 
00014 
00020 bool LASPeakFinder::FindPeakIn( const LASModuleProfile& aProfile, std::pair<double,double>& result, const int offset ) {
00021 
00022   TH1D* histogram = new TH1D( "bufferHistogram", "bufferHistogram", 512, 0, 512 );
00023   TF1* fitFunction = new TF1( "fitFunction", "gaus" );
00024 
00025   std::pair<int,double> largestAmplitude( 0, 0. ); // strip, amplitude
00026   double anAmplitude = 0.;
00027 
00028   // expected beam position (in strips)
00029   const unsigned int meanPosition = 256 + offset;
00030   // backplane "alignment hole" approx. half size (in strips)
00031   const unsigned int halfWindowSize = 33;
00032 
00033   // loop over the strips in the "alignment hole"
00034   // to fill the histogram
00035   // and determine fit parameter estimates
00036   for( unsigned int strip = meanPosition - halfWindowSize; strip < meanPosition + halfWindowSize; ++strip ) {
00037     anAmplitude = aProfile.GetValue( strip );
00038     histogram->SetBinContent( 1 + strip, anAmplitude );
00039     if( anAmplitude > largestAmplitude.second ) {
00040       largestAmplitude.first = strip; largestAmplitude.second = anAmplitude;
00041     }
00042   }
00043 
00044   // loop outside the "alignment hole"
00045   // to determine the noise level = sqrt(variance)
00046   double sum1 = 0., sum2 = 0.;
00047   int nStrips = 0;
00048   for( unsigned int strip = 0; strip < 512; ++strip ) {
00049     if( strip < meanPosition - halfWindowSize || strip > meanPosition + halfWindowSize ) {
00050       anAmplitude = aProfile.GetValue( strip );
00051       sum1 += anAmplitude;
00052       sum2 += pow( anAmplitude, 2 );
00053       nStrips++;
00054     }
00055   }
00056 
00057   // noise as sqrt of the amplitude variance
00058   const double noise = sqrt( 1. / ( nStrips - 1 ) * ( sum2 - pow( sum1, 2 ) / nStrips ) );
00059 
00060   // empty profile?
00061   if( fabs( sum1 ) < 1.e-3 ) {
00062     std:: cout << " [LASPeakFinder::FindPeakIn] ** WARNING: Empty profile." << std::endl;
00063     return false;
00064   }
00065 
00066   // no reasonable peak?
00067   if( largestAmplitude.second < 10. * noise ) {
00068     std::cout << " [LASPeakFinder::FindPeakIn] ** WARNING: No reasonably large peak." << std::endl;
00069     return false;
00070   }
00071 
00072   // prepare fit function: starting values..
00073   fitFunction->SetParameter( 0, largestAmplitude.second ); // amp
00074   fitFunction->SetParameter( 1, largestAmplitude.first ); // mean
00075   fitFunction->SetParameter( 2, 3. ); // width
00076 
00077   // ..and parameter limits
00078   fitFunction->SetParLimits( 0, largestAmplitude.second * 0.3, largestAmplitude.second * 1.8 ); // amp of the order of the peak height
00079   fitFunction->SetParLimits( 1, largestAmplitude.first - 12, largestAmplitude.first + 12 ); // mean around the peak maximum
00080   fitFunction->SetParLimits( 2, 0.5, 8. ); // reasonable width
00081   
00082 
00083   // and go
00084   histogram->Fit( fitFunction, "QWB", "", largestAmplitude.first - 12, largestAmplitude.first + 12 );
00085   //  std::cout << "MEAN: " << fitFunction->GetParameter( 1 ) << "±" << fitFunction->GetParError( 1 ) << ", AMP: " 
00086   //        << fitFunction->GetParameter( 0 ) << ", WIDTH: " << fitFunction->GetParameter( 2 )
00087   //        << ", NOISE: " << noise << std::endl; /////////////////////////////////
00088 
00089   // prepare output
00090   result.first = fitFunction->GetParameter( 1 );
00091   result.second = fitFunction->GetParError( 1 );
00092 
00093   return true;
00094 
00095 }

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