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