00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include <fakeRtsLog.h>
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifdef IS_REAL_L2 //in l2-ana environment
00017 #include "../L2algoUtil/L2EmcDb2012.h"
00018 #include "../L2algoUtil/L2EmcGeom2012.h"
00019 #include "../L2algoUtil/L2Histo.h"
00020 #else
00021 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb2012.h"
00022 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
00023 #endif
00024
00025 #include "L2pedAlgo12.h"
00026 #include "L2pedResults2012.h"
00027
00028
00029
00030 L2pedAlgo12::L2pedAlgo12(const char* name, const char *uid, L2EmcDb2012* db, char* outDir, int resOff)
00031 : L2VirtualAlgo2012(name, uid, db, outDir, false, false, resOff) {
00032
00033
00034
00035
00036 par_pedSubtr=false;
00037 par_saveBinary=false;
00038 par_dbg=0;
00039 par_prescAccept=0;
00040
00041 int i;
00042 char tit[1000];
00043 for(i=0;i<BtowGeom::mxRdo;i++) {
00044 sprintf(tit,"BTOW ADC for rdo=%d; ADC+%d",i,-minAdc);
00045 btowAdc[i]=new L2Histo(20000+i,tit,maxAdc-minAdc+1);
00046 }
00047
00048 for(i=0;i<EtowGeom::mxRdo;i++) {
00049 sprintf(tit,"ETOW ADC for rdo=%d; ADC ",i);
00050 etowAdc[i]=new L2Histo(10000+i,tit,maxAdc-minAdc+1);
00051 }
00052
00053
00054 par_maxMatt=160;
00055 setMaxHist(64);
00056
00057
00058 hA[10]=new L2Histo(10,"total event counter; x=cases",6);
00059 hA[11]=new L2Histo(11,"L2 time used per input event; x: time (CPU 20*kTics); y: events ",500);
00060
00061
00062 hA[20]=new L2Histo(20,"BTOW pedRes Y=ADC-DBped ; x: chan + 160*crate", 4800);
00063 hA[21]=new L2Histo(21,"BTOW pedRes Z=ADC-DBped, saturated @ |3|; x: etaBin ,[-1,+1]; y: phi bin ~sector",40,120);
00064 sprintf(tit,"BTOW pedRes ; x: chan + 160*crate ;y: ADC+%d",-minAdc);
00065
00066 hA[22]=new L2Histo(22,tit,4800,par_maxMatt);
00067
00068
00069 hA[30]=new L2Histo(30,"ETOW pedRes Y=ADC-DBped ; x: chan + 128*crate", 768);
00070 hA[31]=new L2Histo(31,"ETOW pedRes Z=ADC-DBped, saturated @ |3|; x: 12 - Endcap etaBin ,[+1,+2]; y: phi bin ~sector",12,60);
00071 sprintf(tit,"ETOW pedRes ; x: chan + 128*crate ;y: ADC+%d",-minAdc);
00072
00073 hA[33]=new L2Histo(33,tit,768,par_maxMatt);
00074
00075
00076
00077 if (sizeof(L2pedResults2012)!= L2pedResults2012::mySizeChar)
00078 criticalError("L2pedAlgo12 has failed consistency check. sizeof(L2pedAlgo12)!= L2pedResults2012::mySizeChar");
00079 }
00080
00081
00082
00083 int
00084 L2pedAlgo12::initRunUser(int runNo, int *rc_ints, float *rc_floats) {
00085
00086
00087
00088
00089
00090 if(mDb->getRun()!=runNo) return -700;
00091
00092
00093 par_pedSubtr =rc_ints[0]!=0;
00094 par_speedFact =rc_ints[1];
00095 par_saveBinary=rc_ints[2]!=0;
00096 par_dbg =rc_ints[3];
00097 par_prescAccept=rc_ints[4];
00098
00099 if(par_prescAccept<0) par_prescAccept=0;
00100
00101 if(par_speedFact<1) par_speedFact=1;
00102 if(par_speedFact>2) {
00103 if(par_speedFact<4) par_speedFact=2;
00104 else if(par_speedFact<8) par_speedFact=4;
00105 else if(par_speedFact<16) par_speedFact=8;
00106 else if(par_speedFact<32) par_speedFact=16;
00107 else if(par_speedFact<64) par_speedFact=32;
00108 else if(par_speedFact<192) par_speedFact=64;
00109 else par_speedFact=192;
00110 }
00111
00112
00113 s_lastB=s_lastE=0;
00114 s_stepB=BtowGeom::mxRdo/ par_speedFact ;
00115 s_stepE=EtowGeom::mxRdo/ par_speedFact ;
00116
00117
00118
00119 memset(db_btowPed, 0,sizeof(db_btowPed));
00120 memset(db_etowPed, 0,sizeof(db_etowPed));
00121
00122
00123 int i;
00124 int nBtowOk=0, nEtowOk=0;
00125 for(i=0; i<EmcDbIndexMax; i++) {
00126 const L2EmcDb2012::EmcCDbItem *x=mDb->getByIndex(i);
00127 if(mDb->isEmpty(x)) continue;
00128 if (mDb->isBTOW(x) ) {
00129 db_btowPed[x->rdo]=(int) (x->ped);
00130 nBtowOk++;
00131 } else if (mDb->isETOW(x) ) {
00132 db_etowPed[x->rdo]=(int) (x->ped);
00133 nEtowOk++;
00134 }
00135 }
00136
00137 for(i=0;i<BtowGeom::mxRdo;i++) btowAdc[i]->reset();
00138 for(i=0;i<EtowGeom::mxRdo;i++) etowAdc[i]->reset();
00139 for(i=0;i<mxHA;i++) if(hA[i]) hA[i]->reset();
00140
00141 nInp=0;
00142 run_number=runNo;
00143 return 0;
00144 }
00145
00146
00147
00148
00149 bool
00150 L2pedAlgo12::doPedestals(int inpEveId, int* L2Result,
00151 int bemcIn, ushort *bemcData,
00152 int eemcIn, ushort *eemcData){
00153
00154 unsigned long mEveTimeStart;
00155 rdtscl_macro(mEveTimeStart);
00156
00157 nInp++;
00158 hA[10]->fill(0);
00159 if(par_prescAccept>0) {
00160 if((rand()>>4) % par_prescAccept ) return false;
00161 hA[10]->fill(5);
00162 return true;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 short rdo;
00174 if( bemcIn ) {
00175 short first=s_lastB%BtowGeom::mxRdo;
00176 s_lastB=first+s_stepB;
00177 if(first==0) hA[10]->fill(1);
00178
00179 for(rdo=first; rdo<s_lastB; rdo++){
00180 int adc=bemcData[rdo];
00181 if(par_pedSubtr) adc-=db_btowPed[rdo];
00182 btowAdc[rdo]->fill(adc-minAdc);
00183 }
00184 }
00185
00186
00187 if( eemcIn ) {
00188 short first=s_lastE%EtowGeom::mxRdo;
00189 s_lastE=first+s_stepE;
00190 if(first==0) hA[10]->fill(2);
00191
00192 for(rdo=first; rdo<s_lastE; rdo++){
00193 int adc=eemcData[rdo];
00194 if(par_pedSubtr) adc-=db_etowPed[rdo];
00195 etowAdc[rdo]->fill(adc-minAdc);
00196 }
00197 }
00198
00199
00200 L2pedResults2012 out;
00201 memset(&out,0,sizeof(out));
00202 out.int0.decision=
00203 ( (bemcIn>0) <<3 ) +
00204 ( (eemcIn>0) <<4 ) +
00205
00206 ( par_pedSubtr <<6 ) ;
00207
00208 unsigned long mEveTimeStop;
00209 rdtscl_macro(mEveTimeStop);
00210 unsigned long mEveTimeDiff=mEveTimeStop-mEveTimeStart;
00211 int kTick=mEveTimeDiff/1000;
00212 hA[11]->fill(kTick/20);
00213
00214 const ushort maxKT=30000;
00215 out.int0.kTick= kTick>maxKT ? maxKT : (int)kTick;
00216
00217 int *outPlace=L2Result+ mResultOffset;
00218 memcpy(outPlace,&out,sizeof( L2pedResults2012));
00219
00220 if(par_dbg) L2pedResults2012_print(&out);
00221
00222
00223
00224 return true;
00225 }
00226
00227
00228
00229
00230 void
00231 L2pedAlgo12::computeUser(int token ){
00232
00233 criticalError("L2pedAlgo12::computeUser has been called and should not have been. Serious problem in L2");
00234
00235 }
00236
00237
00238
00239 void
00240 L2pedAlgo12::finishRunUser() {
00241 if(run_number<=0) return;
00242
00243 int nBtowLow=0, nBtowHigh=0 ,nEtowLow=0, nEtowHigh=0 ;
00244
00245 if(mLogFile==0) { LOG(ERR,"no open output log file,skip ped_finish()\n"); return;}
00246 fprintf(mLogFile,"#L2-ped algorithm finishRun(%d), compiled: %s , %s\n",run_number,__DATE__,__TIME__);
00247 fprintf(mLogFile,"#params: pedSubtr=%d speedFact=%d saveBin=%d debug=%d prescAccept=%d\n",par_pedSubtr,par_speedFact,par_saveBinary,par_dbg,par_prescAccept);
00248 hA[10]->printCSV(mLogFile);
00249 int iMax=-3, iFWHM=-4;
00250 hA[11]->findMax( &iMax, &iFWHM);
00251 fprintf(mLogFile,"#L2ped CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",iMax, iFWHM,nInp);
00252 if(par_saveBinary) fprintf(mLogFile,"#L2ped will save full spectra for all towers\n");
00253
00254 int par_topAdc=85;
00255 int maxPedDeviation=5;
00256 int iadcHigh=maxAdc - maxPedDeviation;
00257 int iadcLow =minAdc + maxPedDeviation;
00258
00259 char xAxis[100];
00260 sprintf(xAxis,"raw ADC + %d",-minAdc);
00261 if(par_pedSubtr) sprintf(xAxis,"ADC - ped + %d",-minAdc);
00262
00263
00264 fprintf(mLogFile,"# L2ped-Adc spectra, run=%d, Z-scale is ln(yield), only first digit shown; maxPedDev=%d table format:\n# name, ped, sigPed, crate, chan, softID-m-s-e, RDO_ID;\n# ADC spectrum: [%d ... <=-10 ... *=0 ... >=+10 ... :=+20 ... %d], Xaxis=%s\n",run_number,maxPedDeviation,minAdc,par_topAdc,xAxis);
00265
00266 int i;
00267 int nB=0;
00268 for(i=0; i<EmcDbIndexMax; i++) {
00269 const L2EmcDb2012::EmcCDbItem *x=mDb->getByIndex(i);
00270 if(mDb->isEmpty(x)) continue;
00271 if (mDb->isBTOW(x) ||mDb->isETOW(x) ) {
00272 nB++;
00273
00274 int iMax=-3, iFWHM=-4;
00275 char pedQA='?';
00276 L2Histo *h=0;
00277 if(mDb->isBTOW(x)) h= btowAdc[x->rdo];
00278 else if(mDb->isETOW(x)) h= etowAdc[x->rdo];
00279 else continue;
00280
00281
00282 int pedRes=999;
00283 int maxRes=3;
00284 if(h->findMax( &iMax, &iFWHM)) {
00285 pedQA='0';
00286 if(iMax<iadcLow) pedQA='-';
00287 else if(iMax>iadcHigh) pedQA='+';
00288 pedRes=iMax+minAdc;
00289 if(!par_pedSubtr) pedRes=int(pedRes - x->ped);
00290 }
00291
00292 if(mDb->isBTOW(x)) {
00293 if(pedQA=='-' ) nBtowLow++;
00294 else if(pedQA=='+') nBtowHigh++;
00295
00296 int ieta= (x->eta-1);
00297 int iphi= (x->sec-1)*10 + x->sub-'a' ;
00298 int ihard=x->chan+(x->crate-1)*160;
00299 if(x->fail) pedRes =0;
00300 hA[20]->fillW(ihard,pedRes);
00301 if(x->fail) {
00302 pedRes =-100;
00303 } else {
00304 if(pedRes<-maxRes) pedRes=-maxRes;
00305 if(pedRes>maxRes) pedRes=maxRes;
00306 }
00307 hA[21]->fillW(ieta, iphi,pedRes);
00308
00309
00310 const int *Data=h->getData();
00311 for(int k=0;k<par_maxMatt;k++)
00312 hA[22]->fillW(ihard,k,Data[k]);
00313
00314 } else {
00315 if(pedQA=='-' ) nEtowLow++;
00316 else if(pedQA=='+') nEtowHigh++;
00317
00318 int ieta= 12-x->eta;
00319 int iphi= (x->sec-1)*5 + x->sub-'A' ;
00320 int ihard=x->chan+(x->crate-1)*128;
00321 if(x->fail) pedRes =0;
00322 hA[30]->fillW(ihard,pedRes);
00323 if(x->fail) {
00324 pedRes =-100;
00325 } else {
00326 if(pedRes<-maxRes) pedRes=-maxRes;
00327 if(pedRes>maxRes) pedRes=maxRes;
00328 }
00329 hA[31]->fillW(ieta, iphi,pedRes);
00330
00331
00332 const int *Data=h->getData();
00333 for(int k=0;k<par_maxMatt;k++)
00334 hA[33]->fillW(ihard,k,Data[k]);
00335
00336 }
00337
00338 char okC=' ';
00339 if(x->fail) okC='#';
00340 fprintf(mLogFile,"%c%s %3d %4.1f 0x%02x 0x%02x %15s %4d ",okC,x->name,iMax+minAdc,iFWHM/2.3,x->crate, x->chan,x->tube, x->rdo);
00341 h->printPed(mLogFile,minAdc,par_topAdc,' ');
00342 fprintf(mLogFile,"qa=%c\n",pedQA);
00343
00344 }
00345 }
00346
00347
00348 fprintf(mLogFile,"#L2ped_finishRun() # of towers with |ped-pedDB| >10 chan\n# BTOW: nLow=%d nHigh=%d ; ETOW nLow=%d, nHigh=%d\n",nBtowLow, nBtowHigh,nEtowLow, nEtowHigh);
00349 fprintf(mLogFile,"# found peds for nB+E=%d , seen events=%d\n",nB,nInp);
00350
00351
00352
00353 if(mHistFile==0) {
00354 LOG(ERR,"Can't open .hist.bin file,skip ped_finish()\n");
00355 goto end;
00356 }
00357
00358
00359 if(par_saveBinary) {
00360 LOG(DBG,"l2ped_finish() , save FULL spectra binary...\n");
00361
00362
00363 for(i=0; i<EmcDbIndexMax; i++) {
00364 const L2EmcDb2012::EmcCDbItem *x=mDb->getByIndex(i);
00365 if(mDb->isEmpty(x)) continue;
00366 L2Histo *h=0;
00367 char tit[400];
00368 if(mDb->isBTOW(x) ){
00369 h= btowAdc[x->rdo];
00370
00371 sprintf(tit,"BTOW=%s cr/ch=%03d/%03d stat/0x=%04x+%04x soft=%s; %s",x->name, x->crate, x->chan, x->stat, x->fail,x->tube,xAxis);
00372 } else if(mDb->isETOW(x) ) {
00373 h= etowAdc[x->rdo];
00374
00375 sprintf(tit,"ETOW=%s cr/ch=%03d/%03d stat/0x=%04x+%04x pname=%s; %s",x->name, x->crate, x->chan, x->stat, x->fail,x->tube,xAxis);
00376 }
00377 if(h==0) continue;
00378 h->setTitle(tit);
00379 h->write(mHistFile);
00380
00381 }
00382 }
00383
00384 end:
00385 run_number=-2;
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444