00001 #include <string.h>
00002 #include <stdio.h>
00003
00004 #ifdef IS_REAL_L2 //in l2-ana environment
00005 #include "trgStructures.h"
00006 #include "../L2algoUtil/L2Histo.h"
00007 #include "../L2algoUtil/L2EmcDb.h"
00008 #else
00009 #include "StDaqLib/TRG/trgStructures.h"
00010 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
00011 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb.h"
00012 #endif
00013
00014
00015
00016 #include "L2VirtualAlgo2008.h"
00017
00018 L2VirtualAlgo2008::L2VirtualAlgo2008(const char* name, L2EmcDb* db, char* outDir) : mDb(db) {
00019 mxHA=0;
00020 mName1=name;
00021 mOutDir1=outDir;
00022
00023
00024 mEveStream_btow=globL2eventStream2008.get_btow();
00025 mEveStream_etow=globL2eventStream2008.get_etow();
00026
00027 setOflTrigID(0);
00028 mhN =new L2Histo(900,"total events 0=anyInput, 10=anyAccept; x=cases",19);
00029 mhTc=new L2Histo(901,"L2 COMPUTE time per input event; x: COMPUTE time (CPU kTics); y: events ",180);
00030 mhTd=new L2Histo(902,"L2 DECISION time per input event; x: DECISION time (CPU kTics); y: events ",36);
00031 mhTcd=new L2Histo(903,"L2 COMP+DECI time per input event; x: COMP+DECIS time (CPU kTics); y: events ",180);
00032
00033 int mxRunDration=200;
00034 mhRc= new L2Histo(905,"rate of COMPUTE; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
00035 mhRd= new L2Histo(906,"rate of DECISION; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
00036 mhRa= new L2Histo(907,"rate of ACCEPT; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
00037 printf("L2-%s instantiated, logPath='%s'\n",getName(),mOutDir1.c_str());
00038
00039
00040 assert(L2eventStream2008::mxToken == L2eventStream2008::tokenMask+1);
00041 }
00042
00043
00044
00045 int
00046 L2VirtualAlgo2008::initRun( int runNo, int *rc_ints, float *rc_floats) {
00047 if(mDb->getRun()!=runNo) return -700;
00048
00049 if(mRunNumber==runNo) {
00050 if (mLogFile) fprintf(mLogFile,"L2-%s::initRun(%d)=ghost already initilized, Abort run\n",getName(), runNo);
00051 return -701;
00052 }
00053
00054 unsigned int high,low;
00055 rdtsc_macro(low,high);
00056 mRunStartTicks=high; mRunStartTicks <<= 32; mRunStartTicks += low;
00057
00058 mRunNumber =runNo;
00059
00060 char Fname[1000];
00061 sprintf(Fname,"%s/run%d.l2%s.log",mOutDir1.c_str(),mRunNumber,getName());
00062 printf("L2-%s::initRun('%s') ...\n",getName(),Fname);
00063
00064 mLogFile = fopen(Fname,"w");
00065 if( mLogFile==0) printf(" L2-%s() UNABLE to open run summary log file, continue anyhow\n",getName());
00066
00067 int kBad=initRunUser( runNo, rc_ints, rc_floats);
00068
00069 if (mLogFile) {
00070 fprintf(mLogFile,"L2-%s initRun() params checked for consistency, Error flag=0x%04x\n",getName(),kBad);
00071 }
00072
00073 if(kBad) {
00074 if (mLogFile) {
00075 fprintf(mLogFile,"L2-%s initRun() ABORT due to internal logic\n",getName());
00076 fclose(mLogFile);
00077 }
00078 mRunNumber=-55;
00079 }
00080 if (mLogFile) {
00081 fprintf(mLogFile,"L2-%s initRun succesfull\n",getName());
00082
00083 #ifdef ADD_HARDCODED_DELAY
00084 fprintf(mLogFile,"WARN: HARDCODED_DELAY in compute() & decision() is ON\n");
00085 #endif
00086
00087 }
00088
00089
00090
00091 return kBad;
00092 }
00093
00094
00095
00096 void
00097 L2VirtualAlgo2008::finishRun() {
00098
00099 if(mRunNumber<0) return;
00100 if(mLogFile)fprintf(mLogFile,"L2-%s: finishRun(%d) called after %d seconds\n",getName(),mRunNumber ,mSecondsInRun);
00101
00102
00103
00104 char Fname[1000];
00105 sprintf(Fname,"%s/run%d.l2%s.hist.bin",mOutDir1.c_str(),mRunNumber,getName());
00106 printf("\nL2:%s::finishRun('%s') , save histo ...\n",getName(),Fname);
00107 mHistFile = fopen(Fname,"w");
00108
00109
00110 if( mHistFile==0) {
00111 printf(" L2-%s: finishRun() UNABLE to open run summary log file, continue anyhow\n",getName());
00112 if (mLogFile)
00113 fprintf(mLogFile,"L2-%s histos NOT saved, I/O error\n",getName());
00114 } else {
00115
00116 finishRunUser();
00117 int nh=finishCommonHistos();
00118
00119 if (mLogFile)
00120 fprintf(mLogFile,"L2-%s: %d histos saved to '%s'\n",getName(),nh,Fname);
00121 }
00122
00123 mRunNumber=-2;
00124
00125
00126 if (mLogFile) {
00127 fclose(mLogFile);
00128 mLogFile=0;
00129 }
00130
00131 if ( mHistFile) {
00132 fclose(mHistFile);
00133 mHistFile=0;
00134 }
00135
00136 }
00137
00138
00139
00140 int
00141 L2VirtualAlgo2008::finishCommonHistos() {
00142 int j;
00143 int nh=0;
00144 for(j=0;j<mxHA;j++) {
00145 if(hA[j]==0) continue;
00146 hA[j]->write(mHistFile);
00147 nh++;
00148 }
00149
00150 const int nHt=3;
00151 L2Histo *hT[nHt]={mhTc,mhTd,mhTcd};
00152 const char *text[nHt]={"Compute ","Decision ","Deci+Comp"};
00153 int ih;
00154 for(ih=0;ih<nHt;ih++) {
00155 int iMax=-3, iFWHM=-4;
00156 hT[ih]->findMax( &iMax, &iFWHM);
00157 printf("L2-%s %s CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",getName(),text[ih],iMax, iFWHM,mEventsInRun);
00158
00159 if (mLogFile){
00160 fprintf(mLogFile,"L2:%s %s CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",getName(),text[ih],iMax, iFWHM,mEventsInRun);
00161
00162 }
00163 if (mHistFile) {hT[ih]->write(mHistFile); nh++;}
00164 }
00165
00166 if (mHistFile) {
00167 mhN->write(mHistFile); nh++;
00168 mhRc->write(mHistFile);nh++;
00169 mhRd->write(mHistFile);nh++;
00170 mhRa->write(mHistFile);nh++;
00171 }
00172 return nh;
00173 }
00174
00175
00176
00177 L2VirtualAlgo2008::~L2VirtualAlgo2008(){};
00178
00179
00180 int
00181 L2VirtualAlgo2008::readParams(const char *fileN, int mxPar, int *iPar, float *fPar) {
00182
00183
00184
00185
00186
00187 memset(iPar,0,mxPar*sizeof(int));
00188 memset(fPar,0,mxPar*sizeof(int));
00189 FILE *fd=fopen(fileN,"r");
00190 if(fd==0) { printf(" L2VirtualAlgo2008::readParams failed to open =%s=\n",fileN); return -2222;}
00191
00192 int nVal=0;
00193 int nInt=0, nFloat=0;
00194
00195 const int mx=1000;
00196 char buf[mx];
00197 int mode=0;
00198
00199 for(;;) {
00200 char * ret=fgets(buf,mx,fd);
00201
00202 if(ret==0) break;
00203 if(buf[0]==0) continue;
00204 if(buf[0]=='#') continue;
00205 if(buf[0]=='\n') continue;
00206
00207 if (mode==0 && strstr(buf,"INTS")) { mode=1; continue; }
00208 if (mode==1 && strstr(buf,"FLOATS")) { mode=2; continue; }
00209
00210 if(mode==1) {
00211 if(nInt>=mxPar) {nVal=-501; break;}
00212 int ret1=sscanf(buf,"%i",iPar+nInt);
00213 if(ret1!=1) {nVal=-100*mode -nInt; break;}
00214 nInt++; nVal++;
00215 }
00216 else if(mode==2) {
00217 if(nFloat>=mxPar) {nVal=-502; break;}
00218 int ret1=sscanf(buf,"%f",fPar+nFloat);
00219 if(ret1!=1) {nVal= -100*mode -nFloat; break;}
00220 nFloat++; nVal++;
00221 }
00222
00223 }
00224
00225 fclose(fd);
00226 printf(" L2VirtualAlgo2008::readParams %d from '%s'\n",nVal,fileN);
00227 return nVal;
00228 }
00229
00230
00231
00232 void
00233 L2VirtualAlgo2008::compute(int token){
00234
00235 computeStart();
00236 mhN->fill(1);
00237 token&=L2eventStream2008::tokenMask;
00238
00239 #ifdef ADD_HARDCODED_DELAY
00240
00241
00242
00243 for(int i=0;i<3*100;i++) { float x=i*i; x=x;}
00244 #endif
00245
00246 computeUser( token );
00247 computeStop( token);
00248
00249 }
00250
00251
00252 void
00253 L2VirtualAlgo2008::computeStart(){
00254
00255
00256 unsigned int high,low;
00257 rdtsc_macro(low,high);
00258
00259 mComputeTimeStart=low;
00260 unsigned long long ticks = high;
00261 ticks <<= 32;
00262 ticks += low;
00263
00264
00265 mSecondsInRun=(ticks- mRunStartTicks)/par_cpuTicksPerSecond;
00266
00267 mhRc->fill(mSecondsInRun);
00268
00269 mAccept=true;
00270 mEventsInRun++;
00271
00272 mhN->fill(0);
00273 }
00274
00275
00276 void
00277 L2VirtualAlgo2008::computeStop(int token){
00278
00279 rdtscl_macro(mComputeTimeStop);
00280 unsigned long xxx=mComputeTimeStop-mComputeTimeStart;
00281 mComputeTimeDiff[token]=xxx;
00282 int kTick=xxx/1000;
00283
00284 mhTc->fill(kTick);
00285 }
00286
00287
00288
00289 bool
00290 L2VirtualAlgo2008::decision(int token, void **myL2Result){
00291
00292
00293
00294
00295
00296
00297 rdtscl_macro(mDecisionTimeStart);
00298 token&=L2eventStream2008::tokenMask;
00299 mDecisionTimeDiff=0;
00300
00301 mhRd->fill(mSecondsInRun);
00302
00303 #ifdef ADD_HARDCODED_DELAY
00304
00305
00306
00307 for(int i=0;i<3*100;i++) { float x=i*i; x=x;}
00308 #endif
00309
00310 mhN->fill(2);
00311 mAccept=decisionUser(token, myL2Result);
00312
00313
00314 if(mAccept) {
00315 mhN->fill(10);
00316 mhRa->fill(mSecondsInRun);
00317 }
00318 rdtscl_macro(mDecisionTimeStop);
00319 mDecisionTimeDiff=mDecisionTimeStop-mDecisionTimeStart;
00320 int kTick=mDecisionTimeDiff/1000;
00321
00322
00323 mhTd->fill(kTick);
00324 kTick=(mDecisionTimeDiff+mComputeTimeDiff[token])/1000;
00325 mhTcd->fill(kTick);
00326 return mAccept;
00327 }
00328
00329
00330
00331
00332
00333 void
00334 L2VirtualAlgo2008::printCalibratedData(int token){
00335
00336 int i;
00337 const int hitSize=mEveStream_btow[token].get_hitSize();
00338 printf("printCalibratedData-%s: ---BTOW ADC list--- size=%d\n",getName(),hitSize);
00339 const HitTower1 *hit=mEveStream_btow[token].get_hits();
00340 for(i=0;i< hitSize;i++,hit++) {
00341 int adc=hit->adc;
00342 int rdo=hit->rdo;
00343 float et=hit->et;
00344 float ene=hit->ene;
00345 printf(" btow: i=%2d rdo=%4d adc=%d et=%.3f ene=%.3f\n",i,rdo,adc,et,ene);
00346 }
00347 }
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372