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

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

bugs divers

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