Index: trunk/ArchTOIPipe/ProcWSophya/simtoipr.cc
===================================================================
--- trunk/ArchTOIPipe/ProcWSophya/simtoipr.cc	(revision 1476)
+++ trunk/ArchTOIPipe/ProcWSophya/simtoipr.cc	(revision 1478)
@@ -8,17 +8,14 @@
 SimpleDeglitcher::SimpleDeglitcher(int wsz, double ns, int maxnpt)
 {
-  if (wsz < 5) wsz = 5;
-  if (wsz%2 == 0) wsz++;
-  if (maxnpt > wsz) maxnpt = wsz;
+  SetWSize(wsz);
+  SetDetectionParam(ns, ns/2., maxnpt);
+  SetRange(-9.e39, 9.e39);
+  RepBadSamples(true, true);
+
+  totnscount = glnscount = glcount = out_range_nscount = 0;
+  deglitchdone = false;
 
   cout << "SimpleDeglitcher::SimpleDeglitcher() WSize= " << wsz 
        << " nSig=" << ns << " maxNPt=" << maxnpt << endl;
-  
-  wsize = wsz;
-  nsig = ns;
-  maxpoints = maxnpt;
-  totnscount = glnscount = glcount = out_range_nscount = 0;
-  deglitchdone = false;
-  SetRange(-9.e39, 9.e39);
 }
 
@@ -27,4 +24,17 @@
 }
 
+void SimpleDeglitcher::SetDetectionParam(double ns, double ns2, int maxnpt, int wszrec)
+{
+  nsig = (ns > 0.01) ? ns : 1.;
+  nsig2 = (ns2 > 0.01) ? ns2 : nsig/2.;
+  maxpoints = ((maxnpt > 0) && (maxnpt <= wsize)) ? maxnpt : 5;
+  wrecsize = ((wszrec > 0) && (wszrec <= wsize)) ? wszrec : 2*maxpoints;
+}
+
+inline char * _Bool2YesNo(bool fg) 
+{
+  if (fg) return "YES" ;
+  else return "NO" ;
+}
 
 void SimpleDeglitcher::PrintStatus(ostream & os)
@@ -33,5 +43,9 @@
      << " SimpleDeglitcher::PrintStatus() - WindowSize=" << WSize() 
      << "  NbSigmas=" << NbSigmas() << " MaxPoints=" << MaxPoints() << endl;
-  os << "  Range_Min= " << range_min << " Range_Max= " << range_max << endl;
+  os << "  NbSigmas2=" << NbSigmas2() << "  WRecSize= " << WRecSize() 
+     << "  Range_Min= " << range_min << " Range_Max= " << range_max << endl;
+  os << "  RepOutOfRangeSamples: " << _Bool2YesNo(rec_out_range_samples) 
+     << "  RepGlitchSamples: " << _Bool2YesNo(rec_gl_samples) 
+     << "  UseWRec: " << _Bool2YesNo(rec_use_wrec) << endl;
   TOIProcessor::PrintStatus(os);
   if (deglitchdone) os << " Deglitching performed " << endl;
@@ -57,4 +71,7 @@
 }
 
+
+#define FG_OUTOFRANGE 1
+#define FG_GLITCH 2
 void SimpleDeglitcher::run() {
 
@@ -88,7 +105,8 @@
     Timer tm("SimpleDeglitcher::run()");
     Vector vin(wsize);
-
-    int wrec = maxpoints*2;
-    Vector vrec(wrec);
+    Vector vas(wsize);
+
+    int wrecsize = maxpoints*2;
+    Vector vrec(wrecsize);
 
     TVector<int_8> vfg(wsize);
@@ -119,6 +137,12 @@
       vfg(k) = 0;
     } 
