Index: trunk/ArchTOIPipe/Kernel/fitstoiwtr.cc
===================================================================
--- trunk/ArchTOIPipe/Kernel/fitstoiwtr.cc	(revision 1466)
+++ trunk/ArchTOIPipe/Kernel/fitstoiwtr.cc	(revision 1467)
@@ -79,5 +79,5 @@
       cout << " Column[" << j+1 << "] -Flag- Name=" << coln[ck] << endl;
       ck++;
-      coltypes[j+2] = "1J";
+      coltypes[j+2] = "1I";
       colunits[j+2] = "Int_8Flag";
     }
@@ -102,5 +102,5 @@
 
   double* tabdata = new double[ndata];
-  long* tabflag = new long[ndata]; // ? pas int_8 ?? $CHECK$ - Reza
+  int* tabflag = new int[ndata]; // ? pas int_8 ?? $CHECK$ - Reza
   bool* tabck = new bool[ndata];
   int i;
@@ -133,5 +133,5 @@
 	if (outFlags) {
 	  fits_write_col_dbl(fptr, 2*i+2, fitsLine, 1, 1, tabdata+i, &fstatus);
-	  fits_write_col_lng(fptr, 2*i+3, fitsLine, 1, 1, tabflag+i, &fstatus);
+	  fits_write_col_int(fptr, 2*i+3, fitsLine, 1, 1, tabflag+i, &fstatus);
 	} else {
 	  fits_write_col_dbl(fptr, i+2, fitsLine, 1, 1, tabdata+i, &fstatus);
Index: trunk/ArchTOIPipe/ProcWSophya/simtoipr.cc
===================================================================
--- trunk/ArchTOIPipe/ProcWSophya/simtoipr.cc	(revision 1466)
+++ 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;
+  }                                                                             
+}
+
+
+
Index: trunk/ArchTOIPipe/ProcWSophya/simtoipr.h
===================================================================
--- trunk/ArchTOIPipe/ProcWSophya/simtoipr.h	(revision 1466)
+++ trunk/ArchTOIPipe/ProcWSophya/simtoipr.h	(revision 1467)
@@ -115,3 +115,26 @@
 
 
+//  Classe SimpleFanOut
+//  Transforme recopie chaque entree sur M lignes de sortie
+
+class SimpleFanOut : public TOIProcessor {
+public:
+		SimpleFanOut(int nbinput, int mfanout); 
+		~SimpleFanOut();
+
+  inline int	NbInput() const { return nb_input; }
+  inline int	MFanOut() const { return m_fanout; }
+  inline int_8	ProcessedSampleCount() const { return totnscount; }
+
+  virtual void  PrintStatus(ostream & os) ; // const plus tard
+
+  virtual void	init();  
+  virtual void	run();
+
+protected:
+  int nb_input;
+  int m_fanout;
+  int_8 totnscount;   // Nombre total d'echantillon processe
+};
+
 #endif
