Index: trunk/Poubelle/archTOI.old/ssthandler.cc
===================================================================
--- trunk/Poubelle/archTOI.old/ssthandler.cc	(revision 393)
+++ trunk/Poubelle/archTOI.old/ssthandler.cc	(revision 394)
@@ -13,5 +13,5 @@
 #include <math.h>
 #include "ssthandler.h"
-#include <iostream.h>
+#include "pisteetoile.h"
 
 // diodpermut[i] = channel de la diode i
@@ -25,17 +25,25 @@
 
 SSTHandler::SSTHandler()
+:sstchass("SSTChassLogFile")
 {
   diodeHistLength = nb_per_block*2+10;
   diodeT = new int[diodeHistLength*nb_photo_diodes];
   starHistLength = 300;
-  stars = new (vector<star>[starHistLength]);
+
   lastBlkNum = -1;
   
   //Has2Bars(false);
   prcTodo=0;
-  seuil=100;
-}
-
-SSTHandler::SSTHandler(SSTHandler const& x)
+  
+  pPiste = NULL;
+  LastBlockSSTNb = -2;
+  noStarDet = 0;
+  
+  findStarConstruct();
+  
+}
+
+SSTHandler::SSTHandler(SSTHandler const& x) 
+:sstchass("SSTChassLogFile")
 {
   diodeHistLength = x.diodeHistLength;
@@ -43,7 +51,24 @@
   memcpy(diodeT, x.diodeT, diodeHistLength*nb_photo_diodes);
   starHistLength = x.starHistLength;
-  stars = new (vector<star>[starHistLength]);
-  for (int i=0; i<starHistLength; i++)
-    stars[i] = x.stars[i];
+    
+  prcTodo    = x.prcTodo;
+ // has2bars   = x.has2bars;
+  lastBlkNum = x.lastBlkNum;
+  
+  pPiste = NULL;
+  LastBlockSSTNb = x.LastBlockSSTNb;
+  noStarDet = x.noStarDet;
+
+  findStarConstruct();
+  for(int i=0; i<NbPhotDiodBarette; i++) {
+  	*PisteBar[i]=*(x.PisteBar[i]);
+  }
+}
+
+SSTHandler& SSTHandler::operator = (SSTHandler const& x) {
+  delete[] diodeT; 
+  diodeHistLength = x.diodeHistLength;
+  diodeT = new int[diodeHistLength*nb_photo_diodes];
+  memcpy(diodeT, x.diodeT, diodeHistLength*nb_photo_diodes);
     
   prcTodo    = x.prcTodo;
@@ -51,31 +76,11 @@
  // elecOffset = x.elecOffset;
   lastBlkNum = x.lastBlkNum;
-  seuil      = x.seuil;
-}
-
-SSTHandler& SSTHandler::operator = (SSTHandler const& x) {
-  delete[] stars;
-  delete[] diodeT; 
-  diodeHistLength = x.diodeHistLength;
-  diodeT = new int[diodeHistLength*nb_photo_diodes];
-  memcpy(diodeT, x.diodeT, diodeHistLength*nb_photo_diodes);
-  starHistLength = x.starHistLength;
-  stars = new (vector<star>[starHistLength]);
-  for (int i=0; i<starHistLength; i++)
-    stars[i] = x.stars[i];
-    
-  prcTodo    = x.prcTodo;
- // has2bars   = x.has2bars;
- // elecOffset = x.elecOffset;
-  lastBlkNum = x.lastBlkNum;
-  seuil      = x.seuil;
-  
+  for(int i=0; i<NbPhotDiodBarette; i++) *PisteBar[i]=*(x.PisteBar[i]);
   return *this;
 }
 
-SSTHandler::~SSTHandler()
-{
-  delete[] stars;
+SSTHandler::~SSTHandler(){
   delete[] diodeT;
+  findStarDestruct();
 }
 
@@ -85,5 +90,5 @@
   if (prcTodo & findAxis)   prcTodo |= findPeriod;
   if (prcTodo & findPeriod) prcTodo |= findStars;
-  if (prcTodo & findStars)  prcTodo |= rmveOffset;
+  if (prcTodo & findStars)  prcTodo |= permDiode;
 }
 
@@ -96,19 +101,4 @@
   elecOffset = eo;
 }
-
-void SSTHandler::ProcessBlock(block_type_sst* blk)
-{
-  lastBlkNum = numero_block(blk);
-  for (int i = 0; i<nb_per_block*2; i++) {
-    DecodeTMBlock(blk, i, diodeRaw[i]);
-  }
-  if (prcTodo & rmveOffset) {
-    RemoveOffset();
-  }
-  if (prcTodo & findStars) {
-    FindStars();
-  }
-}
-
 
 void SSTHandler::DecodeTMBlock(block_type_sst* blk, int i, int* diod)
@@ -139,5 +129,5 @@
 
 
-void SSTHandler::RemoveOffset()
+void SSTHandler::PermutDiode()
 {
   int j0 = diodeHistLength-(nb_per_block*2);
@@ -187,66 +177,207 @@
 }
 
