diff --git a/Analysis/src/WFAnalysis.cpp b/Analysis/src/WFAnalysis.cpp index ec46708dd2e2a68f446bd5a8d9940c8de4ebe246..6af46304b4f784cca014b33df01997740fc4a8c9 100755 --- a/Analysis/src/WFAnalysis.cpp +++ b/Analysis/src/WFAnalysis.cpp @@ -276,26 +276,41 @@ void WFAnalysis::MedianFilter( Channel* Ch ){ */ void WFAnalysis::GetDifferential( Channel* Ch ){ + + //Pad the front of the vector with 0s for(int bin = 0; bin < Ch->diffSmoothing; bin ++) Ch->vDiff.push_back( 0.0 ); - // Loop over histogram - for( unsigned int bin = Ch->diffSmoothing; bin < Ch->WF_histo->GetNbinsX() - Ch->diffSmoothing ; bin++ ){ - //decalare temp variable to store the sum before and after the i data point - double sum_before = 0; - double sum_after = 0; - //calculate the sum before and after the i data point - for (int i = 0; i < Ch->diffSmoothing; i++ ){ - sum_before += Ch->WF.at( bin - i ); - sum_after += Ch->WF.at( bin + i ); - } + + + //decalare temp variable to store the sum before and after the i data point + double sum_before = 0; + double sum_after = 0; + + // Generate 2 sliding window, sum_before and sum_after + // Calculate the sum before and after the start data point + for (int i = 1; i <= Ch->diffSmoothing; i++ ){ + sum_before += Ch->WF.at( Ch->diffSmoothing - i ); + sum_after += Ch->WF.at( Ch->diffSmoothing + i ); + } + + // Loop over histogram + for( unsigned int bin = Ch->diffSmoothing; bin < Ch->WF_histo->GetNbinsX() - Ch->diffSmoothing - 1 ; bin++ ){ //set the bin to the calculated derivative value Ch->vDiff.push_back( sum_after - sum_before); Ch->FirstDerivative->SetBinContent(bin,(sum_after - sum_before)); + // Move the sum_before window forward + sum_before += Ch->WF.at( bin ); + sum_before -= Ch->WF.at( bin - Ch->diffSmoothing ); + + // Move the sum_after window forward + sum_after -= Ch->WF.at( bin + 1); + sum_after += Ch->WF.at( bin + Ch->diffSmoothing + 1); + }//end derivative loop //Pad the back of the vector with 0s - for(int bin = 0; bin < Ch->diffSmoothing; bin ++) Ch->vDiff.push_back( 0.0 ); + for(int bin = 0; bin <= Ch->diffSmoothing; bin ++) Ch->vDiff.push_back( 0.0 ); } /** @brief GetRMS method for WFAnalysis