00001 #include <stdio.h>
00002
00003
00004 #include <stdio.h>
00005 #include "L2gammaAlgo.h"
00006 #include "L2gammaResult2006.h"
00007
00008 #include "bemcConstants.h"
00009 #include "eemcConstants.h"
00010
00011 void L2gammaAlgo::setLogFile( const char *fname){ mLogFile=fopen(fname,"w"); }
00012 void L2gammaAlgo::setHistFile( const char *fname ){ mHistFile=fopen(fname,"w"); }
00013
00014
00015
00016
00017 L2gammaAlgo::L2gammaAlgo(const char* name, L2EmcDb* db, char* outDir, int resOff)
00018 : L2VirtualAlgo( name, db, outDir, resOff)
00019 {
00020
00021 int geom, thresh;
00022 #if 1
00023 if(strstr(name,"etow_gamma")) {
00024 geom= L2gammaAlgo::kEEmcAlgo; thresh = L2gammaAlgo::kThresh1;
00025 } else if(strstr(name,"etow_ht2")) {
00026 geom= L2gammaAlgo::kEEmcAlgo; thresh = L2gammaAlgo::kThresh2;
00027 } else if(strstr(name,"btow_gamma")) {
00028 geom= L2gammaAlgo::kBEmcAlgo; thresh = L2gammaAlgo::kThresh1;
00029 } else if(strstr(name,"btow_ht2")) {
00030 geom= L2gammaAlgo::kBEmcAlgo; thresh = L2gammaAlgo::kThresh2;
00031 } else {
00032 printf("undefined config of L2gamma-algo ->%s<-, abort L2main\n",name);
00033 assert(1==2);
00034 }
00035 #endif
00036
00037
00038 printf("L2gammaEmCall2006 instantiated geom=%d thresh=%d logpath=%s\n",geom,thresh,mOutDir);
00039 int I_par[]={
00040 1,
00041 0,
00042 0,
00043 0,
00044 0
00045 };
00046 float F_par[]={
00047 2.50,
00048 3.50,
00049 0.,
00050 0.,
00051 0.
00052 };
00053 mThresholdLevel=thresh;
00054 init(geom,I_par,F_par);
00055 mL2input=0;
00056 mPrescale=0;
00057
00058 }
00059
00060
00061
00062
00063
00064
00065
00066 void L2gammaAlgo::init(int geom, int I_par[5], float F_par[5] )
00067 {
00068
00069 printf("+ init geom=%d\n",geom);
00070
00071 mLogFile=0;
00072 mHistFile=0;
00073 mUseOfflineGains = I_par[0];
00074 mUseBbc=false;
00075 setTowerThreshold( F_par[0] );
00076 setPatchThreshold( F_par[1] );
00077
00078 for ( int i=0;i<5;i++ )
00079 {
00080 mDefaultI_par[i]=I_par[i];
00081 mDefaultF_par[i]=F_par[i];
00082 }
00083
00084 if ( I_par[1] ) mPrescale=I_par[1];
00085
00086
00087
00088
00089 mEEmc=geom;mBEmc=!geom;
00090
00091 mNumEtas = (mEEmc)? kEEmcNumEtas : kBEmcNumEtas;
00092 mNumPhis = (mEEmc)? kEEmcNumPhis : kBEmcNumPhis;
00093 mNumSubs = (mEEmc)? kEEmcNumSubs : kBEmcNumSubs;
00094 mNumSecs = (mEEmc)? kEEmcNumSecs : kBEmcNumSecs;
00095
00096 mNumTower = (mEEmc)? kEEmcNumTower : kBEmcNumTower;
00097 mNumClust = mNumTower;
00098 mNumRdo = mNumTower;
00099 mEtaBins = (mEEmc)? kEEmcEtaBins : kBEmcEtaBins;
00100
00101 mMaxADC = (mEEmc)? kEEmcMaxADC : kBEmcMaxADC;
00102 mMaxET = (mEEmc)? kEEmcMaxET : kBEmcMaxET;
00103 mIdealGainT = (mEEmc)? kEEmcIdealGainT : kBEmcIdealGainT;
00104
00105 const char *names[]={"bemc","eemc"};
00106
00107 printf("L2gammaAlgo v0.92 (prociutto) \n");
00108 printf("registering new threshold for %4s \n",names[geom]);
00109
00110 #ifdef DEBUGIT
00111 printf(" \n");
00112
00113 printf("============================================================================\n");
00114
00115
00116
00117
00118
00119 printf("algorithm residing at %p\n",this);
00120 printf("l2algo used as input at %p\n",mL2input);
00121 printf("\n");
00122 printf("\n");
00123 printf("mNumEtas = %-2d\n",mNumEtas);
00124 printf("mNumPhis = %-4d\n",mNumPhis);
00125 printf("mNumSubs = %-4d\n",mNumSubs);
00126 printf("mNumSecs = %-4d\n",mNumSecs);
00127 printf("mNumTower = %-4d\n",mNumTower);
00128 printf("mNumClust = %-4d\n",mNumClust);
00129 printf("mNumRdo = %-4d\n",mNumRdo);
00130 printf("mEtaBins = {");
00131 for ( int i=0;i<mNumEtas+1;i++ ) {
00132 printf("%4.2f, ",mEtaBins[i]);
00133 if ( !((i+1)%4) ) printf("\n ");
00134 }
00135 printf("X}\n");
00136 printf("mMaxADC = %4d\n",mMaxADC);
00137 printf("mMaxET = %4.1f\n",mMaxET);
00138 printf("mIdealGainT = %5.3f\n",mIdealGainT);
00139 printf("\n");
00140 printf("============================================================================\n");
00141
00142 #endif
00143
00144
00145 jbook();
00146
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 int L2gammaAlgo::initRun( int run )
00173 {
00174 return initRun(run,mDefaultI_par,mDefaultF_par);
00175 }
00176 int L2gammaAlgo::initRun( char *myname, int run, int I_par[5], float F_par[5] )
00177 {
00178 return initRun( run, I_par, F_par );
00179 }
00180 int L2gammaAlgo::initRun( int run, int I_par[5], float F_par[5] )
00181 {
00182
00183
00184 printf("%s ::initRun() for run=%d\n",mName,run);
00185
00186 if ( mDb->initRun(run) ) return -7;
00187
00188 fflush(stdout);
00189 mRunNumber = run;
00190
00191
00192
00193 jclear();
00194
00195 mUseOfflineGains = I_par[0];
00196 if ( I_par[1] ) mPrescale = I_par[1];
00197 mUseBbc=false;
00198 setTowerThreshold( F_par[0] );
00199 setPatchThreshold( F_par[1] );
00200
00201
00202
00203
00204
00205 {
00206
00207 if ( F_par[1] < 0. ) {
00208 printf("L2gammaAlgo::ERROR -- patch threshold < 0\n");
00209 return 1;
00210 }
00211 if ( F_par[1] > 2.0 * F_par[0] ) {
00212 printf("L2gammaAlgo::ERROR -- patch threshold > 2x tower threshold\n");
00213 return 1;
00214 }
00215
00216 if ( mL2input )
00217 {
00218 if ( F_par[0] < mL2input->getPatchThreshold() ) {
00219 printf("L2gammaAlgo::WARNING -- l2input to this algorithm has a patch threshold < tower threshold\n");
00220
00221 }
00222 }
00223
00224 if ( I_par[0] < 0 || I_par[0] > 1 )
00225 {
00226 printf("L2gammaAlgo::ERROR -- I_par[0] should be 0 (use ideal gains) or 1 (use online gains)\n");
00227 }
00228
00229 }
00230
00231
00232
00233 if ( mLogFile && mLogFile != stdout ) fclose(mLogFile);
00234 if ( mHistFile ) fclose(mHistFile);
00235
00236
00237
00238 char clog[128];
00239 sprintf(clog,"%s/run%d.l2%s.out",mOutDir,run,mName);
00240
00241
00242
00243 mLogFile=fopen(clog,"w");
00244
00245 if(mLogFile==0) printf("%s ::initRun() for run=%d UNABLE to open log-file=%s=, it is not fatal, continue initialization\n",mName,run,clog);
00246
00247 if(mLogFile) {
00248 fprintf(mLogFile,"\n\n====================================================================================================================================\n");
00249 fprintf(mLogFile,"L2gammaAlgo start of run %d summary, compiled: %s , %s\n",run,__DATE__,__TIME__);
00250 fprintf(mLogFile,"calorimeter: %s\n",mName);
00251 fprintf(mLogFile,"run: %d\n",run);
00252 fprintf(mLogFile,"use offline gains I[0]: %d\n",mUseOfflineGains);
00253 fprintf(mLogFile,"prescaled accept I[1]: %d\n",mPrescale);
00254 fprintf(mLogFile,"threshold level: %d\n",mThresholdLevel);
00255 fprintf(mLogFile,"tower threshold F[0]: %-5.2f [GeV]\n",mTowerThreshold);
00256 fprintf(mLogFile,"patch threshold F[1]: %-5.2f [GeV]\n",mPatchThreshold);
00257 fprintf(mLogFile,"patch size: %s\n","3x3");
00258 fprintf(mLogFile,"logfile: %s\n",clog);
00259
00260 fprintf(mLogFile,"database at: %p\n",mDb);
00261 fprintf(mLogFile,"l2 input at: %p\n",mL2input);
00262
00263 fprintf(mLogFile,"\nDetector Geometry\n");
00264 fprintf(mLogFile,"mEtaBins = {");
00265 for ( int i=0;i<mNumEtas+1;i++ ) {
00266 fprintf(mLogFile,"%4.2f, ",mEtaBins[i]);
00267 if ( !((i+1)%4) ) fprintf(mLogFile,"\n ");
00268 }
00269 fprintf(mLogFile,"X}\n");
00270 fprintf(mLogFile,"mMaxADC = %4d\n",mMaxADC);
00271 fprintf(mLogFile,"mMaxET = %4.1f\n",mMaxET);
00272 fprintf(mLogFile,"mIdealGainT = %5.3f\n",mIdealGainT);
00273 fprintf(mLogFile,"\n");
00274
00275 fprintf(mLogFile,"\n");
00276 }
00277 if ( !mDb ) return 100;
00278
00279
00280
00282 mNumberInput = 0;
00283 mNumberAcceptHT = 0;
00284 mNumberAcceptTP = 0;
00285
00286 mNumberLive=0;
00287
00289 if(mLogFile) fprintf(mLogFile,"initialize EMC ascii database\n\n");
00290
00291 if(mLogFile) fprintf(mLogFile,"\n");
00292
00296
00297 float *emc_ideal_gains = new float[mNumEtas];
00298 if(mLogFile) fprintf(mLogFile,"configure ideal gains for %d eta bins\n",mNumEtas);
00299 for ( int i=0;i<mNumEtas;i++ )
00300 {
00301 float eta_mean = mEtaBins[ i ] + mEtaBins[ i+1 ];
00302 eta_mean /= 2.0;
00303 emc_ideal_gains[i] = mMaxADC / mMaxET / cosh(eta_mean);
00304 if(mLogFile) fprintf(mLogFile,"+ etabin=%d eta=%5.2f bemc ideal gain=%5.2f\n", i+1, eta_mean, emc_ideal_gains[i]);
00305
00306 }
00307
00308 if ( mNumEtas != 12 && mNumEtas != 40 ) {
00309 if(mLogFile) fprintf(mLogFile,"L2gammaEmCal::FATAL ERROR -- expect 12 or 40 etabins, got %d\n",mNumEtas);
00310 if(mLogFile) fprintf(mLogFile," + likely cause -- invalid runtime parameter\n");
00311 delete emc_ideal_gains;
00312 return 10;
00313 }
00314
00316 for ( int index=0;index<mNumTower;index++ )
00317 {
00318 mTowerAdcThreshold[index]=0;
00319 mPatchAdcThreshold[index]=mPatchThreshold;
00320 mTowerPed[index]=0.;
00321 mPatchPed[index]=0.;
00322 mTowerFrequency[index]=0;
00323 mPatchFrequency[index]=0;
00324 mTowerGain[index]=-1.0;
00325 }
00326
00327 if(mLogFile) fprintf(mLogFile,"\nInitializing tower ADC threshods\n");
00328
00330 for ( int index=0; index<EmcDbIndexMax; index++ )
00331 {
00332
00333
00334
00335 const L2EmcDb::EmcCDbItem *x = mDb->getByIndex(index);
00336 if ( x==0 ) continue;
00337
00338 #if 1
00339
00340 if ( mBEmc && !mDb->isBTOW(x) )
00341 continue;
00342 if ( mEEmc && !mDb->isETOW(x) )
00343 continue;
00344 #endif
00345
00346
00347
00348
00349
00350
00351 int sec = x->sec - 1;
00352 int sub = 8192;
00353 if ( mEEmc ) sub = x->sub - 'A';
00354 if ( mBEmc ) sub = x->sub - 'a';
00355 int eta = x->eta - 1;
00356 int phi = phibin(sec,sub);
00357 int tow = tower(phi,eta);
00358 int rdo = x->rdo;
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 float ped = x->ped;
00369 float gain = x->gain;
00370 float ideal = emc_ideal_gains[eta];
00371
00372
00373
00374
00375 ushort stat = x->stat;
00376 ushort fail = x->fail;
00377 if ( gain < 0.0 ) {
00378 if(mLogFile) fprintf(mLogFile,"L2gammaEmCal::WARN %s gain=%5.2f will be masked\n",x->name,x->gain);
00379 fail = 0xffff;
00380 }
00381
00382 mTowerStat[rdo]=stat; mPatchStat[rdo]=stat;
00383 mTowerFail[rdo]=fail; mPatchFail[rdo]=fail;
00384 mTowerGain[rdo]=gain;
00385 mTowerGainIdeal[rdo]=ideal;
00386
00387
00388 if ( !fail )
00389 mTowerAdcCorrection[rdo]=(mUseOfflineGains)?ideal/gain:1.0;
00390 else
00391 mTowerAdcCorrection[rdo]=0.;
00392
00393
00394
00395 if ( !fail ) {
00396 mTowerAdcThreshold[rdo] = (ushort)(ped + (mIdealGainT * mTowerThreshold)/mTowerAdcCorrection[rdo] + 0.5 );
00397 mTowerPed[rdo]=ped;
00398 }
00399 else {
00400 mTowerAdcThreshold[rdo] = 4095;
00401 mTowerPed[rdo]=4095;
00402 }
00403
00404
00405
00406 mRdo2tower[ rdo ] = tow;
00407 mTower2rdo[ tow ] = rdo;
00408
00409 if ( !fail ) mNumberLive++;
00410
00411 }
00412
00413
00414
00415
00416
00417
00418
00419 if(mLogFile) fprintf(mLogFile,"Initializing 3x3 tower cluster thresholds\n");
00420
00423 for ( int index=0; index<EmcDbIndexMax; index++ )
00424 {
00425
00426
00427
00428 const L2EmcDb::EmcCDbItem *x = mDb->getByIndex(index);
00429 if ( x==0 ) continue;
00430
00431
00432 if ( mBEmc && !mDb->isBTOW(x) )
00433 continue;
00434 if ( mEEmc && !mDb->isETOW(x) )
00435 continue;
00436
00437
00438 int sec = x->sec - 1;
00439 int sub = 8192;
00440 if ( mEEmc ) sub = x->sub - 'A';
00441 if ( mBEmc ) sub = x->sub - 'a';
00442 int eta = x->eta - 1;
00443 int phi = phibin(sec,sub);
00444
00445 int rdo = x->rdo;
00446
00447 mPatchStat[rdo] |= x->stat;
00448 mPatchFail[rdo] |= x->fail;
00449
00450
00451
00452
00453
00454 if ( x->fail ) continue;
00455 if ( mTowerFail[rdo] ) continue;
00456
00457
00458
00459 float ped = x->ped;
00460
00461
00466 int nn=0;
00467 for ( int ieta=eta-1;ieta<=eta+1;ieta++ )
00468 for ( int iphi=phi-1;iphi<=phi+1;iphi++ )
00469 {
00470
00471 if ( ieta < 0 || ieta > mNumEtas-1 ) continue;
00472 int jeta=ieta;
00473 int jphi=(iphi+mNumPhis)%mNumPhis;
00474 int jtow=tower(jphi,jeta);
00475 int jrdo=mTower2rdo[jtow];
00476
00477 mPatchAdcThreshold[jrdo] += ped/mIdealGainT *
00478 mTowerAdcCorrection[rdo];
00479
00480 if ( jtow == 12 ) {
00481 if(mLogFile) fprintf(mLogFile,"+ name=%s ped=%5.2f gfact=%5.2f thresh=%5.2f\n",x->name,x->ped,mTowerAdcCorrection[rdo],mPatchAdcThreshold[jrdo] );
00482 }
00483
00484
00485
00486 mPatchPed[jrdo]+=ped;
00487
00489 mRdoPatch[rdo][nn++]=jrdo;
00490
00491
00492 }
00493 mNumPatch[rdo]=nn;
00494
00495 }
00496
00497
00498
00499
00500
00501
00502
00503
00504 for ( int index=0;index<EmcDbIndexMax;index++ )
00505 {
00506
00507
00508
00509
00510 const L2EmcDb::EmcCDbItem *x = mDb->getByIndex(index);
00511 if ( x==0 ) continue;
00512
00513 #if 1
00514
00515 if ( mBEmc && !mDb->isBTOW(x) )
00516 continue;
00517 if ( mEEmc && !mDb->isETOW(x) )
00518 continue;
00519 #endif
00520
00521 float gain=x->gain;
00522 int rdo=x->rdo;
00523 if ( gain==0. ) gain=-1.;
00524
00525 int tow=mRdo2tower[rdo];
00526 int eta=tow%mNumEtas;
00527
00528
00529
00530
00531
00532
00533 #if 0
00534 fprintf(mLogFile,"tower=%s tow=%d rdo=%d mask=%2x gain=%5.2f ideal=%5.2f ped=%5.2f thr=%d %5.2f GeV patch=%5.2f + %5.2f GeV\n",
00535 x->name,
00536 mRdo2tower[rdo],
00537 rdo,
00538 x->fail,
00539 x->gain,
00540 emc_ideal_gains[eta],
00541 x->ped,
00542 mTowerAdcThreshold[rdo],
00543 (float)((mTowerAdcThreshold[rdo]-x->ped)*mMaxET/mMaxADC),
00544 mPatchAdcThreshold[rdo]-mPatchThreshold,
00545 mPatchThreshold);
00546 #endif
00547
00548 #if 1
00549 if(mLogFile) fprintf(mLogFile,"tower=%s tow=%d rdo=%d ideal=%5.2f thr=%d patch=%5.2f GeV\n",
00550 x->name,
00551 mRdo2tower[rdo],
00552 rdo,
00553 emc_ideal_gains[eta],
00554 mTowerAdcThreshold[rdo],
00555 mPatchAdcThreshold[rdo]);
00556 #endif
00557
00558
00559
00560 }
00561
00562 #ifdef DEBUG_PATCH_THRESHOLDS
00563 for ( int rdo=0;rdo<mNumRdo;rdo++ )
00564 printPatchConfig(rdo);
00565 #endif
00566
00567 if(mLogFile) {
00568 fprintf(mLogFile,"\n");
00569 fprintf(mLogFile,"total number of towers: %d\n",mNumTower);
00570 fprintf(mLogFile,"number of unmasked towers: %d\n",mNumberLive);
00571 fprintf(mLogFile,"\n====================================================================================================================================\n\n");
00572 fflush(mLogFile);
00573 }
00574
00575
00576
00577
00578 delete emc_ideal_gains;
00579
00580 return 0;
00581
00582 }
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606 bool L2gammaAlgo::doEvent( int L0trigger, int inputEventID, TrgDataType* trgData,
00607 int bemcIn, unsigned short *bemcData,
00608 int eemcIn, unsigned short *eemcData )
00609 {
00610 rdtscl_macro(mEveTimeStart);
00611 mAccept=false;
00612 if ( mEEmc ) {
00613 mAccept=doEvent( inputEventID, trgData, eemcIn, eemcData );
00614 }
00615 else {
00616 mAccept=doEvent( inputEventID, trgData, bemcIn, bemcData );
00617 }
00618
00619 rdtscl_macro(mEveTimeStop);
00620 mEveTimeDiff=mEveTimeStop-mEveTimeStart;
00621 int kTick=mEveTimeDiff/1000;
00622
00623 mhT->fill(kTick);
00624
00625 return mAccept;
00626 }
00627
00628
00629 bool L2gammaAlgo::doEvent( int inpEveId, TrgDataType* trgData,
00630 int emcIn, unsigned short *emcData )
00631 {
00632
00633 clear();
00634
00635 if ( !emcIn ) return false;
00636
00637 mNumberInput++;
00638
00639 mHistos[0].fill(0);
00640
00641
00642
00643
00644
00645
00646 unsigned long eval_time_start;
00647 unsigned long eval_time_stop;
00648 rdtscl_macro(eval_time_start);
00649
00650 bool eht = false;
00651 bool trig = false;
00652
00653 float sum=0.;
00654 ushort htrdo=8192;
00655 ushort tprdo=8192;
00656
00657 nRdosHT=0;
00658 nRdosTP=0;
00659
00660
00661 #ifdef OPT_PREPROCESS
00662
00663
00664
00665
00666
00667 if ( mL2input ) {
00668 for ( ushort i=0;i<mL2input->getNumberOfHighTowers();i++ )
00669 {
00670 ushort rdo=mL2input->mListOfRdosHT[i];
00671 #ifdef DEBUG
00672 mHistos[3].fill( emcData[rdo] );
00673 #endif
00674 if ( emcData[rdo] < mTowerAdcThreshold[rdo]) continue;
00675 mADCofHT[ nRdosHT ] = emcData[rdo];
00676 mListOfRdosHT[ nRdosHT++ ] = rdo;
00677 mHistos[4].fill( emcData[rdo] );
00678
00679 eht=true;
00680 htrdo=rdo;
00681 sum = 0.;
00682 for ( int i=0;i<mNumPatch[rdo];i++ ) {
00683 int prdo=mRdoPatch[rdo][i];
00684 if ( mTowerFail[prdo] ) continue;
00685 sum += emcData[prdo] / mIdealGainT * mTowerAdcCorrection[rdo];
00686 }
00687 ushort sumx2=(ushort)(2.0*sum);
00688 #ifdef DEBUG
00689 mHistos[7].fill( sumx2 );
00690 #endif
00691
00692 if ( sum > mPatchAdcThreshold[rdo] ) {
00693 trig=true;
00694 tprdo=rdo;
00695 mETofTP[ nRdosTP ] = sum-mPatchAdcThreshold[rdo]+mPatchThreshold;
00696 mListOfRdosTP[ nRdosTP++ ] = rdo;
00697 mHistos[5].fill( emcData[rdo] );
00698 mHistos[8].fill( sumx2 );
00699 }
00700 }
00701 goto QA;
00702 }
00703 #endif
00704
00705
00706
00707
00708
00709
00710
00711 for ( ushort rdo=0; rdo<mNumRdo; rdo++ )
00712 {
00713
00714
00715
00716
00717 #ifdef DEBUG
00718 mHistos[3].fill( emcData[rdo] );
00719 #endif
00720 if ( emcData[rdo] < mTowerAdcThreshold[rdo]) continue;
00721
00722
00723
00724
00725
00726 mADCofHT[ nRdosHT ] = emcData[rdo];
00727 mListOfRdosHT[ nRdosHT++ ] = rdo;
00728 mHistos[4].fill( emcData[rdo] );
00729
00730 eht=true;
00731 htrdo=rdo;
00732
00733
00734
00735 sum = 0.;
00736 for ( int i=0;i<mNumPatch[rdo];i++ ) {
00737 int prdo=mRdoPatch[rdo][i];
00738 if ( mTowerFail[prdo] ) continue;
00739 sum += emcData[prdo] / mIdealGainT * mTowerAdcCorrection[rdo];
00740 }
00741 ushort sumx2=(ushort)(2.0*sum);
00742 #ifdef DEBUG
00743 mHistos[7].fill( sumx2 );
00744 #endif
00745
00746
00747
00748
00749 if ( sum > mPatchAdcThreshold[rdo] ) {
00750 trig=true;
00751 tprdo=rdo;
00752 mETofTP[ nRdosTP ] = sum-mPatchAdcThreshold[rdo]+mPatchThreshold;
00753 mListOfRdosTP[ nRdosTP++ ] = rdo;
00754 mHistos[5].fill( emcData[rdo] );
00755 mHistos[8].fill( sumx2 );
00756 }
00757
00758 }
00759
00760
00761
00762
00763
00764 #ifdef OPT_PREPROCESS
00765 QA:
00766 #endif
00767
00768 bool prescaleAccept = false;
00769 if ( mPrescale>0 )
00770 prescaleAccept = !((mNumberInput) % mPrescale);
00771
00772 mHistos[1].fill(nRdosHT);
00773 mHistos[2].fill(nRdosTP);
00774
00775
00776 if ( eht ) {
00777 mNumberAcceptHT++;
00778 mTowerFrequency[mRdo2tower[htrdo]]++;
00779 mHistos[0].fill(1);
00780 }
00781
00782 if ( trig ) {
00783 mNumberAcceptTP++;
00784 mPatchFrequency[mRdo2tower[tprdo]]++;
00785 mHistos[0].fill(2);
00786 }
00787 rdtscl_macro(eval_time_stop);
00788
00789
00790 unsigned long qa_time_start;
00791 unsigned long qa_time_stop;
00792 rdtscl_macro(qa_time_start);
00793
00794
00795
00796
00797 L2gammaResult mResult;
00798
00799 mResult.result_version = LEVEL2_GAMMA_RESULT_VERSION;
00800 mResult.threshold = (mThresholdLevel==kThresh1)? (0x1|0x2) : (0x4|0x8);
00801
00802 mResult.elapsed=0x00;
00803
00804 mResult.trigger = 0x00;
00805
00806 float max=0.;
00807 ushort maxrdo=8192;
00808
00809
00810 if ( eht )
00811 mResult.trigger |= 0x1;
00812
00813
00814
00815 for ( ushort i=0;i<nRdosTP;i++ )
00816 {
00817 if ( mETofTP[i] > max ) {
00818 max=mETofTP[i];
00819 maxrdo=mListOfRdosTP[i];
00820 }
00821 }
00822 if ( maxrdo < mNumRdo )
00823 {
00824 mResult.trigger |= 0x2;
00825 ushort tow=mRdo2tower[maxrdo];
00826 mResult.phibin=tow/mNumEtas;
00827 mResult.etabin=tow%mNumEtas;
00828 max-=mPatchAdcThreshold[maxrdo];
00829 max+=mPatchThreshold;
00830 if ( max < 127.5 )
00831 mResult.ptclusterx2 = ((ushort)(2.0*max));
00832 else
00833 mResult.ptclusterx2 = 255;
00834 float pttow=emcData[maxrdo]-mTowerPed[maxrdo];
00835 pttow*=mTowerAdcCorrection[maxrdo]/mIdealGainT;
00836 if ( pttow < 127.5 )
00837 mResult.pttowerx2 = (ushort)(2.0*pttow);
00838 else
00839 mResult.pttowerx2 = 255;
00840 if ( mEEmc ) mResult.phibin |= 0x8;
00841 }
00842
00843
00844
00845 if ( prescaleAccept )
00846 mResult.trigger |= 0x4;
00847
00848
00849 if ( trig || prescaleAccept )
00850 mResult.trigger |= 0x8;
00851
00852 rdtscl_macro(qa_time_stop);
00853 int dt=(int)(qa_time_stop-eval_time_start)/1000;
00854
00855 mEvalTime += dt;
00856
00857
00861 unsigned int *pResult
00862
00863 = &trgData->TrgSum.L2Result[mResultOffset+2*mEEmc+mThresholdLevel];
00864
00868 memcpy(pResult,&mResult,sizeof(L2gammaResult));
00869
00870 #ifdef __L2GAMMA_PRINT_RESULT__
00871 print_L2gammaResult(mResult);
00872 #endif
00873
00879 if ( trig || prescaleAccept )
00880 {
00881 mHistos[0].fill(3);
00882 for ( ushort i=0;i<nRdosHT;i++ ) {
00883 int tower=(int)mRdo2tower[mListOfRdosHT[i]];
00884 mHistos[6].fill( (mADCofHT[i] - (ushort)mTowerPed[ mListOfRdosHT[i] ])/5,tower );
00885 mHistos[13].fill(tower);
00886 }
00887
00888 for ( ushort i=0;i<nRdosTP;i++ ) {
00889 int tower=(int)mRdo2tower[mListOfRdosTP[i]];
00890 mHistos[9].fill( (int)(mETofTP[i]*2),tower );
00891 mHistos[14].fill( tower );
00892 }
00893
00894 mHistos[18].fill( dt );
00895
00896 }
00897
00898 mHistos[15].fill(dt);
00899 if ( eht ) mHistos[16].fill(dt);
00900 if ( trig ) mHistos[17].fill(dt);
00901
00902 return ( trig || prescaleAccept );
00903
00904 }
00905
00906
00907
00908
00909 void L2gammaAlgo::clear()
00910 {
00911
00912 nRdosHT=0;
00913 nRdosTP=0;
00914
00915 }
00916
00917
00918
00919 void L2gammaAlgo::finishRun()
00920 {
00921
00922 int run=mRunNumber;
00923
00924
00925 char chis[128];
00926 printf("%s/run%d.l2%s.hist.bin\n",mOutDir,run,mName);
00927 sprintf(chis,"%s/run%d.l2%s.hist.bin",mOutDir,run,mName);
00928 printf("L2gamma:saving '%s'\n",chis);
00929
00930
00931
00932 mHistFile=fopen(chis,"w");
00933 if(mHistFile==0) printf("%s ::finishRun() for run=%d UNABLE to open histo-file=%s=, continue initialization\n",mName,run,chis);
00934
00935
00936
00937
00938
00939 if ( !mNumberInput ) mNumberInput=-1;
00940 if ( !mNumberLive ) mNumberLive=-1;
00941
00942
00943
00944
00945 if(mLogFile) {
00946 fprintf(mLogFile,"\n\n===================================================================================================================================\n");
00947 fprintf(mLogFile,"L2gammaAlgo end of run %d summary\n",run);
00948 fprintf(mLogFile,"run: %d\n",run);
00949 fprintf(mLogFile,"tower threshold: %5.2f [GeV]\n",mTowerThreshold);
00950 fprintf(mLogFile,"patch threshold: %5.2f [GeV]\n",mPatchThreshold);
00951 fprintf(mLogFile,"patch size: 3x3\n");
00952 fprintf(mLogFile,"eval time: %d [kTicks]\n",mEvalTime);
00953 fprintf(mLogFile,"avg time/event: %d [kTicks]\n",mEvalTime/mNumberInput);
00954 fprintf(mLogFile,"# input: %d\n",mNumberInput);
00955 fprintf(mLogFile,"# ht accept: %d\n",mNumberAcceptHT);
00956 fprintf(mLogFile,"# ht+tp accept: %d\n",mNumberAcceptTP);
00957 fprintf(mLogFile,"\n");
00958
00959
00960
00961
00962
00963 float avgt=0,avgp=0;
00964 int maxtf=0,maxpf=0;
00965
00966 for ( int tow=0;tow<mNumTower;tow++ )
00967 {
00968 avgt+=mTowerFrequency[tow];
00969 avgp+=mPatchFrequency[tow];
00970 if ( mTowerFrequency[tow]>maxtf ) maxtf=mTowerFrequency[tow];
00971 if ( mPatchFrequency[tow]>maxpf ) maxpf=mTowerFrequency[tow];
00972 }
00973 avgt=( ((float)avgt)/(float)mNumberLive );
00974 avgp=( ((float)avgp)/(float)mNumberLive );
00975
00976
00977 fprintf(mLogFile,"max # ht accept in one tower: %d\n",maxtf);
00978 fprintf(mLogFile,"max # ht+tp accept in one tower: %d\n",maxpf);
00979 fprintf(mLogFile,"avg # ht accept: %6f\n",avgt);
00980 fprintf(mLogFile,"avg # ht+tp accept: %6f\n",avgp);
00981 fprintf(mLogFile,"expected # ht accept in one tower: %6f\n",((float)mNumberAcceptHT)/mNumberLive);
00982 fprintf(mLogFile,"expected # ht+tp accept in one tower: %6f\n",((float)mNumberAcceptTP)/mNumberLive);
00983 fprintf(mLogFile,"\n");
00984
00985
00986
00987
00988 #ifndef DEBUG_PATCH_THRESHOLDS
00989 fprintf(mLogFile,"Summary of hot towers (# ht accept > 3*average or # ht+tp accept > 3*average):\n\n");
00990
00991 for ( int tow=0;tow<mNumTower;tow++ )
00992 {
00993 int phi=tow/mNumEtas;
00994 int eta=tow%mNumEtas;
00995 int sec=phi/mNumSubs;
00996 int sub=phi%mNumSubs;
00997 int myrdo=mTower2rdo[tow];
00998
00999
01000 bool hott = (mTowerFrequency[tow] > (3.0*avgt));
01001 bool hotp = 0 && (mPatchFrequency[tow] > (3.0*avgp));
01002
01003
01004
01005 if ( hott||hotp ) fprintf(mLogFile,"\n");
01006
01007 if ( mBEmc )
01008 {
01009 if ( hott )
01010 fprintf(mLogFile,"L2gammaAlgo::WARNING -- possible hot tower %02dt%c%02d freq=%d avg=%5.2f\n",sec+1,sub+'a',eta+1,mTowerFrequency[tow],avgt);
01011 if ( hotp )
01012 fprintf(mLogFile,"L2gammaAlgo::WARNING -- possible hot patch %02dt%c%02d freq=%d avg=%5.2f\n",sec+1,sub+'a',eta+1,mPatchFrequency[tow],avgp);
01013 }
01014 else
01015 {
01016 if ( hott )
01017 fprintf(mLogFile,"L2gammaAlgo::WARNING -- possible hot tower %02dT%c%02d freq=%d avg=%5.2f\n",sec+1,sub+'A',eta+1,mTowerFrequency[tow],avgt);
01018 if ( hotp )
01019 fprintf(mLogFile,"L2gammaAlgo::WARNING -- possible hot patch %02dT%c%02d freq=%d avg=%5.2f\n",sec+1,sub+'A',eta+1,mPatchFrequency[tow],avgp);
01020 }
01021
01022 if ( hott || hotp )
01023 {
01024
01025 printPatchConfig( myrdo );
01026
01027 }
01028
01029 }
01030 }
01031 #endif
01032
01033 if ( mLogFile ) {
01034 fprintf(mLogFile,"\n====================================================================================================================================\n\n");
01035 finishCommonHistos();
01036
01037 fflush(mLogFile);
01038 }
01039
01040 finish();
01041
01042 }
01043
01044 void L2gammaAlgo::finish()
01045 {
01046
01047 if ( mHistFile ) {
01048 for ( unsigned int i=0;i<sizeof(mHistos)/sizeof(L2Histo);i++)
01049 {
01050 mHistos[i].write(mHistFile);
01051 }
01052 fflush(mHistFile);
01053 }
01054 }
01055
01056
01057
01058 void L2gammaAlgo::jclear()
01059 {
01060 for ( int i=0;i<19;i++ ) mHistos[i].reset();
01061 }
01062 void L2gammaAlgo::jbook()
01063 {
01064
01065
01066
01067 if ( 1 ) {
01068
01069
01070
01071 mHistos[0]=L2Histo(100, "Counter. 0=N_{input} 1=N_{ht} 2=N_{clust} 3=N_{accept}", 10);
01072 mHistos[1]=L2Histo(101, "N high towers > threshold", 10);
01073 mHistos[2]=L2Histo(102, "N clusters > threshold", 10);
01074
01075
01076
01077 mHistos[3]=L2Histo(103, "Raw ADC of ht (if DEBUG)", 1024);
01078 mHistos[4]=L2Histo(104, "Raw ADC of ht, ht > threshold", 1024);
01079 mHistos[5]=L2Histo(105, "Raw ADC of ht, cluster > threshold", 1024);
01080 mHistos[6]=L2Histo(106, "HT event accepted; X: (ADC-ped)/5; Y: tower index", 128,mNumTower);
01081
01082
01083
01084 mHistos[7]=L2Histo(107, "2*ET sum of cluster, ht > threshold", 32);
01085 mHistos[8]=L2Histo(108, "2*ET sum of cluster, cluster > threshold", 32);
01086 mHistos[9]=L2Histo(109, "2*ET sum of cluster - ped/gain, event accepted", 32,mNumTower);
01087
01088
01089
01090 mHistos[10]=L2Histo(110, "Raw BBC TAC difference+256 (not implemented)", 1);
01091 mHistos[11]=L2Histo(111, "Raw BBC TAC difference+256, ht > threshold (not implemented)", 1);
01092 mHistos[12]=L2Histo(112, "Raw BBC TAC difference+256, cluster > threshold (not implemented)" , 1);
01093
01094
01095
01096 mHistos[13]=L2Histo(113, "High tower > threshold frequency, X: ieta+mNumEta*iphi", mNumTower);
01097 mHistos[14]=L2Histo(114, "Cluster > threshold frequency, X: ieta+mNumEta*iphi", mNumTower);
01098
01099
01100
01101 mHistos[15]=L2Histo(115, "kTicks / input event", 128);
01102 mHistos[16]=L2Histo(116, "kTicks / ht event", 128);
01103 mHistos[17]=L2Histo(117, "kTicks / cluster event", 128);
01104 mHistos[18]=L2Histo(118, "kTicks / trigger", 128);
01105
01106 }
01107
01108
01109 }
01110
01111
01112 #ifndef DEBUG_PATCH_THRESHOLDS
01113 void L2gammaAlgo::printPatchConfig( int rdo )
01114 {
01115
01116
01117 int tow = mRdo2tower[rdo];
01118 int phi = tow/mNumEtas;
01119 int eta = tow%mNumEtas;
01120
01121 int sec = phi/mNumSubs;
01122 int sub = phi%mNumSubs;
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149 if ( mEEmc )
01150 fprintf(mLogFile,"etow: %02dT%c%02d\n",sec+1,sub+'A',eta+1);
01151 if ( mBEmc )
01152 fprintf(mLogFile,"btow: %02dt%c%02d\n",sec+1,sub+'a',eta+1);
01153
01154 fprintf(mLogFile,"index: %4d\n",tow);
01155 fprintf(mLogFile,"gain: %6.3f [ideal=%6.3f]\n",mTowerGain[rdo],mTowerGainIdeal[rdo]);
01156 fprintf(mLogFile,"ped: %6.3f\n",mTowerPed[rdo]);
01157 fprintf(mLogFile,"thr: %3d [%4.2f GeV]\n",mTowerAdcThreshold[rdo],mTowerAdcThreshold[rdo]/mIdealGainT);
01158 fprintf(mLogFile,"\n");
01159
01160
01161 fprintf(mLogFile,"loaded peds gain factors status bits mask bits frequency > thr\n");
01162
01163 for ( int i_eta=eta+1;i_eta>=eta-1;i_eta-- ) {
01164
01165 int phi_l = (phi+mNumPhis-1)%mNumPhis;
01166 int phi_r = (phi+mNumPhis+1)%mNumPhis;
01167 int phi_c = phi;
01168
01169
01170 float peds[]={0,0,0};
01171 float gains[]={0,0,0};
01172 ushort stats[]={0xff,0xff,0xff};
01173 ushort fails[]={0xff,0xff,0xff};
01174 int freqs[]={0,0,0};
01175
01176
01177 if ( i_eta < mNumEtas && i_eta >= 0 ) {
01178
01179 int tow_l = tower(phi_l,i_eta);
01180 int tow_r = tower(phi_r,i_eta);
01181 int tow_c = tower(phi_c,i_eta);
01182
01183 int rdo_l = mTower2rdo[tow_l];
01184 int rdo_r = mTower2rdo[tow_r];
01185 int rdo_c = mTower2rdo[tow_c];
01186
01187 peds[0]=mTowerPed[rdo_l];peds[1]=mTowerPed[rdo_c];peds[2]=mTowerPed[rdo_r];
01188 gains[0]=mTowerAdcCorrection[rdo_l];gains[1]=mTowerAdcCorrection[rdo_c];gains[2]=mTowerAdcCorrection[rdo_r];
01189 stats[0]=mTowerStat[rdo_l];stats[1]=mTowerStat[rdo_c];stats[2]=mTowerStat[rdo_r];
01190 fails[0]=mTowerFail[rdo_l];fails[1]=mTowerFail[rdo_c];fails[2]=mTowerFail[rdo_r];
01191 freqs[0]=mTowerFrequency[tow_l];freqs[1]=mTowerFrequency[tow_c];freqs[2]=mTowerFrequency[tow_r];
01192
01193
01194 }
01195
01196 fprintf(mLogFile, "%4.1f | %4.1f | %4.1f\t", peds[0],peds[1],peds[2] );
01197 fprintf(mLogFile, "%4.2f | %4.2f | %4.2f\t", gains[0],gains[1],gains[2] );
01198 fprintf(mLogFile, "%2x | %2x | %2x \t", stats[0],stats[1],stats[2] );
01199 fprintf(mLogFile, "%2x | %2x | %2x \t", fails[0],fails[1],fails[2] );
01200 fprintf(mLogFile, "%4.0f | %4.0f | %4.0f\t", (float)freqs[0],(float)freqs[1],(float)freqs[2] );
01201
01202 fprintf(mLogFile,"\n");
01203
01204 if ( i_eta > eta-1 ) {
01205 fprintf(mLogFile, "-----+------+-----\t");
01206 fprintf(mLogFile, "-----+------+-----\t");
01207 fprintf(mLogFile, "-----+------+-----\t");
01208 fprintf(mLogFile, "-----+------+-----\t");
01209 fprintf(mLogFile, "-----+------+-----\t");
01210 fprintf(mLogFile, "\n" );
01211 }
01212
01213 }
01214
01215 }
01216 #endif
01217 #ifdef DEBUG_PATCH_THRESHOLDS
01218 void L2gammaAlgo::printPatchConfig( int rdo )
01219 {
01220
01221
01222 int tow = mRdo2tower[rdo];
01223 int phi = tow/mNumEtas;
01224 int eta = tow%mNumEtas;
01225
01226 int sec = phi/mNumSubs;
01227 int sub = phi%mNumSubs;
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254 if ( mEEmc )
01255 fprintf(mLogFile,"etow: %02dT%c%02d\n",sec+1,sub+'A',eta+1);
01256 if ( mBEmc )
01257 fprintf(mLogFile,"btow: %02dt%c%02d\n",sec+1,sub+'a',eta+1);
01258
01259 fprintf(mLogFile,"index: %4d\n",tow);
01260 fprintf(mLogFile,"gain: %6.3f [ideal=%6.3f]\n",mTowerGain[rdo],mTowerGainIdeal[rdo]);
01261 fprintf(mLogFile,"ped: %6.3f\n",mTowerPed[rdo]);
01262 fprintf(mLogFile,"thr: %3d [%4.2f GeV]\n",mTowerAdcThreshold[rdo],mTowerAdcThreshold[rdo]/mIdealGainT);
01263 fprintf(mLogFile,"patch: %6.3f\n",mPatchAdcThreshold[rdo]);
01264 fprintf(mLogFile,"\n");
01265
01266
01267 fprintf(mLogFile,"loaded peds gain factors status bits mask bits frequency > thr\n");
01268
01269 for ( int i_eta=eta+1;i_eta>=eta-1;i_eta-- ) {
01270
01271 int phi_l = (phi+mNumPhis-1)%mNumPhis;
01272 int phi_r = (phi+mNumPhis+1)%mNumPhis;
01273 int phi_c = phi;
01274
01275 int phis[]={phi_l,phi,phi_r};
01276 float peds[]={0,0,0};
01277 float gains[]={0,0,0};
01278 ushort stats[]={0xff,0xff,0xff};
01279 ushort fails[]={0xff,0xff,0xff};
01280 int freqs[]={0,0,0};
01281
01282 float corr[]={0.,0.,0.};
01283
01284
01285 if ( i_eta < mNumEtas && i_eta >= 0 ) {
01286
01287 int tow_l = tower(phi_l,i_eta);
01288 int tow_r = tower(phi_r,i_eta);
01289 int tow_c = tower(phi_c,i_eta);
01290
01291 int rdo_l = mTower2rdo[tow_l];
01292 int rdo_r = mTower2rdo[tow_r];
01293 int rdo_c = mTower2rdo[tow_c];
01294
01295 peds[0]=mTowerPed[rdo_l];peds[1]=mTowerPed[rdo_c];peds[2]=mTowerPed[rdo_r];
01296
01297 gains[0]=(float)mIdealGainT;
01298 gains[1]=(float)mIdealGainT;
01299 gains[2]=(float)mIdealGainT;
01300
01301 stats[0]=mTowerStat[rdo_l];stats[1]=mTowerStat[rdo_c];stats[2]=mTowerStat[rdo_r];
01302 fails[0]=mTowerFail[rdo_l];fails[1]=mTowerFail[rdo_c];fails[2]=mTowerFail[rdo_r];
01303 freqs[0]=mTowerFrequency[tow_l];freqs[1]=mTowerFrequency[tow_c];freqs[2]=mTowerFrequency[tow_r];
01304
01305 corr[0]=mTowerAdcCorrection[rdo_l];
01306 corr[1]=mTowerAdcCorrection[rdo_c];
01307 corr[2]=mTowerAdcCorrection[rdo_r];
01308
01309
01310 }
01311
01312 fprintf(mLogFile, "%4.1f | %4.1f | %4.1f\t", peds[0],peds[1],peds[2] );
01313 fprintf(mLogFile, "%4.0f | %4.0f | %4.0f\t", gains[0],gains[1],gains[2] );
01314 fprintf(mLogFile, "%2x | %2x | %2x \t", stats[0],stats[1],stats[2] );
01315 fprintf(mLogFile, "%2x | %2x | %2x \t", fails[0],fails[1],fails[2] );
01316 fprintf(mLogFile, "%4.2f | %4.2f | %4.2f\t",
01317 corr[0]*peds[0]/gains[0],
01318 corr[1]*peds[1]/gains[1],
01319 corr[2]*peds[2]/gains[2]);
01320
01321 fprintf(mLogFile,"\n");
01322
01323 if ( i_eta > eta-1 ) {
01324 fprintf(mLogFile, "-----+------+-----\t");
01325 fprintf(mLogFile, "-----+------+-----\t");
01326 fprintf(mLogFile, "-----+------+-----\t");
01327 fprintf(mLogFile, "-----+------+-----\t");
01328 fprintf(mLogFile, "-----+------+-----\t");
01329 fprintf(mLogFile, "\n" );
01330 }
01331 else
01332 fprintf(mLogFile, "\n\n" );
01333
01334 }
01335
01336 }
01337
01338 #endif