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