Index: trunk/ArchTOIPipe/ProcWSophya/simtoipr.cc
===================================================================
--- trunk/ArchTOIPipe/ProcWSophya/simtoipr.cc	(revision 1464)
+++ trunk/ArchTOIPipe/ProcWSophya/simtoipr.cc	(revision 1467)
@@ -87,48 +87,93 @@
   try {
     Timer tm("SimpleDeglitcher::run()");
+    Vector vin(wsize);
+
+    int wrec = maxpoints*2;
+    Vector vrec(wrec);
+
+    TVector<int_8> vfg(wsize);
+    int wsz2 = wsize/2;
     // Le debut 
-    Vector vin(wsize);
-    TVector<int_8> vfg(wsize);
     int k;
-    for(k=0; k<wsize; k++) 
+    for(k=0; k<wsz2; k++) 
       getData(0, k+snb, vin(k), vfg(k));
 
-    double s = vin.Sum();
-    double mean = s/wsize;
-    for(k=0; k<wsize; k++) {
-      if ( (vin(k) < range_min) || (vin(k) > range_max) ) {
-	vin(k) = mean;
-	vfg(k) |= 2;
-	out_range_nscount++;
-      }
-    }
-    s = vin.Sum();
-    mean = s/wsize;
-
-    double s2 = vin.SumX2();
-    double sigma = sqrt(s2/wsize-mean*mean);
+    int nokdebut = 0.;
+    double s = 0.;    
+    double mean = 0.;
+    double s2 = 0.;
+    double sigma = 0.;
+    for(k=0; k<wsz2; k++) {
+      if ( vfg(k) != 0) continue;
+      if ( (vin(k) < range_min) || (vin(k) > range_max) ) continue;
+      s += vin(k);
+      s2 += vin(k)*vin(k);
+      nokdebut++;
+    }
+    if (nokdebut > 0) {
+      mean = s/nokdebut;
+      if (nokdebut > 1)  sigma = sqrt(s2/nokdebut-mean*mean);
+    }
+    for(k=wsz2; k<wsize; k++) {
+      vin(k) = mean;
+      vfg(k) = 0;
+    } 
+
+    for(k=0; k<wrec; k++) {
+      if ( (vin(k) < range_min) || (vin(k) > range_max) ) vrec(k)=mean;
+      else vrec(k)=vin(k);    
+    }
+
+    bool fgokdebut = false;
+
     int kgl = -1;
     int ii,lastput;
     bool fgglitch = false;
     double valcur,valsub,valadd;
+    double lastvalok = mean;
     uint_8 fgcur;
-
+    bool fgokcur=false;
     // Boucle sur les sampleNum 
 
-    for(k=0;k<=sne-snb;k++) {
+    int knext;
+    int kfin = sne-snb;
+    for(k=0;k<=kfin;k++) {
       totnscount++;
 //       if (k%10000 == 0) cout << " DBG: K=" << k << endl;
+      knext = k+wsz2;
       // Calcul mean-sigma
-      if (k>=wsize)  {
-	valsub = vin(k%wsize);
-	getData(0, k+snb, vin(k%wsize), vfg(k%wsize));
-	valadd = vin(k%wsize);
-	if ( (valadd < range_min) || (valadd > range_max) ) 
+      if (knext<=kfin)  {
+	valsub = vin(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;
-	s += (valadd-valsub);
-	s2 += (valadd*valadd-valsub*valsub);
-	mean = s/wsize;
-	sigma = sqrt(s2/wsize-mean*mean);
-      }
+	  fgokcur = false;
+	}
+	else fgokcur = true;
+	if ( (valsub < range_min) || (valsub > range_max) ) 
+	  valsub = mean;
+	if (!fgokdebut && fgokcur) {
+	  s += valadd;
+	  s2 += valadd*valadd;
+	  nokdebut++;
+	  mean = s/nokdebut;
+	  if (nokdebut > 1)  sigma = sqrt(s2/nokdebut-mean*mean);
+	  if (nokdebut >= wsize)  { 
+	    fgokdebut = true;
+	    cout << " SimpleDeglitcher::DebugInfo -  nokdebut=" << nokdebut 
+		 << " k=" << k << " knext=" << knext 
+		 << "\n ...DebugInfo mean=" << mean 
+		 << " sigma=" << sigma << " s=" << s << " s2=" << s2 << endl;
+	  }
+	} 
+	else {
+	  s += (valadd-valsub);
+	  s2 += (valadd*valadd-valsub*valsub);
+	  mean = s/wsize;
+	  sigma = sqrt(s2/wsize-mean*mean);
+	}
+      }
+
 
       //  On gere les sorties Mean et Sigma
@@ -140,5 +185,4 @@
 	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;
@@ -146,4 +190,6 @@
 	out_range_nscount++;
       }
+      valcur = vin(k%wsize);
+      fgcur = vfg(k%wsize);
 	
       if (!fgout)   continue;  // Pas de sortie out (deglitche)
@@ -158,10 +204,14 @@
 //            }
 
-      if (valcur < mean+nsig*sigma) {  // inferieur au seuil
+      double curnsig = nsig;
+      if (fgglitch)  curnsig /= 2.;
+	
+      if (valcur < mean+curnsig*sigma) {  // inferieur au seuil
 	if (fgglitch) {  
 	  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, mean, vfg(ii%wsize)|5);
+	      putData(0, ii+snb, recval, vfg(ii%wsize)|5);
 	      glnscount++; 
 	    }
@@ -178,4 +228,5 @@
 	lastput = snb+k;
 	kgl = -1;  fgglitch = false;
+	vrec(k%wrec) = lastvalok = valcur;
       }
       else {  // Superieur au seuil
@@ -186,4 +237,5 @@
 	    lastput = snb+k;
 	    fgglitch = false;  
+	    vrec(k%wrec) = lastvalok = valcur;	    
 	  }
 	}
@@ -196,4 +248,5 @@
 	    lastput = snb+k;
 	  }
