Index: /trunk/ArchTOIPipe/TestPipes/SMakefile
===================================================================
--- /trunk/ArchTOIPipe/TestPipes/SMakefile	(revision 2046)
+++ /trunk/ArchTOIPipe/TestPipes/SMakefile	(revision 2047)
@@ -32,5 +32,5 @@
 endif
 ifeq ($(MACHEROS),Linux)
-LIBS := $(LIBS) -ldl -lf2c
+LIBS := $(LIBS) -ldl 
 endif
 
@@ -67,3 +67,3 @@
 	../cleantstpipes.csh
 
-all :  mesovh mesovh2 tsttoi2map simtst simofftst aksj02 tstmap2toi
+all :  mesovh mesovh2 tsttoi2map simtst simofftst aksj02 tstmap2toi toistat
Index: /trunk/ArchTOIPipe/TestPipes/aksj02.cc
===================================================================
--- /trunk/ArchTOIPipe/TestPipes/aksj02.cc	(revision 2046)
+++ /trunk/ArchTOIPipe/TestPipes/aksj02.cc	(revision 2047)
@@ -42,7 +42,8 @@
 	 << "         [-dbg] [-wtoi sz] [-wclean wsz,nbw] [-range min,max] \n"
 	 << "         [-soff wsz,nptfit,degpol] [-soffnt PPFName] \n"
-	 << "         [-bgalcut bmin,bmax] [-bgal toiname] [-bgalfile pointFitsName] \n"
 	 << "         [-soff2 wsz,nptfit,degpol] [-soff2nt PPFName] \n"
-	 << "         [-bgal2cut bmin,bmax] \n"
+	 << "         [-bgal toiname] [-bgalfile pointFitsName] [-bgalnf N]\n"
+	 << "         [-bgal toiname] [-bgalfile pointFitsName] \n"
+	 << "         [-bgalcut bmin,bmax]  [-bgal2cut bmin,bmax] \n"
 	 << "         [-gfilt wsz,sigma] \n" 
 	 << "         [-nooutflg] [-wrtms] [-wrtifo] [-prstat] [-useseqbuff] \n"
@@ -61,4 +62,5 @@
          << "   -bgal toiname: Galactic latitude toiname \n"
          << "   -bgalfile pointFitsName: Pointing FITS file name (for gal. latitude) \n"
+	 << "   -bgalnf n: Number of bgalfiles pointFitsName%d.fits \n"
 	 << "   -soff2 wsz,nptfit,degpol : set SimpleOffsetEstimator_2 parameters\n"
 	 << "   -soff2nt PPFName : Writes out SimpleOffsetEstimator_2 NTuple \n"
@@ -106,6 +108,6 @@
 
   // cleaner parameters
-  double clean_wsz = 256;
-  double clean_nbw = 5;
+  int clean_wsz = 256;
+  int clean_nbw = 5;
 
   // SimpleOffsetEstimator parameters
@@ -139,4 +141,5 @@
   double bmax2 = 10.;
   bool bgal2cut = false;
+  int bgalNbFiles = 0;
 
   // File names 
@@ -222,4 +225,9 @@
       ia++;
     }
