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