-int SSTHandler::getStarZ(int imesure, int istar) // for last block
-{
-  int j0 = starHistLength-(nb_per_block*2);
-  if (imesure+j0<0 || imesure>=nb_per_block*2) return -99999;
-  if (istar < 0 || istar >= stars[imesure+j0].size()) return -99999;
-  return stars[imesure+j0][istar].z;
-}
-
-int SSTHandler::getStarF(int imesure, int istar) // for last block
-{
-  int j0 = starHistLength-(nb_per_block*2);
-  if (imesure+j0<0 || imesure>=nb_per_block*2) return -99999;
-  if (istar < 0 || istar >= stars[imesure+j0].size()) return -99999;
-  return stars[imesure+j0][istar].m;
-}
-
-
-void SSTHandler::FindStars()  // sur les 72 derniers echantillons dans diode.
-{
-  int js0 = starHistLength-(nb_per_block*2);
-  int jd0 = diodeHistLength-(nb_per_block*2);
-  // Decalage vers la gauche de la taille d'un bloc
-  for (int i=0; i<js0; i++) {
-    stars[i] = stars[i+(nb_per_block*2)];
-  }
-  
-  // Recherche d'etoiles
-  for (int i=0; i<(nb_per_block*2); i++) {
-    stars[js0+i].clear();
-    for (int idiode=0; idiode<46; idiode++) {
-      if (diode(jd0+i,idiode) < -seuil) {
-        if (has2bars) {
-          for (int j=-2; j>=-5; j--) {
-            if (diode(jd0+i+j,(idiode+elecOffset)%46) < -seuil) {
-              star s;
-              s.z = idiode;
-              s.m = -diode(jd0+i,idiode);
-              s.t = i; // $CHECK$ need absolute time...
-              stars[js0+i].push_back(s);
-              break;
-            } else if (idiode<45 && diode(jd0+i+j,(idiode+1+elecOffset)%46) > seuil) {
-              star s;
-              s.z = idiode+.5;
-              s.m = -diode(jd0+i,idiode);
-              s.t = i; // $CHECK$ need absolute time...
-              stars[js0+i].push_back(s);
-              break;
-            }
-          }
-        } else {
-          if (diode(jd0+i,idiode) > diode(jd0+i-1,idiode)) {
-            star s;
-            s.z = idiode;
-            s.m = -diode(jd0+i-1,idiode);
-            s.t = i; // $CHECK$ need absolute time...
-            stars[js0+i].push_back(s);
-          }
-        }
-      }
-    }
-  }
-
-
-}
+int SSTHandler::FindStars(block_type_sst* blk) {
+	NbStarInBlock=0;
+	LastBlockStarVec.clear();
+	
+	int NoFirstSpInBlock=FirstSplNb(blk);
+  	int jd0 = diodeHistLength-NbSampleBlock;
+
+	bool FlagLBlockPerdu=false;
+	int ThisBlockNumb=numero_block(blk);
+	
+	if ((LastBlockSSTNb+1)==ThisBlockNumb) LastBlockSSTNb++;
+	else {
+		FlagLBlockPerdu=true;
+		LastBlockSSTNb=ThisBlockNumb;
+	}
+
+//	On cherche les etoiles	
+	for(int NoDiode=0;NoDiode<NbPhotDiodBarette; NoDiode++) {	
+		pPiste=PisteBar[NoDiode];
+		
+	// Traitons une diode pendant un block
+		offseDataDiod=0;
+		for (int i=0; i<NbSampleBlock; i++) Diodedata[i]=diode(jd0+i,NoDiode);
+
+		if (FlagLBlockPerdu)  {
+			pPiste->fill(Diodedata+offseDataDiod,FirstSplNb(blk));
+			offseDataDiod+=PhDiodTabLong;
+		}
+		else {
+			pPiste->push(Diodedata);
+			offseDataDiod+=Pousslong;
+		}
+		
+	// Recherchons les etoiles
+		while (true) {
+			if (pPiste->traque()) {				// On a trouve!
+				LastStar=pPiste->DonneEtoile();
+/*				double dum= LastStar.TEchan;
+				pair<const double, SSTEtoile> unepaire=make_pair(dum,LastStar);
+				StarHistoryMap.insert(unepaire);	
+*/
+				StarHistoryMap[LastStar.TEchan]=LastStar;
+					//On empile sur la map.
+				NbStarInBlock++;
+#ifdef SST_DEBUG			
+			// On écrit les pistes ayant déclénchées
+				int NoDiodeEvt=LastStar.NoDiode;
+				if(pPisteDump[NoDiodeEvt]->is_open())	
+					for(int noSamp=0; noSamp<NbSampleBlock;noSamp++)
+						(*pPisteDump[NoDiodeEvt])<<noStarDet<<'\t'<<NoDiode<<'\t'<<NoFirstSpInBlock+noSamp<<'\t'<<Diodedata[noSamp]<<endl;
+#endif
+				noStarDet++;
+			}
+		// Est-on en bout de Piste?
+			if(offseDataDiod+Pousslong>=NbSampleBlock) break;
+			else {
+				pPiste->push(Diodedata+offseDataDiod);
+				offseDataDiod+=Pousslong;
+			}
+		}
+	}
+	
+//	On ne garde que les NbEtInSetMax dernières étoiles
+	int nbEtoileInMap=StarHistoryMap.size();
+	if(nbEtoileInMap>NbEtInMapMax) {
+		int nbErase=nbEtoileInMap-NbEtInMapMax;
+		StarHistIter StarIter=StarHistoryMap.begin();
+		for(int i=0;i<nbErase;i++){
+			StarHistoryMap.erase(StarIter);
+			StarIter++;
+		}
+		nbEtoileInMap-=nbErase;
+	}			
+	
+//	On stocke les etoiles du block dans LastBlockStarVec
+	map<double,SSTEtoile>::reverse_iterator StarHistReIter= StarHistoryMap.rbegin() ;
+	for(int i=0; i<NbStarInBlock; i++, StarHistReIter++) 
+		LastBlockStarVec.push_back((*StarHistReIter).second);
+	
+//#ifdef SST_DEBUG
+	if(NbStarInBlock>0) {
+			
+	// On écrit les étoiles detectées
+		vector<SSTEtoile>::reverse_iterator StarVecRevIter;
+		StarVecRevIter=LastBlockStarVec.rbegin();
+		for(int i=0; i<NbStarInBlock;i++,StarVecRevIter++) 
+			(*StarVecRevIter).print(sstchass);
+	}
+		
+//#endif
+
+	return NbStarInBlock;
+}
+
+void SSTHandler::findStarConstruct() {
+	PisteBar=new PisteEtoile*[NbPhotDiodBarette];
+	for(int i=0; i<NbPhotDiodBarette; i++) PisteBar[i]=new PisteEtoile(i);
+	StarHistoryMap.clear();
+	sstchass<<LastStar.printHeader()<<endl;
+	
+#ifdef SST_DEBUG	
+	char s[32];
+	string fileName;
+//	if(pPisteDump==NULL)
+		pPisteDump= new ofstream* [NbChanDump];
+	
+	for (int pistNumb=0; pistNumb<NbChanDump; pistNumb++) {
+		sprintf(s,"%i", pistNumb);	
+		string piste=s;
+		fileName="pisteno"+piste+"dump";
+/*		
+		if ((*pPisteDump[pistNumb]).is_open()) {
+			cerr<<"Erreur: le fichier "<<fileName<<" devrait être libre"<<endl;
+				//On libère
+			(*pPisteDump[pistNumb]).close();
+			delete pPisteDump[pistNumb];
+		}
+*/			
+		pPisteDump[pistNumb]=new ofstream(fileName.c_str(),ios::out|ios::trunc);
+		
+		if(!(*pPisteDump[pistNumb]).is_open()) {
+			cerr<<"Warning Echec à l'ouverture du fichier: "<<fileName<<endl;
+		}
+	}
+#endif
+	return;
+}
+
+void SSTHandler::findStarDestruct() {
+	for(int i=0; i<NbPhotDiodBarette; i++) {
+		delete PisteBar[i];
+	}
+	delete[] PisteBar;
+		
+#ifdef SST_DEBUG	
+	for (int pistNumb=0; pistNumb<NbChanDump; pistNumb++) {
+		pPisteDump[pistNumb]->close();
+		delete pPisteDump[pistNumb];
+	}
+	delete 	pPisteDump;
+#endif
+
+	return;
+}
+
+int SSTHandler::getNumbStar(int iSampl) {
+	StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
+	StarHistIter IterHigh=StarHistoryMap.upper_bound((double)(iSampl+1.));
+	int Compteur=0;
+	while (!(IterLow==IterHigh)) {IterLow++; Compteur++;}
+	return Compteur;
+}
+
+double SSTHandler::getStarF(int iSampl, int istar){ 
+	if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<istar)) return -1;
+	else {
+		StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
+		while(!(istar==0)) {
+			IterLow++;
+			istar--;
+		}
+	return (double) ((*IterLow).second).InpCurrent;
+	}
+}
+
+
+int SSTHandler::getStarZ(int iSampl, int istar) {
+	if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<istar)) return -1;
+	StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
+	while(!(istar==0)) {
+		IterLow++;
+		istar--;
+	}
+	return ((*IterLow).second).NoDiode;
+}
+
+double SSTHandler::getStarTime(int iSampl, int istar) {
+	if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<istar)) return -1;
+	StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
+	while(!(istar==0)) {
+		IterLow++;
+		istar--;
+	}
+	return ((*IterLow).second).TEchan;
+}
+
+int SSTHandler::FirstSplNb(block_type_sst* blk){
+	return NbSampleBlock*numero_block(blk);	// BUGG	A verifier
+}
+
+
+void SSTHandler::ProcessBlock(block_type_sst* blk)
+{
+  lastBlkNum = numero_block(blk);
+  for (int i = 0; i<nb_per_block*2; i++) {
+    DecodeTMBlock(blk, i, diodeRaw[i]);
+  }
+  if (prcTodo & permDiode) {
+    PermutDiode();
+  }
+  if (prcTodo & findStars) {
+    FindStars(blk);
+  }
+}
+