+    else if (strcmp(arg[ia],"-bgalnf") == 0) {
+      if (ia == narg-1) Usage(true);
+      bgalNbFiles = atoi(arg[ia+1]);
+      ia++;
+    }
     else if (strcmp(arg[ia],"-bgal") == 0) {
       if (ia == narg-1) Usage(true);
@@ -307,7 +315,21 @@
     FITSTOIReader* rbgal = NULL;
     if (bgalfile) {  // if Galactic cut 
-      cout << "> Creating bgal FITSTOIReader object - InFile=" << pointfile 
+      string pointfileI = pointfile;
+      MuTyV numf=0;
+      if(bgalNbFiles > 0) {
+	pointfileI += (string)numf + ".fits";
+      }
+      cout << "> Creating bgal FITSTOIReader object - InFile=" << pointfileI
 	   << " bgaltoiname= " << bgaltoi << endl;
-      rbgal = new FITSTOIReader(pointfile); 
+      rbgal = new FITSTOIReader(pointfileI);
+      if(bgalNbFiles > 0) 
+	for(int kff=1; kff<bgalNbFiles; kff++) {
+	  numf=kff;
+	  pointfileI = pointfile;
+	  pointfileI += (string)numf + ".fits";
+	  cout << " bgal FITSTOIReader.addFile( " << pointfileI << " )" << endl;
+	  rbgal->addFile( pointfileI );
+	}
+      
     }
     
Index: /trunk/ArchTOIPipe/TestPipes/toistat.cc
===================================================================
--- /trunk/ArchTOIPipe/TestPipes/toistat.cc	(revision 2047)
+++ /trunk/ArchTOIPipe/TestPipes/toistat.cc	(revision 2047)
@@ -0,0 +1,229 @@
+// ArchTOIPipe           (C)     CEA/DAPNIA/SPP IN2P3/LAL
+//                               Eric Aubourg
+//                               Christophe Magneville
+//                               Reza Ansari
+
+/*   Calcul de statistique simples sur TOI  
+
+----------------   Exemple d'appel  ---------------------
+csh> toistat -start 104385384 -end 104399964 -range -500,500 \
+            -intoi boloMuV_26 -wtoi 8192 -wclean 512,5 \
+	    inputbolo.fits filt.fits xx.ppf
+*/
+
+
+
+#include "machdefs.h"
+#include <math.h>
+#include <unistd.h>
+
+#include "toimanager.h"
+#include "cgt.h"
+#include "fitstoirdr.h"
+#include "fitstoiwtr.h"
+#include "simtoipr.h"
+#include "simoffset.h"
+#include "simcleaner.h"
+#include "nooppr.h"
+#include "timing.h"
+#include "histinit.h"
+#include "ntuple.h"
+#include "fitsntuple.h"
+
+#include "psighand.h"
+#include <stdexcept>
+
+void Usage(bool fgerr)
+{
+  cout << endl;
+  if (fgerr) {
+    cout << " toistat : Argument Error ! toistat -h for usage " << endl; 
+    exit(1);
+  }
+  else {
+    cout << " toistat : produce a stat NTuple (mean,sigma) from a TOI" << endl;
+    cout << "\n Usage : toistat [-intoi toiname] [-start snb] [-end sne] \n" 
+	 << "         [-wtoi sz] [-wclean wsz,nbw] [-range min,max] \n"
+	 << "         [-outppf] [-noprstat] [-useseqbuff] \n"
+	 << "         inFitsName outFileName \n" 
+	 << "   -start snb : sets the start sample num \n"
+	 << "   -end sne : sets the end sample num \n"
+	 << "   -range min,max : sets the acceptable range for intoi \n"
+	 << "              default= -16000,16000\n"
+	 << "   -intoi toiName : select input TOI name (def bolo)\n"
+	 << "   -wtoi sz : sets TOISeqBuff buffer size (def= 8192)\n"
+	 << "   -wclean wsz,nbw : sets cleaner window parameters (256,5) \n"
+	 << "   -outppf : Write the NTuple in PPF format (default: FITS) \n"
+	 << "   -noprstat : DO NOT PrintStat with ProcSampleCounter \n"
+	 << "   -useseqbuff : Use TOISeqBuffered TOI's (default: TOISegmented) \n"
+	 << endl;
+  }
+  if (fgerr) exit(1); 
+}
+
+int main(int narg, char** arg) {
+
+  if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) Usage(false);
+
+  cout << "toistat starting - Decoding arguments " << " narg=" << narg << endl;
+
+  bool fgsetstart = false;
+
+  bool fgprstat = true;
+  bool fgsegmented = true;
+  bool fgoutppf = false;
+
+  int dbglev = 0;
+
+  int wtoi = 8192;
+
+  int nmax = 10;
+  int istart = 0;
+  int iend = 0;
+
+  double range_min = -16000;
+  double range_max = 16000.;
+
+  // cleaner parameters
+  int clean_wsz = 256;
+  int clean_nbw = 5;
+
+  // File names 
+  string infile;
+  string outfile;
+  string intoi = "bolo"; 
+
+  if (narg < 3) Usage(true);
+  int ko=1;
+  // decoding arguments 
+  for(int ia=1; ia<narg; ia++) {
+    if (strcmp(arg[ia],"-start") == 0) {
+      if (ia == narg-1) Usage(true);  // -start est suivi d'un argument 
+      istart = atoi(arg[ia+1]); ia++;
+      fgsetstart = true;
+    }    
+    else if (strcmp(arg[ia],"-end") == 0) {
+      if (ia == narg-1) Usage(true);  
+      iend = atoi(arg[ia+1]); ia++;
+    }    
+    else if (strcmp(arg[ia],"-wtoi") == 0) {
+      if (ia == narg-1) Usage(true);  
+      wtoi = atoi(arg[ia+1]); ia++;
+    }    
+    else if (strcmp(arg[ia],"-wclean") == 0) {
+      if (ia == narg-1) Usage(true);  
+      sscanf(arg[ia+1],"%d,%d",  &clean_wsz, &clean_nbw);
+    }    
+    else if (strcmp(arg[ia],"-range") == 0) {
+      if (ia == narg-1) Usage(true);
+      sscanf(arg[ia+1],"%lf,%lf",&range_min, &range_max);
+      ia++;
+    }    
+    else if (strcmp(arg[ia],"-intoi") == 0) {
+      if (ia == narg-1) Usage(true);  
+      intoi = arg[ia+1]; ia++;
+    }    
+    else if (strcmp(arg[ia],"-outppf") == 0)  fgoutppf = true;
+
+    else if (strcmp(arg[ia],"-noprstat") == 0)  fgprstat = false;
+    else if (strcmp(arg[ia],"-useseqbuff") == 0)  fgsegmented = false;
+
+    else { ko = ia; break; }  // Debut des noms
+  }
+
+  if (iend < istart) iend = istart+wtoi*(nmax+5);
+  if ((narg-ko) < 2)  Usage(true); 
+  infile = arg[ko];
+  outfile = arg[ko+1];
+  //  outppfname = arg[ko+2];
+
+  cout << " Initializing SOPHYA ... " << endl;
+  SophyaInit();
+  //  SophyaConfigureSignalhandling(true);
+
+  InitTim();
+
+  cout << ">>>> toistat: Infile= " << infile << " outFile=" 
+       << outfile << endl; 
+  cout << "  iStart= " << istart << " iEnd= " << iend << endl;
+  cout << ">>>> InTOIName= " << intoi << endl;
+
+  try {
+    TOIManager* mgr = TOIManager::getManager();
+  
+    //  mgr->setRequestedSample(11680920,11710584);
+    //  mgr->setRequestedSample(104121000, 104946120);
+    if (fgsetstart) 
+      mgr->setRequestedSample(istart, iend);
+
+    cout << "> Creating FITSTOIReader object - InFile=" << infile << endl;
+    FITSTOIReader r(infile); 
+
+    cout << "> Creating SimpleCleaner() " << endl;
+    SimpleCleaner cleaner(clean_wsz, clean_nbw);
+    cout << " Setting Range for cleaner: " << range_min 
+	 << " - " << range_max << endl;
+    cleaner.SetRange(range_min, range_max);
+    cleaner.FillMeanSigNTuple(true);
+
+    CGT plombier(fgsegmented, wtoi);
+    //    plombier.SetDebugLevel(dbglev);
+
+    cout << "> Connecting Processors through plombier ... " << endl;
+    string inname = "in";
+    string outname = "out";
+    plombier.Connect(r, intoi, cleaner, inname);
+
+    cout << "> Plombier status before start" << endl;
+    cout << plombier ; 
+
+    cout << cleaner;
+
+    PrtTim("starting processors");
+    plombier.Start();
+
+
+    // ------------------- Impression continu de stat ------------------------ 
+    if (fgprstat) {
+      ProcSampleCounter<SimpleCleaner> stats(cleaner);
+      stats.InfoMessage() = "toistat/Info";
+      stats.PrintStats();
+    }
+    // -----------------------------------------------------------------------
+
+    cout << cleaner;
+
+    mgr->joinAll();
+    PrtTim("End threads");
+    cleaner.GetMeanSigNTuple().Info()["TOIFILE"] = infile;
+    cleaner.GetMeanSigNTuple().Info()["TOINAME"] = intoi;
+  
+    if (fgoutppf) {
+      cout << " \n --------------------------------------------------------- " << endl;
+      cout << " Saving Stats (mean-sigma) NTuple to PPF file " << outfile << endl;
+      POutPersist pos(outfile);
+      pos << cleaner.GetMeanSigNTuple();
+      cout << " \n --------------------------------------------------------- " << endl;
+    }
+    else {
+      cout << " \n --------------------------------------------------------- " << endl;
+      cout << " Saving Stats (mean-sigma NTuple to FITS file " << outfile << endl;
+      FitsOutFile  fos(outfile, FitsFile::clear);
+      fos << cleaner.GetMeanSigNTuple();
+      cout << " \n --------------------------------------------------------- " << endl;      
+    }
+  }
+  catch (PThrowable & exc) {
+    cerr << "\n toistat: Catched Exception \n" << (string)typeid(exc).name() 
+	 << " - Msg= " << exc.Msg() << endl;
+  }
+  catch (const std::exception & sex) {
+    cerr << "\n toistat: Catched std::exception \n" 
+	 << (string)typeid(sex).name() << endl; 
+  }
+  catch (...) {
+    cerr << "\n toistat: some other exception was caught ! " << endl;
+  }
+
+  return(0);
+}
