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. );
00026 double anAmplitude = 0.;
00027
00028
00029 const unsigned int meanPosition = 256 + offset;
00030
00031 const unsigned int halfWindowSize = 33;
00032
00033
00034
00035
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
00045
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
00058 const double noise = sqrt( 1. / ( nStrips - 1 ) * ( sum2 - pow( sum1, 2 ) / nStrips ) );
00059
00060
00061 if( fabs( sum1 ) < 1.e-3 ) {
00062 std:: cout << " [LASPeakFinder::FindPeakIn] ** WARNING: Empty profile." << std::endl;
00063 return false;
00064 }
00065
00066
00067 if( largestAmplitude.second < 10. * noise ) {
00068 std::cout << " [LASPeakFinder::FindPeakIn] ** WARNING: No reasonably large peak." << std::endl;
00069 return false;
00070 }
00071
00072
00073 fitFunction->SetParameter( 0, largestAmplitude.second );
00074 fitFunction->SetParameter( 1, largestAmplitude.first );
00075 fitFunction->SetParameter( 2, 3. );
00076
00077
00078 fitFunction->SetParLimits( 0, largestAmplitude.second * 0.3, largestAmplitude.second * 1.8 );
00079 fitFunction->SetParLimits( 1, largestAmplitude.first - 12, largestAmplitude.first + 12 );
00080 fitFunction->SetParLimits( 2, 0.5, 8. );
00081
00082
00083
00084 histogram->Fit( fitFunction, "QWB", "", largestAmplitude.first - 12, largestAmplitude.first + 12 );
00085
00086
00087
00088
00089
00090 result.first = fitFunction->GetParameter( 1 );
00091 result.second = fitFunction->GetParError( 1 );
00092
00093 return true;
00094
00095 }