00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include <time.h>
00005 #include <math.h>
00006 #include <fakeRtsLog.h>
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef IS_REAL_L2 //in l2-ana environment
00018 #include "../L2algoUtil/L2EmcDb2012.h"
00019 #include "../L2algoUtil/L2Histo.h"
00020 #else //full path needed for cvs'd code
00021 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb2012.h"
00022 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
00023 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcGeom2012.h"
00024 #endif
00025
00026 #include "L2bemcGamma2012.h"
00027
00028
00029
00030
00031 L2bemcGamma2012::L2bemcGamma2012(const char* name, const char *uid, L2EmcDb2012* db, L2EmcGeom2012 *geoX, char* outDir, int resOff) : L2VirtualAlgo2012( name, uid, db, outDir, true, false, resOff) {
00032
00033
00034
00035
00036 mGeom=geoX;
00037 if (!mGeom)
00038 criticalError("L2bemcGamma is broken -- can't find geom.");
00039
00040 setMaxHist(16);
00041 createHisto();
00042
00043
00044 if (sizeof(L2gammaResult2012)!= L2gammaResult2012::mySizeChar)
00045 criticalError("L2bemcGamma has failed consistency check. sizeof(L2gammaResult2012)!= L2gammaResult2012::mySizeChar");
00046
00047 }
00048
00049
00050
00051 int
00052 L2bemcGamma2012::initRunUser( int runNo, int *rc_ints, float *rc_floats) {
00053
00054
00055 par_dbg = rc_ints[0];
00056 par_RndAcceptPrescale = rc_ints[1];
00057 par_seedEtThres = rc_floats[0];
00058 par_clusterEtThres = rc_floats[1];
00059
00060
00061 int kBad=0;
00062 kBad+=0x00001 * (par_seedEtThres<1.0);
00063 kBad+=0x00002 * (par_clusterEtThres<par_seedEtThres);
00064
00065
00066 if (mLogFile) {
00067 fprintf(mLogFile,"L2%s algorithm initRun(R=%d), compiled: %s , %s\n params:\n",
00068 getName(),mRunNumber,__DATE__,__TIME__);
00069 fprintf(mLogFile," - use seedThres=%.2f (GeV), debug=%d, prescale=%d (0=off,1=100proc, 2=50proc, etc)\n",
00070 par_seedEtThres,par_dbg,par_RndAcceptPrescale);
00071 fprintf(mLogFile," - accept event cluster Thres=%.2f (GeV)\n",
00072 par_clusterEtThres);
00073 fprintf(mLogFile,"initRun() params checked for consistency, Error flag=0x%04x\n",
00074 kBad);
00075 }
00076
00077
00078 int i;
00079 for (i=0; i<mxHA;i++) if(hA[i])hA[i]->reset();
00080 memset(mBtow,0,sizeof(mBtow));
00081
00082 if(kBad>0) return -1*kBad;
00083 else if(kBad<0) return kBad;
00084
00085
00086
00087 char txt[1000];
00088
00089 sprintf(txt,"BTOW-decision: acc seed Tw ET>%.2f GeV; x:tower internal ID; y: counts",par_clusterEtThres);
00090 hA[7]->setTitle(txt);
00091 sprintf(txt,"#BTOW decision acc seed Tw, Et>%.2f GeV; eta bin [-1,+1]; y: phi bin ~ TCP sector",par_clusterEtThres);
00092 hA[14]->setTitle(txt);
00093
00094
00095 for ( int index=0; index<EmcDbIndexMax; index++ )
00096 {
00097 const L2EmcDb2012::EmcCDbItem *x = mDb->getByIndex(index);
00098 if ( x==0 ) continue;
00099 if ( !mDb->isBTOW(x) ) continue;
00100 int sec = x->sec - 1;
00101 int sub = 8192;
00102 sub = x->sub - 'a';
00103 int eta = x->eta - 1;
00104 int phi = BtowGeom::mxSubs *sec + sub;
00105 int tow = BtowGeom::mxEtaBin *phi + eta;
00106 int rdo = x->rdo;
00107 if (tow<0 || tow>mxBtow || rdo<0 || rdo>mxBtow) return -101;
00108
00109 mTower2rdo[ tow ] = rdo;
00110 mRdo2tower[ rdo ] = tow;
00111 }
00112 return 0;
00113
00114 }
00115
00116
00117
00118
00119
00120 float
00121 L2bemcGamma2012::sumET(int phi, int eta) {
00122 int tow = BtowGeom::mxEtaBin *((phi+BtowGeom::mxPhiBin)%BtowGeom::mxPhiBin) + ((eta+BtowGeom::mxEtaBin)%BtowGeom::mxEtaBin);
00123
00124 const int maxTowers = BtowGeom::mxEtaBin * BtowGeom::mxPhiBin;
00125 int towPlusOne;
00126 float sum;
00127 sum=0;
00128 sum=wrkBtow_et[tow];
00129 towPlusOne = tow+1;
00130 towPlusOne%= maxTowers;
00131 sum+=wrkBtow_et[towPlusOne];
00132
00133 tow+=BtowGeom::mxEtaBin;
00134 tow%=maxTowers;
00135
00136 sum+=wrkBtow_et[tow];
00137
00138 towPlusOne = tow+1;
00139 towPlusOne%= maxTowers;
00140 sum+=wrkBtow_et[towPlusOne];
00141
00142 return sum;
00143 }
00144
00145
00146
00147
00148 void
00149 L2bemcGamma2012::computeUser(int token){
00150
00151
00152
00153
00154
00155
00156 clearEvent(token);
00157
00158
00159 int i;
00160
00161 L2bemcGammaEvent2012 *btowEve=mBtow+token;
00162
00163 LOG(DBG, "token = %d mBtow = 0x%x btowEve=0x%x",token, mBtow, btowEve);
00164
00165 const HitTower1 *hit=mEveStream_btow[token].get_hits();
00166 const int hitSize=mEveStream_btow[token].get_hitSize();
00167
00168 LOG(DBG, "Hits = %d",hitSize);
00169
00170 for(i=0;i< hitSize;i++,hit++) {
00171 int tower=mRdo2tower[hit->rdo];
00172 wrkBtow_et[tower]=hit->et;
00173 if(hit->et<par_seedEtThres)continue;
00174 wrkBtow_tower_seed[wrkBtow_tower_seed_size++]=tower;
00175 }
00176 hA[2]->fill(hitSize);
00177 int seedTow=-1,seedEta=-1,seedPhi=-1;
00178 float clustET=0;
00179 btowEve->isFresh=L2bemcGammaEvent2012::kDataFresh;
00180
00181 LOG(DBG, "seed size = %d",wrkBtow_tower_seed_size);
00182
00183 for(i=0; i<wrkBtow_tower_seed_size;i++) {
00184 seedTow=wrkBtow_tower_seed[i];
00185 seedEta=seedTow%BtowGeom::mxEtaBin;
00186 seedPhi=seedTow/BtowGeom::mxEtaBin;
00187
00188
00189 if (seedEta < BtowGeom::mxEtaBin) {
00190 clustET = sumET(seedPhi,seedEta);
00191 LOG(DBG, "clustET1=%f, thresh=%f",clustET, par_clusterEtThres);
00192
00193 if(clustET>par_clusterEtThres) goto ACCEPT;
00194 clustET = sumET(seedPhi-1,seedEta);
00195
00196 LOG(DBG, "clustET2=%f, thresh=%f",clustET, par_clusterEtThres);
00197
00198 if(clustET>par_clusterEtThres) goto ACCEPT;
00199 }
00200 if (seedEta > 0 ) {
00201 clustET = sumET(seedPhi-1,seedEta-1);
00202
00203 LOG(DBG, "clustET3=%f, thresh=%f",clustET, par_clusterEtThres);
00204
00205 if(clustET>par_clusterEtThres) goto ACCEPT;
00206 clustET = sumET(seedPhi,seedEta-1);
00207
00208 LOG(DBG, "clustET4=%f, thresh=%f",clustET, par_clusterEtThres);
00209
00210 if(clustET>par_clusterEtThres) goto ACCEPT;
00211 }
00212 }
00213
00214 btowEve->resultBlob.clusterEt=0;
00215 btowEve->resultBlob.meanEtaBin=0;
00216 btowEve->resultBlob.meanPhiBin=0;
00217 btowEve->resultBlob.trigger=0;
00218 btowEve->seedTwID=-1;
00219 btowEve->clusterET=0;
00220 btowEve->seedET=0;
00221 return;
00222
00223 ACCEPT:
00224 btowEve->clusterET=clustET;
00225 btowEve->seedET=wrkBtow_et[seedTow];
00226 btowEve->resultBlob.clusterEt=(unsigned char)(clustET*256.0/60.0);
00227 btowEve->resultBlob.meanEtaBin=seedEta;
00228 btowEve->resultBlob.meanPhiBin=seedPhi;
00229 btowEve->seedTwID=seedTow;
00230 btowEve->resultBlob.trigger=2;
00231
00232 LOG(DBG, "trigger = %d",btowEve->resultBlob.trigger);
00233 return;
00234 }
00235
00236
00237
00238
00239
00240
00241 bool
00242 L2bemcGamma2012::decisionUser(int token, int *myL2Result){
00243
00244
00245
00246
00247
00248 L2bemcGammaEvent2012 *btowEve=mBtow+token;
00249 LOG(DBG, "token = %d mBtow = 0x%x btowEve=0x%x",token, mBtow, btowEve);
00250
00251 LOG(DBG, "trigger = %d",btowEve->resultBlob.trigger);
00252
00253 bool triggerDecision=(btowEve->resultBlob.trigger>0);
00254
00255
00256 if (par_RndAcceptPrescale>0) btowEve->resultBlob.trigger+=mRandomAccept;
00257
00258
00259 if(btowEve->isFresh>L2bemcGammaEvent2012::kDataFresh) mhN->fill(6);
00260
00261 btowEve->isFresh++;
00262
00263
00264
00265
00266 if(btowEve->resultBlob.trigger&2) {
00267 mhN->fill(15);
00268 hA[6]->fill((int)btowEve->clusterET);
00269 hA[7]->fill(btowEve->seedTwID);
00270 hA[11]->fill((int)btowEve->seedET);
00271 hA[12]->fill((int)(100.*btowEve->seedET/btowEve->clusterET));
00272 }
00273
00274 if(btowEve->resultBlob.trigger&1) mhN->fill(16);
00275
00276 memcpy(myL2Result,&(btowEve->resultBlob),sizeof(L2gammaResult2012));
00277
00278 return triggerDecision;
00279 }
00280
00281
00282
00283
00284 void
00285 L2bemcGamma2012::finishRunUser() {
00286
00287
00288 if (mLogFile){
00289 fprintf(mLogFile,"finishRunUser-%s bhla bhla\n",getName());
00290 }
00291
00292 const int *hist15Data = hA[7]->getData();
00293 for (int i = 0; i < BtowGeom::mxEtaBin; i++) {
00294 for (int j = 0; j < BtowGeom::mxPhiBin; j++) {
00295 hA[14]->fillW(i,j, hist15Data[i+j*BtowGeom::mxEtaBin]);
00296 }
00297 }
00298
00299 }
00300
00301
00302
00303
00304 void
00305 L2bemcGamma2012::createHisto() {
00306 setMaxHist(16);
00307
00308
00309 hA[2]=new L2Histo(2,"BTOW-compute: #towers w/ energy /event; x: # BTOW towers; y: counts", 100);
00310 hA[6]=new L2Histo(6,"BTOW-decision: accepted clust ... ; x: ET(GeV)", 30);
00311
00312 hA[7]=new L2Histo(7,"BTOW: accepted Seed Tower .....", 5000);
00313 hA[11]=new L2Histo(11,"BTOW: decision Cluster Seed Et; ET GeV", 30);
00314 hA[12]=new L2Histo(12,"BTOW: decision ;100*Seed Et/Cluster Et", 100);
00315
00316 hA[14]=new L2Histo(14,"BTOW: hot tower projection", BtowGeom::mxEtaBin, BtowGeom::mxPhiBin);
00317
00318 }
00319
00320
00321
00322 void
00323 L2bemcGamma2012::clearEvent(int token){
00324 memset(wrkBtow_et,0,sizeof(wrkBtow_et));
00325 wrkBtow_tower_seed_size=0;
00326 memset(&(mBtow[token].resultBlob),0, sizeof(L2gammaResult2012));
00327 }
00328
00329
00330
00331 void
00332 L2bemcGamma2012::print2(){
00333 int i;
00334 printf("pr2-%s: ---BTOW ADC 2D array, only non-zero\n",getName());
00335
00336 for(i=0;i<mxBtow;i++) {
00337 if(wrkBtow_et[i]<=0) continue;
00338 int rdo=mTower2rdo[i];
00339 float et=wrkBtow_et[i];
00340 printf(" btow: tower=%4d rdo=%4d et=%.3f \n",i,rdo,et);
00341 }
00342
00343 }
00344
00345
00346
00347 void
00348 L2bemcGamma2012::print3(){
00349 int i;
00350 printf("pr3-%s: ---seed list, size=%d\n",getName(),wrkBtow_tower_seed_size);
00351
00352 for(i=0;i<wrkBtow_tower_seed_size;i++) {
00353 int tower=wrkBtow_tower_seed[i];
00354 float et=wrkBtow_et[tower];
00355 printf(" btow: i=%4d tower=%4d et=%.3f \n",i,tower,et);
00356 }
00357
00358 }
00359
00360 #if 0
00361
00362
00363
00364 void
00365 L2bemcGamma2012::print4(int token, int hitSize){
00366 int i;
00367 printf("print4 IS NOT Fully FUNCTIONAL **********************\n");
00368 printf("pr1-%s: ---BTOW Sorted ADC list--- size=%d\n",getName(),hitSize);
00369
00370 for(i=0;i< hitSize;i++) {
00371 int adc=0;
00372 int rdo=0;
00373 float et=wrkBtow_et[wrkBtow_tower_index[i]];
00374 float ene=0;
00375 printf(" tower=%2d ",wrkBtow_tower_index[i]);
00376 printf(" btow: i=%2d rdo=%4d adc=%d et=%.3f ene=%.3f\n",i,rdo,adc,et,ene);
00377 }
00378 }
00379
00380 #endif
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423