+	  vrec(k%wrec) = lastvalok;	    
 	}
       }
@@ -257,8 +310,9 @@
   totnscount = 0;
   coef = new double[wsz];
+  for(int k=0; k<wsz; k++) coef[k] = 0.;
   switch (fk) {
     case UserFilter :
       throw ParmError("SimpleFilter: Error in filter Kind (UserFilter)!");
-      break;
+      //      break;
     case MeanFilter :
       for(int kk=0; kk<wsz; kk++) 
@@ -271,5 +325,5 @@
     case GaussFilter :
       for(int kk=-(wsz/2); kk<=(wsz/2); kk++) 
-	coef[kk] = a*exp((double)(kk*kk)/(s*s));
+	coef[kk+(wsz/2)] = a*exp(-(double)(kk*kk)/(s*s));
       break;
     case DiffFilter :
@@ -280,5 +334,5 @@
     default :
       throw ParmError("SimpleFilter: Error in filter Kind (UnknownFilter)!");
-      break;
+      //      break;
   }
 }
@@ -371,6 +425,6 @@
     for(k=0;k<=sne-snb;k++) {
       double sortie = 0;
-      for(int ii=0; ii<wsize; ii++) {
-	sortie += vin(ii)*coef[(ii+wsz2)%wsize];
+      for(int ii=-wsz2; ii<=wsz2; ii++) {
+	sortie += vin((ii+k+wsize)%wsize)*coef[ii+wsz2];
       }
       putData(0,k+snb,sortie,vfg(k%wsize));
@@ -510,3 +564,132 @@
 
 
-
+// ---------------------------------------------------------------
+// -------------------- Classe SimpleFanOut -----------------------
+// ---------------------------------------------------------------
+
+SimpleFanOut::SimpleFanOut(int nbinput, int mfanout)
+{
+  if (nbinput < 1) 
+    throw ParmError("SimpleFanOut::SimpleFanOut() NbInput < 1 !");
+  if (mfanout < 1) 
+    throw ParmError("SimpleFanOut::SimpleFanOut() M_FanOut < 1 !");
+
+  nb_input = nbinput;
+  m_fanout = mfanout;
+  totnscount = 0;
+}
+
+SimpleFanOut::~SimpleFanOut()
+{
+}
+
+
+void SimpleFanOut::PrintStatus(ostream & os)
+{
+  os << "\n ------------------------------------------------------ \n" 
+     << " SimpleFanOut::PrintStatus() - NbInput=" << NbInput() 
+     << " M_FanOut=" << MFanOut() << endl; 
+  TOIProcessor::PrintStatus(os);
+  os << endl;
+  os << " ProcessedSampleCount=" << ProcessedSampleCount() << endl;
+  os << " ------------------------------------------------------ " << endl;
+}
+
+void SimpleFanOut::init() {
+  cout << "SimpleFanOut::init NbInput=" << nb_input << endl;
+  char buff[64];
+  for(int k=0; k<nb_input; k++) {
+    sprintf(buff,"in%d", k);
+    declareInput(buff);
+    for(int j=0; j<m_fanout; j++) {
+      sprintf(buff,"out%d_%d", k, j);
+      declareOutput(buff);
+    }
+  }
+
+  name = "SimpleFanOut";
+  //  upExtra = 1;
+}
+
+void SimpleFanOut::run() {
+  //  TOIManager* mgr = TOIManager::getManager();
+  int snb = getMinIn();
+  int sne = getMaxIn();
+
+  TVector<int_4> in_index(nb_input);
+  TMatrix<int_4> out_index(nb_input, m_fanout);
+  in_index = -1;
+  out_index = -1;
+  int nbconin = 0;
+  bool fggin = false;
+  char buff[64];
+  for(int ki=0;ki<nb_input;ki++) {
+    sprintf(buff,"in%d", ki);
+    int idx = getInputTOIIndex(buff);
+    if (!checkInputTOIIndex(idx)) continue;
+    nbconin++;
+    in_index(ki) = idx;
+    bool fgout = false;
+    for(int jo=0; jo<m_fanout; jo++) {
+      sprintf(buff,"out%d_%d", ki, jo);
+      int odx = getOutputTOIIndex(buff);
+      if (checkOutputTOIIndex(odx)) {
+	out_index(ki, jo) = odx;
+	fgout = true;
+      }
+    }
+    if (!fgout) {
+      string msg_err = 
+	"SimpleFanOut::run() - No connected Output for Input TOI (" 
+        + getInName(ki) + ") !";
+      cerr << msg_err << endl;
+      throw ParmError(msg_err);
+    }
+  }
+  if (nbconin == 0) {
+    cerr << " SimpleFanOut::run() - No Input TOI connected! "
+	 << endl;
+    throw ParmError("SimpleFanOut::run() No Inout TOI connected!");
+  }
+
+  /*
+  for(int ki=0;ki<nb_input;ki++) {
+    cout << " SimpleFanOut::run() In(" << ki << ") Index=" << in_index(ki) 
+	 << " Name=" << getInName(in_index(ki)) << endl;
+    for(int jo=0; jo<m_fanout; jo++) 
+      cout << " .... Out(" << ki << "," << jo << ") Index=" << out_index(ki, jo)
+	   << " Name=" << getOutName(out_index(ki, jo)) << endl;
+  }
+  */
+  cout << " SimpleFanOut::run() SNRange=" << snb << " - " << sne << endl; 
+  
+
+  try {
+    Timer tm("SimpleFanOut::run()");
+    double valin = 0.;
+    int_8 fgin = 0;
+    for(int k=snb;k<=sne;k++) {
+      for(int i=0;i<nb_input;i++) {
+	if (in_index(i) < 0) continue;
+	valin = 0;
+	fgin = 0;
+	getData(in_index(i), k, valin, fgin);
+	for(int j=0; j<m_fanout; j++) {
+	  if (out_index(i, j) < 0)  continue;
+	  putData(out_index(i, j), k, valin, fgin);
+	}
+      }
+      totnscount++;
+    }  // Boucle sur les num-sample
+    cout << " SimpleFanOut::run() - End of processing " 
+	 << " ProcessedSampleCount=" << ProcessedSampleCount() << endl;
+  }  // Bloc try 
+
+  catch (PException & exc) {
+    cerr << "SimpleFanOut: Catched Exception " << (string)typeid(exc).name()
+         << "\n .... Msg= " << exc.Msg() << endl;
+  }                                                                             
+}
+
+
+