Index: trunk/ArchTOIPipe/TestPipes/simtst.cc
===================================================================
--- trunk/ArchTOIPipe/TestPipes/simtst.cc	(revision 1466)
+++ trunk/ArchTOIPipe/TestPipes/simtst.cc	(revision 1467)
@@ -22,8 +22,6 @@
   }
   else {
-    cout << "\n Usage : simtst [-sproc] [-rwtoi] [-dbg] [-start snb] [-end sne] \n" 
-	 << "         [-w1 sz] [-w2 sz] inFitsName outFitsName ppfFileName \n" 
-	 << "   -sproc : Run RzSimpleTOIProc (default RzTOIProc) \n"
-	 << "   -rwtoi : Use Regular Window TOI (default TOISeqBuffered) \n" 
+    cout << "\n Usage : simtst [-dbg] [-start snb] [-end sne] \n" 
+	 << "         [-wtoi sz] [-w2 sz] inFitsName outFitsName ppfFileName \n" 
 	 << "   -dbg : sets TOISeqBuffered debug level to 1 \n"
 	 << "   -start snb : sets the start sample num \n"
@@ -31,7 +29,7 @@
 	 << "   -range min,max : sets the acceptable range for intoi \n"
 	 << "              default= -16000,16000\n"
-	 << "   -intoi toiName : select input TOI name (def boloMuV_11)\n"
-	 << "   -w1 sz : sets main data window size (def= 8192)\n"
-	 << "   -w2 sz : sets secondary window size (def= 512) \n"
+	 << "   -intoi toiName : select input TOI name (def boloMuV_27)\n"
+	 << "   -wtoi sz : sets TOISeqBuff buffer size (def= 8192)\n"
+	 << "   -wdegli sz : sets deglitcher window size (def= 512) \n"
 	 << endl;
     exit(0);
@@ -45,9 +43,7 @@
   cout << "simtst starting - Decoding arguments " << " narg=" << narg << endl;
 
-  bool fgsimple = false;
-  bool fgrwtoi = false;
   bool fgdbg = false;
   int w1 = 8192;
-  int w2 = 256;
+  int w2 = 512;
   int nmax = 10;
   int istart = 104121000+w1*5;
@@ -58,5 +54,5 @@
   string outfile;
   string ppffile;
-  string intoi = "boloMuV_11";
+  string intoi = "boloMuV_27";
 
   if (narg < 4) Usage(true);
@@ -72,9 +68,9 @@
       iend = atoi(arg[ia+1]); ia++;
     }    
