00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include <assert.h>
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "L2EmcDb.h"
00018
00019
00020
00021 L2EmcDb::L2EmcDb(char *inpP, char *logP) {
00022 printf("L2EmcDb::constr: ver20, inpPath='%s', logPath='%s'\n",inpP,logP);
00023 strncpy(inpPath,inpP,mxTxt);
00024 strncpy(logPath,logP,mxTxt);
00025 clearTables();
00026 setPedFile("pedestal.current");
00027 setMaskFile("towerMask.current");
00028 }
00029
00030 void L2EmcDb::setPedFile(const char *c){ strncpy(pedFile,c,mxTxt); }
00031 void L2EmcDb::setMaskFile(const char *c){ strncpy(maskFile,c,mxTxt); }
00032
00033
00034
00035 int
00036 L2EmcDb::initRun(int runNo){
00037 if(runNo==run_number) return 0;
00038 if(runNo>0) finishRun();
00039 clearTables();
00040 run_number=runNo;
00041 char fname[1000];
00042
00043 printf("L2EmcDb::initRun, inpPath='%s', logPath='%s'\n",inpPath,logPath);
00044 int err=0;
00045 sprintf(fname,"%s/btowDb.current",inpPath); err+=readAsciiDb(fname,db_labels[0]);
00046 sprintf(fname,"%s/etowDb.current",inpPath); err+=readAsciiDb(fname,db_labels[1]);
00047
00048 sprintf(fname,"%s/btowCrateMask.current",inpPath); err+=changeMaskFullCrate(fname,'B',db_labels[2]);
00049 sprintf(fname,"%s/etowCrateMask.current",inpPath); err+=changeMaskFullCrate(fname,'E',db_labels[3]);
00050
00051 sprintf(fname,"%s/%s",inpPath,maskFile); err+=changeMaskByName(fname,db_labels[4]);
00052 sprintf(fname,"%s/%s",inpPath,pedFile); err+=changePedsByName(fname,db_labels[5]);
00053
00054 if(err) {
00055 printf("\n\n total CRASH of L2EmcDb::initRun(%d), reason=some error in ASCII DB\n\n",runNo);
00056 return err;
00057 }
00058 printf("L2EmcDb::initRun(%d) done\n", runNo);
00059 return 0;
00060 }
00061
00062
00063
00064 void
00065 L2EmcDb::finishRun(){
00066
00067 if(run_number<0) return;
00068
00069
00070
00071 char dbFname[1000];
00072 sprintf(dbFname,"%s/run%d.l2db.out",logPath,run_number);
00073
00074
00075 FILE *dbFd=fopen(dbFname,"w");
00076 if(dbFd) {
00077 fprintf(dbFd,"#L2EmcDb finishRun(%d), compiled: %s , %s\n",run_number,__DATE__,__TIME__);
00078
00079 int i;
00080 for(i=0;i<txMxLbl;i++) fprintf(dbFd,"#%d-%s",i,db_labels[i]);
00081 fprintf(dbFd,"#\n");
00082 writeAsciiDb(dbFd);
00083 fclose(dbFd);
00084 printf("L2EmcDb:finishRun() dbDump='%s' ...\n",dbFname);
00085 } else {
00086 if (dbFd)
00087 fprintf(dbFd,"L2EmcDb:FinishRun(%d) dbDump NOT saved, I/O error\n",run_number);
00088 }
00089
00090 clearTables();
00091 }
00092
00093
00094
00095
00096 L2EmcDb::~L2EmcDb(){
00097 finishRun();
00098 }
00099
00100
00101
00102 void
00103 L2EmcDb::clearTables(){
00104 run_number=-4;
00105 int i;
00106 for(i=0; i<EmcDbIndexMax; i++)
00107 clearItem(dbByIndex+i);
00108
00109 memset(db_labels,0,sizeof(db_labels));
00110 }
00111
00112
00113
00114 void
00115 L2EmcDb::clearItem(struct EmcCDbItem *x) {
00116 x->name[0]=0;
00117 x->tube[0]=0;
00118 x->crate=-1; x->chan=-1;
00119 x->gain=-2;
00120 x->ped=-3;
00121 x->sec=-4;
00122 x->sub='Z';
00123 x->eta=-5;
00124 x->thr=-6;
00125 x->sigPed=-7;
00126 x->strip=-299;
00127 x->plane='X';
00128 x->stat=0xaa; x->fail=0xbb;
00129 x->rdo=123456;
00130 x->key=-999;
00131 }
00132
00133
00134
00135
00136 void
00137 L2EmcDb::printItem(const EmcCDbItem *x) {
00138 printf("gEmcCDb:");
00139 if(x==0) { printf(" NULL pointer\n"); return; }
00140
00141 if(x->name[0]==0) {
00142 printf(" item not defined ???\n");
00143 return;
00144 }
00145
00146 if(strchr(x->name,'U') || strchr(x->name,'V') )
00147 printf(" %s crate=%d chan=%3d sec=%2d plane=%c strip=%3d gain=%.3f ped=%.2f sPed=%.2f ADC_thr=%.2f stat=0x%4.4x fail=0x%4.4x pix=%s rdo=%d key=%d\n",x->name,x->crate,x->chan,x->sec,x->plane,x->strip,x->gain,x->ped,x->sigPed,x->thr,x->stat,x->fail,x->tube,x->rdo,x->key);
00148 else
00149 printf(" %s crate=%d chan=%3d sec=%2d sub=%c eta=%2d gain=%.3f ped=%.2f sPed=%.2f ADC_thr=%.2f stat=0x%4.4x fail=0x%4.4x tube=%s rdo=%d key=%d\n",x->name,x->crate,x->chan,x->sec,x->sub,x->eta,x->gain,x->ped,x->sigPed,x->thr,x->stat,x->fail,x->tube,x->rdo,x->key);
00150 }
00151
00152
00153
00154 const L2EmcDb::EmcCDbItem *
00155 L2EmcDb::getByIndex(int i){
00156 if(i<0 || i>=EmcDbIndexMax) return 0;
00157 return dbByIndex+i;
00158 }
00159
00160
00161
00162 int
00163 L2EmcDb::importItem(EmcCDbItem *x, FILE *fd){
00164
00165
00166
00167
00168
00169
00170
00171 clearItem(x);
00172 const int mx=1000;
00173 char buf[mx];
00174
00175 char * ret=fgets(buf,mx,fd);
00176
00177 if(ret==0) return 0;
00178
00179 if(buf[0]=='#') return 1;
00180
00181 char name0[mx];
00182 int ret1=sscanf(buf,"%s",name0);
00183
00184
00185 if(ret1==0) return -1;
00186
00187 int n=0;
00188 if(name0[2]=='U' || name0[2]=='V') {
00189 n=sscanf(buf,"%s %d %d %d %c %d %f %f %f %x %x %s %d",x->name,&x->crate,&x->chan,&x->sec,&x->plane,&x->strip,&x->gain,&x->ped,&x->thr,&x->stat,&x->fail,x->tube,&x->rdo);
00190 }
00191 else if (name0[2]=='T' || name0[2]=='P' || name0[2]=='Q' || name0[2]=='R'|| name0[2]=='t' ) {
00192 n=sscanf(buf,"%s %i %i %d %c %d %f %f %f %x %x %s %d",x->name,&x->crate,&x->chan,&x->sec,&x->sub,&x->eta,&x->gain,&x->ped,&x->thr,&x->stat,&x->fail,x->tube,&x->rdo);
00193 }
00194 else
00195 return -3;
00196
00197
00198
00199
00200 if(n!=13) return -1000-n;
00201
00202 return 2;
00203 }
00204
00205
00206
00207
00208 int
00209 L2EmcDb::name2index(char *name){
00210
00211 int index=-1;
00212 int sec=atoi(name);
00213 assert(sec>0 && sec<=12);
00214
00215 index=(sec-1)*1000;
00216
00217 if(sec<10 && name[0]!='0') name--;
00218 char key=name[2];
00219
00220 switch(key) {
00221 case 'R': index+=100;
00222 case 'Q': index+=100;
00223 case 'P': index+=100;
00224 case 'T': {
00225 int sub =name[3];
00226 assert(sub>='A' && sub<='E');
00227 int eta=atoi(name+4);
00228 assert(eta>0 && eta <=12);
00229 index+=(sub-'A')*12 +eta;
00230 } break;
00231 case 'V': index+=300;
00232 case 'U': {index+=400;
00233 int strip=atoi(name+3);
00234 assert(strip>0 && strip<=288);
00235 index+=strip;
00236 } break;
00237
00238 case 't' : index= EindexMax+BtowName2Index(sec,name+3); break;
00239
00240 default:
00241 printf("EEname2Index('%s') Logical Error3: invalid index=%d\n",name,index);
00242 exit(-1);
00243 }
00244
00245
00246
00247 assert(index>=0);
00248 assert(index<EmcDbIndexMax);
00249
00250 return index;
00251
00252 }
00253
00254
00255
00256 bool
00257 L2EmcDb::isEmpty(const EmcCDbItem *x){
00258 if(x==0) return true;
00259 return x->name[0]==0;
00260 }
00261
00262
00263
00264 bool
00265 L2EmcDb::isBTOW(const EmcCDbItem *x){
00266 if (isEmpty( x)) return false;
00267 if (x->name[2]!='t') return false;
00268 if (x->crate<1 || x->crate> BTOW_MAXFEE) return false;
00269 if (x->chan<0 || x->chan > BTOW_DATSIZE ) return false;
00270 return true;
00271 }
00272
00273
00274
00275 bool
00276 L2EmcDb::isETOW(const EmcCDbItem *x){
00277 if (isEmpty( x)) return false;
00278 if (x->name[2]!='T') return false;
00279 if (x->crate<1 || x->crate> ETOW_MAXFEE) return false;
00280 if (x->chan<0 || x->chan > ETOW_DATSIZE ) return false;
00281 return true;
00282 }
00283
00284
00285
00286 int
00287 L2EmcDb::BtowName2Index(int sect, char *xee){
00288
00289
00290 int index=-1;
00291
00292 char sub=xee[0];
00293 int etaB=atoi(xee+1);
00294
00295
00296 if(etaB<1 || etaB>40) {
00297 printf("BtowName2Index() Logical Error5: invalid sect=%d, xee=%s=\n",sect,xee);
00298 exit(-1);
00299 }
00300
00301 if(sub<'a' || sub>'j') {
00302 printf("BtowName2Index() Logical Error6: invalid sect=%d, xee=%s=\n",sect,xee);
00303 exit(-1);
00304 }
00305 index=etaB +400*(sect-1) + (sub-'a')*40;
00306
00307
00308
00309 return index;
00310 }
00311
00312
00313
00314
00315 int
00316 L2EmcDb::readAsciiDb(char *fname, char *lbl) {
00317 int err=77;
00318 FILE *fd=0;
00319 int nL=0;
00320 int nR=0;
00321
00322 fd=fopen(fname,"r");
00323 if(fd==0) { err=-1; goto crashIt;}
00324
00325 {
00326 const int mx=1000;
00327 char buf[mx];
00328 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
00329 }
00330 while (1) {
00331
00332
00333 struct EmcCDbItem x;
00334
00335 err = importItem(&x,fd);
00336 nL++;
00337 if(err==0) break;
00338 if(err==1) continue;
00339 if(err <0) goto crashIt;
00340
00341
00342 int key= name2index(x.name);
00343
00344
00345 x.key=key;
00346
00347 if(key<1 || key>=EmcDbIndexMax) { err=-22; goto crashIt;}
00348
00349
00350
00351
00352
00353 struct EmcCDbItem *y=dbByIndex+key;
00354
00355 if(!isEmpty(y)) { err=999; goto crashIt; }
00356 *y=x;
00357 nR++;
00358
00359
00360
00361 }
00362
00363 printf("L2EmcDb::readAsciiDb(%s) ... nL=%d nR=%d\n",fname,nL,nR);
00364 return 0;
00365
00366 crashIt:
00367 printf("\n\n total CRASH of L2EmcDb::readAsciiDb(%s) err=%d nL=%d\n\n",fname,err,nL);
00368 return err;
00369 }
00370
00371
00372
00373
00374
00375 void
00376 L2EmcDb::exportItem(struct EmcCDbItem *x, FILE *fd) {
00377
00378 if(x->name[0]==0) return;
00379
00380 if(strchr(x->name,'U') || strchr(x->name,'V') )
00381 fprintf(fd,"%s %3d %3d %2d %c %4d %.3f %.2f %.2f 0x%4.4x 0x%4.4x %s %d\n",x->name,x->crate,x->chan,x->sec,x->plane,x->strip,x->gain,x->ped,x->thr,x->stat,x->fail,x->tube,x->rdo);
00382 else
00383 fprintf(fd,"%s %d %3d %2d %c %2d %.3f %.2f %.2f 0x%4.4x 0x%4.4x %s %d\n",x->name,x->crate,x->chan,x->sec,x->sub,x->eta,x->gain,x->ped,x->thr,x->stat,x->fail,x->tube,x->rdo);
00384 }
00385
00386
00387
00388
00389 void
00390 L2EmcDb::writeAsciiDb(FILE *fd) {
00391 fprintf(fd,"#------------------------------\n");
00392 fprintf(fd,"#TOWERS: name crate chan sec sub etaBin gain ped thres stat fail tube rdo\n");
00393 int i;
00394 int n1=0,n2=0;
00395 for(i=0; i<EmcDbIndexMax ; i++) {
00396 EmcCDbItem *x=dbByIndex+i;
00397 n1++;
00398 if(isEmpty(x)) continue;
00399 exportItem(x,fd);
00400
00401 n2++;
00402 }
00403 fprintf(fd,"# total %d items out of %d possible\n",n2,n1);
00404 }
00405
00406
00407
00408
00409 int
00410 L2EmcDb::changeMaskFullCrate(const char *fname, char BEflag, char *lbl) {
00411
00412
00413
00414
00415
00416
00417
00418 printf("L2EmcDb::changeMaskFullCrate(%s)\n",fname);
00419 FILE *fd=fopen(fname,"r");
00420 int nd=0,nl=0;
00421 const int mx=1000;
00422 char buf[mx];
00423 int err=-1;
00424
00425 if(fd==0) goto crashIt;
00426
00427 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
00428
00429 uint crate, stat, fail;
00430
00431 while(1) {
00432 char *ret=fgets(buf,mx,fd);
00433 if(ret==0) break;
00434
00435 nl++;
00436 if(buf[0]=='#') continue;
00437 int n=sscanf(buf,"%x %x %x",&crate, &stat, &fail);
00438 if(n!=3) { err=-2; goto crashIt; }
00439 printf("# crate=0x%02x set: stat=0x%x fail=0x%x\n",crate,stat,fail);
00440 int i;
00441 for(i=0; i<EmcDbIndexMax; i++) {
00442 struct EmcCDbItem *x=dbByIndex+i;
00443 if(isEmpty(x)) { continue; }
00444 if(x->crate!=(int)crate) continue;
00445 if ((BEflag=='B' && isBTOW(x)) ||
00446 (BEflag=='E' && isETOW(x))) {
00447 x->stat=stat;
00448 x->fail=fail;
00449 nd++;
00450 }
00451 }
00452
00453 }
00454 fclose(fd);
00455
00456 printf(" change stat & fail bits done inpLine=%d nChanged=%d\n",nl,nd); return 0;
00457
00458 crashIt:
00459 printf("\n\n total CRASH of L2EmcDb::changeMaskFullCrate(%s) err=%d nL=%d\n\n",fname,err,nl);
00460 return err;
00461 }
00462
00463
00464
00465 int
00466 L2EmcDb::changePedsByName(const char *fname, char *lbl) {
00467
00468
00469
00470
00471
00472
00473 printf("L2EmcDb::changePedsByName(%s)\n",fname);
00474 FILE *fd=fopen(fname,"r");
00475 int nd=0,nl=0,ne=0;
00476 const int mx=1000;
00477 char buf[mx];
00478 int err=-1;
00479
00480 if(fd==0) goto crashIt;
00481
00482 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
00483
00484 char cVal[100];
00485 float pVal,sVal;
00486
00487 while(1) {
00488 char *ret=fgets(buf,mx,fd);
00489 if(ret==0) break;
00490 nl++;
00491 if(buf[0]=='#') continue;
00492 int n=sscanf(buf,"%s %f %f",cVal,&pVal,&sVal);
00493 if(n!=3) { err=-2; goto crashIt; }
00494 int key= name2index(cVal);
00495 struct EmcCDbItem *x=dbByIndex+key;
00496
00497 if(isEmpty(x)) { ne++; continue; }
00498
00499
00500
00501 if(pVal<0) { printf("Ignore: %s",buf); continue; }
00502 x->ped=pVal;
00503
00504
00505 nd++;
00506 }
00507 fclose(fd);
00508
00509 printf(" changePedsByName() done inpLine=%d nChanged=%d nNotMappedAndIgnored=%d \n",nl,nd,ne); return 0;
00510
00511 crashIt:
00512 printf("\n\n total CRASH of L2EmcDb::changePedsByName(%s) err=%d nL=%d\n\n",fname,err,nl);
00513 return err;
00514 }
00515
00516
00517
00518 int
00519 L2EmcDb::changeMaskByName(const char *fname, char *lbl) {
00520
00521
00522
00523
00524
00525
00526 printf("L2EmcDb::changeMaskByName(%s)\n",fname);
00527 FILE *fd=fopen(fname,"r");
00528 int nd=0,nl=0,ne=0;
00529 const int mx=1000;
00530 char buf[mx];
00531 int err=-1;
00532
00533 if(fd==0) goto crashIt;
00534 char cVal[100];
00535 uint stat, fail;
00536 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
00537
00538 while(1) {
00539 char *ret=fgets(buf,mx,fd);
00540 if(ret==0) break;
00541
00542 nl++;
00543 if(buf[0]=='#') continue;
00544 int n=sscanf(buf,"%s %x %x",cVal,&stat, &fail);
00545 if(n!=3) { err=-2; goto crashIt; }
00546 int key= name2index(cVal);
00547 struct EmcCDbItem *x=dbByIndex+key;
00548
00549 if(isEmpty(x)) { ne++; continue; }
00550
00551
00552
00553 x->stat=stat;
00554 x->fail=fail;
00555 nd++;
00556 }
00557 fclose(fd);
00558
00559 printf(" changeMaskByName() done inpLine=%d nChanged=%d nNotMappedAndIgnored=%d \n",nl,nd,ne); return 0;
00560
00561 crashIt:
00562 printf("\n\n total CRASH of L2EmcDb::changeMaskByName(%s) err=%d nL=%d\n\n",fname,err,nl);
00563 return err;
00564 }
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592