Index: /fact/tools/rootmacros/fpeak_cdf.C
===================================================================
--- /fact/tools/rootmacros/fpeak_cdf.C	(revision 12378)
+++ /fact/tools/rootmacros/fpeak_cdf.C	(revision 12379)
@@ -37,4 +37,7 @@
 #include "DrsCalibration.h"
 
+#include "SpikeRemoval.h"
+#include "SpikeRemoval.C"
+
 bool breakout=false;
 
@@ -53,16 +56,8 @@
 
 vector<float> Ameas(FAD_MAX_SAMPLES);  // copy of the data (measured amplitude
-vector<float> N1mean(FAD_MAX_SAMPLES); // mean of the +1 -1 ch neighbors
 vector<float> Vcorr(FAD_MAX_SAMPLES);  // corrected Values
-vector<float> Vdiff(FAD_MAX_SAMPLES);  // numerical derivative
-
 vector<float> Vslide(FAD_MAX_SAMPLES);  // sliding average result
 vector<float> Vcfd(FAD_MAX_SAMPLES);    // CDF result
 vector<float> Vcfd2(FAD_MAX_SAMPLES);    // CDF result + 2nd sliding average
-
-
-//float getValue( int, int );
-void computeN1mean( int );
-void removeSpikes( int );
 
 // histograms
@@ -130,4 +125,7 @@
 
 	fits * datafile;
+	// Opens the raw data file and 'binds' the variables given as
+	// Parameters to the data file. So they are filled with
+	// raw data as soon as datafile->GetRow(int) is called.
 	NEvents = OpenDataFile( datafilename, &datafile,
 		AllPixelDataVector, StartCellVector, CurrentEventID,
@@ -177,9 +175,7 @@
 				RegionOfInterest, AllPixelDataVector, StartCellVector);
 
-			// operates on Ameas[] and writes to N1mean[]
-			computeN1mean( RegionOfInterest );
-
-			// operates on Ameas[] and N1mean[], then writes to Vcorr[]
-			removeSpikes( RegionOfInterest );
+			// finds spikes in the raw data, and interpolates the value
+			// spikes are: 1 or 2 slice wide, positive non physical artifacts
+			removeSpikes (Ameas, Vcorr);
 
 			// filter Vcorr with sliding average using FIR filter function
@@ -288,66 +284,4 @@
 }
 
-void removeSpikes(int Samples){
-
-    const float fract = 0.8;
-    float x, xp, xpp, x3p;
-
-    // assume that there are no spikes
-    for ( int i = 0; i <  Samples; i++) Vcorr[i] = Ameas[i];
-
-// find the spike and replace it by mean value of neighbors
-    for ( int i = 0; i < Samples; i++) {
-
-
-    x = Ameas[i] - N1mean[i];
-
-        if ( x < -5. ){ // a spike candidate
-            // check consistency with a single channel spike
-            xp = Ameas[i+1] - N1mean[i+1];
-            xpp = Ameas[i+2] - N1mean[i+2];
-            x3p = Ameas[i+3] - N1mean[i+3];
-
-
-            if ( Ameas[i+2] - ( Ameas[i] + Ameas[i+3] )/2. > 10. ){
-                Vcorr[i+1] = ( Ameas[i] + Ameas[i+3] )/2.;
-                Vcorr[i+2] = ( Ameas[i] + Ameas[i+3] )/2.;
-                i = i + 3; 
-            }
-            else{
-    
-                if ( ( xp > -2.*x*fract ) && ( xpp < -10. ) ){
-                    Vcorr[i+1] = N1mean[i+1];
-                    N1mean[i+2] = (Ameas[i+1] - Ameas[i+3] / 2.);
-                    i = i + 2;//do not care about the next sample it was the spike
-                }
-                // treatment for the end of the pipeline must be added !!!
-            }
-        }
-        else{
-             // do nothing
-        }
-    } // end of spike search and correction
-	for ( int i = 0; i < Samples; i++ ) debugHistos[ Vcorr_ ].SetBinContent( i, Vcorr[i] );
-}
-
-void computeN1mean( int Samples ){
-// compute the mean of the left and right neighbors of a channel
-
-    for( int i = 2; i < Samples - 2; i++){
-/*        if (i == 0){ // use right sample es mean
-            N1mean[i] = Ameas[i+1];
-        }
-        else if ( i == Samples-1 ){ //use left sample as mean
-            N1mean[i] = Ameas[i-1];
-        }
-        else{
-            N1mean[i] = ( Ameas[i-1] + Ameas[i+1] ) / 2.;
-        }
-*/
-        N1mean[i] = ( Ameas[i-1] + Ameas[i+1] ) / 2.;
-    }
-} // end of computeN1mean computation
-
-
 // booking and parameter settings for all histos
 void BookHistos( ){
