00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include <time.h>
00005 #include <math.h>
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifdef IS_REAL_L2 //in l2-ana environment
00017 #include "../L2algoUtil/L2EmcDb.h"
00018 #include "../L2algoUtil/L2Histo.h"
00019 #else //full path needed for cvs'd code
00020 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb.h"
00021 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
00022 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcGeom.h"
00023 #endif
00024
00025 #include "L2eemcGamma2009.h"
00026
00027
00028
00029
00030 void L2eemcGamma2009::quickSort(int array[], int start, int end)
00031 {
00032 int i = start;
00033 int k = end;
00034
00035 if (end - start >= 1)
00036 {
00037 float pivot = wrkEtow_et[array[start]];
00038
00039 while (k > i)
00040 {
00041 while (wrkEtow_et[array[i]] <= pivot && i <= end && k > i)
00042 i++;
00043 while (wrkEtow_et[array[k]] > pivot && k >= start && k >= i)
00044 k--;
00045 if (k > i)
00046 swap(array, i, k);
00047 }
00048 swap(array, start, k);
00049
00050 quickSort(array, start, k - 1);
00051 quickSort(array, k + 1, end);
00052 }
00053 else
00054 {
00055 return;
00056 }
00057 }
00058
00059 void L2eemcGamma2009::swap(int array[], int index1, int index2)
00060
00061
00062 {
00063 int temp = array[index1];
00064 array[index1] = array[index2];
00065 array[index2] = temp;
00066 }
00067
00068
00069
00070
00071
00072 L2eemcGamma2009::L2eemcGamma2009(const char* name, L2EmcDb* db, L2EmcGeom *geoX, char* outDir, int resOff) : L2VirtualAlgo2009( name, db, outDir, false,true, resOff) {
00073
00074
00075
00076 mGeom=geoX;
00077 if (!mGeom)
00078 criticalError("L2eemcGamma is broken -- can't find geom.");
00079
00080 setMaxHist(16);
00081 createHisto();
00082
00083
00084 if (sizeof(L2gammaResult2009)!= L2gammaResult2009::mySizeChar)
00085
00086 criticalError("L2eemcGamma has failed consistency check. sizeof(L2gammaResult2009)!= L2gammaResult2009::mySizeChar");
00087
00088
00089
00090 }
00091
00092
00093
00094 int
00095 L2eemcGamma2009::initRunUser( int runNo, int *rc_ints, float *rc_floats) {
00096
00097
00098 par_dbg = rc_ints[0];
00099 par_RndAcceptPrescale = rc_ints[1];
00100 par_seedEtThres = rc_floats[0];
00101 par_clusterEtThres= rc_floats[1];
00102 par_eventEtThres = rc_floats[2];
00103
00104
00105 int kBad=0;
00106 kBad+=0x00001 * (par_seedEtThres<1.0);
00107 kBad+=0x00002 * (par_clusterEtThres<par_seedEtThres);
00108
00109 if (mLogFile) {
00110 fprintf(mLogFile,"L2%s algorithm initRun(R=%d), compiled: %s , %s\n params:\n",getName(),mRunNumber,__DATE__,__TIME__);
00111 fprintf(mLogFile," - use Thres/GeV seed=%.2f, clusterList=%.2f debug=%d, prescale=%d\n", par_seedEtThres,par_clusterEtThres, par_dbg,par_RndAcceptPrescale);
00112
00113 fprintf(mLogFile," - accept event cluster Thres/GeV=%.2f\n",par_eventEtThres);
00114 fprintf(mLogFile,"initRun() params checked for consistency, Error flag=0x%04x\n",kBad);
00115 }
00116
00117
00118
00119 int i;
00120 for (i=0; i<mxHA;i++) if(hA[i])hA[i]->reset();
00121
00122 if(kBad) return -1*kBad;
00123
00124 memset(mEtow,0,sizeof(mEtow));
00125
00126
00127 char txt[1000];
00128 sprintf(txt,"ETOW-compute: #seed towers ET>%.2f GeV / event; x: # ETOW towers; y: counts",par_seedEtThres);
00129 hA[3]->setTitle(txt);
00130
00131 sprintf(txt,"ETOW-decision: #cluster ET>%.2f GeV / event ; x: # ETOW towers; y: counts",par_clusterEtThres);
00132 hA[4]->setTitle(txt);
00133
00134 sprintf(txt,"ETOW-decision: acc cluster ET>%.2f GeV; x:cluster ET(GeV) ; y: counts",par_eventEtThres);
00135 hA[4]->setTitle(txt);
00136
00137
00138
00139 for ( int index=0; index<EmcDbIndexMax; index++ )
00140 {
00141 const L2EmcDb::EmcCDbItem *x = mDb->getByIndex(index);
00142 if ( x==0 ) continue;
00143 if ( !mDb->isETOW(x) ) continue;
00144 int sec = x->sec - 1;
00145 int sub = 8192;
00146 sub = x->sub - 'A';
00147 int eta = x->eta - 1;
00148 int phi = EtowGeom::mxSubs *sec + sub;
00149 int tow = EtowGeom::mxEtaBin *phi + eta;
00150 int rdo = x->rdo;
00151 if (tow<0 || tow>mxEtow || rdo<0 || rdo>mxEtow) return -101;
00152
00153 mTower2rdo[ tow ] = rdo;
00154 mRdo2tower[ rdo ] = tow;
00155 }
00156 return 0;
00157
00158 }
00159
00160
00161
00162 int
00163 L2eemcGamma2009::countNonZeroTow(int phi, int eta) {
00164 int tow = EtowGeom::mxEtaBin *((phi+EtowGeom::mxPhiBin)%EtowGeom::mxPhiBin) + ((eta+EtowGeom::mxEtaBin)%EtowGeom::mxEtaBin);
00165 const int maxTowers = EtowGeom::mxEtaBin * EtowGeom::mxPhiBin;
00166 int towPlusOne;
00167 int count=0;
00168
00169 if (etow_used[tow]==0) {
00170 if(wrkEtow_et[tow]!=0) count++;
00171 }
00172
00173 towPlusOne = tow+1;
00174 towPlusOne%= maxTowers;
00175 if (etow_used[towPlusOne]==0) {
00176 if(wrkEtow_et[towPlusOne]!=0) count++;
00177 }
00178
00179 tow+=EtowGeom::mxEtaBin;
00180 tow%=maxTowers;
00181 if (etow_used[tow]==0) {
00182 if(wrkEtow_et[tow]!=0) count++;
00183 }
00184
00185 towPlusOne = tow+1;
00186 towPlusOne%= maxTowers;
00187 if (etow_used[towPlusOne]==0) {
00188 if(wrkEtow_et[towPlusOne]!=0) count++;
00189 }
00190 return count;
00191 }
00192
00193
00194
00195
00196 void
00197 L2eemcGamma2009::flagUsed(int phi, int eta) {
00198 int tow = EtowGeom::mxEtaBin *((phi+EtowGeom::mxPhiBin)%EtowGeom::mxPhiBin) + ((eta+EtowGeom::mxEtaBin)%EtowGeom::mxEtaBin);
00199 const int maxTowers = EtowGeom::mxEtaBin * EtowGeom::mxPhiBin;
00200 int towPlusOne;
00201 etow_used[tow] = 1;
00202
00203 towPlusOne = tow+1;
00204 towPlusOne%= maxTowers;
00205 etow_used[towPlusOne] = 1;
00206
00207 tow+=EtowGeom::mxEtaBin;
00208 tow%=maxTowers;
00209 etow_used[tow] = 1;
00210
00211 towPlusOne = tow+1;
00212 towPlusOne%= maxTowers;
00213 etow_used[towPlusOne] = 1;
00214 }
00215
00216
00217
00218 void
00219 L2eemcGamma2009::averageEtaPhi(int phi, int eta, float *avePhi, float *aveEta) {
00220 int tow = EtowGeom::mxEtaBin *((phi+EtowGeom::mxPhiBin)%EtowGeom::mxPhiBin) + ((eta+EtowGeom::mxEtaBin)%EtowGeom::mxEtaBin);
00221 const int maxTowers = EtowGeom::mxEtaBin * EtowGeom::mxPhiBin;
00222 int towPlusOne;
00223 float ETsum;
00224 float etaSum;
00225 float phiSum;
00226
00227 ETsum=wrkEtow_et[tow];
00228 etaSum = wrkEtow_et[tow]*(float)eta;
00229 phiSum = wrkEtow_et[tow]*(float)phi;
00230
00231 towPlusOne = tow+1;
00232 towPlusOne%= maxTowers;
00233 ETsum+=wrkEtow_et[towPlusOne];
00234 etaSum += wrkEtow_et[towPlusOne]*(float)(eta+1);
00235 phiSum += wrkEtow_et[towPlusOne]*(float)phi;
00236
00237 tow+=EtowGeom::mxEtaBin;
00238 tow%=maxTowers;
00239 ETsum+=wrkEtow_et[tow];
00240 etaSum += wrkEtow_et[tow]*(float)eta;
00241 phiSum += wrkEtow_et[tow]*(float)(phi+1);
00242
00243 towPlusOne = tow+1;
00244 towPlusOne%= maxTowers;
00245 ETsum+=wrkEtow_et[towPlusOne];
00246 etaSum += wrkEtow_et[towPlusOne]*(float)(eta+1);
00247 phiSum += wrkEtow_et[towPlusOne]*(float)(phi+1);
00248
00249
00250 *aveEta = etaSum/ETsum;
00251 *avePhi = phiSum/ETsum;
00252
00253 }
00254
00255
00256
00257
00258 float
00259 L2eemcGamma2009::sumET(int phi, int eta) {
00260 int tow = EtowGeom::mxEtaBin *((phi+EtowGeom::mxPhiBin)%EtowGeom::mxPhiBin) + ((eta+EtowGeom::mxEtaBin)%EtowGeom::mxEtaBin);
00261
00262 const int maxTowers = EtowGeom::mxEtaBin * EtowGeom::mxPhiBin;
00263 int towPlusOne;
00264 float sum;
00265 sum=0;
00266 if (etow_used[tow]==0) {
00267 sum=wrkEtow_et[tow];
00268 }
00269 towPlusOne = tow+1;
00270 towPlusOne%= maxTowers;
00271 if (etow_used[towPlusOne]==0) {
00272 sum+=wrkEtow_et[towPlusOne];
00273 }
00274
00275
00276
00277 tow+=EtowGeom::mxEtaBin;
00278 tow%=maxTowers;
00279 if (etow_used[tow]==0) {
00280 sum+=wrkEtow_et[tow];
00281 }
00282 towPlusOne = tow+1;
00283 towPlusOne%= maxTowers;
00284 if (etow_used[towPlusOne]==0) {
00285 sum+=wrkEtow_et[towPlusOne];
00286 }
00287
00288
00289
00290 return sum;
00291 }
00292
00293
00294
00295
00296 void
00297 L2eemcGamma2009::computeUser(int token){
00298
00299
00300
00301
00302
00303
00304
00305 clearEvent(token);
00306
00307
00308 int i;
00309
00310
00311 L2eemcGammaEvent2009 *etowEve=mEtow+token;
00312 const HitTower1 *hit=mEveStream_etow[token].get_hits();
00313 const int hitSize=mEveStream_etow[token].get_hitSize();
00314 for(i=0;i< hitSize;i++,hit++) {
00315
00316 int tower=mRdo2tower[hit->rdo];
00317 wrkEtow_tower_index[i]=tower;
00318 wrkEtow_et[tower]=hit->et;
00319
00320
00321 }
00322 hA[2]->fill(hitSize);
00323
00324
00325 quickSort(wrkEtow_tower_index,0,hitSize-1);
00326
00327
00328
00329 for(i=hitSize-1;i >= 0; i--) {
00330 if(wrkEtow_et[wrkEtow_tower_index[i]] < par_seedEtThres) {
00331 i = -1;
00332 continue;
00333 }
00334 int seedTow=wrkEtow_tower_index[i];
00335 int seedEta=seedTow%EtowGeom::mxEtaBin;
00336 int seedPhi=seedTow/EtowGeom::mxEtaBin;
00337
00338
00340
00341
00342
00343
00344
00345 float maxET;
00346 float sum;
00347
00348 if (etow_used[seedTow]!=0) continue;
00349 wrkEtow_tower_seed_size++;
00350
00351 int high_quadrant;
00352 high_quadrant = 0;
00353 maxET = 0;
00354 if(seedEta < EtowGeom::mxEtaBin) {
00355 maxET = sumET(seedPhi,seedEta);
00356 sum=sumET(seedPhi-1,seedEta);
00357
00358 if(maxET<sum) {
00359 maxET=sum;
00360 high_quadrant = 1;
00361 }
00362 }
00363 if (seedEta > 0) {
00364 sum=sumET(seedPhi-1,seedEta-1);
00365
00366 if(maxET<sum) {
00367 maxET=sum;
00368 high_quadrant = 2;
00369 }
00370 sum=sumET(seedPhi,seedEta-1);
00371
00372 if(maxET<sum) {
00373 maxET=sum;
00374 high_quadrant = 3;
00375 }
00376 }
00377
00378
00379 if(maxET<par_clusterEtThres)continue;
00380
00381
00382 hA[6]->fill((int)(maxET));
00383 hA[7]->fill(seedTow);
00384 hA[8]->fill(hitSize-i);
00385
00386
00387 hA[11]->fill((int)wrkEtow_et[seedTow]);
00388 hA[12]->fill((int)(10.*wrkEtow_et[seedTow]/maxET));
00389
00390 if(etowEve->size>=L2eemcGammaEvent2009::mxClust) continue;
00391 etowEve->clusterET[etowEve->size]=maxET;
00392 etowEve->clusterQuad[etowEve->size]=high_quadrant;
00393 etowEve->clusterSeedTow[etowEve->size]=seedTow;
00394
00395
00396
00397
00398
00399 int numNonZeroTow=0;
00400 float clusterEta;
00401 float clusterPhi;
00402 if (high_quadrant==0) {
00403 numNonZeroTow = countNonZeroTow(seedPhi, seedEta);
00404 flagUsed(seedPhi, seedEta);
00405 averageEtaPhi(seedPhi, seedEta,&clusterPhi, &clusterEta);
00406 } else if (high_quadrant==1) {
00407 numNonZeroTow = countNonZeroTow(seedPhi-1, seedEta);
00408 flagUsed(seedPhi-1, seedEta);
00409 averageEtaPhi(seedPhi-1, seedEta,&clusterPhi, &clusterEta);
00410 } else if (high_quadrant==2) {
00411 numNonZeroTow = countNonZeroTow(seedPhi-1, seedEta-1);
00412 flagUsed(seedPhi-1, seedEta-1);
00413 averageEtaPhi(seedPhi-1, seedEta-1,&clusterPhi, &clusterEta);
00414 } else if (high_quadrant==3) {
00415 numNonZeroTow = countNonZeroTow(seedPhi, seedEta-1);
00416 flagUsed(seedPhi, seedEta-1);
00417 averageEtaPhi(seedPhi, seedEta-1,&clusterPhi, &clusterEta);
00418 }
00419 hA[13]->fill(numNonZeroTow);
00420
00421 etowEve->clusterEta[etowEve->size]=clusterEta;
00422 etowEve->clusterPhi[etowEve->size]=clusterPhi;
00423
00424 etowEve->size++;
00425
00426 }
00427
00428 hA[3]->fill(wrkEtow_tower_seed_size);
00429
00430 hA[4]->fill(etowEve->size);
00431
00432
00433
00434
00435
00436
00437
00438 if(par_dbg>0){
00439 printf("dbg=%s, etow-adcL-size=%d\n",getName(),hitSize);
00440
00441 print2();
00442 print3();
00443 }
00444 }
00445
00446
00447
00448
00449 bool
00450 L2eemcGamma2009::decisionUser(int token, int *myL2Result){
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 L2eemcGammaEvent2009 *etowEve=mEtow+token;
00463
00464
00465 int ic;
00466
00467
00468 if(etowEve->size>= L2eemcGammaEvent2009::mxClust) mhN->fill(5);
00469 if(etowEve->isFresh>L2eemcGammaEvent2009::kDataFresh) mhN->fill(6);
00470 etowEve->isFresh++;
00471
00472 hA[4]->fill(etowEve->size);
00473
00474 for(ic=0;ic<etowEve->size;ic++) {
00475 float clustET=etowEve->clusterET[ic];
00476 hA[5]->fill((int)clustET);
00477
00478 if(clustET<par_eventEtThres) continue;
00479 hA[6]->fill((int)clustET);
00480 }
00481
00482
00483
00484
00485
00486
00487
00488 float maxClusterEt;
00489 maxClusterEt=-1;
00490 int maxClusterID;
00491 maxClusterID =-1;
00492
00493
00494 for(ic=0;ic<etowEve->size;ic++) {
00495 if(etowEve->clusterET[ic]>maxClusterEt) {
00496 maxClusterEt = etowEve->clusterET[ic];
00497 maxClusterID = ic;
00498 }
00499 }
00500 if(maxClusterEt>par_eventEtThres) {
00501 etowEve->resultBlob.clusterEt=(unsigned char)(etowEve->clusterET[maxClusterID]*256.0/60.0);
00502 etowEve->resultBlob.TowerID=(unsigned short)etowEve->clusterSeedTow[maxClusterID]+(etowEve->clusterQuad[maxClusterID]<<13);
00503 etowEve->resultBlob.meanEtaBin=(unsigned char)((int)(etowEve->clusterEta[maxClusterID]*256.0/EtowGeom::mxEtaBin)%256);
00504 etowEve->resultBlob.meanPhiBin=(unsigned char)((int)(etowEve->clusterPhi[maxClusterID]*256.0/EtowGeom::mxPhiBin)%256);
00505 etowEve->resultBlob.isolationSum=(unsigned char)1;
00506 etowEve->resultBlob.Time=(unsigned char)(mComputeTimeDiff[token]/1000);
00507 etowEve->resultBlob.trigger=(unsigned char)5;
00508 if (mRandomAccept) {
00509 etowEve->resultBlob.trigger=(unsigned char)7;
00510 }
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520 hA[9]->fill(etowEve->clusterQuad[maxClusterID]);
00521 hA[10]->fill((int)etowEve->clusterET[maxClusterID]);
00522 if(etowEve->size>= L2eemcGammaEvent2009::mxClust) mhN->fill(15);
00523 mhN->fill(15);
00524 memcpy(myL2Result,&(etowEve->resultBlob),sizeof(L2gammaResult2009));
00525 return true;
00526 }
00527
00528
00529 if (mRandomAccept) {
00530 etowEve->resultBlob.trigger=(unsigned char)6;
00531 etowEve->resultBlob.Time=(unsigned char)(mComputeTimeDiff[token]/1000);
00532 mhN->fill(16);
00533 memcpy(myL2Result,&(etowEve->resultBlob),sizeof(L2gammaResult2009));
00534 return false;
00535 }
00536
00537 memcpy(myL2Result,&(etowEve->resultBlob),sizeof(L2gammaResult2009));
00538 return false;
00539
00540 }
00541
00542
00543
00544
00545 void
00546 L2eemcGamma2009::finishRunUser() {
00547
00548
00549 if (mLogFile){
00550 fprintf(mLogFile,"finishRunUser-%s bhla bhla\n",getName());
00551 }
00552
00553 }
00554
00555
00556
00557
00558 void
00559 L2eemcGamma2009::createHisto() {
00560 setMaxHist(15);
00561
00562
00563 hA[2]=new L2Histo(2,"ETOW-compute: #towers w/ energy /event; x: # ETOW towers; y: counts", 100);
00564 hA[3]=new L2Histo(3,"ETOW-compute: #seed ....... ", 100);
00565 hA[4]=new L2Histo(4,"ETOW-decision: #clust ....... ", 50);
00566
00567 hA[5]=new L2Histo(5,"ETOW-decision: any cluster ; x: ET(GeV)", 30);
00568 hA[6]=new L2Histo(6,"ETOW-decision: accepted clust ... ; x: ET(GeV)", 30);
00569
00570 hA[7]=new L2Histo(7,"ETOW: Seed Tower Number", 5000);
00571 hA[8]=new L2Histo(8,"ETOW: Seed Tower Rank", 20);
00572 hA[9]=new L2Histo(9,"ETOW: Seed Tower Quadrant", 5);
00573 hA[10]=new L2Histo(10,"ETOW: Cluster Et Sum GeV", 30);
00574 hA[11]=new L2Histo(11,"ETOW: Cluster Seed Et GeV", 30);
00575 hA[12]=new L2Histo(12,"ETOW: 10*Seed Et/Cluster Et GeV", 30);
00576 hA[13]=new L2Histo(13,"ETOW: # non-zero towers per cluster", 5);
00577
00578
00579 }
00580
00581
00582
00583 void
00584 L2eemcGamma2009::clearEvent(int token){
00585 memset(wrkEtow_et,0,sizeof(wrkEtow_et));
00586 memset(etow_used,0,sizeof(etow_used));
00587 memset(wrkEtow_tower_seed,0,sizeof(wrkEtow_tower_seed));
00588 wrkEtow_tower_seed_size=0;
00589
00590 mEtow[token].size=0;
00591
00592 mEtow[token].isFresh=L2eemcGammaEvent2009::kDataFresh;
00593
00594 memset(&(mEtow[token].resultBlob),0, sizeof(L2gammaResult2009));
00595 }
00596
00597
00598
00599 void
00600 L2eemcGamma2009::print2(){
00601 int i;
00602 printf("pr2-%s: ---ETOW ADC 2D array, only non-zero\n",getName());
00603
00604 for(i=0;i<mxEtow;i++) {
00605 if(wrkEtow_et[i]<=0) continue;
00606 int rdo=mTower2rdo[i];
00607 float et=wrkEtow_et[i];
00608 printf(" etow: tower=%4d rdo=%4d et=%.3f \n",i,rdo,et);
00609 }
00610
00611 }
00612
00613
00614
00615 void
00616 L2eemcGamma2009::print3(){
00617 int i;
00618 printf("pr3-%s: ---seed list, size=%d\n",getName(),wrkEtow_tower_seed_size);
00619
00620 for(i=0;i<wrkEtow_tower_seed_size;i++) {
00621 int tower=wrkEtow_tower_seed[i];
00622 float et=wrkEtow_et[tower];
00623 printf(" etow: i=%4d tower=%4d et=%.3f \n",i,tower,et);
00624 }
00625
00626 }
00627
00628
00629
00630 void
00631 L2eemcGamma2009::print4(int token, int hitSize){
00632 int i;
00633 printf("print4 IS NOT Fully FUNCTIONAL **********************\n");
00634 printf("pr1-%s: ---ETOW Sorted ADC list--- size=%d\n",getName(),hitSize);
00635
00636 for(i=0;i< hitSize;i++) {
00637 int adc=0;
00638 int rdo=0;
00639 float et=wrkEtow_et[wrkEtow_tower_index[i]];
00640 float ene=0;
00641 printf(" tower=%2d ",wrkEtow_tower_index[i]);
00642 printf(" etow: i=%2d rdo=%4d adc=%d et=%.3f ene=%.3f\n",i,rdo,adc,et,ene);
00643 }
00644 }
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673