21 L2EmcDb::L2EmcDb(
char *inpP,
char *logP) {
22 printf(
"L2EmcDb::constr: ver20, inpPath='%s', logPath='%s'\n",inpP,logP);
23 strncpy(inpPath,inpP,mxTxt);
24 strncpy(logPath,logP,mxTxt);
36 L2EmcDb::initRun(
int runNo){
37 if(runNo==run_number)
return 0;
38 if(runNo>0) finishRun();
43 printf(
"L2EmcDb::initRun, inpPath='%s', logPath='%s'\n",inpPath,logPath);
45 sprintf(fname,
"%s/btowDb.current",inpPath); err+=readAsciiDb(fname,db_labels[0]);
46 sprintf(fname,
"%s/etowDb.current",inpPath); err+=readAsciiDb(fname,db_labels[1]);
48 sprintf(fname,
"%s/btowCrateMask.current",inpPath); err+=changeMaskFullCrate(fname,
'B',db_labels[2]);
49 sprintf(fname,
"%s/etowCrateMask.current",inpPath); err+=changeMaskFullCrate(fname,
'E',db_labels[3]);
51 sprintf(fname,
"%s/%s",inpPath,maskFile); err+=changeMaskByName(fname,db_labels[4]);
52 sprintf(fname,
"%s/%s",inpPath,pedFile); err+=changePedsByName(fname,db_labels[5]);
55 printf(
"\n\n total CRASH of L2EmcDb::initRun(%d), reason=some error in ASCII DB\n\n",runNo);
58 printf(
"L2EmcDb::initRun(%d) done\n", runNo);
67 if(run_number<0)
return;
72 sprintf(dbFname,
"%s/run%d.l2db.out",logPath,run_number);
75 FILE *dbFd=fopen(dbFname,
"w");
77 fprintf(dbFd,
"#L2EmcDb finishRun(%d), compiled: %s , %s\n",run_number,__DATE__,__TIME__);
80 for(i=0;i<txMxLbl;i++) fprintf(dbFd,
"#%d-%s",i,db_labels[i]);
84 printf(
"L2EmcDb:finishRun() dbDump='%s' ...\n",dbFname);
87 fprintf(dbFd,
"L2EmcDb:FinishRun(%d) dbDump NOT saved, I/O error\n",run_number);
103 L2EmcDb::clearTables(){
106 for(i=0; i<EmcDbIndexMax; i++)
107 clearItem(dbByIndex+i);
109 memset(db_labels,0,
sizeof(db_labels));
115 L2EmcDb::clearItem(
struct EmcCDbItem *x) {
118 x->crate=-1; x->chan=-1;
128 x->stat=0xaa; x->fail=0xbb;
137 L2EmcDb::printItem(
const EmcCDbItem *x) {
139 if(x==0) { printf(
" NULL pointer\n");
return; }
142 printf(
" item not defined ???\n");
146 if(strchr(x->name,
'U') || strchr(x->name,
'V') )
147 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);
149 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);
155 L2EmcDb::getByIndex(
int i){
156 if(i<0 || i>=EmcDbIndexMax)
return 0;
163 L2EmcDb::importItem(EmcCDbItem *x, FILE *fd){
175 char * ret=fgets(buf,mx,fd);
179 if(buf[0]==
'#')
return 1;
182 int ret1=sscanf(buf,
"%s",name0);
185 if(ret1==0)
return -1;
188 if(name0[2]==
'U' || name0[2]==
'V') {
189 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);
191 else if (name0[2]==
'T' || name0[2]==
'P' || name0[2]==
'Q' || name0[2]==
'R'|| name0[2]==
't' ) {
192 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);
200 if(n!=13)
return -1000-n;
209 L2EmcDb::name2index(
char *name){
213 assert(sec>0 && sec<=12);
217 if(sec<10 && name[0]!=
'0') name--;
221 case 'R': index+=100;
222 case 'Q': index+=100;
223 case 'P': index+=100;
226 assert(sub>=
'A' && sub<=
'E');
227 int eta=atoi(name+4);
228 assert(eta>0 && eta <=12);
229 index+=(sub-
'A')*12 +eta;
231 case 'V': index+=300;
232 case 'U': {index+=400;
233 int strip=atoi(name+3);
234 assert(strip>0 && strip<=288);
238 case 't' : index= EindexMax+BtowName2Index(sec,name+3);
break;
241 printf(
"EEname2Index('%s') Logical Error3: invalid index=%d\n",name,index);
248 assert(index<EmcDbIndexMax);
257 L2EmcDb::isEmpty(
const EmcCDbItem *x){
258 if(x==0)
return true;
259 return x->name[0]==0;
265 L2EmcDb::isBTOW(
const EmcCDbItem *x){
266 if (isEmpty( x))
return false;
267 if (x->name[2]!=
't')
return false;
268 if (x->crate<1 || x->crate> BTOW_MAXFEE)
return false;
269 if (x->chan<0 || x->chan > BTOW_DATSIZE )
return false;
276 L2EmcDb::isETOW(
const EmcCDbItem *x){
277 if (isEmpty( x))
return false;
278 if (x->name[2]!=
'T')
return false;
279 if (x->crate<1 || x->crate> ETOW_MAXFEE)
return false;
280 if (x->chan<0 || x->chan > ETOW_DATSIZE )
return false;
287 L2EmcDb::BtowName2Index(
int sect,
char *xee){
293 int etaB=atoi(xee+1);
296 if(etaB<1 || etaB>40) {
297 printf(
"BtowName2Index() Logical Error5: invalid sect=%d, xee=%s=\n",sect,xee);
301 if(sub<'a' || sub>
'j') {
302 printf(
"BtowName2Index() Logical Error6: invalid sect=%d, xee=%s=\n",sect,xee);
305 index=etaB +400*(sect-1) + (sub-
'a')*40;
316 L2EmcDb::readAsciiDb(
char *fname,
char *lbl) {
323 if(fd==0) { err=-1;
goto crashIt;}
328 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
335 err = importItem(&x,fd);
339 if(err <0)
goto crashIt;
342 int key= name2index(x.name);
347 if(key<1 || key>=EmcDbIndexMax) { err=-22;
goto crashIt;}
353 struct EmcCDbItem *y=dbByIndex+key;
355 if(!isEmpty(y)) { err=999;
goto crashIt; }
363 printf(
"L2EmcDb::readAsciiDb(%s) ... nL=%d nR=%d\n",fname,nL,nR);
367 printf(
"\n\n total CRASH of L2EmcDb::readAsciiDb(%s) err=%d nL=%d\n\n",fname,err,nL);
376 L2EmcDb::exportItem(
struct EmcCDbItem *x, FILE *fd) {
378 if(x->name[0]==0)
return;
380 if(strchr(x->name,
'U') || strchr(x->name,
'V') )
381 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);
383 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);
390 L2EmcDb::writeAsciiDb(FILE *fd) {
391 fprintf(fd,
"#------------------------------\n");
392 fprintf(fd,
"#TOWERS: name crate chan sec sub etaBin gain ped thres stat fail tube rdo\n");
395 for(i=0; i<EmcDbIndexMax ; i++) {
396 EmcCDbItem *x=dbByIndex+i;
398 if(isEmpty(x))
continue;
403 fprintf(fd,
"# total %d items out of %d possible\n",n2,n1);
410 L2EmcDb::changeMaskFullCrate(
const char *fname,
char BEflag,
char *lbl) {
418 printf(
"L2EmcDb::changeMaskFullCrate(%s)\n",fname);
419 FILE *fd=fopen(fname,
"r");
425 if(fd==0)
goto crashIt;
427 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
429 unsigned int crate, stat, fail;
432 char *ret=fgets(buf,mx,fd);
436 if(buf[0]==
'#')
continue;
437 int n=sscanf(buf,
"%x %x %x",&crate, &stat, &fail);
438 if(n!=3) { err=-2;
goto crashIt; }
439 printf(
"# crate=0x%02x set: stat=0x%x fail=0x%x\n",crate,stat,fail);
441 for(i=0; i<EmcDbIndexMax; i++) {
442 struct EmcCDbItem *x=dbByIndex+i;
443 if(isEmpty(x)) {
continue; }
444 if(x->crate!=(
int)crate)
continue;
445 if ((BEflag==
'B' && isBTOW(x)) ||
446 (BEflag==
'E' && isETOW(x))) {
456 printf(
" change stat & fail bits done inpLine=%d nChanged=%d\n",nl,nd);
return 0;
459 printf(
"\n\n total CRASH of L2EmcDb::changeMaskFullCrate(%s) err=%d nL=%d\n\n",fname,err,nl);
466 L2EmcDb::changePedsByName(
const char *fname,
char *lbl) {
473 printf(
"L2EmcDb::changePedsByName(%s)\n",fname);
474 FILE *fd=fopen(fname,
"r");
480 if(fd==0)
goto crashIt;
482 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
488 char *ret=fgets(buf,mx,fd);
491 if(buf[0]==
'#')
continue;
492 int n=sscanf(buf,
"%s %f %f",cVal,&pVal,&sVal);
493 if(n!=3) { err=-2;
goto crashIt; }
494 int key= name2index(cVal);
495 struct EmcCDbItem *x=dbByIndex+key;
497 if(isEmpty(x)) { ne++;
continue; }
501 if(pVal<0) { printf(
"Ignore: %s",buf);
continue; }
509 printf(
" changePedsByName() done inpLine=%d nChanged=%d nNotMappedAndIgnored=%d \n",nl,nd,ne);
return 0;
512 printf(
"\n\n total CRASH of L2EmcDb::changePedsByName(%s) err=%d nL=%d\n\n",fname,err,nl);
519 L2EmcDb::changeMaskByName(
const char *fname,
char *lbl) {
526 printf(
"L2EmcDb::changeMaskByName(%s)\n",fname);
527 FILE *fd=fopen(fname,
"r");
533 if(fd==0)
goto crashIt;
535 unsigned int stat, fail;
536 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
539 char *ret=fgets(buf,mx,fd);
543 if(buf[0]==
'#')
continue;
544 int n=sscanf(buf,
"%s %x %x",cVal,&stat, &fail);
545 if(n!=3) { err=-2;
goto crashIt; }
546 int key= name2index(cVal);
547 struct EmcCDbItem *x=dbByIndex+key;
549 if(isEmpty(x)) { ne++;
continue; }
559 printf(
" changeMaskByName() done inpLine=%d nChanged=%d nNotMappedAndIgnored=%d \n",nl,nd,ne);
return 0;
562 printf(
"\n\n total CRASH of L2EmcDb::changeMaskByName(%s) err=%d nL=%d\n\n",fname,err,nl);
void setPedFile(const char *c)
void setMaskFile(const char *c)