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