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

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

bug

File size: 10.0 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 // cout << "find " << NoFirstSpInBlock << "\n";
187 bool FlagLBlockPerdu=false;
188 int ThisBlockNumb=numero_block(blk);
189
190 if ((LastBlockSSTNb+1)==ThisBlockNumb) LastBlockSSTNb++;
191 else {
192 FlagLBlockPerdu=true;
193 LastBlockSSTNb=ThisBlockNumb;
194 }
195
196// On cherche les etoiles
197 for(int NoDiode=0;NoDiode<NbPhotDiodBarette; NoDiode++) {
198 pPiste=PisteBar[NoDiode];
199
200 // Traitons une diode pendant un block
201 offseDataDiod=0;
202 for (int i=0; i<NbSampleBlock; i++) Diodedata[i]=diode(jd0+i,NoDiode);
203
204 if (FlagLBlockPerdu) {
205 pPiste->fill(Diodedata+offseDataDiod,FirstSplNb(blk));
206 offseDataDiod+=PhDiodTabLong;
207 }
208 else {
209 pPiste->push(Diodedata,Pousslong);
210 offseDataDiod+=Pousslong;
211 }
212
213 // Recherchons les etoiles
214 while (true) {
215 if (pPiste->traque()) { // On a trouve!
216 LastStar=pPiste->DonneEtoile();
217/* double dum= LastStar.TEchan;
218 pair<const double, SSTEtoile> unepaire=make_pair(dum,LastStar);
219 StarHistoryMap.insert(unepaire);
220*/
221 StarHistoryMap[LastStar.TEchan]=LastStar;
222 //On empile sur la map.
223 NbStarInBlock++;
224#ifdef SST_DEBUG
225 // On écrit les pistes ayant déclénchées
226 int NoDiodeEvt=LastStar.NoDiode;
227 if(pPisteDump[NoDiodeEvt]->is_open())
228 for(int noSamp=0; noSamp<NbSampleBlock;noSamp++)
229 (*pPisteDump[NoDiodeEvt])<<noStarDet<<'\t'<<NoDiode<<'\t'<<NoFirstSpInBlock+noSamp<<'\t'<<Diodedata[noSamp]<<endl;
230#endif
231 noStarDet++;
232 }
233 // Est-on en bout de Piste?
234 int npouss = Pousslong;
235 if (offseDataDiod+npouss > NbSampleBlock) npouss = NbSampleBlock-offseDataDiod;
236 if (npouss<=0) break;
237 pPiste->push(Diodedata+offseDataDiod,npouss);
238 offseDataDiod+=npouss;
239 }
240 }
241
242// On ne garde que les NbEtInSetMax dernières étoiles
243 int nbEtoileInMap=StarHistoryMap.size();
244 if(nbEtoileInMap>NbEtInMapMax) {
245 int nbErase=nbEtoileInMap-NbEtInMapMax;
246 StarHistIter StarIter=StarHistoryMap.begin();
247 for(int i=0;i<nbErase;i++){
248 StarIter++;
249 }
250 StarHistoryMap.erase(StarHistoryMap.begin(),StarIter);
251 nbEtoileInMap-=nbErase;
252 }
253
254// On stocke les etoiles du block dans LastBlockStarVec
255 map<double,SSTEtoile>::reverse_iterator StarHistReIter= StarHistoryMap.rbegin() ;
256 for(int i=0; i<NbStarInBlock; i++, StarHistReIter++)
257 LastBlockStarVec.push_back((*StarHistReIter).second);
258
259//#ifdef SST_DEBUG
260 if(NbStarInBlock>0) {
261
262 // On écrit les étoiles detectées
263 vector<SSTEtoile>::reverse_iterator StarVecRevIter;
264 StarVecRevIter=LastBlockStarVec.rbegin();
265 for(int i=0; i<NbStarInBlock;i++,StarVecRevIter++)
266 (*StarVecRevIter).print(sstchass);
267 }
268
269//#endif
270
271 return NbStarInBlock;
272}
273
274void SSTHandler::findStarConstruct() {
275 PisteBar=new PisteEtoile*[NbPhotDiodBarette];
276 for(int i=0; i<NbPhotDiodBarette; i++) PisteBar[i]=new PisteEtoile(i);
277 StarHistoryMap.clear();
278 sstchass<<LastStar.printHeader()<<endl;
279
280#ifdef SST_DEBUG
281 char s[32];
282 string fileName;
283// if(pPisteDump==NULL)
284 pPisteDump= new ofstream* [NbChanDump];
285
286 for (int pistNumb=0; pistNumb<NbChanDump; pistNumb++) {
287 sprintf(s,"%i", pistNumb);
288 string piste=s;
289 fileName="pisteno"+piste+"dump";
290/*
291 if ((*pPisteDump[pistNumb]).is_open()) {
292 cerr<<"Erreur: le fichier "<<fileName<<" devrait être libre"<<endl;
293 //On libère
294 (*pPisteDump[pistNumb]).close();
295 delete pPisteDump[pistNumb];
296 }
297*/
298 pPisteDump[pistNumb]=new ofstream(fileName.c_str(),ios::out|ios::trunc);
299
300 if(!(*pPisteDump[pistNumb]).is_open()) {
301 cerr<<"Warning Echec à l'ouverture du fichier: "<<fileName<<endl;
302 }
303 }
304#endif
305 return;
306}
307
308void SSTHandler::findStarDestruct() {
309 for(int i=0; i<NbPhotDiodBarette; i++) {
310 delete PisteBar[i];
311 }
312 delete[] PisteBar;
313
314#ifdef SST_DEBUG
315 for (int pistNumb=0; pistNumb<NbChanDump; pistNumb++) {
316 pPisteDump[pistNumb]->close();
317 delete pPisteDump[pistNumb];
318 }
319 delete pPisteDump;
320#endif
321
322 return;
323}
324
325int SSTHandler::getNumbStar(int iSampl) {
326 StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
327 StarHistIter IterHigh=StarHistoryMap.upper_bound((double)(iSampl+1.));
328 int Compteur=0;
329 while (!(IterLow==IterHigh)) {IterLow++; Compteur++;}
330 return Compteur;
331}
332
333double SSTHandler::getStarF(int iSampl, int istar){
334 if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<=istar)) return -1;
335 else {
336 StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
337 while(!(istar==0)) {
338 IterLow++;
339 istar--;
340 }
341 return (double) ((*IterLow).second).InpCurrent;
342 }
343}
344
345
346int SSTHandler::getStarZ(int iSampl, int istar) {
347 if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<=istar)) return -1;
348 StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
349 while(!(istar==0)) {
350 IterLow++;
351 istar--;
352 }
353 return ((*IterLow).second).NoDiode;
354}
355
356double SSTHandler::getStarTime(int iSampl, int istar) {
357 if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<=istar)) return -1;
358 StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
359 while(!(istar==0)) {
360 IterLow++;
361 istar--;
362 }
363 return ((*IterLow).second).TEchan;
364}
365
366int SSTHandler::FirstSplNb(block_type_sst* blk){
367 return NbSampleBlock*numero_block(blk); // BUGG A verifier
368}
369
370
371void SSTHandler::ProcessBlock(block_type_sst* blk)
372{
373 lastBlkNum = numero_block(blk);
374 for (int i = 0; i<nb_per_block*2; i++) {
375 DecodeTMBlock(blk, i, diodeRaw[i]);
376 }
377 if (prcTodo & permDiode) {
378 PermutDiode();
379 }
380 if (prcTodo & findStars) {
381 FindStars(blk);
382 }
383}
384
Note: See TracBrowser for help on using the repository browser.