-    else if (strcmp(arg[ia],"-w1") == 0) {
+    else if (strcmp(arg[ia],"-wtoi") == 0) {
       if (ia == narg-1) Usage(true);  
       w1 = atoi(arg[ia+1]); ia++;
     }    
-    else if (strcmp(arg[ia],"-w2") == 0) {
+    else if (strcmp(arg[ia],"-wdegli") == 0) {
       if (ia == narg-1) Usage(true);  
       w2 = atoi(arg[ia+1]); ia++;
@@ -89,6 +85,4 @@
       intoi = arg[ia+1]; ia++;
     }    
-    else if (strcmp(arg[ia],"-sproc") == 0)  fgsimple = true;
-    else if (strcmp(arg[ia],"-rwtoi") == 0)  fgrwtoi = true;
     else if (strcmp(arg[ia],"-dbg") == 0)  fgdbg = true;
 
@@ -131,6 +125,6 @@
     TOISeqBuffered * toiin = new TOISeqBuffered("f2in", w1);
     if (fgdbg) toiin->setDebugLevel(1);
-    TOISeqBuffered * toiout = new TOISeqBuffered("out", w1);
-    if (fgdbg) toiout->setDebugLevel(1);
+    TOISeqBuffered * toidegli = new TOISeqBuffered("degli", w1);
+    if (fgdbg) toidegli->setDebugLevel(1);
     TOISeqBuffered * toimean = new TOISeqBuffered("mean", w1);
     if (fgdbg) toimean->setDebugLevel(1);
@@ -161,24 +155,67 @@
     degl.SetRange(range_min, range_max);
     degl.addInput("in", toiin);
-    degl.addOutput("out", toiout);
+    degl.addOutput("out", toidegli);
     degl.addOutput("mean", toimean);
     degl.addOutput("sigma", toisig);
     degl.addOutput("incopie", toiincopie);
- 
-    /*   
-    cout << " Creating a GaussianFilter SimplFilterObject " << endl;
-    SimpleFilter filt(..)
-    TOISeqBuffered * toiincopie = new TOISeqBuffered("incopie", w1);
-    if (fgdbg) toiincopie->setDebugLevel(1);
-    */
-
+    
+    cout << " Creating a FanOut SimpleFanOut Object " << endl;
+    SimpleFanOut fanout(2,2);
+    TOISeqBuffered * toidegli0 = new TOISeqBuffered("degli0", w1);
+    if (fgdbg) toidegli0->setDebugLevel(1);
+    TOISeqBuffered * toidegli1 = new TOISeqBuffered("degli1", w1);
+
+    TOISeqBuffered * toimean0 = new TOISeqBuffered("mean0", w1);
+    if (fgdbg) toimean0->setDebugLevel(1);
+    TOISeqBuffered * toimean1 = new TOISeqBuffered("mean1", w1);
+    if (fgdbg) toimean1->setDebugLevel(1);
+    
+    fanout.addInput("in0", toidegli);
+    fanout.addOutput("out0_0", toidegli0);
+    fanout.addOutput("out0_1", toidegli1);
+    fanout.addInput("in1", toimean);
+    fanout.addOutput("out1_0", toimean0);
+    fanout.addOutput("out1_1", toimean1);
+
+    cout << " Creating an Adder SimpleAdder Object " << endl;
+    SimpleAdder adder(2);
+    adder.addInput("in0", toidegli0);
+    adder.addInput("in1", toimean0);
+    adder.SetGain(0, 1.);
+    adder.SetGain(1, -1.);
+    TOISeqBuffered * toideglioff = new TOISeqBuffered("deglioff", w1);
+    if (fgdbg) toideglioff->setDebugLevel(1);
+    adder.addOutput("out", toideglioff);
+
+    cout << " Creating a GaussianFilter SimpleFilter Object " << endl;
+    SimpleFilter filt(8, SimpleFilter::GaussFilter, 1., 2.);
+
+    filt.addInput("in", toideglioff);
+    TOISeqBuffered * toiout = new TOISeqBuffered("out", w1);
+    if (fgdbg) toiout->setDebugLevel(1);
+    TOISeqBuffered * toideglioffcopie = new TOISeqBuffered("deglioffcopie", w1);
+    if (fgdbg) toideglioffcopie->setDebugLevel(1);
+    filt.addOutput("out", toiout);
+    filt.addOutput("incopie", toideglioffcopie);
+    
+
+    cout << " Connecting to output (FitsWriter) " << endl;
+    
     w.setOutFlags(true);
     w.addInput("in", toiincopie);
-    w.addInput("degli", toiout);
-    w.addInput("mean", toimean);
+    w.addInput("out", toiout);
+    w.addInput("degli", toidegli1);
+    w.addInput("deglioff", toideglioffcopie);
+    w.addInput("mean", toimean1);
     w.addInput("sigma", toisig);
 
     cout << " ------ FITSReaderTOI::PrintStatus() : ----- " << endl;
     r.PrintStatus(cout);  
+    cout << " ------ FanOut::PrintStatus() : ----- " << endl;
+    fanout.PrintStatus(cout);  
+    cout << " ------ Adder::PrintStatus() : ----- " << endl;
+    adder.PrintStatus(cout);  
+    cout << " ------ Filter::PrintStatus() : ----- " << endl;
+    filt.PrintStatus(cout);  
     cout << "----- FITSWriterTOI::PrintStatus() : ----- " << endl;
     w.PrintStatus(cout);  
@@ -187,12 +224,18 @@
     r.start();
     degl.start();
+    fanout.start();
+    adder.start();
+    filt.start();
     w.start();
 
-    /*    
+    /*
     for(int jj=0; jj<3; jj++) {
       cout << *toiin; 
+      cout << *toimean; 
+      cout << *toimean1; 
       cout << *toiout; 
       sleep(1);
     }
+    
     */
 
@@ -205,8 +248,13 @@
     //    w.PrintStatus(cout);  
 
+    cout << " ------ toiin, toidegli and toiout Status information ------- " << endl;
+    cout << *toiin; 
+    cout << *toidegli; 
+    cout << *toiout;
+
     cout << degl;
-    cout << " ------ toiin and toiout Status information ------- " << endl;
-    cout << *toiin; 
-    cout << *toiout;
+    cout << filt;
+    cout << adder;
+
   }
   catch (PThrowable & exc) {