-
-    for(k=0; k<wrec; k++) {
+    for(k=0; k<wsize; k++) {
+      vas(k) = mean;
+      if ( vfg(k) != 0) continue;
+      if ( (vin(k) < range_min) || (vin(k) > range_max) ) continue;
+      vas(k) = vin(k);
+    }
+
+    for(k=0; k<wrecsize; k++) {
       if ( (vin(k) < range_min) || (vin(k) > range_max) ) vrec(k)=mean;
       else vrec(k)=vin(k);    
@@ -144,14 +168,16 @@
       // Calcul mean-sigma
       if (knext<=kfin)  {
-	valsub = vin(knext%wsize);
+	valsub = vas(knext%wsize);
 	getData(0, knext+snb, vin(knext%wsize), vfg(knext%wsize));
 	valadd = vin(knext%wsize);
-	if ( (valadd < range_min) || (valadd > range_max) ) {
-	  valadd = mean;
+	if ( vfg(knext%wsize) || 
+	     (valadd < range_min) || (valadd > range_max) ) {
+	  vas(knext%wsize) = valadd = mean;
 	  fgokcur = false;
 	}
-	else fgokcur = true;
-	if ( (valsub < range_min) || (valsub > range_max) ) 
-	  valsub = mean;
+	else { 
+	  vas(knext%wsize) = valadd = vin(knext%wsize);
+	  fgokcur = true; 
+	}
 	if (!fgokdebut && fgokcur) {
 	  s += valadd;
@@ -185,7 +211,9 @@
 	putData(3, k+snb, vin(k%wsize), vfg(k%wsize));
 
+      valcur = vin(k%wsize);
       if ( (valcur < range_min) || (valcur > range_max) ) {
-	vin(k%wsize) = valcur = mean;
-	vfg(k%wsize) |= 2;
+	valcur = (rec_use_wrec) ? vrec.Sum()/wrecsize : mean;
+	if (rec_out_range_samples) vin(k%wsize) = valcur;
+	vfg(k%wsize) |= FG_OUTOFRANGE;
 	out_range_nscount++;
       }
@@ -204,6 +232,5 @@
 //            }
 
-      double curnsig = nsig;
-      if (fgglitch)  curnsig /= 2.;
+      double curnsig = (fgglitch) ? nsig2 : nsig;
 	
       if (valcur < mean+curnsig*sigma) {  // inferieur au seuil
@@ -211,12 +238,20 @@
 	  if (k-kgl < maxpoints) {   // On vient de detecter un glitch
 	    glcount++;
-	    double recval = vrec.Sum()/wrec;
-	    for(ii=kgl; ii<k; ii++) {
-	      putData(0, ii+snb, recval, vfg(ii%wsize)|5);
-	      glnscount++; 
+	    if (rec_gl_samples) { // On change la valeur des samples
+	      double recval = (rec_use_wrec) ? vrec.Sum()/wrecsize : mean;
+	      for(ii=kgl; ii<k; ii++) {
+		putData(0, ii+snb, recval, vfg(ii%wsize)|FG_GLITCH);
+		glnscount++; 
+	      }
+	    }
+	    else {   // On ne fait que flagger les echantillons
+	      for(ii=kgl; ii<k; ii++) {
+		putData(0, ii+snb, vin(ii%wsize), vfg(ii%wsize)|FG_GLITCH);
+		glnscount++; 
+	      }
 	    }
 	    lastput = snb+k-1;
-	  }
-	  else {
+	  }  // - Fin de detection de glitch
+	  else {  // Trop long - ce n'est pas un glitch ...
 	    for(ii=kgl; ii<k; ii++) {
 	      putData(0, ii+snb, vin(ii%wsize), vfg(ii%wsize));
@@ -225,8 +260,8 @@
 	  } 
 	}
-	putData(0, k+snb, valcur, 0);
+	putData(0, k+snb, vin(k%wsize), vfg(k%wsize));
 	lastput = snb+k;
 	kgl = -1;  fgglitch = false;
-	vrec(k%wrec) = lastvalok = valcur;
+	vrec(k%wrecsize) = lastvalok = valcur;
       }
       else {  // Superieur au seuil
@@ -237,5 +272,5 @@
 	    lastput = snb+k;
 	    fgglitch = false;  
-	    vrec(k%wrec) = lastvalok = valcur;	    
+	    vrec(k%wrecsize) = lastvalok = valcur;	    
 	  }
 	}
@@ -245,8 +280,8 @@
 	  }
 	  else { // On est toujours dans une serie > seuil
-	    putData(0, k+snb, valcur, fgcur);
-	    lastput = snb+k;
+	    putData(0, k+snb, vin(k%wsize), vfg(k%wsize));
+	    lastput = snb+k;   lastvalok = valcur;
 	  }
-	  vrec(k%wrec) = lastvalok;	    
+	  vrec(k%wrecsize) = lastvalok;	    
 	}
       }
