5 #ifdef IS_REAL_L2 //in l2-ana environment
7 #include "../L2algoUtil/L2Histo.h"
8 #include "../L2algoUtil/L2EmcDb.h"
9 #else //full path needed for cvs'd code
10 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
11 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb.h"
16 #include "L2VirtualAlgo2009.h"
18 L2VirtualAlgo2009::L2VirtualAlgo2009(
const char* name,
L2EmcDb* db,
char* outDir,
bool needbarrel,
bool needendcap,
int resOff) : mDb(db) {
23 mNeeds_barrel=needbarrel;
24 mNeeds_endcap=needendcap;
29 mEveStream_btow=globL2eventStream2009.get_btow();
30 mEveStream_etow=globL2eventStream2009.get_etow();
33 mhN =
new L2Histo(900,
"total events. 0=anyInput 10=anyAccept 11=normalAccept 12=rndAccept",19);
34 mhTc=
new L2Histo(901,
"L2 COMPUTE time per input event; x: COMPUTE time (CPU kTics); y: events ",180);
35 mhTd=
new L2Histo(902,
"L2 DECISION time per input event; x: DECISION time (CPU kTics); y: events ",36);
36 mhTcd=
new L2Histo(903,
"L2 COMP+DECI time per input event; x: COMP+DECIS time (CPU kTics); y: events ",180);
38 int mxRunDration=2000;
39 mhRc=
new L2Histo(905,
"rate of COMPUTE; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
40 mhRd=
new L2Histo(906,
"rate of DECISION; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
41 mhRa=
new L2Histo(907,
"rate of ACCEPT; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
46 if (!(L2eventStream2009::mxToken == L2eventStream2009::tokenMask+1))
49 sprintf(err,
"Algo %s has failed consistency check '(L2eventStream2009::mxToken == L2eventStream2009::tokenMask+1)'",name);
57 L2VirtualAlgo2009::initRun(
int runNo,
int *rc_ints,
float *rc_floats) {
61 sprintf(err,
"%s is flagged as broken. Aborting init.",getName());
69 if(mDb->getRun()!=runNo)
return -700;
71 if(mRunNumber==runNo) {
72 if (mLogFile) fprintf(mLogFile,
"#L2-%s::initRun(%d)=ghost already initilized, Abort run\n",getName(), runNo);
86 unsigned int high,low;
87 rdtsc_macro(low,high);
88 mRunStartTicks=high; mRunStartTicks <<= 32; mRunStartTicks += low;
94 sprintf(Fname,
"%s/run%d.l2%s.log",mOutDir1.c_str(),mRunNumber,getName());
97 mLogFile = fopen(Fname,
"w");
98 if( mLogFile==0) printf(
" L2-%s() UNABLE to open run summary log file, continue anyhow\n",getName());
101 par_RndAcceptPrescale=0;
104 int kBad=initRunUser( runNo, rc_ints, rc_floats);
114 if (par_RndAcceptPrescale<0) par_RndAcceptPrescale=0;
115 if (par_RndAcceptPrescale>0)
116 mRndAcceptCounter=rand()%par_RndAcceptPrescale;
120 fprintf(mLogFile,
"#L2-%s initRun() params checked for consistency, Error flag=0x%04x\n",getName(),kBad);
125 fprintf(mLogFile,
"#L2-%s initRun() ABORT due to internal logic\n",getName());
133 fprintf(mLogFile,
"#L2-%s random accept counter started at %d\n",getName(),mRndAcceptCounter);
134 fprintf(mLogFile,
"#L2-%s initRun successful\n",getName());
136 #ifdef ADD_HARDCODED_DELAY
137 fprintf(mLogFile,
"#WARN: HARDCODED_DELAY in compute() & decision() is ON\n");
150 L2VirtualAlgo2009::finishRun() {
152 if(mRunNumber<0)
return;
153 if(mLogFile)fprintf(mLogFile,
"#L2-%s: finishRun(%d) called after %d seconds\n",getName(),mRunNumber ,mSecondsInRun);
158 sprintf(Fname,
"%s/run%d.l2%s.hist.bin",mOutDir1.c_str(),mRunNumber,getName());
160 mHistFile = fopen(Fname,
"w");
164 printf(
" L2-%s: finishRun() UNABLE to open run summary log file, continue anyhow\n",getName());
166 fprintf(mLogFile,
"#L2-%s histos NOT saved, I/O error\n",getName());
170 mhN->printCSV(mLogFile);
171 int nh=finishCommonHistos();
173 fprintf(mLogFile,
"#L2-%s: %d histos saved to '%s'\n",getName(),nh,Fname);
176 if (mLogFile && useDsmMask)
178 fprintf(mLogFile,
"#L2-%s: %d DSM masks are used.\n",getName(),nmasks);
179 for (
int i=0;i<nmasks;i++)
180 fprintf(mLogFile,
"# Mask %d: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
181 i,DsmMask[i][0],DsmMask[i][1],DsmMask[i][2],DsmMask[i][3],DsmMask[i][4],
182 DsmMask[i][5],DsmMask[i][6],DsmMask[i][7]);
185 for (
int i=0; i<mxHA;i++)
if(hA[i])hA[i]->reset();
201 L2VirtualAlgo2009::finishCommonHistos() {
204 for(j=0;j<mxHA;j++) {
205 if(hA[j]==0)
continue;
206 hA[j]->write(mHistFile);
210 L2Histo *hT[nHt]={mhTc,mhTd,mhTcd};
211 const char *text[nHt]={
"Compute ",
"Decision ",
"Deci+Comp"};
213 for(ih=0;ih<nHt;ih++) {
214 int iMax=-3, iFWHM=-4;
215 hT[ih]->findMax( &iMax, &iFWHM);
219 fprintf(mLogFile,
"#L2:%s %s CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",getName(),text[ih],iMax, iFWHM,mEventsInRun);
222 if (mHistFile) {hT[ih]->write(mHistFile); nh++;}
225 mhN->write(mHistFile); nh++;
226 mhRc->write(mHistFile);nh++;
227 mhRd->write(mHistFile);nh++;
228 mhRa->write(mHistFile);nh++;
235 L2VirtualAlgo2009::~L2VirtualAlgo2009(){};
239 L2VirtualAlgo2009::readParams(
const char *fileN,
int mxPar,
int *iPar,
float *fPar) {
245 memset(iPar,0,mxPar*
sizeof(
int));
246 memset(fPar,0,mxPar*
sizeof(
int));
247 FILE *fd=fopen(fileN,
"r");
248 if(fd==0) { printf(
" L2VirtualAlgo2009::readParams failed to open =%s=\n",fileN);
return -2222;}
251 int nInt=0, nFloat=0;
258 char * ret=fgets(buf,mx,fd);
261 if(buf[0]==0)
continue;
262 if(buf[0]==
'#')
continue;
263 if(buf[0]==
'\n')
continue;
265 if (mode==0 && strstr(buf,
"INTS")) { mode=1;
continue; }
266 if (mode==1 && strstr(buf,
"FLOATS")) { mode=2;
continue; }
269 if(nInt>=mxPar) {nVal=-501;
break;}
270 int ret1=sscanf(buf,
"%i",iPar+nInt);
271 if(ret1!=1) {nVal=-100*mode -nInt;
break;}
275 if(nFloat>=mxPar) {nVal=-502;
break;}
276 int ret1=sscanf(buf,
"%f",fPar+nFloat);
277 if(ret1!=1) {nVal= -100*mode -nFloat;
break;}
284 printf(
" L2VirtualAlgo2009::readParams %d from '%s'\n",nVal,fileN);
291 L2VirtualAlgo2009::compute(
int token){
295 token&=L2eventStream2009::tokenMask;
297 #ifdef ADD_HARDCODED_DELAY
301 for(
int i=0;i<3*100;i++) {
float x=i*i; x=x;}
304 computeUser( token );
311 L2VirtualAlgo2009::computeStart(){
314 unsigned int high,low;
315 rdtsc_macro(low,high);
317 mComputeTimeStart=low;
318 unsigned long long ticks = high;
323 mSecondsInRun=(ticks- mRunStartTicks)/par_cpuTicksPerSecond;
325 mhRc->fill(mSecondsInRun);
335 L2VirtualAlgo2009::computeStop(
int token){
337 rdtscl_macro(mComputeTimeStop);
338 unsigned long xxx=mComputeTimeStop-mComputeTimeStart;
339 mComputeTimeDiff[token]=xxx;
348 L2VirtualAlgo2009::decision(
int token,
bool barrel_is_in,
bool endcap_is_in,
int *myL2Result){
354 rdtscl_macro(mDecisionTimeStart);
355 token&=L2eventStream2009::tokenMask;
358 mhRd->fill(mSecondsInRun);
360 #ifdef ADD_HARDCODED_DELAY
364 for(
int i=0;i<3*100;i++) {
float x=i*i; x=x;}
369 if (par_RndAcceptPrescale>0)
371 mRndAcceptCounter=(mRndAcceptCounter+1)%par_RndAcceptPrescale;
372 mRandomAccept=(mRndAcceptCounter==0);
376 if ( (!endcap_is_in && mNeeds_endcap) || (!barrel_is_in && mNeeds_barrel) )
382 mAccept=decisionUser(token, myL2Result+mResultOffset);
387 if(mAccept || mRandomAccept) {
389 mhRa->fill(mSecondsInRun);
397 rdtscl_macro(mDecisionTimeStop);
398 mDecisionTimeDiff=mDecisionTimeStop-mDecisionTimeStart;
399 int kTick=mDecisionTimeDiff/1000;
403 kTick=(mDecisionTimeDiff+mComputeTimeDiff[token])/1000;
405 return (mAccept || mRandomAccept);
413 L2VirtualAlgo2009::printCalibratedData(
int token){
416 const int hitSize=mEveStream_btow[token].get_hitSize();
417 printf(
"printCalibratedData-%s: ---BTOW ADC list--- size=%d\n",getName(),hitSize);
419 for(i=0;i< hitSize;i++,hit++) {
424 printf(
" btow: i=%2d rdo=%4d adc=%d et=%.3f ene=%.3f\n",i,rdo,adc,et,ene);
432 L2VirtualAlgo2009::criticalError(
const char* message){
435 LOG(CRIT,
"%s",message,0,0,0,0);
437 printf(
"CRITICAL MESSAGE: %s\n",message);
445 bool L2VirtualAlgo2009::checkDsmMask(
unsigned short *lastDSM)
449 if (!useDsmMask)
return 1;
451 if (nmasks==0)
return 1;
455 for (
int i=0;i<nmasks;i++)
458 for (
int j=0;j<8 && isGood;j++)
462 if((swap_bytes(lastDSM[j]) & DsmMask[i][j]) != DsmMask[i][j]) isGood=
false;
464 if (isGood)
return 1;
470 int L2VirtualAlgo2009::readDsmMask(
const char *fileN)
472 for (
int i=0;i<kMaximumNumberOfDsmMasks;i++)
473 for (
int j=0;j<8;j++)
476 FILE *fd=fopen(fileN,
"r");
478 printf(
" L2VirtualAlgo2009::readDsmMask failed to open =%s=. Assuming no mask.\n",fileN);
490 char * ret=fgets(buf,mx,fd);
493 if(buf[0]==0)
continue;
494 if(buf[0]==
'#')
continue;
495 if(buf[0]==
'\n')
continue;
496 if(nmasks>=kMaximumNumberOfDsmMasks) {printf(
" L2VirtualAlgo2009::readDsmMask: Too many masks %s.\n",fileN);
return -3333;}
497 int ret1=sscanf(buf,
"%hu",&(DsmMask[nmasks][n%8]));
498 if(ret1!=1) {printf(
" L2VirtualAlgo2009::readDsmMask: Problem reading %s.\n",fileN);
return -4444;}
500 if (n%8==0) nmasks++;
503 if (n%8!=0){printf(
" L2VirtualAlgo2009::readDsmMask: Wrong number of arguments in %s (n=%d).\n",fileN,n);
return -3333;}
508 unsigned short L2VirtualAlgo2009::swap_bytes(
unsigned short in)
510 unsigned short out=0;
511 unsigned char *a, *b;
513 a=(
unsigned char*)(&in);
514 b=(
unsigned char*)(&out);