source: Sophya/trunk/Poubelle/archTOI.old/ssthandler.cc@ 534

Last change on this file since 534 was 534, checked in by ansari, 26 years ago

V2

File size: 10.5 KB
Line 
1// ssthandler.cc
2// Eric Aubourg CEA/DAPNIA/SPP juillet 1999
3
4
5// Prediction mouvement d'etoiles entre un tour et le suivant...
6// si TS -> TS + dT, H -> H + dT, dT=dH
7//
8// dz = - cos phi sin az dH (check sign)
9// daz = (sin phi - cos az cotg z cos phi) dH (check sign)
10//
11// free parameters = period + phase
12
13#include <math.h>
14#include <iostream>
15#include "ssthandler.h"
16#include "pisteetoile.h"
17
18
19ofstream SSTHandler::sstchass("SSTChassLogFile");
20bool SSTHandler::sstchassinit=false;
21
22// diodpermut[i] = channel de la diode i
23int SSTHandler::diodpermut[46]=
24 { 8,24,40, 9,25,41,10,26,42,11,
25 27,43,16,32, 1,17,33, 2,18,34,
26 3,19,35,12,28,44,13,29,45,14,
27 30,46,15,31,47,20,36, 5,21,37,
28 6,22,38, 7,23,39};
29 // voies 0 et 4 non connectees, voie 1 en panne.
30
31SSTHandler::SSTHandler()
32{
33 diodeHistLength = nb_per_block*2+10;
34 diodeT = new int[diodeHistLength*nb_photo_diodes];
35 starHistLength = 300;
36
37 lastBlkNum = -1;
38
39 //Has2Bars(false);
40 prcTodo=0;
41
42 pPiste = NULL;
43 LastBlockSSTNb = -2;
44 noStarDet = 0;
45
46 findStarConstruct();
47
48}
49
50SSTHandler::SSTHandler(SSTHandler const& x)
51{
52 diodeHistLength = x.diodeHistLength;
53 diodeT = new int[diodeHistLength*nb_photo_diodes];
54 memcpy(diodeT, x.diodeT, diodeHistLength*nb_photo_diodes);
55 starHistLength = x.starHistLength;
56
57 prcTodo = x.prcTodo;
58 // has2bars = x.has2bars;
59 lastBlkNum = x.lastBlkNum;
60
61 pPiste = NULL;
62 LastBlockSSTNb = x.LastBlockSSTNb;
63 noStarDet = x.noStarDet;
64
65 findStarConstruct();
66 for(int i=0; i<NbPhotDiodBarette; i++) {
67 *PisteBar[i]=*(x.PisteBar[i]);
68 }
69}
70
71SSTHandler& SSTHandler::operator = (SSTHandler const& x) {
72 delete[] diodeT;
73 diodeHistLength = x.diodeHistLength;
74 diodeT = new int[diodeHistLength*nb_photo_diodes];
75 memcpy(diodeT, x.diodeT, diodeHistLength*nb_photo_diodes);
76
77 prcTodo = x.prcTodo;
78 // has2bars = x.has2bars;
79 // elecOffset = x.elecOffset;
80 lastBlkNum = x.lastBlkNum;
81 for(int i=0; i<NbPhotDiodBarette; i++) *PisteBar[i]=*(x.PisteBar[i]);
82 return *this;
83}
84
85SSTHandler::~SSTHandler(){
86 delete[] diodeT;
87 findStarDestruct();
88}
89
90void SSTHandler::NeedProcess(int prcMask)
91{
92 prcTodo |= prcMask;
93 if (prcTodo & findAxis) prcTodo |= findPeriod;
94 if (prcTodo & findPeriod) prcTodo |= findStars;
95 if (prcTodo & findStars) prcTodo |= permDiode;
96}
97
98bool SSTHandler::has2bars = false;
99int SSTHandler::elecOffset = 1;
100
101void SSTHandler::Has2Bars(bool has, int eo)
102{
103 has2bars = has;
104 elecOffset = eo;
105}
106
107void SSTHandler::DecodeTMBlock(block_type_sst* blk, int i, int* diod)
108{
109 int j; // 0-5 : numero du bloc de 8 diodes
110 int k; // 0-2 : indice du bloc de 4 bits (une diode = 12 bits = 3 blocs de 4 bits)
111 int l; // 0-7 : indice de la diode dans son bloc (8 diodes * 4 bits = 1 mot de 32 bits)
112
113 // numero de la diode (0-47) = j*8+l;
114 // indice dans le bloc sst du mot de 32 bits (0-17) = j*3+k;
115 // indice dans mot de 32 bits du premier bit utile = 4*l;
116
117 for (j=0; j<48; j++) diod[j] = 0;
118
119 for (j=0; j<6; j++)
120 for (k=0; k<3; k++)
121 for (l=0; l<8; l++) {
122 int4 word = blk->sst[i][j*3+k];
123 word = (word >> (4*l)) & 0xF;
124 // printf("diode %d mot %d valeur %d\n", j*8+l, k, word);
125 diod[j*8+l] = (diod[j*8+l] << 4) + word;
126 }
127
128 //for (j=0; j<48; j++) if (diod[j]>2047) diod[j] -= 4096;
129 for (j=0; j<48; j++) diod[j] -= 2048;
130
131}
132
133
134void SSTHandler::PermutDiode()
135{
136 int j0 = diodeHistLength-(nb_per_block*2);
137
138 // Decalage vers la gauche de la taille d'un bloc
139 memcpy(diodeT, diodeT + (nb_per_block*2)*nb_photo_diodes, j0*nb_photo_diodes);
140
141 for (int j=0; j<nb_per_block*2; j++) {
142 // permutation des diodes
143 for (int i=0; i<46; i++)
144 diode(j+j0,i) = diodeRaw[j][diodpermut[i]];
145
146 /*
147 // calcul d'un fond sur la rangee. Moyenne clippee.
148 float m = 0; float sig = 1.e10;
149 for (int k=0; k<2; k++) {
150 float s=0; float s2=0; int n=0;
151 for (int i=0; i<46; i++) {
152 if (fabs(diode(j+j0,i)-m)<3*sig+1) {
153 s += diode(j+j0,i); s2 += diode(j+j0,i)*diode(j+j0,i); n++;
154 }
155 }
156 if (n>0) {
157 m = s/n; sig = sqrt(s2/n - m*m);
158 } else {
159 m = 0; break;
160 }
161 }
162 for (int i=0; i<46; i++)
163 diode(j+j0,i) -= m;
164 */
165 }
166}
167
168int SSTHandler::getRawSignal(int imesure, int idiode) // for last block
169{
170 if (imesure<0 || imesure>=nb_per_block*2 || idiode<0 || idiode>=48) return -99999;
171 return diodeRaw[imesure][idiode];
172}
173
174int SSTHandler::getSignal(int imesure, int idiode) // for last block
175{
176 int j0 = diodeHistLength-(nb_per_block*2);
177 if (imesure+j0<0 || imesure>=nb_per_block*2 ||
178 idiode<0 || idiode>=nb_photo_diodes) return -99999;
179 return diode(imesure+j0, idiode);
180}
181
182
183// D. Yvon, CEA/DAPNIA/SPP Saclay, 08/99
184
185
186#ifdef SSTStatLog
187 static ofstream sststat("SSTStatLog");
188 static int compteurBlock=0;
189 static int nbStar=0;
190 #include "ssthardware.h"
191 #include "archparam.h"
192#endif
193
194
195int SSTHandler::FindStars(block_type_sst* blk) {
196 NbStarInBlock=0;
197 LastBlockStarVec.clear();
198
199 int NoFirstSpInBlock=FirstSplNb(blk);
200 int jd0 = diodeHistLength-NbSampleBlock;
201
202 bool FlagLBlockPerdu=false;
203 int ThisBlockNumb=numero_block(blk);
204
205 if ((LastBlockSSTNb+1)==ThisBlockNumb) LastBlockSSTNb++;
206 else {
207 FlagLBlockPerdu=true;
208 LastBlockSSTNb=ThisBlockNumb;
209 }
210
211// On cherche les etoiles
212 for(int NoDiode=0;NoDiode<NbPhotDiodBarette; NoDiode++) {
213 pPiste=PisteBar[NoDiode];
214
215 // Traitons une diode pendant un block
216 offseDataDiod=0;
217 for (int i=0; i<NbSampleBlock; i++) Diodedata[i]=diode(jd0+i,NoDiode);
218
219 if (FlagLBlockPerdu) {
220 pPiste->fill(Diodedata+offseDataDiod,FirstSplNb(blk),PhDiodTabLong);
221 offseDataDiod+=PhDiodTabLong;
222 }
223 else {
224 pPiste->push(Diodedata,Pousslong);
225 offseDataDiod+=Pousslong;
226 }
227
228 // Recherchons les etoiles
229 while (true) {
230 if (pPiste->traque()) { // On a trouve!
231 LastStar=pPiste->DonneEtoile();
232/* double dum= LastStar.TEchan;
233 pair<const double, SSTEtoile> unepaire=make_pair(dum,LastStar);
234 StarHistoryMap.insert(unepaire);
235*/
236 StarHistoryMap[LastStar.TEchan]=LastStar;
237 //On empile sur la map.
238 NbStarInBlock++;
239#ifdef SSTDUMP
240 // On écrit les pistes ayant déclénchées
241 int NoDiodeEvt=LastStar.NoDiode;
242 if(NoDiodeEvt<NbChanDump) {
243 if(pPisteDump[NoDiodeEvt]->is_open())
244 for(int noSamp=0; noSamp<NbSampleBlock;noSamp++)
245 (*pPisteDump[NoDiodeEvt])<<noStarDet<<'\t'<<NoDiode<<'\t'<<NoFirstSpInBlock+noSamp<<'\t'<<Diodedata[noSamp]<<endl;
246 }
247#endif
248 noStarDet++;
249 }
250 // Est-on en bout de Piste?
251 if (offseDataDiod+Pousslong > NbSampleBlock) break;
252 pPiste->push(Diodedata+offseDataDiod,Pousslong);
253 offseDataDiod+=Pousslong;
254 }
255 }
256
257// On ne garde que les NbEtInSetMax dernières étoiles
258 int nbEtoileInMap=StarHistoryMap.size();
259 if(nbEtoileInMap>NbEtInMapMax) {
260 int nbErase=nbEtoileInMap-NbEtInMapMax;
261 StarHistIter StarIter=StarHistoryMap.begin();
262 for(int i=0;i<nbErase;i++){
263 StarIter++;
264 }
265 StarHistoryMap.erase(StarHistoryMap.begin(),StarIter);
266 nbEtoileInMap-=nbErase;
267 }
268
269// On stocke les etoiles du block dans LastBlockStarVec
270 map<double,SSTEtoile>::reverse_iterator StarHistReIter= StarHistoryMap.rbegin() ;
271 for(int i=0; i<NbStarInBlock; i++, StarHistReIter++)
272 LastBlockStarVec.push_back((*StarHistReIter).second);
273
274#ifdef SSTChassLog
275 if(NbStarInBlock>0) {
276 // On écrit les étoiles detectées
277 vector<SSTEtoile>::reverse_iterator StarVecRevIter;
278 StarVecRevIter=LastBlockStarVec.rbegin();
279 for(int i=0; i<NbStarInBlock;i++,StarVecRevIter++)
280 (*StarVecRevIter).print(sstchass);
281 }
282#endif
283
284#ifdef SSTStatLog
285 int nbBlockincircle=(int) (PeriodRotTeles/(archParam.acq.perEch*72.));
286// if((compteurBlock%nbBlockincircle)==0)
287 {
288// if((compteurBlock%5)==0) {
289 sststat<<numero_block(blk)<<'\t'<<nbStar<<'\t'<<noStarDet<<endl;
290 nbStar=0;
291 }
292 nbStar+=NbStarInBlock;
293 compteurBlock++;
294#endif
295
296 return NbStarInBlock;
297}
298
299void SSTHandler::findStarConstruct() {
300 PisteBar=new PisteEtoile*[NbPhotDiodBarette];
301 for(int i=0; i<NbPhotDiodBarette; i++) PisteBar[i]=new PisteEtoile(i);
302 StarHistoryMap.clear();
303 if (!sstchassinit) {
304 sstchass<<LastStar.printHeader()<<endl;
305 sstchassinit = true;
306 }
307
308#ifdef SSTDUMP
309 char s[32];
310 string fileName;
311// if(pPisteDump==NULL)
312 pPisteDump= new ofstream* [NbChanDump];
313
314 for (int pistNumb=0; pistNumb<NbChanDump; pistNumb++) {
315 sprintf(s,"%i", pistNumb);
316 string piste=s;
317 fileName="pisteno"+piste+"dump";
318/*
319 if ((*pPisteDump[pistNumb]).is_open()) {
320 cerr<<"Erreur: le fichier "<<fileName<<" devrait être libre"<<endl;
321 //On libère
322 (*pPisteDump[pistNumb]).close();
323 delete pPisteDump[pistNumb];
324 }
325*/
326 pPisteDump[pistNumb]=new ofstream(fileName.c_str(),ios::out|ios::app);
327
328 if(!(*pPisteDump[pistNumb]).is_open()) {
329 cerr<<"Warning Echec à l'ouverture du fichier: "<<fileName<<endl;
330 }
331 }
332#endif
333 return;
334}
335
336void SSTHandler::findStarDestruct() {
337 for(int i=0; i<NbPhotDiodBarette; i++) {
338 delete PisteBar[i];
339 }
340 delete[] PisteBar;
341
342#ifdef SSTDUMP
343 for (int pistNumb=0; pistNumb<NbChanDump; pistNumb++) {
344 pPisteDump[pistNumb]->close();
345 delete pPisteDump[pistNumb];
346 }
347 delete[] pPisteDump;
348#endif
349
350 return;
351}
352
353int SSTHandler::getNumbStar(int iSampl) {
354 StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
355 StarHistIter IterHigh=StarHistoryMap.upper_bound((double)(iSampl+1.));
356 int Compteur=0;
357 while (!(IterLow==IterHigh)) {IterLow++; Compteur++;}
358 return Compteur;
359}
360
361double SSTHandler::getStarF(int iSampl, int istar){
362 if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<=istar)) return -1;
363 else {
364 StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
365 while(!(istar==0)) {
366 IterLow++;
367 istar--;
368 }
369 return (double) ((*IterLow).second).InpCurrent;
370 }
371}
372
373
374int SSTHandler::getStarZ(int iSampl, int istar) {
375 if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<=istar)) return -1;
376 StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
377 while(!(istar==0)) {
378 IterLow++;
379 istar--;
380 }
381 return ((*IterLow).second).NoDiode;
382}
383
384double SSTHandler::getStarTime(int iSampl, int istar) {
385 if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<=istar)) return -1;
386 StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
387 while(!(istar==0)) {
388 IterLow++;
389 istar--;
390 }
391 return ((*IterLow).second).TEchan;
392}
393
394int SSTHandler::FirstSplNb(block_type_sst* blk){
395 return NbSampleBlock*numero_block(blk); // BUGG A verifier
396}
397
398
399void SSTHandler::ProcessBlock(block_type_sst* blk)
400{
401 lastBlkNum = numero_block(blk);
402 for (int i = 0; i<nb_per_block*2; i++) {
403 DecodeTMBlock(blk, i, diodeRaw[i]);
404 }
405 if (prcTodo & permDiode) {
406 PermutDiode();
407 }
408 if (prcTodo & findStars) {
409 FindStars(blk);
410 }
411}
412
Note: See TracBrowser for help on using the repository browser.