00001 #include <string.h>
00002 #include <stdio.h>
00003 #include <cstdlib>
00004
00005 #include "fakeRtsLog.h"
00006
00007 #ifdef IS_REAL_L2 //in l2-ana environment
00008 #include "../L2algoUtil/L2Histo.h"
00009 #include "../L2algoUtil/L2EmcDb2012.h"
00010 #else //full path needed for cvs'd code
00011 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
00012 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb2012.h"
00013 #endif
00014
00015
00016
00017 #include "L2VirtualAlgo2012.h"
00018
00019 L2VirtualAlgo2012::L2VirtualAlgo2012(const char* name, const char *uid, L2EmcDb2012* db, char* outDir, bool needbarrel, bool needendcap, int resOff) : mDb(db) {
00020 algoIsOkay=true;
00021 mxHA=0;
00022 mName1=name;
00023 mName1 = mName1 + "." + uid;
00024 mOutDir1=outDir;
00025 mNeeds_barrel=needbarrel;
00026 mNeeds_endcap=needendcap;
00027 mRunNumber=-1;
00028 mResultOffset=resOff;
00029
00030
00031 mEveStream_btow=globL2eventStream2012.get_btow();
00032 mEveStream_etow=globL2eventStream2012.get_etow();
00033
00034 setOflTrigID(0);
00035 mhN =new L2Histo(900,"total events. 0=anyInput 10=anyAccept 11=normalAccept 12=rndAccept",19);
00036 mhTc=new L2Histo(901,"L2 COMPUTE time per input event; x: COMPUTE time (CPU kTics); y: events ",180);
00037 mhTd=new L2Histo(902,"L2 DECISION time per input event; x: DECISION time (CPU kTics); y: events ",36);
00038 mhTcd=new L2Histo(903,"L2 COMP+DECI time per input event; x: COMP+DECIS time (CPU kTics); y: events ",180);
00039
00040 int mxRunDration=2000;
00041 mhRc= new L2Histo(905,"rate of COMPUTE; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
00042 mhRd= new L2Histo(906,"rate of DECISION; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
00043 mhRa= new L2Histo(907,"rate of ACCEPT; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
00044
00045
00046
00047 if (!(L2eventStream2012::mxToken == L2eventStream2012::tokenMask+1))
00048 {
00049 char err[200];
00050 sprintf(err,"Algo %s has failed consistency check '(L2eventStream2012::mxToken == L2eventStream2012::tokenMask+1)'",name);
00051 criticalError(err);
00052 }
00053 }
00054
00055
00056
00057 int
00058 L2VirtualAlgo2012::initRun( int runNo, int *rc_ints, float *rc_floats) {
00059 if(!algoIsOkay)
00060 {
00061 char err[100];
00062 sprintf(err,"%s is flagged as broken. Aborting init.",getName());
00063 criticalError(err);
00064 return -999;
00065 }
00066
00067 useDsmMask=false;
00068
00069
00070 if(mDb->getRun()!=runNo) return -700;
00071
00072 if(mRunNumber==runNo) {
00073 if (mLogFile) fprintf(mLogFile,"#L2-%s::initRun(%d)=ghost already initilized, Abort run\n",getName(), runNo);
00074 return -701;
00075 }
00076
00077
00078 mhN->reset();
00079 mhTc->reset();
00080 mhTd->reset();
00081 mhTcd->reset();
00082 mhRc->reset();
00083 mhRd->reset();
00084 mhRa->reset();
00085
00086
00087 unsigned int high,low;
00088 rdtsc_macro(low,high);
00089 mRunStartTicks=high; mRunStartTicks <<= 32; mRunStartTicks += low;
00090
00091 mRunNumber =runNo;
00092 mEventsInRun=0;
00093
00094 char Fname[1000];
00095 sprintf(Fname,"%s/run%d.l2%s.log",mOutDir1.c_str(),mRunNumber,getName());
00096
00097 mLogFile = fopen(Fname,"w");
00098 if( mLogFile==0) {
00099 LOG(ERR," L2-%s() UNABLE to open run summary log file, continue anyhow\n",getName());
00100 }
00101
00102
00103 par_RndAcceptPrescale=0;
00104 mRndAcceptCounter=0;
00105
00106 int kBad=initRunUser( runNo, rc_ints, rc_floats);
00107
00108
00109
00110 mRandomAccept=false;
00111
00112
00113
00114
00115
00116 if (par_RndAcceptPrescale<0) par_RndAcceptPrescale=0;
00117 if (par_RndAcceptPrescale>0)
00118 mRndAcceptCounter=rand()%par_RndAcceptPrescale;
00119
00120
00121 if (mLogFile) {
00122 fprintf(mLogFile,"#L2-%s initRun() params checked for consistency, Error flag=0x%04x\n",getName(),kBad);
00123 }
00124
00125 if(kBad<0) {
00126 if (mLogFile) {
00127 fprintf(mLogFile,"#L2-%s initRun() ABORT due to internal logic\n",getName());
00128 fclose(mLogFile);
00129 }
00130 mRunNumber=-55;
00131 return kBad;
00132 }
00133
00134 if (mLogFile) {
00135 fprintf(mLogFile,"#L2-%s random accept counter started at %d\n",getName(),mRndAcceptCounter);
00136 fprintf(mLogFile,"#L2-%s initRun successful\n",getName());
00137
00138 #ifdef ADD_HARDCODED_DELAY
00139 fprintf(mLogFile,"#WARN: HARDCODED_DELAY in compute() & decision() is ON\n");
00140 #endif
00141
00142 }
00143
00144 return kBad;
00145 }
00146
00147
00148
00149 void
00150 L2VirtualAlgo2012::finishRun() {
00151
00152 if(mRunNumber<0) return;
00153 if(mLogFile)fprintf(mLogFile,"#L2-%s: finishRun(%d) called after %d seconds\n",getName(),mRunNumber ,mSecondsInRun);
00154
00155
00156
00157 char Fname[1000];
00158 sprintf(Fname,"%s/run%d.l2%s.hist.bin",mOutDir1.c_str(),mRunNumber,getName());
00159 mHistFile = fopen(Fname,"w");
00160
00161
00162 if( mHistFile==0) {
00163 LOG(ERR," L2-%s: finishRun() UNABLE to open run summary log file, continue anyhow\n",getName());
00164 if (mLogFile)
00165 fprintf(mLogFile,"#L2-%s histos NOT saved, I/O error\n",getName());
00166 } else {
00167 finishRunUser();
00168 if (mLogFile)
00169 mhN->printCSV(mLogFile);
00170 int nh=finishCommonHistos();
00171 if (mLogFile)
00172 fprintf(mLogFile,"#L2-%s: %d histos saved to '%s'\n",getName(),nh,Fname);
00173 }
00174
00175 if (mLogFile && useDsmMask)
00176 {
00177 fprintf(mLogFile,"#L2-%s: %d DSM masks are used.\n",getName(),nmasks);
00178 for (int i=0;i<nmasks;i++)
00179 fprintf(mLogFile,"# Mask %d: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
00180 i,DsmMask[i][0],DsmMask[i][1],DsmMask[i][2],DsmMask[i][3],DsmMask[i][4],
00181 DsmMask[i][5],DsmMask[i][6],DsmMask[i][7]);
00182 }
00183 mRunNumber=-2;
00184 for (int i=0; i<mxHA;i++) if(hA[i])hA[i]->reset();
00185
00186 if (mLogFile) {
00187 fclose(mLogFile);
00188 mLogFile=0;
00189 }
00190
00191 if ( mHistFile) {
00192 fclose(mHistFile);
00193 mHistFile=0;
00194 }
00195 }
00196
00197
00198
00199 int
00200 L2VirtualAlgo2012::finishCommonHistos() {
00201 int j;
00202 int nh=0;
00203 for(j=0;j<mxHA;j++) {
00204 if(hA[j]==0) continue;
00205 hA[j]->write(mHistFile);
00206 nh++;
00207 }
00208 const int nHt=3;
00209 L2Histo *hT[nHt]={mhTc,mhTd,mhTcd};
00210 const char *text[nHt]={"Compute ","Decision ","Deci+Comp"};
00211 int ih;
00212 for(ih=0;ih<nHt;ih++) {
00213 int iMax=-3, iFWHM=-4;
00214 hT[ih]->findMax( &iMax, &iFWHM);
00215
00216 if (mLogFile){
00217 fprintf(mLogFile,"#L2:%s %s CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",getName(),text[ih],iMax, iFWHM,mEventsInRun);
00218
00219 }
00220 if (mHistFile) {hT[ih]->write(mHistFile); nh++;}
00221 }
00222 if (mHistFile) {
00223 mhN->write(mHistFile); nh++;
00224 mhRc->write(mHistFile);nh++;
00225 mhRd->write(mHistFile);nh++;
00226 mhRa->write(mHistFile);nh++;
00227 }
00228 return nh;
00229 }
00230
00231
00232
00233 L2VirtualAlgo2012::~L2VirtualAlgo2012(){};
00234
00235
00236 int
00237 L2VirtualAlgo2012::readParams(const char *fileN, int mxPar, int *iPar, float *fPar) {
00238
00239
00240
00241
00242
00243 memset(iPar,0,mxPar*sizeof(int));
00244 memset(fPar,0,mxPar*sizeof(int));
00245 FILE *fd=fopen(fileN,"r");
00246 if(fd==0) { LOG(ERR,"L2VirtualAlgo2012::readParams failed to open =%s=\n",fileN); return -2222;}
00247
00248 int nVal=0;
00249 int nInt=0, nFloat=0;
00250
00251 const int mx=1000;
00252 char buf[mx];
00253 int mode=0;
00254
00255 for(;;) {
00256 char * ret=fgets(buf,mx,fd);
00257 if(ret==0) break;
00258 if(buf[0]==0) continue;
00259 if(buf[0]=='#') continue;
00260 if(buf[0]=='\n') continue;
00261
00262 if (mode==0 && strstr(buf,"INTS")) { mode=1; continue; }
00263 if (mode==1 && strstr(buf,"FLOATS")) { mode=2; continue; }
00264 if(mode==1) {
00265 if(nInt>=mxPar) {nVal=-501; break;}
00266 int ret1=sscanf(buf,"%i",iPar+nInt);
00267 if(ret1!=1) {nVal=-100*mode -nInt; break;}
00268 nInt++; nVal++;
00269 }
00270 else if(mode==2) {
00271 if(nFloat>=mxPar) {nVal=-502; break;}
00272 int ret1=sscanf(buf,"%f",fPar+nFloat);
00273 if(ret1!=1) {nVal= -100*mode -nFloat; break;}
00274 nFloat++; nVal++;
00275 }
00276
00277 }
00278
00279 fclose(fd);
00280 LOG(DBG,"L2VirtualAlgo2012::readParams %d from '%s'\n",nVal,fileN);
00281 return nVal;
00282 }
00283
00284
00285
00286 void
00287 L2VirtualAlgo2012::compute(int token){
00288
00289 computeStart();
00290 mhN->fill(1);
00291 token&=L2eventStream2012::tokenMask;
00292
00293 #ifdef ADD_HARDCODED_DELAY
00294
00295
00296
00297 for(int i=0;i<3*100;i++) { float x=i*i; x=x;}
00298 #endif
00299
00300 computeUser( token );
00301 computeStop( token);
00302
00303 }
00304
00305
00306 void
00307 L2VirtualAlgo2012::computeStart(){
00308
00309
00310 unsigned int high,low;
00311 rdtsc_macro(low,high);
00312
00313 mComputeTimeStart=low;
00314 unsigned long long ticks = high;
00315 ticks <<= 32;
00316 ticks += low;
00317
00318
00319 mSecondsInRun=(ticks- mRunStartTicks)/par_cpuTicksPerSecond;
00320
00321 mhRc->fill(mSecondsInRun);
00322
00323 mAccept=true;
00324 mEventsInRun++;
00325
00326 mhN->fill(0);
00327 }
00328
00329
00330 void
00331 L2VirtualAlgo2012::computeStop(int token){
00332
00333 rdtscl_macro(mComputeTimeStop);
00334 unsigned long xxx=mComputeTimeStop-mComputeTimeStart;
00335 mComputeTimeDiff[token]=xxx;
00336 int kTick=xxx/1000;
00337 mhTc->fill(kTick);
00338 }
00339
00340
00341
00342 bool
00343 L2VirtualAlgo2012::decision(int token, bool barrel_is_in, bool endcap_is_in, int *myL2Result){
00344
00345
00346
00347
00348
00349
00350 LOG(DBG, "Decision...");
00351
00352 rdtscl_macro(mDecisionTimeStart);
00353 token&=L2eventStream2012::tokenMask;
00354 mDecisionTimeDiff=0;
00355
00356 mhRd->fill(mSecondsInRun);
00357
00358 #ifdef ADD_HARDCODED_DELAY
00359
00360
00361
00362 for(int i=0;i<3*100;i++) { float x=i*i; x=x;}
00363 #endif
00364
00365 mhN->fill(2);
00366
00367 if (par_RndAcceptPrescale>0)
00368 {
00369 mRndAcceptCounter=(mRndAcceptCounter+1)%par_RndAcceptPrescale;
00370 mRandomAccept=(mRndAcceptCounter==0);
00371 }
00372
00373
00374
00375 LOG(DBG, "bin %d needsb %d ee %d ne %d",
00376 barrel_is_in, mNeeds_barrel, endcap_is_in, mNeeds_endcap);
00377
00378 if ( (!endcap_is_in && mNeeds_endcap) || (!barrel_is_in && mNeeds_barrel) )
00379 {
00380 mAccept=0;
00381 }
00382 else
00383 {
00384 mAccept=decisionUser(token, myL2Result+mResultOffset);
00385 }
00386
00387 if(mAccept || mRandomAccept) {
00388 mhN->fill(10);
00389 mhRa->fill(mSecondsInRun);
00390 }
00391 if(mRandomAccept)
00392 mhN->fill(12);
00393 if(mAccept)
00394 mhN->fill(11);
00395
00396
00397 rdtscl_macro(mDecisionTimeStop);
00398 mDecisionTimeDiff=mDecisionTimeStop-mDecisionTimeStart;
00399 int kTick=mDecisionTimeDiff/1000;
00400
00401 mhTd->fill(kTick);
00402 kTick=(mDecisionTimeDiff+mComputeTimeDiff[token])/1000;
00403 mhTcd->fill(kTick);
00404 return (mAccept || mRandomAccept);
00405 }
00406
00407
00408
00409
00410
00411 void
00412 L2VirtualAlgo2012::printCalibratedData(int token){
00413
00414 int i;
00415 const int hitSize=mEveStream_btow[token].get_hitSize();
00416 LOG(NOTE,"printCalibratedData-%s: ---BTOW ADC list--- size=%d\n",getName(),hitSize);
00417 const HitTower1 *hit=mEveStream_btow[token].get_hits();
00418 for(i=0;i< hitSize;i++,hit++) {
00419 int adc=hit->adc;
00420 int rdo=hit->rdo;
00421 float et=hit->et;
00422 float ene=hit->ene;
00423 LOG(NOTE," btow: i=%2d rdo=%4d adc=%d et=%.3f ene=%.3f\n",i,rdo,adc,et,ene);
00424 }
00425 }
00426
00427
00428
00429
00430 void
00431 L2VirtualAlgo2012::criticalError(const char* message){
00432 algoIsOkay=false;
00433 #ifdef IS_REAL_L2
00434 LOG(CRIT,"%s",message,0,0,0,0);
00435 #else
00436 LOG(CRIT,"CRITICAL MESSAGE: %s\n",message);
00437
00438 #endif
00439 return;
00440 }
00441
00442
00443
00444 bool L2VirtualAlgo2012::checkDsmMask(unsigned short *lastDSM)
00445 {
00446 if (!useDsmMask) return 1;
00447 if (nmasks==0) return 1;
00448
00449 bool isGood;
00450 for (int i=0;i<nmasks;i++)
00451 {
00452 isGood=true;
00453 for (int j=0;j<8 && isGood;j++)
00454 {
00455 if((swap_bytes(lastDSM[j]) & DsmMask[i][j]) != DsmMask[i][j]) isGood=false;
00456 }
00457 if (isGood) return 1;
00458 }
00459 return 0;
00460 }
00461
00462 int L2VirtualAlgo2012::readDsmMask(const char *fileN)
00463 {
00464 for (int i=0;i<kMaximumNumberOfDsmMasks;i++)
00465 for (int j=0;j<8;j++)
00466 DsmMask[i][j]=0;
00467
00468 FILE *fd=fopen(fileN,"r");
00469 if(fd==0) {
00470 LOG(ERR," L2VirtualAlgo2012::readDsmMask failed to open =%s=. Assuming no mask.\n",fileN);
00471 return 0;
00472 useDsmMask=false;
00473 }
00474
00475 int n=0;
00476 nmasks=0;
00477
00478 const int mx=1000;
00479 char buf[mx];
00480
00481 for(;;) {
00482 char * ret=fgets(buf,mx,fd);
00483 if(ret==0) break;
00484 if(buf[0]==0) continue;
00485 if(buf[0]=='#') continue;
00486 if(buf[0]=='\n') continue;
00487 if(nmasks>=kMaximumNumberOfDsmMasks) {LOG(ERR," L2VirtualAlgo2012::readDsmMask: Too many masks %s.\n",fileN); return -3333;}
00488 int ret1=sscanf(buf,"%hu",&(DsmMask[nmasks][n%8]));
00489 if(ret1!=1) {LOG(ERR," L2VirtualAlgo2012::readDsmMask: Problem reading %s.\n",fileN); return -4444;}
00490 n++;
00491 if (n%8==0) nmasks++;
00492 }
00493 fclose(fd);
00494 if (n%8!=0){LOG(ERR," L2VirtualAlgo2012::readDsmMask: Wrong number of arguments in %s (n=%d).\n",fileN,n); return -3333;}
00495 useDsmMask=true;
00496 return 0;
00497 }
00498
00499 unsigned short L2VirtualAlgo2012::swap_bytes(unsigned short in)
00500 {
00501 unsigned short out=0;
00502 unsigned char *a, *b;
00503
00504 a=(unsigned char*)(&in);
00505 b=(unsigned char*)(&out);
00506
00507 b[1]=a[0];
00508 b[0]=a[1];
00509 return out;
00510 }
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558