5 #include "fakeRtsLog.h"
7 #ifdef IS_REAL_L2 //in l2-ana environment
8 #include "../L2algoUtil/L2Histo.h"
9 #include "../L2algoUtil/L2EmcDb2012.h"
10 #else //full path needed for cvs'd code
11 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
12 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb2012.h"
17 #include "L2VirtualAlgo2012.h"
19 L2VirtualAlgo2012::L2VirtualAlgo2012(
const char* name,
const char *uid,
L2EmcDb2012* db,
char* outDir,
bool needbarrel,
bool needendcap,
int resOff) : mDb(db) {
23 mName1 = mName1 +
"." + uid;
25 mNeeds_barrel=needbarrel;
26 mNeeds_endcap=needendcap;
31 mEveStream_btow=globL2eventStream2012.get_btow();
32 mEveStream_etow=globL2eventStream2012.get_etow();
35 mhN =
new L2Histo(900,
"total events. 0=anyInput 10=anyAccept 11=normalAccept 12=rndAccept",19);
36 mhTc=
new L2Histo(901,
"L2 COMPUTE time per input event; x: COMPUTE time (CPU kTics); y: events ",180);
37 mhTd=
new L2Histo(902,
"L2 DECISION time per input event; x: DECISION time (CPU kTics); y: events ",36);
38 mhTcd=
new L2Histo(903,
"L2 COMP+DECI time per input event; x: COMP+DECIS time (CPU kTics); y: events ",180);
40 int mxRunDration=2000;
41 mhRc=
new L2Histo(905,
"rate of COMPUTE; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
42 mhRd=
new L2Histo(906,
"rate of DECISION; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
43 mhRa=
new L2Histo(907,
"rate of ACCEPT; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
47 if (!(L2eventStream2012::mxToken == L2eventStream2012::tokenMask+1))
50 sprintf(err,
"Algo %s has failed consistency check '(L2eventStream2012::mxToken == L2eventStream2012::tokenMask+1)'",name);
58 L2VirtualAlgo2012::initRun(
int runNo,
int *rc_ints,
float *rc_floats) {
62 sprintf(err,
"%s is flagged as broken. Aborting init.",getName());
70 if(mDb->getRun()!=runNo)
return -700;
72 if(mRunNumber==runNo) {
73 if (mLogFile) fprintf(mLogFile,
"#L2-%s::initRun(%d)=ghost already initilized, Abort run\n",getName(), runNo);
87 unsigned int high,low;
88 rdtsc_macro(low,high);
89 mRunStartTicks=high; mRunStartTicks <<= 32; mRunStartTicks += low;
95 sprintf(Fname,
"%s/run%d.l2%s.log",mOutDir1.c_str(),mRunNumber,getName());
97 mLogFile = fopen(Fname,
"w");
99 LOG(ERR,
" L2-%s() UNABLE to open run summary log file, continue anyhow (%s)",getName(),Fname);
103 par_RndAcceptPrescale=0;
106 int kBad=initRunUser( runNo, rc_ints, rc_floats);
116 if (par_RndAcceptPrescale<0) par_RndAcceptPrescale=0;
117 if (par_RndAcceptPrescale>0)
118 mRndAcceptCounter=rand()%par_RndAcceptPrescale;
122 fprintf(mLogFile,
"#L2-%s initRun() params checked for consistency, Error flag=0x%04x\n",getName(),kBad);
127 fprintf(mLogFile,
"#L2-%s initRun() ABORT due to internal logic\n",getName());
135 fprintf(mLogFile,
"#L2-%s random accept counter started at %d\n",getName(),mRndAcceptCounter);
136 fprintf(mLogFile,
"#L2-%s initRun successful\n",getName());
138 #ifdef ADD_HARDCODED_DELAY
139 fprintf(mLogFile,
"#WARN: HARDCODED_DELAY in compute() & decision() is ON\n");
150 L2VirtualAlgo2012::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());
159 mHistFile = fopen(Fname,
"w");
163 LOG(ERR,
" L2-%s: finishRun() UNABLE to open run summary log file, continue anyhow (fn=%s)\n",getName(),Fname);
165 fprintf(mLogFile,
"#L2-%s histos NOT saved, I/O error\n",getName());
169 mhN->printCSV(mLogFile);
170 int nh=finishCommonHistos();
172 fprintf(mLogFile,
"#L2-%s: %d histos saved to '%s'\n",getName(),nh,Fname);
175 if (mLogFile && useDsmMask)
177 fprintf(mLogFile,
"#L2-%s: %d DSM masks are used.\n",getName(),nmasks);
178 for (
int i=0;i<nmasks;i++)
179 fprintf(mLogFile,
"# Mask %d: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
180 i,DsmMask[i][0],DsmMask[i][1],DsmMask[i][2],DsmMask[i][3],DsmMask[i][4],
181 DsmMask[i][5],DsmMask[i][6],DsmMask[i][7]);
184 for (
int i=0; i<mxHA;i++)
if(hA[i])hA[i]->reset();
200 L2VirtualAlgo2012::finishCommonHistos() {
203 for(j=0;j<mxHA;j++) {
204 if(hA[j]==0)
continue;
205 hA[j]->write(mHistFile);
209 L2Histo *hT[nHt]={mhTc,mhTd,mhTcd};
210 const char *text[nHt]={
"Compute ",
"Decision ",
"Deci+Comp"};
212 for(ih=0;ih<nHt;ih++) {
213 int iMax=-3, iFWHM=-4;
214 hT[ih]->findMax( &iMax, &iFWHM);
217 fprintf(mLogFile,
"#L2:%s %s CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",getName(),text[ih],iMax, iFWHM,mEventsInRun);
220 if (mHistFile) {hT[ih]->write(mHistFile); nh++;}
223 mhN->write(mHistFile); nh++;
224 mhRc->write(mHistFile);nh++;
225 mhRd->write(mHistFile);nh++;
226 mhRa->write(mHistFile);nh++;
233 L2VirtualAlgo2012::~L2VirtualAlgo2012(){};
237 L2VirtualAlgo2012::readParams(
const char *fileN,
int mxPar,
int *iPar,
float *fPar) {
243 memset(iPar,0,mxPar*
sizeof(
int));
244 memset(fPar,0,mxPar*
sizeof(
int));
245 FILE *fd=fopen(fileN,
"r");
246 if(fd==0) { LOG(ERR,
"L2VirtualAlgo2012::readParams failed to open =%s=\n",fileN);
return -2222;}
249 int nInt=0, nFloat=0;
256 char * ret=fgets(buf,mx,fd);
258 if(buf[0]==0)
continue;
259 if(buf[0]==
'#')
continue;
260 if(buf[0]==
'\n')
continue;
262 if (mode==0 && strstr(buf,
"INTS")) { mode=1;
continue; }
263 if (mode==1 && strstr(buf,
"FLOATS")) { mode=2;
continue; }
265 if(nInt>=mxPar) {nVal=-501;
break;}
266 int ret1=sscanf(buf,
"%i",iPar+nInt);
267 if(ret1!=1) {nVal=-100*mode -nInt;
break;}
271 if(nFloat>=mxPar) {nVal=-502;
break;}
272 int ret1=sscanf(buf,
"%f",fPar+nFloat);
273 if(ret1!=1) {nVal= -100*mode -nFloat;
break;}
280 LOG(DBG,
"L2VirtualAlgo2012::readParams %d from '%s'\n",nVal,fileN);
287 L2VirtualAlgo2012::compute(
int token){
291 token&=L2eventStream2012::tokenMask;
293 #ifdef ADD_HARDCODED_DELAY
297 for(
int i=0;i<3*100;i++) {
float x=i*i; x=x;}
300 computeUser( token );
307 L2VirtualAlgo2012::computeStart(){
310 unsigned int high,low;
311 rdtsc_macro(low,high);
313 mComputeTimeStart=low;
314 unsigned long long ticks = high;
319 mSecondsInRun=(ticks- mRunStartTicks)/par_cpuTicksPerSecond;
321 mhRc->fill(mSecondsInRun);
331 L2VirtualAlgo2012::computeStop(
int token){
333 rdtscl_macro(mComputeTimeStop);
334 unsigned long xxx=mComputeTimeStop-mComputeTimeStart;
335 mComputeTimeDiff[token]=xxx;
343 L2VirtualAlgo2012::decision(
int token,
bool barrel_is_in,
bool endcap_is_in,
int *myL2Result){
350 LOG(DBG,
"Decision...");
352 rdtscl_macro(mDecisionTimeStart);
353 token&=L2eventStream2012::tokenMask;
356 mhRd->fill(mSecondsInRun);
358 #ifdef ADD_HARDCODED_DELAY
362 for(
int i=0;i<3*100;i++) {
float x=i*i; x=x;}
367 if (par_RndAcceptPrescale>0)
369 mRndAcceptCounter=(mRndAcceptCounter+1)%par_RndAcceptPrescale;
370 mRandomAccept=(mRndAcceptCounter==0);
375 LOG(DBG,
"bin %d needsb %d ee %d ne %d",
376 barrel_is_in, mNeeds_barrel, endcap_is_in, mNeeds_endcap);
378 if ( (!endcap_is_in && mNeeds_endcap) || (!barrel_is_in && mNeeds_barrel) )
384 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;
402 kTick=(mDecisionTimeDiff+mComputeTimeDiff[token])/1000;
404 return (mAccept || mRandomAccept);
412 L2VirtualAlgo2012::printCalibratedData(
int token){
415 const int hitSize=mEveStream_btow[token].get_hitSize();
416 LOG(NOTE,
"printCalibratedData-%s: ---BTOW ADC list--- size=%d\n",getName(),hitSize);
418 for(i=0;i< hitSize;i++,hit++) {
423 LOG(NOTE,
" btow: i=%2d rdo=%4d adc=%d et=%.3f ene=%.3f\n",i,rdo,adc,et,ene);
431 L2VirtualAlgo2012::criticalError(
const char* message){
434 LOG(CRIT,
"%s",message,0,0,0,0);
436 LOG(CRIT,
"CRITICAL MESSAGE: %s\n",message);
444 bool L2VirtualAlgo2012::checkDsmMask(
unsigned short *lastDSM)
446 if (!useDsmMask)
return 1;
447 if (nmasks==0)
return 1;
450 for (
int i=0;i<nmasks;i++)
453 for (
int j=0;j<8 && isGood;j++)
455 if((swap_bytes(lastDSM[j]) & DsmMask[i][j]) != DsmMask[i][j]) isGood=
false;
457 if (isGood)
return 1;
462 int L2VirtualAlgo2012::readDsmMask(
const char *fileN)
464 for (
int i=0;i<kMaximumNumberOfDsmMasks;i++)
465 for (
int j=0;j<8;j++)
468 FILE *fd=fopen(fileN,
"r");
470 LOG(ERR,
" L2VirtualAlgo2012::readDsmMask failed to open =%s=. Assuming no mask.\n",fileN);
482 char * ret=fgets(buf,mx,fd);
484 if(buf[0]==0)
continue;
485 if(buf[0]==
'#')
continue;
486 if(buf[0]==
'\n')
continue;
487 if(nmasks>=kMaximumNumberOfDsmMasks) {LOG(ERR,
" L2VirtualAlgo2012::readDsmMask: Too many masks %s.\n",fileN);
return -3333;}
488 int ret1=sscanf(buf,
"%hu",&(DsmMask[nmasks][n%8]));
489 if(ret1!=1) {LOG(ERR,
" L2VirtualAlgo2012::readDsmMask: Problem reading %s.\n",fileN);
return -4444;}
491 if (n%8==0) nmasks++;
494 if (n%8!=0){LOG(ERR,
" L2VirtualAlgo2012::readDsmMask: Wrong number of arguments in %s (n=%d).\n",fileN,n);
return -3333;}
499 unsigned short L2VirtualAlgo2012::swap_bytes(
unsigned short in)
501 unsigned short out=0;
502 unsigned char *a, *b;
504 a=(
unsigned char*)(&in);
505 b=(
unsigned char*)(&out);