5 #include <fakeRtsLog.h>
18 #include "L2EmcDb2012.h"
22 L2EmcDb2012::L2EmcDb2012(
char *inpP,
char *logP,
char *uid) {
23 LOG(DBG,
"L2EmcDb2012::constr: ver20, inpPath='%s', logPath='%s'\n",inpP,logP);
24 strncpy(inpPath,inpP,mxTxt);
25 strncpy(logPath,logP,mxTxt);
38 L2EmcDb2012::initRun(
int runNo){
39 if(runNo==run_number)
return 0;
40 if(runNo>0) finishRun();
45 LOG(DBG,
"L2EmcDb2012::initRun, inpPath='%s', logPath='%s'\n",inpPath,logPath);
47 sprintf(fname,
"%s/btowDb.current",inpPath); err+=readAsciiDb(fname,db_labels[0]);
48 sprintf(fname,
"%s/etowDb.current",inpPath); err+=readAsciiDb(fname,db_labels[1]);
50 sprintf(fname,
"%s/btowCrateMask.current",inpPath); err+=changeMaskFullCrate(fname,
'B',db_labels[2]);
51 sprintf(fname,
"%s/etowCrateMask.current",inpPath); err+=changeMaskFullCrate(fname,
'E',db_labels[3]);
53 sprintf(fname,
"%s/%s",inpPath,maskFile); err+=changeMaskByName(fname,db_labels[4]);
54 sprintf(fname,
"%s/%s",inpPath,pedFile); err+=changePedsByName(fname,db_labels[5]);
57 LOG(CRIT,
"total CRASH of L2EmcDb2012::initRun(%d), reason=some error in ASCII DB\n\n",runNo);
60 LOG(DBG,
"L2EmcDb2012::initRun(%d) done\n", runNo);
67 L2EmcDb2012::finishRun(){
69 if(run_number<0)
return;
74 sprintf(dbFname,
"%s/run%d.l2db.%s.out",logPath,run_number,muid);
77 FILE *dbFd=fopen(dbFname,
"w");
79 fprintf(dbFd,
"#L2EmcDb2012 finishRun(%d), compiled: %s , %s\n",run_number,__DATE__,__TIME__);
82 for(i=0;i<txMxLbl;i++) fprintf(dbFd,
"#%d-%s",i,db_labels[i]);
86 LOG(DBG,
"L2EmcDb2012:finishRun() dbDump='%s' ...\n",dbFname);
89 fprintf(dbFd,
"L2EmcDb2012:FinishRun(%d) dbDump NOT saved, I/O error\n",run_number);
98 L2EmcDb2012::~L2EmcDb2012(){
105 L2EmcDb2012::clearTables(){
108 for(i=0; i<EmcDbIndexMax; i++)
109 clearItem(dbByIndex+i);
111 memset(db_labels,0,
sizeof(db_labels));
117 L2EmcDb2012::clearItem(
struct EmcCDbItem *x) {
120 x->crate=-1; x->chan=-1;
130 x->stat=0xaa; x->fail=0xbb;
139 L2EmcDb2012::printItem(
const EmcCDbItem *x) {
141 if(x==0) { LOG(NOTE,
"NULL pointer\n");
return; }
144 printf(
" item not defined ???\n");
148 if(strchr(x->name,
'U') || strchr(x->name,
'V') )
149 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);
151 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);
157 L2EmcDb2012::getByIndex(
int i){
158 if(i<0 || i>=EmcDbIndexMax)
return 0;
165 L2EmcDb2012::importItem(EmcCDbItem *x, FILE *fd){
177 char * ret=fgets(buf,mx,fd);
181 if(buf[0]==
'#')
return 1;
184 int ret1=sscanf(buf,
"%s",name0);
187 if(ret1==0)
return -1;
190 if(name0[2]==
'U' || name0[2]==
'V') {
191 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);
193 else if (name0[2]==
'T' || name0[2]==
'P' || name0[2]==
'Q' || name0[2]==
'R'|| name0[2]==
't' ) {
194 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 L2EmcDb2012::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 LOG(ERR,
"EEname2Index('%s') Logical Error3: invalid index=%d\n",name,index);
246 assert(index<EmcDbIndexMax);
255 L2EmcDb2012::isEmpty(
const EmcCDbItem *x){
256 if(x==0)
return true;
257 return x->name[0]==0;
263 L2EmcDb2012::isBTOW(
const EmcCDbItem *x){
264 if (isEmpty( x))
return false;
265 if (x->name[2]!=
't')
return false;
266 if (x->crate<1 || x->crate> BTOW_MAXFEE)
return false;
267 if (x->chan<0 || x->chan > BTOW_DATSIZE )
return false;
274 L2EmcDb2012::isETOW(
const EmcCDbItem *x){
275 if (isEmpty( x))
return false;
276 if (x->name[2]!=
'T')
return false;
277 if (x->crate<1 || x->crate> ETOW_MAXFEE)
return false;
278 if (x->chan<0 || x->chan > ETOW_DATSIZE )
return false;
285 L2EmcDb2012::BtowName2Index(
int sect,
char *xee){
290 int etaB=atoi(xee+1);
293 if(etaB<1 || etaB>40) {
294 LOG(ERR,
"BtowName2Index() Logical Error5: invalid sect=%d, xee=%s=\n",sect,xee);
298 if(sub<'a' || sub>
'j') {
299 LOG(ERR,
"BtowName2Index() Logical Error6: invalid sect=%d, xee=%s=\n",sect,xee);
302 index=etaB +400*(sect-1) + (sub-
'a')*40;
311 L2EmcDb2012::readAsciiDb(
char *fname,
char *lbl) {
318 if(fd==0) { err=-1;
goto crashIt;}
322 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
329 err = importItem(&x,fd);
333 if(err <0)
goto crashIt;
336 int key= name2index(x.name);
340 if(key<1 || key>=EmcDbIndexMax) { err=-22;
goto crashIt;}
346 struct EmcCDbItem *y=dbByIndex+key;
348 if(!isEmpty(y)) { err=999;
goto crashIt; }
356 LOG(DBG,
"L2EmcDb2012::readAsciiDb(%s) ... nL=%d nR=%d\n",fname,nL,nR);
360 LOG(CRIT,
"total CRASH of L2EmcDb2012::readAsciiDb(%s) err=%d nL=%d\n\n",fname,err,nL);
369 L2EmcDb2012::exportItem(
struct EmcCDbItem *x, FILE *fd) {
371 if(x->name[0]==0)
return;
373 if(strchr(x->name,
'U') || strchr(x->name,
'V') )
374 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);
376 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);
383 L2EmcDb2012::writeAsciiDb(FILE *fd) {
384 fprintf(fd,
"#------------------------------\n");
385 fprintf(fd,
"#TOWERS: name crate chan sec sub etaBin gain ped thres stat fail tube rdo\n");
388 for(i=0; i<EmcDbIndexMax ; i++) {
389 EmcCDbItem *x=dbByIndex+i;
391 if(isEmpty(x))
continue;
395 fprintf(fd,
"# total %d items out of %d possible\n",n2,n1);
402 L2EmcDb2012::changeMaskFullCrate(
const char *fname,
char BEflag,
char *lbl) {
410 LOG(DBG,
"L2EmcDb2012::changeMaskFullCrate(%s)\n",fname);
411 FILE *fd=fopen(fname,
"r");
417 if(fd==0)
goto crashIt;
419 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
421 unsigned int crate, stat, fail;
424 char *ret=fgets(buf,mx,fd);
428 if(buf[0]==
'#')
continue;
429 int n=sscanf(buf,
"%x %x %x",&crate, &stat, &fail);
430 if(n!=3) { err=-2;
goto crashIt; }
431 LOG(DBG,
"# crate=0x%02x set: stat=0x%x fail=0x%x\n",crate,stat,fail);
433 for(i=0; i<EmcDbIndexMax; i++) {
434 struct EmcCDbItem *x=dbByIndex+i;
435 if(isEmpty(x)) {
continue; }
436 if(x->crate!=(
int)crate)
continue;
437 if ((BEflag==
'B' && isBTOW(x)) ||
438 (BEflag==
'E' && isETOW(x))) {
448 LOG(DBG,
"change stat & fail bits done inpLine=%d nChanged=%d\n",nl,nd);
return 0;
451 LOG(CRIT,
"total CRASH of L2EmcDb2012::changeMaskFullCrate(%s) err=%d nL=%d\n\n",fname,err,nl);
458 L2EmcDb2012::changePedsByName(
const char *fname,
char *lbl) {
465 LOG(DBG,
"L2EmcDb2012::changePedsByName(%s)\n",fname);
466 FILE *fd=fopen(fname,
"r");
472 if(fd==0)
goto crashIt;
474 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
480 char *ret=fgets(buf,mx,fd);
483 if(buf[0]==
'#')
continue;
484 int n=sscanf(buf,
"%s %f %f",cVal,&pVal,&sVal);
485 if(n!=3) { err=-2;
goto crashIt; }
486 int key= name2index(cVal);
487 struct EmcCDbItem *x=dbByIndex+key;
489 if(isEmpty(x)) { ne++;
continue; }
492 if(pVal<0) { LOG(DBG,
"Ignore: %s",buf);
continue; }
500 LOG(DBG,
"changePedsByName() done inpLine=%d nChanged=%d nNotMappedAndIgnored=%d \n",nl,nd,ne);
return 0;
503 LOG(CRIT,
"total CRASH of L2EmcDb2012::changePedsByName(%s) err=%d nL=%d\n\n",fname,err,nl);
510 L2EmcDb2012::changeMaskByName(
const char *fname,
char *lbl) {
517 LOG(DBG,
"L2EmcDb2012::changeMaskByName(%s)\n",fname);
518 FILE *fd=fopen(fname,
"r");
524 if(fd==0)
goto crashIt;
526 unsigned int stat, fail;
527 if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1);
530 char *ret=fgets(buf,mx,fd);
534 if(buf[0]==
'#')
continue;
535 int n=sscanf(buf,
"%s %x %x",cVal,&stat, &fail);
536 if(n!=3) { err=-2;
goto crashIt; }
537 int key= name2index(cVal);
538 struct EmcCDbItem *x=dbByIndex+key;
540 if(isEmpty(x)) { ne++;
continue; }
549 LOG(DBG,
"changeMaskByName() done inpLine=%d nChanged=%d nNotMappedAndIgnored=%d \n",nl,nd,ne);
return 0;
552 LOG(CRIT,
"total CRASH of L2EmcDb2012::changeMaskByName(%s) err=%d nL=%d\n\n",fname,err,nl);
void setPedFile(const char *c)
void setMaskFile(const char *c)