| 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 | #ifdef SST_DUMPLOG
|
|---|
| 18 | ofstream SSTHandler::sstchass("SSTChassLogFile");
|
|---|
| 19 | bool SSTHandler::sstchassinit=false;
|
|---|
| 20 | #endif
|
|---|
| 21 |
|
|---|
| 22 | // diodpermut[i] = channel de la diode i
|
|---|
| 23 | int 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 |
|
|---|
| 31 | SSTHandler::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 |
|
|---|
| 50 | SSTHandler::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 |
|
|---|
| 71 | SSTHandler& 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 |
|
|---|
| 85 | SSTHandler::~SSTHandler(){
|
|---|
| 86 | delete[] diodeT;
|
|---|
| 87 | findStarDestruct();
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void 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 |
|
|---|
| 98 | bool SSTHandler::has2bars = false;
|
|---|
| 99 | int SSTHandler::elecOffset = 1;
|
|---|
| 100 |
|
|---|
| 101 | void SSTHandler::Has2Bars(bool has, int eo)
|
|---|
| 102 | {
|
|---|
| 103 | has2bars = has;
|
|---|
| 104 | elecOffset = eo;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | void 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 |
|
|---|
| 134 | void 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 |
|
|---|
| 168 | int 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 |
|
|---|
| 174 | int 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 | int SSTHandler::FindStars(block_type_sst* blk) {
|
|---|
| 183 | NbStarInBlock=0;
|
|---|
| 184 | LastBlockStarVec.clear();
|
|---|
| 185 |
|
|---|
| 186 | int NoFirstSpInBlock=FirstSplNb(blk);
|
|---|
| 187 | int jd0 = diodeHistLength-NbSampleBlock;
|
|---|
| 188 |
|
|---|
| 189 | bool FlagLBlockPerdu=false;
|
|---|
| 190 | int ThisBlockNumb=numero_block(blk);
|
|---|
| 191 |
|
|---|
| 192 | if ((LastBlockSSTNb+1)==ThisBlockNumb) LastBlockSSTNb++;
|
|---|
| 193 | else {
|
|---|
| 194 | FlagLBlockPerdu=true;
|
|---|
| 195 | LastBlockSSTNb=ThisBlockNumb;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | // On cherche les etoiles
|
|---|
| 199 | for(int NoDiode=0;NoDiode<NbPhotDiodBarette; NoDiode++) {
|
|---|
| 200 | pPiste=PisteBar[NoDiode];
|
|---|
| 201 |
|
|---|
| 202 | // Traitons une diode pendant un block
|
|---|
| 203 | offseDataDiod=0;
|
|---|
| 204 | for (int i=0; i<NbSampleBlock; i++) Diodedata[i]=diode(jd0+i,NoDiode);
|
|---|
| 205 |
|
|---|
| 206 | if (FlagLBlockPerdu) {
|
|---|
| 207 | pPiste->fill(Diodedata+offseDataDiod,FirstSplNb(blk),PhDiodTabLong);
|
|---|
| 208 | offseDataDiod+=PhDiodTabLong;
|
|---|
| 209 | }
|
|---|
| 210 | else {
|
|---|
| 211 | pPiste->push(Diodedata,Pousslong);
|
|---|
| 212 | offseDataDiod+=Pousslong;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | // Recherchons les etoiles
|
|---|
| 216 | while (true) {
|
|---|
| 217 | if (pPiste->traque()) { // On a trouve!
|
|---|
| 218 | LastStar=pPiste->DonneEtoile();
|
|---|
| 219 | /* double dum= LastStar.TEchan;
|
|---|
| 220 | pair<const double, SSTEtoile> unepaire=make_pair(dum,LastStar);
|
|---|
| 221 | StarHistoryMap.insert(unepaire);
|
|---|
| 222 | */
|
|---|
| 223 | StarHistoryMap[LastStar.TEchan]=LastStar;
|
|---|
| 224 | //On empile sur la map.
|
|---|
| 225 | NbStarInBlock++;
|
|---|
| 226 | #ifdef SST_DEBUG
|
|---|
| 227 | // On écrit les pistes ayant déclénchées
|
|---|
| 228 | int NoDiodeEvt=LastStar.NoDiode;
|
|---|
| 229 | if(pPisteDump[NoDiodeEvt]->is_open())
|
|---|
| 230 | for(int noSamp=0; noSamp<NbSampleBlock;noSamp++)
|
|---|
| 231 | (*pPisteDump[NoDiodeEvt])<<noStarDet<<'\t'<<NoDiode<<'\t'<<NoFirstSpInBlock+noSamp<<'\t'<<Diodedata[noSamp]<<endl;
|
|---|
| 232 | #endif
|
|---|
| 233 | noStarDet++;
|
|---|
| 234 | }
|
|---|
| 235 | // Est-on en bout de Piste?
|
|---|
| 236 | if (offseDataDiod+Pousslong > NbSampleBlock) break;
|
|---|
| 237 | pPiste->push(Diodedata+offseDataDiod,Pousslong);
|
|---|
| 238 | offseDataDiod+=Pousslong;
|
|---|
| 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_DUMPLOG
|
|---|
| 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 |
|
|---|
| 274 | void SSTHandler::findStarConstruct() {
|
|---|
| 275 | PisteBar=new PisteEtoile*[NbPhotDiodBarette];
|
|---|
| 276 | for(int i=0; i<NbPhotDiodBarette; i++) PisteBar[i]=new PisteEtoile(i);
|
|---|
| 277 | StarHistoryMap.clear();
|
|---|
| 278 | #ifdef SST_DUMPLOG
|
|---|
| 279 | if (!sstchassinit) {
|
|---|
| 280 | sstchass<<LastStar.printHeader()<<endl;
|
|---|
| 281 | sstchassinit = true;
|
|---|
| 282 | }
|
|---|
| 283 | #endif
|
|---|
| 284 |
|
|---|
| 285 | #ifdef SST_DEBUG
|
|---|
| 286 | char s[32];
|
|---|
| 287 | string fileName;
|
|---|
| 288 | // if(pPisteDump==NULL)
|
|---|
| 289 | pPisteDump= new ofstream* [NbChanDump];
|
|---|
| 290 |
|
|---|
| 291 | for (int pistNumb=0; pistNumb<NbChanDump; pistNumb++) {
|
|---|
| 292 | sprintf(s,"%i", pistNumb);
|
|---|
| 293 | string piste=s;
|
|---|
| 294 | fileName="pisteno"+piste+"dump";
|
|---|
| 295 | /*
|
|---|
| 296 | if ((*pPisteDump[pistNumb]).is_open()) {
|
|---|
| 297 | cerr<<"Erreur: le fichier "<<fileName<<" devrait être libre"<<endl;
|
|---|
| 298 | //On libère
|
|---|
| 299 | (*pPisteDump[pistNumb]).close();
|
|---|
| 300 | delete pPisteDump[pistNumb];
|
|---|
| 301 | }
|
|---|
| 302 | */
|
|---|
| 303 | pPisteDump[pistNumb]=new ofstream(fileName.c_str(),ios::out|ios::trunc);
|
|---|
| 304 |
|
|---|
| 305 | if(!(*pPisteDump[pistNumb]).is_open()) {
|
|---|
| 306 | cerr<<"Warning Echec à l'ouverture du fichier: "<<fileName<<endl;
|
|---|
| 307 | }
|
|---|
| 308 | }
|
|---|
| 309 | #endif
|
|---|
| 310 | return;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | void SSTHandler::findStarDestruct() {
|
|---|
| 314 | for(int i=0; i<NbPhotDiodBarette; i++) {
|
|---|
| 315 | delete PisteBar[i];
|
|---|
| 316 | }
|
|---|
| 317 | delete[] PisteBar;
|
|---|
| 318 |
|
|---|
| 319 | #ifdef SST_DEBUG
|
|---|
| 320 | for (int pistNumb=0; pistNumb<NbChanDump; pistNumb++) {
|
|---|
| 321 | pPisteDump[pistNumb]->close();
|
|---|
| 322 | delete pPisteDump[pistNumb];
|
|---|
| 323 | }
|
|---|
| 324 | delete pPisteDump;
|
|---|
| 325 | #endif
|
|---|
| 326 |
|
|---|
| 327 | return;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | int SSTHandler::getNumbStar(int iSampl) {
|
|---|
| 331 | StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
|
|---|
| 332 | StarHistIter IterHigh=StarHistoryMap.upper_bound((double)(iSampl+1.));
|
|---|
| 333 | int Compteur=0;
|
|---|
| 334 | while (!(IterLow==IterHigh)) {IterLow++; Compteur++;}
|
|---|
| 335 | return Compteur;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | double SSTHandler::getStarF(int iSampl, int istar){
|
|---|
| 339 | if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<=istar)) return -1;
|
|---|
| 340 | else {
|
|---|
| 341 | StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
|
|---|
| 342 | while(!(istar==0)) {
|
|---|
| 343 | IterLow++;
|
|---|
| 344 | istar--;
|
|---|
| 345 | }
|
|---|
| 346 | return (double) ((*IterLow).second).InpCurrent;
|
|---|
| 347 | }
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 | int SSTHandler::getStarZ(int iSampl, int istar) {
|
|---|
| 352 | if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<=istar)) return -1;
|
|---|
| 353 | StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
|
|---|
| 354 | while(!(istar==0)) {
|
|---|
| 355 | IterLow++;
|
|---|
| 356 | istar--;
|
|---|
| 357 | }
|
|---|
| 358 | return ((*IterLow).second).NoDiode;
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | double SSTHandler::getStarTime(int iSampl, int istar) {
|
|---|
| 362 | if( (getNumbStar(iSampl)==0)||(getNumbStar(iSampl)<=istar)) return -1;
|
|---|
| 363 | StarHistIter IterLow=StarHistoryMap.lower_bound((double)iSampl);
|
|---|
| 364 | while(!(istar==0)) {
|
|---|
| 365 | IterLow++;
|
|---|
| 366 | istar--;
|
|---|
| 367 | }
|
|---|
| 368 | return ((*IterLow).second).TEchan;
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | int SSTHandler::FirstSplNb(block_type_sst* blk){
|
|---|
| 372 | return NbSampleBlock*numero_block(blk); // BUGG A verifier
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 | void SSTHandler::ProcessBlock(block_type_sst* blk)
|
|---|
| 377 | {
|
|---|
| 378 | lastBlkNum = numero_block(blk);
|
|---|
| 379 | if (prcTodo != 0) {
|
|---|
| 380 | for (int i = 0; i<nb_per_block*2; i++) {
|
|---|
| 381 | DecodeTMBlock(blk, i, diodeRaw[i]);
|
|---|
| 382 | }
|
|---|
| 383 | }
|
|---|
| 384 | if (prcTodo & permDiode) {
|
|---|
| 385 | PermutDiode();
|
|---|
| 386 | }
|
|---|
| 387 | if (prcTodo & findStars) {
|
|---|
| 388 | FindStars(blk);
|
|---|
| 389 | }
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|