StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
L2EmcDb.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 
6 /*********************************************************************
7  * $Id: L2EmcDb.cxx,v 1.6 2010/04/18 06:05:32 pibero Exp $
8  * \author Jan Balewski, IUCF, 2006
9  *********************************************************************
10  * Descripion:
11  * StRoot-free DB container , common for BTOW + ETOW + ESMD
12  * see also: http://www.star.bnl.gov/protected/spin/balewski/2006-L2JetAlgo/algo-L2EmcDb/
13  *********************************************************************
14  */
15 
16 
17 #include "L2EmcDb.h"
18 
19 //=====================================
20 //=====================================
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);
25  clearTables();
26  setPedFile("pedestal.current");// default ped file
27  setMaskFile("towerMask.current");// default mask file
28 }
29 
30 void L2EmcDb::setPedFile(const char *c){ strncpy(pedFile,c,mxTxt); }
31 void L2EmcDb::setMaskFile(const char *c){ strncpy(maskFile,c,mxTxt); }
32 
33 //==============================
34 //==============================
35 int
36 L2EmcDb::initRun(int runNo){
37  if(runNo==run_number) return 0; // re-initialization is not needed
38  if(runNo>0) finishRun(); // save final DB for previous run - if any
39  clearTables();
40  run_number=runNo;
41  char fname[1000];
42 
43  printf("L2EmcDb::initRun, inpPath='%s', logPath='%s'\n",inpPath,logPath);
44  int err=0;
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]);
47 
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]);
50 
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]);
53 
54  if(err) {
55  printf("\n\n total CRASH of L2EmcDb::initRun(%d), reason=some error in ASCII DB\n\n",runNo);
56  return err;
57  }
58  printf("L2EmcDb::initRun(%d) done\n", runNo);
59  return 0;
60 }
61 
62 //==============================
63 //==============================
64 void
65 L2EmcDb::finishRun(){
66  //discards all DB info for the first time
67  if(run_number<0) return;
68 
69  // dumping final DB config
70 
71  char dbFname[1000];
72  sprintf(dbFname,"%s/run%d.l2db.out",logPath,run_number);
73 
74 
75  FILE *dbFd=fopen(dbFname,"w");
76  if(dbFd) {
77  fprintf(dbFd,"#L2EmcDb finishRun(%d), compiled: %s , %s\n",run_number,__DATE__,__TIME__);
78  // dums lables of all corrections
79  int i;
80  for(i=0;i<txMxLbl;i++) fprintf(dbFd,"#%d-%s",i,db_labels[i]);
81  fprintf(dbFd,"#\n");
82  writeAsciiDb(dbFd);
83  fclose(dbFd);
84  printf("L2EmcDb:finishRun() dbDump='%s' ...\n",dbFname);
85  } else {
86  if (dbFd)
87  fprintf(dbFd,"L2EmcDb:FinishRun(%d) dbDump NOT saved, I/O error\n",run_number);
88  }
89 
90  clearTables();
91 }
92 
93 
94 //=====================================
95 //=====================================
96 L2EmcDb::~L2EmcDb(){
97  finishRun();
98 }
99 
100 //=====================================
101 //=====================================
102 void
103 L2EmcDb::clearTables(){
104  run_number=-4;
105  int i;
106  for(i=0; i<EmcDbIndexMax; i++)
107  clearItem(dbByIndex+i);
108 
109  memset(db_labels,0,sizeof(db_labels));
110 }
111 
112 //=====================================
113 //=====================================
114 void
115 L2EmcDb::clearItem(struct EmcCDbItem *x) {
116  x->name[0]=0;
117  x->tube[0]=0;
118  x->crate=-1; x->chan=-1;
119  x->gain=-2;
120  x->ped=-3;
121  x->sec=-4;
122  x->sub='Z';
123  x->eta=-5;
124  x->thr=-6;
125  x->sigPed=-7;
126  x->strip=-299;
127  x->plane='X';
128  x->stat=0xaa; x->fail=0xbb;
129  x->rdo=123456;
130  x->key=-999;
131 }
132 
133 
134 //=====================================
135 //=====================================
136 void
137 L2EmcDb::printItem(const EmcCDbItem *x) {
138  printf("gEmcCDb:");
139  if(x==0) { printf(" NULL pointer\n"); return; }
140 
141  if(x->name[0]==0) {
142  printf(" item not defined ???\n");
143  return;
144  }
145 
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);
148  else
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);
150 }
151 
152 //=====================================
153 //=====================================
154 const L2EmcDb::EmcCDbItem *
155 L2EmcDb::getByIndex(int i){
156  if(i<0 || i>=EmcDbIndexMax) return 0;
157  return dbByIndex+i;
158 }
159 
160 //=====================================
161 //=====================================
162 int
163 L2EmcDb::importItem(EmcCDbItem *x, FILE *fd){
164  /* return:
165  <0 : error in input
166  0 : EOF
167  1 : line ignored
168  2 : valid input
169  */
170 
171  clearItem(x);
172  const int mx=1000;
173  char buf[mx];
174 
175  char * ret=fgets(buf,mx,fd);
176 
177  if(ret==0) return 0;
178 
179  if(buf[0]=='#') return 1;
180 
181  char name0[mx];
182  int ret1=sscanf(buf,"%s",name0);
183 
184 
185  if(ret1==0) return -1;
186 
187  int n=0;
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);
190  }
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);
193  }
194  else
195  return -3;
196 
197  /* printf("aaa name='%s' n=%d\n",name0,n); */
198 
199 
200  if(n!=13) return -1000-n;
201 
202  return 2;
203 }
204 
205 
206 //=====================================
207 //=====================================
208 int
209 L2EmcDb::name2index(char *name){
210 
211  int index=-1;
212  int sec=atoi(name);
213  assert(sec>0 && sec<=12);
214 
215  index=(sec-1)*1000;
216 
217  if(sec<10 && name[0]!='0') name--; /* compensate for missing proceeding zero in sectorID */
218  char key=name[2];
219 
220  switch(key) {
221  case 'R': index+=100;
222  case 'Q': index+=100;
223  case 'P': index+=100;
224  case 'T': {
225  int sub =name[3];
226  assert(sub>='A' && sub<='E');
227  int eta=atoi(name+4);
228  assert(eta>0 && eta <=12);
229  index+=(sub-'A')*12 +eta;
230  } break;
231  case 'V': index+=300;
232  case 'U': {index+=400;
233  int strip=atoi(name+3);
234  assert(strip>0 && strip<=288);
235  index+=strip;
236  } break;
237  /* extension for BTOW towers */
238  case 't' : index= EindexMax+BtowName2Index(sec,name+3); break;
239 
240  default:
241  printf("EEname2Index('%s') Logical Error3: invalid index=%d\n",name,index);
242  exit(-1);
243  }
244 
245  /* printf("EmcName2Index(%s)-->%d\n",name,index); */
246 
247  assert(index>=0);
248  assert(index<EmcDbIndexMax);
249 
250  return index;
251 
252 }
253 
254 //=====================================
255 //=====================================
256 bool
257 L2EmcDb::isEmpty(const EmcCDbItem *x){
258  if(x==0) return true;
259  return x->name[0]==0;
260 }
261 
262 //=====================================
263 //=====================================
264 bool
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;
270  return true;
271 }
272 
273 //=====================================
274 //=====================================
275 bool
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;
281  return true;
282 }
283 
284 //=====================================
285 //=====================================
286 int
287 L2EmcDb::BtowName2Index(int sect, char *xee){
288  /* RETURN index range [0,4799] */
289 
290  int index=-1;
291  /*printf("in BtowName2Index, sect=%d, xee=%s=\n",sect,xee);*/
292  char sub=xee[0];
293  int etaB=atoi(xee+1);
294 
295 
296  if(etaB<1 || etaB>40) {
297  printf("BtowName2Index() Logical Error5: invalid sect=%d, xee=%s=\n",sect,xee);
298  exit(-1);
299  }
300 
301  if(sub<'a' || sub>'j') {
302  printf("BtowName2Index() Logical Error6: invalid sect=%d, xee=%s=\n",sect,xee);
303  exit(-1);
304  }
305  index=etaB +400*(sect-1) + (sub-'a')*40;
306  /* printf("sub=%c etaB=%d index=%d\n",sub,etaB,index); */
307 
308 
309  return index;
310 }
311 
312 
313 //==============================
314 //==============================
315 int
316 L2EmcDb::readAsciiDb(char *fname, char *lbl) {
317  int err=77;
318  FILE *fd=0;
319  int nL=0;
320  int nR=0; /* count good records */
321 
322  fd=fopen(fname,"r");
323  if(fd==0) { err=-1; goto crashIt;}
324  /* printf(" readAsciiDb(%s) ...\n",fname); */
325  {
326  const int mx=1000;
327  char buf[mx];
328  if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1); //preserve label
329  }
330  while (1) {
331  /* -- Read in each individual entry. The return value is checked
332  -- for success or failure */
333  struct EmcCDbItem x;
334 
335  err = importItem(&x,fd);
336  nL++;
337  if(err==0) break;
338  if(err==1) continue;
339  if(err <0) goto crashIt;
340 
341  /* -- recover Index for the specified (named) item */
342  int key= name2index(x.name);
343  /* printf(" name='%s' key=%d, inpKey=%d, inpKey \n",x.name,key,x.key); */
344 
345  x.key=key;
346 
347  if(key<1 || key>=EmcDbIndexMax) { err=-22; goto crashIt;}
348 
349 
350  /* -- Copy the database record read in from the file into
351  local lookup table */
352 
353  struct EmcCDbItem *y=dbByIndex+key;
354 
355  if(!isEmpty(y)) { err=999; goto crashIt; }
356  *y=x; /* save content of this DB record & update lookup tables */
357  nR++;
358  /* tmp, do not verify duplicated mapping problems , fix it later*/
359 
360  /* EmcDbItemStruct_print(&x); */
361  }
362 
363  printf("L2EmcDb::readAsciiDb(%s) ... nL=%d nR=%d\n",fname,nL,nR);
364  return 0;
365 
366  crashIt:
367  printf("\n\n total CRASH of L2EmcDb::readAsciiDb(%s) err=%d nL=%d\n\n",fname,err,nL);
368  return err;
369 }
370 
371 
372 
373 /*--------------------------------------------------
374  --------------------------------------------------*/
375 void
376 L2EmcDb::exportItem(struct EmcCDbItem *x, FILE *fd) {
377 
378  if(x->name[0]==0) return; /* item not defined */
379 
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);
382  else
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);
384 }
385 
386 
387 /* ------------------------------------
388  ------------------------------------ */
389 void
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");
393  int i;
394  int n1=0,n2=0;
395  for(i=0; i<EmcDbIndexMax ; i++) {
396  EmcCDbItem *x=dbByIndex+i;
397  n1++;
398  if(isEmpty(x)) continue;
399  exportItem(x,fd);
400  // if(strstr(x->name,"01TA")) printf("c %s %f\n",x->name,x->ped);
401  n2++;
402  }
403  fprintf(fd,"# total %d items out of %d possible\n",n2,n1);
404 }
405 
406 
407 //==============================
408 //==============================
409 int
410 L2EmcDb::changeMaskFullCrate(const char *fname, char BEflag, char *lbl) {
411 
412  /* Replace stat&fatal masks for channels already initialized from the DB
413  in specified crate
414  format : {hexCrateID hexStat hexFatal , anythingElse}
415  lines starting with '#' are ignored
416  empty lines are not permitted
417  */
418  printf("L2EmcDb::changeMaskFullCrate(%s)\n",fname);
419  FILE *fd=fopen(fname,"r");
420  int nd=0,nl=0;
421  const int mx=1000;
422  char buf[mx];
423  int err=-1;
424 
425  if(fd==0) goto crashIt;
426 
427  if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1); //preserve label
428 
429  unsigned int crate, stat, fail;
430 
431  while(1) {
432  char *ret=fgets(buf,mx,fd);
433  if(ret==0) break;
434 
435  nl++;
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);
440  int i;
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))) {
447  x->stat=stat;
448  x->fail=fail;
449  nd++;
450  }
451  }
452 
453  }
454  fclose(fd);
455 
456  printf(" change stat & fail bits done inpLine=%d nChanged=%d\n",nl,nd); return 0;
457 
458  crashIt:
459  printf("\n\n total CRASH of L2EmcDb::changeMaskFullCrate(%s) err=%d nL=%d\n\n",fname,err,nl);
460  return err;
461 }
462 
463 //==============================
464 //==============================
465 int
466 L2EmcDb::changePedsByName(const char *fname, char *lbl) {
467 
468  /* Replace peds only for channels already initialized from the DB
469  format : {name, ped, sigPed anythingElse}
470  lines starting with '#' are ignored
471  empty lines are not permitted
472  */
473  printf("L2EmcDb::changePedsByName(%s)\n",fname);
474  FILE *fd=fopen(fname,"r");
475  int nd=0,nl=0,ne=0;
476  const int mx=1000;
477  char buf[mx];
478  int err=-1;
479 
480  if(fd==0) goto crashIt;
481 
482  if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1); //preserve label
483 
484  char cVal[100];
485  float pVal,sVal;
486 
487  while(1) {
488  char *ret=fgets(buf,mx,fd);
489  if(ret==0) break;
490  nl++;
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;
496 
497  if(isEmpty(x)) { ne++; continue; } // skip unmapped channels
498  // printf("%s %p\n",cVal,x);
499 
500  // replace only initialized channels
501  if(pVal<0) { printf("Ignore: %s",buf); continue; }
502  x->ped=pVal;
503 
504 
505  nd++;
506  }
507  fclose(fd);
508 
509  printf(" changePedsByName() done inpLine=%d nChanged=%d nNotMappedAndIgnored=%d \n",nl,nd,ne); return 0;
510 
511  crashIt:
512  printf("\n\n total CRASH of L2EmcDb::changePedsByName(%s) err=%d nL=%d\n\n",fname,err,nl);
513  return err;
514 }
515 
516 //==============================
517 //==============================
518 int
519 L2EmcDb::changeMaskByName(const char *fname, char *lbl) {
520 
521 /* Replace stat&fail masks only for channels already initialized from the DB
522  format : {name, hexStat, hexFail, anythingElse}
523  lines starting with '#' are ignored
524  empty lines are not permitted
525 */
526  printf("L2EmcDb::changeMaskByName(%s)\n",fname);
527  FILE *fd=fopen(fname,"r");
528  int nd=0,nl=0,ne=0;
529  const int mx=1000;
530  char buf[mx];
531  int err=-1;
532 
533  if(fd==0) goto crashIt;
534  char cVal[100];
535  unsigned int stat, fail;
536  if(fgets(buf,mx,fd)) strncpy(lbl,buf,txMxSize-1); //preserve label
537 
538  while(1) {
539  char *ret=fgets(buf,mx,fd);
540  if(ret==0) break;
541 
542  nl++;
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;
548 
549  if(isEmpty(x)) { ne++; continue; } // skip unmapped channels
550  // printf("%s %p\n",cVal,x);
551 
552  // replace only initialized channels
553  x->stat=stat;
554  x->fail=fail;
555  nd++;
556  }
557  fclose(fd);
558 
559  printf(" changeMaskByName() done inpLine=%d nChanged=%d nNotMappedAndIgnored=%d \n",nl,nd,ne); return 0;
560 
561  crashIt:
562  printf("\n\n total CRASH of L2EmcDb::changeMaskByName(%s) err=%d nL=%d\n\n",fname,err,nl);
563  return err;
564 }
565 
566 
567 /*
568 *********************************************************************
569  $Log: L2EmcDb.cxx,v $
570  Revision 1.6 2010/04/18 06:05:32 pibero
571  Address compiler warnings.
572 
573  Revision 1.5 2007/11/06 22:07:24 balewski
574  added timeStamp controlled L2 setup from Jason
575 
576  Revision 1.4 2007/10/25 23:00:30 balewski
577  logic bug fix
578 
579  Revision 1.2 2007/10/22 23:10:03 balewski
580  split L2 to generic and year specific, not finished
581 
582  Revision 1.1 2007/10/11 00:33:13 balewski
583  L2algo added
584 
585  Revision 1.4 2006/03/28 19:33:22 balewski
586  ver16b , in L2new
587 
588  Revision 1.3 2006/03/11 17:08:32 balewski
589  now CVS comments should work
590 
591 */
592 
void setPedFile(const char *c)
Definition: L2EmcDb.cxx:30
void setMaskFile(const char *c)
Definition: L2EmcDb.cxx:31