16 #ifdef IS_REAL_L2 //in l2-ana environment
17 #include "../L2algoUtil/L2EmcDb.h"
18 #include "../L2algoUtil/L2Histo.h"
20 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb.h"
21 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
24 #include "L2pedAlgo.h"
25 #include "L2pedResults2006.h"
29 L2pedAlgo::L2pedAlgo(
const char* name,
L2EmcDb* db,
char* outDir,
int resOff)
41 for(i=0;i<MaxBtowRdo;i++) {
43 sprintf(tit,
"BTOW ADC for rdo=%d; ADC+%d",i,-minAdc);
44 btowAdc[i]=
new L2Histo(20000+i,tit,maxAdc-minAdc+1);
47 for(i=0;i<MaxEtowRdo;i++) {
49 sprintf(tit,
"ETOW ADC for rdo=%d; ADC ",i);
50 etowAdc[i]=
new L2Histo(10000+i,tit,maxAdc-minAdc+1);
54 memset(hA,0,
sizeof(hA));
55 hA[10]=
new L2Histo(10, (
char*)
"total event counter; x=cases",6);
56 hA[11]=
new L2Histo(11, (
char*)
"L2 time used per input event; x: time (CPU 20*kTics); y: events ",500);
59 hA[20]=
new L2Histo(20, (
char*)
"BTOW pedRes Y=ADC-DBped ; x: chan + 160*crate", 4800);
60 hA[21]=
new L2Histo(21, (
char*)
"BTOW pedRes Z=ADC-DBped, saturated @ |3|; x: etaBin ,[-1,+1]; y: phi bin ~sector",40,120);
63 hA[30]=
new L2Histo(30, (
char*)
"ETOW pedRes Y=ADC-DBped ; x: chan + 128*crate", 768);
64 hA[31]=
new L2Histo(31, (
char*)
"ETOW pedRes Z=ADC-DBped, saturated @ |3|; x: 12 - Endcap etaBin ,[+1,+2]; y: phi bin ~sector",12,60);
67 printf(
"L2pedAlgo instantiated, logPath='%s'\n",mOutDir);
74 L2pedAlgo::initRun(
int runNo,
int *rc_ints,
float *rc_floats) {
78 if(mDb->initRun(runNo))
return -27;
84 par_pedSubtr =rc_ints[0]!=0;
85 par_speedFact =rc_ints[1];
86 par_saveBinary=rc_ints[2]!=0;
88 par_prescAccept=rc_ints[4];
90 if(par_prescAccept<0) par_prescAccept=0;
92 if(par_speedFact<1) par_speedFact=1;
94 if(par_speedFact<4) par_speedFact=2;
95 else if(par_speedFact<8) par_speedFact=4;
96 else if(par_speedFact<16) par_speedFact=8;
97 else if(par_speedFact<32) par_speedFact=16;
98 else if(par_speedFact<64) par_speedFact=32;
99 else if(par_speedFact<192) par_speedFact=64;
100 else par_speedFact=192;
105 s_stepB=MaxBtowRdo/ par_speedFact ;
106 s_stepE=MaxEtowRdo/ par_speedFact ;
110 memset(db_btowPed, 0,
sizeof(db_btowPed));
111 memset(db_etowPed, 0,
sizeof(db_etowPed));
115 int nBtowOk=0, nEtowOk=0;
116 for(i=0; i<EmcDbIndexMax; i++) {
118 if(mDb->isEmpty(x))
continue;
119 if (mDb->isBTOW(x) ) {
120 db_btowPed[x->rdo]=(int) (x->ped);
122 }
else if (mDb->isETOW(x) ) {
123 db_etowPed[x->rdo]=(int) (x->ped);
128 printf(
"L2ped algorithm init()... params:\n dbg=%d, pedSubtr=%d saveBinHist=%d speedFact=%d prescAccept=%d\n mapped channels: nBtow=%d nEtow=%d\n",par_dbg,par_pedSubtr,par_saveBinary,par_speedFact,par_prescAccept,nBtowOk,nEtowOk);
130 for(i=0;i<MaxBtowRdo;i++) btowAdc[i]->reset();
131 for(i=0;i<MaxEtowRdo;i++) etowAdc[i]->reset();
132 for(i=0;i<mxHA;i++)
if(hA[i]) hA[i]->reset();
143 L2pedAlgo::doEvent(
int L0trg,
int inpEveId,
TrgDataType* trgData,
144 int bemcIn,
unsigned short *bemcData,
145 int eemcIn,
unsigned short *eemcData){
149 rdtscl_macro(mEveTimeStart);
152 if(par_prescAccept>0) {
153 if((rand()>>4) % par_prescAccept )
return false;
169 short first=s_lastB%MaxBtowRdo;
170 s_lastB=first+s_stepB;
171 if(first==0) hA[10]->fill(1);
174 for(rdo=first; rdo<s_lastB; rdo++){
175 int adc=bemcData[rdo];
176 if(par_pedSubtr) adc-=db_btowPed[rdo];
177 btowAdc[rdo]->fill(adc-minAdc);
184 short first=s_lastE%MaxEtowRdo;
185 s_lastE=first+s_stepE;
186 if(first==0) hA[10]->fill(2);
189 for(rdo=first; rdo<s_lastE; rdo++){
190 int adc=eemcData[rdo];
191 if(par_pedSubtr) adc-=db_etowPed[rdo];
192 etowAdc[rdo]->fill(adc-minAdc);
199 memset(&out,0,
sizeof(out));
205 ( par_pedSubtr <<6 ) ;
207 rdtscl_macro(mEveTimeStop);
208 mEveTimeDiff=mEveTimeStop-mEveTimeStart;
209 int kTick=mEveTimeDiff/1000;
210 hA[11]->fill(kTick/20);
213 const unsigned short maxKT=30000;
214 out.int0.kTick= kTick>maxKT ? maxKT : (int)kTick;
216 unsigned int *outPlace=myTrigData->TrgSum.L2Result+ mResultOffset;
219 if(par_dbg) L2pedResults2006_print(&out);
227 L2pedAlgo::finishRun() {
228 if(run_number<=0)
return;
229 int nBtowLow=0, nBtowHigh=0 ,nEtowLow=0, nEtowHigh=0 ;
232 sprintf(fname,
"%s/run%d.l2ped.out",mOutDir,run_number);
233 printf(
"L2ped_finish('%s') , finding pedestals...\n",fname);
235 FILE *fd=fopen(fname,
"w");
236 if(fd==0) {printf(
"failed to open output %s file,skip ped_finish()\n",fname);
return;}
237 fprintf(fd,
"#L2-ped algorithm finishRun(%d), compiled: %s , %s\n",run_number,__DATE__,__TIME__);
238 fprintf(fd,
"#params: pedSubtr=%d speedFact=%d saveBin=%d debug=%d prescAccept=%d\n",par_pedSubtr,par_speedFact,par_saveBinary,par_dbg,par_prescAccept);
239 hA[10]->printCSV(fd);
240 int iMax=-3, iFWHM=-4;
241 hA[11]->findMax( &iMax, &iFWHM);
242 fprintf(fd,
"#L2ped CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",iMax, iFWHM,nInp);
243 printf(
"L2ped CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",iMax, iFWHM,nInp);
244 if(par_saveBinary) fprintf(fd,
"#L2ped will save full spectra for all towers\n");
247 int maxPedDeviation=5;
252 int iadcHigh=maxAdc - maxPedDeviation;
253 int iadcLow =minAdc + maxPedDeviation;
256 sprintf(xAxis,
"raw ADC + %d",-minAdc);
257 if(par_pedSubtr) sprintf(xAxis,
"ADC - ped + %d",-minAdc);
260 fprintf(fd,
"# 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);
264 for(i=0; i<EmcDbIndexMax; i++) {
266 if(mDb->isEmpty(x))
continue;
267 if (mDb->isBTOW(x) ||mDb->isETOW(x) ) {
271 int iMax=-3, iFWHM=-4;
274 if(mDb->isBTOW(x)) h= btowAdc[x->rdo];
275 else if(mDb->isETOW(x)) h= etowAdc[x->rdo];
281 if(h->findMax( &iMax, &iFWHM)) {
283 if(iMax<iadcLow) pedQA=
'-';
284 else if(iMax>iadcHigh) pedQA=
'+';
286 if(!par_pedSubtr) pedRes=int(pedRes - x->ped);
290 if(pedQA==
'-' ) nBtowLow++;
291 else if(pedQA==
'+') nBtowHigh++;
293 int ieta= (x->eta-1);
294 int iphi= (x->sec-1)*10 + x->sub-
'a' ;
295 int ihard=x->chan+(x->crate-1)*160;
296 if(x->fail) pedRes =0;
297 hA[20]->fillW(ihard,pedRes);
301 if(pedRes<-maxRes) pedRes=-maxRes;
302 if(pedRes>maxRes) pedRes=maxRes;
304 hA[21]->fillW(ieta, iphi,pedRes);
306 if(pedQA==
'-' ) nEtowLow++;
307 else if(pedQA==
'+') nEtowHigh++;
310 int iphi= (x->sec-1)*5 + x->sub-
'A' ;
311 int ihard=x->chan+(x->crate-1)*128;
312 if(x->fail) pedRes =0;
313 hA[30]->fillW(ihard,pedRes);
317 if(pedRes<-maxRes) pedRes=-maxRes;
318 if(pedRes>maxRes) pedRes=maxRes;
320 hA[31]->fillW(ieta, iphi,pedRes);
326 fprintf(fd,
"%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);
327 h->printPed(fd,minAdc,par_topAdc,
' ');
328 fprintf(fd,
"qa=%c\n",pedQA);
334 fprintf(fd,
"#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);
335 fprintf(fd,
"# found peds for nB+E=%d , seen events=%d\n",nB,nInp);
336 printf(
"l2ped_finish() found peds for nB+E=%d , seen events=%d\n",nB,nInp);
341 sprintf(fname,
"%s/run%d.l2ped.hist.bin",mOutDir,run_number);
345 printf(
"failed to open output %s file,skip ped_finish()\n",fname);
349 for(
int j=0;j<mxHA;j++) {
350 if(hA[j]==0)
continue;
355 printf(
"l2ped_finish('%s') , save FULL spectra binary...\n",fname);
358 for(i=0;i<mxHA;i++)
if(hA[i]) hA[i]->write(fd);
359 for(i=0; i<EmcDbIndexMax; i++) {
361 if(mDb->isEmpty(x))
continue;
367 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);
369 }
else if(mDb->isETOW(x) ) {
372 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);
379 printf(
"l2ped_finish() binary full spectra saved\n");