8 #include "St_DataSetIter.h"
12 #include "StEventTypes.h"
16 #include "EEmcDbItem.h"
17 #include "EEmcDbCrate.h"
18 #include "StEEmcUtil/EEfeeRaw/EEname2Index.h"
20 #include "tables/St_eemcDbADCconf_Table.h"
21 #include "tables/St_eemcDbPMTcal_Table.h"
22 #include "tables/St_eemcDbPMTname_Table.h"
23 #include "tables/St_eemcDbPIXcal_Table.h"
24 #include "tables/St_eemcDbPMTped_Table.h"
25 #include "tables/St_eemcDbPMTstat_Table.h"
26 #include "tables/St_kretDbBlobS_Table.h"
27 #include "cstructs/eemcConstDB.hh"
28 #include "cstructs/kretConstDB.hh"
30 #include <StMessMgr.h>
38 mfirstSecID=mlastSecID=mNSector=0;
48 for(i=0;i<MaxAnyCrate;i++){
50 if(i==0 || (i>MaxTwCrateID && i<MinMapmtCrateID) )
continue;
52 memset(byCrate[i],0,
sizeof(
EEmcDbItem *)*MaxAnyCh);
56 setDBname(
"Calibrations/eemc");
61 chGainL =
new TString [mxChGain];
65 chMaskL =
new TString [mxChMask];
78 StEEmcDb::~StEEmcDb(){
87 delete [] mDbsectorID;
97 void StEEmcDb::setThreshold(
float x){
99 LOG_INFO <<
"::setThres KsigOverPed="<<KsigOverPed<<
", threshold=ped+sig*KsigOverPed"<< endm;
105 void StEEmcDb::setPreferredFlavor(
const char *flavor,
const char *nameMask){
106 strncpy(dbFlavor.flavor,flavor,DbFlavor::mx);
107 strncpy(dbFlavor.nameMask,nameMask,DbFlavor::mx);
108 LOG_INFO <<
"::setPreferredFlavor(flav='"<<dbFlavor.flavor <<
"', mask='"<< dbFlavor.nameMask<<
"')" <<endm;
119 mNSector=mlastSecID - mfirstSecID+1;
121 LOG_ERROR <<
":: Problem mNSector==0" <<endm;
123 mDbADCconf=(eemcDbADCconf_st **)
new void *[mNSector];
124 mDbPMTcal= (eemcDbPMTcal_st **)
new void *[mNSector];
125 mDbPMTname=(eemcDbPMTname_st **)
new void *[mNSector];
126 mDbPIXcal= (eemcDbPIXcal_st **)
new void *[mNSector];
127 mDbPMTped= (eemcDbPMTped_st **)
new void *[mNSector];
128 mDbPMTstat=(eemcDbPMTstat_st **)
new void *[mNSector];
129 mDbsectorID=
new int [mNSector];
133 LOG_INFO <<
":: Use sectors from "<< mfirstSecID<<
" to "<< mlastSecID<<endm;
141 const EEmcDbCrate* StEEmcDb::getFiber(
int icr)
const {
151 void StEEmcDb::setFiberOff(
int icr) {
152 if(icr<0 || icr>=nFiber ) {
153 LOG_WARN <<
"::setFiberOff(icr="<<icr<<
") out of range, ignorred" << endm;
156 mDbFiber[icr].
useIt=
false;
164 void StEEmcDb::clearItemArray(){
170 for(i=0; i<EEindexMax; i++)
174 for(i=0;i<MaxAnyCrate;i++) {
175 if(byCrate[i]==NULL)
continue;
176 for(j=0;j<MaxAnyCh;j++)
180 memset(byStrip,0,
sizeof(byStrip));
182 if(mDbFiber)
delete [] mDbFiber;
190 LOG_FATAL <<
":: Cannot initialize arrays in clearItemArray()" << endm;
193 for(i=0; i<mNSector; i++) {
216 if ( mAsciiDbase.Length() > 0 ) {
217 LOG_WARN <<
"loadTables: Database not reloaded, values taken from " << mAsciiDbase.Data() << endm;
219 LOG_INFO <<
"loadTables: use(flav='"<<dbFlavor.flavor <<
"', mask='"<< dbFlavor.nameMask<<
"')" <<endm;
221 requestDataBase(anyMaker);
224 for(
int is = 0;(is < mNSector) && ok;is++) {
225 if (optimizeMapping(is)) {
226 LOG_FATAL<<
"::loadTables Total failure, no DB info for Endcap was retrived, all ETOW channels will be cleared for every event. Fix the problem! JB"<<endm;
235 for(
int is=0;is<nChGain;is++) changeGainsAction(chGainL[is].Data());
236 for(
int is=0;is<nChMask;is++) changeMaskAction(chMaskL[is].Data());
238 for(
int icr = 0;icr < getNFiber();icr++) {
242 LOG_INFO<<(*fiber)<<endm;
244 LOG_INFO <<
"::loadTables Found "<< nFound<<
" EEMC related tables "<<endm;
253 void StEEmcDb::requestDataBase(
StMaker *anyMaker){
257 for(ifl=0;ifl<2;ifl++) {
259 if( dbFlavor.flavor[0]==0)
continue;
260 LOG_INFO <<
"::RequestDataBase()-->ifl="<<ifl<<
" try flavor='"<<dbFlavor.flavor <<
"' for mask='"<< dbFlavor.nameMask<<
"')" <<endm;
262 anyMaker->SetFlavor(dbFlavor.flavor,dbFlavor.nameMask);
263 mask=dbFlavor.nameMask;
266 TDataSet *eedb = anyMaker ? anyMaker->GetDataBase(dbName) : 0;
268 LOG_FATAL <<
"::RequestDataBase() Could not find dbName ="<<dbName <<endm;
275 for(is=0; is< mNSector; is++) {
276 int secID=is+mfirstSecID;
278 mDbsectorID[is]=secID;
279 getTable<St_eemcDbADCconf,eemcDbADCconf_st>(eedb,secID,
"eemcADCconf",mask,mDbADCconf+is);
281 getTable<St_eemcDbPMTcal,eemcDbPMTcal_st>(eedb,secID,
"eemcPMTcal",mask,mDbPMTcal+is);
283 getTable<St_eemcDbPMTname,eemcDbPMTname_st>(eedb,secID,
"eemcPMTname",mask,mDbPMTname+is);
285 getTable<St_eemcDbPIXcal,eemcDbPIXcal_st>(eedb,secID,
"eemcPIXcal",mask,mDbPIXcal+is);
287 getTable<St_eemcDbPMTped,eemcDbPMTped_st>(eedb,secID,
"eemcPMTped",mask,mDbPMTped+is);
289 getTable<St_eemcDbPMTstat,eemcDbPMTstat_st>(eedb,secID,
"eemcPMTstat",mask,mDbPMTstat+is);
295 getTable<St_kretDbBlobS,kretDbBlobS_st>(eedb,13,
"eemcCrateConf",mask,&mDbFiberConfBlob);
296 if(mDbFiberConfBlob) {
297 LOG_DEBUG<<
"::RequestDataBase() eemcCrateConf dump "<<endm;
306 int StEEmcDb::optimizeMapping(
int is){
308 LOG_INFO <<
" conf ADC map for sector="<< mDbsectorID[is] <<endm;
309 assert(mDbsectorID[is]>0);
311 eemcDbADCconf_st *t= mDbADCconf[is];
314 LOG_INFO <<
"mapping="<< t->comment <<endm;
317 for(j=0;j<EEMCDbMaxAdc; j++) {
318 char *name=t->name+j*EEMCDbMaxName;
320 if(*name==EEMCDbStringDelim)
continue;
325 int key=EEname2Index(name);
326 assert(key>=0 && key<EEindexMax);
330 LOG_INFO<<(*x)<<endm;
331 assert(x->isEmpty());
333 x->crate=t->crate[j];
334 x->
chan=t->channel[j];
337 x->setDefaultTube(MinMapmtCrateID);
340 assert(x->crate>=0 && x->crate<MaxAnyCrate);
341 assert(x->
chan>=0 && x->
chan<MaxAnyCh);
342 assert(byCrate[x->crate]);
343 if(byCrate[x->crate][x->
chan]) {
344 LOG_FATAL <<
"::Fatal Error of eemc DB records: the same crate="<<x->crate<<
", ch="<<x->
chan<<
" entered twice. Whole EEMC DB is erased from memory, all data will be ignored, FIX IT !, JB"<<endm;
347 LOG_FATAL<<(*byCrate[x->crate][x->
chan])<<endm;
348 LOG_FATAL<<(*x)<<endm;
352 byCrate[x->crate][x->
chan]=x;
353 if(x->isSMD()) byStrip[x->sec-1][x->plane-
'U'][x->strip-1]=x;
363 void StEEmcDb::optimizeOthers(
int is){
365 int secID= mDbsectorID[is];
368 EEindexRange(secID,ix1,ix2);
370 LOG_DEBUG<<
" EEindexRange("<<secID<<
","<<ix1<<
","<<ix2<<
")"<<endm;
376 eemcDbPMTcal_st *calT=mDbPMTcal[is];
377 if(calT) LOG_INFO <<
"tower calib="<<calT->comment<<endm;
378 eemcDbPMTname_st *tubeTw=mDbPMTname[is];
379 if(tubeTw) LOG_INFO <<
"PMT names="<<tubeTw->comment<<endm;
381 eemcDbPIXcal_st *calM= mDbPIXcal[is];
382 if(calM) LOG_INFO <<
"MAPMT calib="<<calM->comment<<endm;
384 eemcDbPMTped_st *ped=mDbPMTped[is];
385 if(ped) LOG_INFO <<
"ped tower & MAPMT="<<ped->comment<<endm;
387 eemcDbPMTstat_st *stat=mDbPMTstat[is];
388 if(stat) LOG_INFO <<
"status tower & MAPMT="<<stat->comment<<endm;
391 for(key=ix1;key<ix2; key++) {
393 if(x->isEmpty())
continue;
398 int mx=
sizeof(ped->name)/EEMCDbMaxName;
400 char *name1=ped->name+j*EEMCDbMaxName;
401 if(strncmp(name,name1,strlen(name)))
continue;
403 x->sigPed=ped->sig[j];
404 x->thr=ped->ped[j]+KsigOverPed*ped->sig[j];
412 if(calT&& name[2]==
'T') {
414 int mx=
sizeof(calT->name)/EEMCDbMaxName;
416 char *name1=calT->name+j*EEMCDbMaxName;
417 if(strncmp(name,name1,strlen(name)))
continue;
418 x->gain=calT->gain[j];
424 if(calM && name[2]!=
'T') {
426 int mx=
sizeof(calM->name)/EEMCDbMaxName;
429 char *name1=calM->name+j*EEMCDbMaxName;
430 if(strncmp(name,name1,strlen(name)))
continue;
431 x->gain=calM->gain[j];
436 if(tubeTw && name[2]==
'T') {
438 int mx=
sizeof(tubeTw->name)/EEMCDbMaxName;
440 char *name1=tubeTw->name+j*EEMCDbMaxName;
441 if(strncmp(name,name1,strlen(name)))
continue;
442 x->setTube(tubeTw->tubeName+j*EEMCDbMaxName);
451 int mx=
sizeof(stat->name)/EEMCDbMaxName;
453 char *name1=stat->name+j*EEMCDbMaxName;
454 if(strncmp(name,name1,strlen(name)))
continue;
455 x->stat=stat->stat[j];
456 x->fail=stat->fail[j];
471 void StEEmcDb::exportAscii(
const char *fname)
const{
472 LOG_INFO <<
"::exportAscii(\'" << fname <<
"') ..." << endm;
474 FILE * fd=fopen(fname,
"w");
480 fprintf(fd,
"# EEmcDb::exportAscii(), see StRoot/StEEmcDb/EEmcDbItem::exportAscii() for definition\n");
484 fprintf(fd,
"%d #fibers: {name,crID,crIDswitch,fiber,nCh,nHead,type,useIt}\n",nFiber);
485 for(j=0;j<nFiber;j++)
486 mDbFiber[j].exportAscii(fd);
488 fprintf(fd,
"#tw/pre/post: {name,crate,chan,sec,plane,strip,gain,ped,thr,stat,fail,tube,key}\n");
489 fprintf(fd,
"#or \n");
490 fprintf(fd,
"#smd: {name,crate,chan,sec,sub,eta,gain,ped,thr,stat,fail,tube,key}\n");
493 for(j=0;j<EEindexMax; j++) {
495 if(x->isEmpty())
continue;
499 LOG_INFO <<
"::exportAscii() nTot=" << nTot <<
", done" << endm;
509 void StEEmcDb::optimizeFibers (){
512 if( mDbFiberConfBlob==0) {
513 LOG_FATAL <<
" EEMC FiberConf not found"<<endm;
517 assert(mDbFiberConfBlob);
531 static char blobBuffer[KRETmxBlobSlen+1];
532 char *blob = blobBuffer;
535 int len = strlen(mDbFiberConfBlob->dataS);
536 assert(len<KRETmxBlobSlen );
537 strncpy(blob,mDbFiberConfBlob->dataS,len);
539 blob=strtok(blob,
";");
540 if(strstr(blob,
"<ver1>")==0) {
541 LOG_FATAL <<
" EEMC FiberConf missing opening key "<<endm;
546 while((blob=strtok(0,
";"))) {
548 if(strstr(blob,
"<#>"))
continue;
549 if(strstr(blob,
"</ver1>"))
goto done;
555 LOG_DEBUG<<
" mOptimizeFibers() map start: , nFiber="<<nFiber<<endm;
560 mDbFiber[icr].setAll(blob);
567 LOG_FATAL <<
" mOptimizeFibers() map missing/wrong terminating key "<<endm;
570 LOG_FATAL <<
" EEMC FiberConf error, without it decoding of the _raw_ EEMC data from StEvent is not working (all data are dropped). However, this missing table is irreleveant for muDst analysis, JB"<<endm;
574 LOG_INFO <<
" mOptimizeFibers() map found for nFiber="<<nFiber<<endm;
575 if(icr==nFiber)
return;
577 LOG_FATAL <<
" mOptimizeFibers() map nFiber missmatch is="<<icr<<
" should be="<<nFiber<<endm;
588 assert(i<EEindexMax);
590 if(x->isEmpty())
return 0;
597 const EEmcDbItem* StEEmcDb::getByCrate(
int crateID,
int channel)
const {
602 if(crateID>=MinTwCrateID && crateID<=MaxTwCrateID) {
606 if(channel>=max)
return 0;
608 }
else if (crateID>=MinMapmtCrateID && crateID<=MaxMapmtCrateID ){
612 }
else if (crateID>=17 && crateID<=46 ){
620 assert( byCrate[crateID]);
623 return byCrate[crateID][channel];
632 template <
class St_T,
class T_st>
void StEEmcDb
633 ::getTable(
TDataSet *eedb,
int secID, TString tabName, TString mask, T_st** outTab ) {
639 if(!mask.IsNull() && !tabName.Contains(mask))
return ;
642 sprintf(name,
"sector%2.2d/%s",secID,tabName.Data());
644 sprintf(name,
"misc/%s",tabName.Data());
647 LOG_DEBUG<<
" request="<< name <<endm;
649 St_T *ds= (St_T *)eedb->
Find(name);
651 LOG_WARN <<
" sector="<<secID<<
" table='"<< name <<
" not Found in DB, continue "<<endm;
655 if(ds->GetNRows()!=1) {
656 LOG_WARN <<
" sector="<<secID<<
" table='"<< name <<
" no records, continue "<<endm;
660 T_st *tab=(T_st *) ds->GetArray();
663 LOG_WARN <<
" GetArray() failed"<<endm;
668 LOG_DEBUG<<
" map="<< (*outTab)->comment<<
"'"<<endm;
679 StEEmcDb::getByStrip0(
int isec,
int iuv,
int istrip)
const {
681 assert(isec>=0 && isec<MaxSectors);
682 assert(iuv>=0 && iuv<MaxSmdPlains);
683 assert(istrip>=0 && istrip<MaxSmdStrips);
684 return byStrip[isec][iuv][istrip];
694 StEEmcDb::getTile(
int sec,
char sub,
int eta,
char type)
const {
696 sprintf(name,
"%2.2d%c%c%2.2d",sec,type,sub,eta);
697 int key=EEname2Index(name);
706 StEEmcDb::getStrip(
int sec,
char uv,
int strip)
const {
708 sprintf(name,
"%2.2d%c%3.3d",sec,uv,strip);
709 int key=EEname2Index(name);
719 void StEEmcDb::setAsciiDatabase(
const Char_t *ascii )
738 mNSector = mlastSecID - mfirstSecID + 1;
741 FILE *fd=fopen(ascii,
"r");
744 if(fd==0)
goto crashIt;
753 int ret = item.importAscii(fd);
757 if(ret <0)
goto crashIt;
760 int key=EEname2Index(item.
name);
764 LOG_WARN<<Form(
": name='%s' key=%d!=inpKey=%d, inpKey ignored",item.
name,key,item.key)<<endm;
767 assert(key>=0 && key<EEindexMax);
770 int isec=item.sec-sec1;
771 if (sec1>sec2 ) isec+=12;
773 if( isec <0 )
continue;
774 if( isec >= mNSector)
continue;
780 if(!x->isEmpty())
goto crashIt;
784 assert(byCrate[x->crate]);
785 if(byCrate[x->crate][x->
chan]) {
786 LOG_FATAL<<Form(
"Fatal Error of eemc DB records: the same crate=%d / channel=%d entered twice for :",x->crate,x->
chan)<<endm;
789 LOG_FATAL<<(*byCrate[x->crate][x->
chan])<<endm;
790 LOG_FATAL<<(*x)<<endm;
793 byCrate[x->crate][x->
chan]=x;
800 for ( Int_t mySec = 1; mySec <= 12; mySec++ )
801 for ( Char_t uv =
'U'; uv <=
'V'; uv++ )
802 for ( Int_t myStrip = 1; myStrip <= 288; myStrip++ ) byStrip[mySec-1][uv-
'U'][myStrip-1] = (
EEmcDbItem*)getStrip(mySec,uv,myStrip);
806 LOG_INFO<<Form(
"setAsciiDataBase() done, found %d valid records",nd)<<endm;
811 LOG_FATAL<<Form(
"EEmcDb - no/corrupted input file, continue")<<endm;
820 void StEEmcDb::changeGains(
char *fname) {
821 assert(nChGain+1< mxChGain);
822 chGainL[nChGain]=fname;
829 StEEmcDb::changeMask(
char *fname) {
830 assert(nChMask+1< mxChMask);
831 chMaskL[nChMask]=fname;
838 void StEEmcDb::changeGainsAction(
const char *fname) {
846 LOG_WARN <<
" ::changeGains('"<<fname<<
"')"<<endm;
847 FILE *fd=fopen(fname,
"r");
857 char *ret=fgets(buf,mx,fd);
861 if(buf[0]==
'#')
continue;
862 int n=sscanf(buf,
"%s %f",cVal,&xVal);
864 int key=EEname2Index(cVal);
867 if(x->isEmpty())
continue;
869 if(xVal<=0) LOG_WARN<<Form(
"Warning ! buf=%s=",buf)<<endm;
877 LOG_INFO <<
" ::changeGains('"<<fname<<
"') done inpLines="<<nl<<
" nChanged="<<nd<<endm;
884 void StEEmcDb::changeMaskAction(
const char *fname) {
892 LOG_INFO <<
" ::changeMask('"<<fname<<
"') "<<endm;
894 FILE *fd=fopen(fname,
"r");
905 char *ret=fgets(buf,mx,fd);
909 if(buf[0]==
'#')
continue;
910 int n=sscanf(buf,
"%s %d %d",cVal,&xStat,&xFail);
912 int key=EEname2Index(cVal);
914 if(x->isEmpty())
continue;
924 LOG_INFO <<
" ::changeMask('"<<fname<<
"') done inpLines="<<nl<<
" nChanged="<<nd<<endm;
953 case kEndcapEmcTowerId:
954 sub=
'A'+Bsub-1; cT=
'T';
955 x=getTile(sec,sub,eta,cT);
break;
957 case kEndcapEmcPreShowerId:
958 sub=
'A'+(Bsub-1)%5; cT=
'P'+(Bsub-1)/5;
959 x=getTile(sec,sub,eta,cT);
break;
961 case kEndcapSmdVStripId:
963 case kEndcapSmdUStripId:
964 x=getByStrip(sec,uv,strip);
break;
967 LOG_WARN <<
"::getDb(), wrong detectorId=" << StDetId <<
". This is fatal - bug in the code, fix it, JB"<<endm;
const EEmcDbItem * getByIndex(int ikey) const
returns full DB info for one pixel
char name[StEEmcNameLen]
ASCII name of the channel, see Readme.
int useIt
flag to ignore data from misconfig/broken crate/box
const EEmcDbItem * StBarrelIndex2Item(int StDetId, int Bmod, int Beta, int Bsub) const
void loadTables(StMaker *anyMaker)
to access STAR-DB
void setSectors(int, int)
limit the range of sectors for speed
virtual TDataSet * Find(const char *path) const