00001 #include <stdio.h>
00002 #include <assert.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <time.h>
00006 #include <math.h>
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef IS_REAL_L2 //in l2-ana environment
00021 #include "../L2algoUtil/L2EmcDb.h"
00022 #include "../L2algoUtil/L2Histo.h"
00023 #else
00024 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb.h"
00025 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
00026 #endif
00027
00028 #include "L2jetAlgo2006.h"
00029 #include "L2jetResults2006.h"
00030 #include "Map_DeltaPhiJets.h"
00031
00032
00033
00034 L2jetAlgo2006::L2jetAlgo2006(const char* name, L2EmcDb* db, char* outDir, int resOff)
00035 : L2VirtualAlgo( name, db, outDir, resOff) {
00036
00037
00038
00039 par_maxADC=4095;
00040 par_maxEt=60;
00041 par_adcMask= (unsigned short) (-0x10);
00042 par_pedOff=0x10/2;
00043 createHisto();
00044 run_number=-1;
00045 printf("L2jetAlgo2006 instantiated, logPath='%s'\n",mOutDir);
00046 eve_Jet[0]=new L2Jet;
00047 eve_Jet[1]=new L2Jet;
00048 }
00049
00050
00051
00052 bool
00053 L2jetAlgo2006::paramsChanged( int *rc_ints, float *rc_floats) {
00054 int i;
00055 for(i=0;i<5;i++)
00056 if(rc_ints[i]!=raw_ints[i]) goto foundProblem;
00057
00058 for(i=0;i<5;i++)
00059 if(fabs(rc_floats[i]-raw_floats[i])>0.00001) goto foundProblem;
00060
00061 return false;
00062
00063 foundProblem:
00064 if (mLogFile) fprintf(mLogFile,"L2jet-ghost initRun - inconsistent params, ABORT initialization\n");
00065 return true;
00066 }
00067
00068
00069
00070 int
00071 L2jetAlgo2006::initRun( int runNo, int *rc_ints, float *rc_floats) {
00072
00073
00074 if(mDb->initRun(runNo)) return -7;
00075
00076
00077 if(run_number==runNo) {
00078 if (mLogFile) fprintf(mLogFile,"L2jet::initRun-%s(%d)=ghost already initilized, only check params\n",mName, runNo);
00079 printf("L2jet::initRun-%s(%d)=ghost already initilized, only checking params\n",mName, runNo);
00080 int ret= paramsChanged(rc_ints, rc_floats);
00081
00082 if(ret){
00083 run_number=-77;
00084 if (mLogFile) {
00085 fprintf(mLogFile,"L2jet algorithm init: crashA in internal logic\n");
00086 fclose(mLogFile);
00087 }
00088 return ret;
00089 }
00090 }
00091
00092
00093 int i;
00094 for (i=0; i<mxHA;i++) if(hA[i])hA[i]->reset();
00095
00096
00097 memset(db_btowThr, 0xFFFF,sizeof(db_btowThr));
00098 memset(db_btowPedS, 0, sizeof(db_btowPedS));
00099 memset(db_btowGainCorr, 0, sizeof(db_btowGainCorr));
00100 memset(db_btowL2PhiBin, 0, sizeof(db_btowL2PhiBin));
00101 memset(db_btowL2PatchBin,0, sizeof(db_btowL2PatchBin));
00102
00103 memset(db_etowThr, 0xFFFF,sizeof(db_etowThr));
00104 memset(db_etowPedS, 0 ,sizeof(db_etowPedS));
00105 memset(db_etowGainCorr, 0 ,sizeof(db_etowGainCorr));
00106 memset(db_etowL2PhiBin, 0 ,sizeof(db_etowL2PhiBin));
00107 memset(db_etowL2PatchBin,0 ,sizeof(db_etowL2PatchBin));
00108
00109
00110
00111
00112
00113
00114
00115 int par_IgainCorrOne=30;
00116 int par_IgainCorrMin=5;
00117 int par_IgainCorrMax=60;
00118
00119 run_startUnix=time(0);
00120 run_number =runNo;
00121 raw_ints =rc_ints;
00122 raw_floats =rc_floats;
00123 run_nEventOneJet=run_nEventDiJet= run_nEventRnd=0;
00124
00125 char Fname[1000];
00126 sprintf(Fname,"%s/run%d.l2jet.out",mOutDir,run_number);
00127 printf("L2jet::initRun-%s('%s') ...\n",mName,Fname);
00128
00129 mEventsInRun=0;
00130 mLogFile = fopen(Fname,"w");
00131 if( mLogFile==0) printf(" L2jetAlgo2006() UNABLE to open run summary log file, continue anyhow\n");
00132
00133
00134
00135 par_cutTag = rc_ints[0];
00136 par_useBtowEast= (rc_ints[1]&1 )>0;
00137 par_useBtowWest= (rc_ints[1]&2)>0;
00138 par_useEndcap = rc_ints[2]&1;
00139 int par_adcThr = rc_ints[3];
00140 par_minPhiBinDiff=rc_ints[4];
00141
00142 par_oneJetThr = rc_floats[0];
00143 par_diJetThrHigh= rc_floats[1];
00144 par_diJetThrLow = rc_floats[2];
00145 par_rndAccProb = rc_floats[3];
00146 par_dbg =(int)rc_floats[4];
00147
00148
00149 par_energyScale=par_maxADC*par_IgainCorrOne/par_maxEt;
00150
00151 float monTwEtThr=2.0;
00152 par_hotTwEtThr= (int)(monTwEtThr*par_energyScale);
00153
00154 par_rndAccThr= int(par_rndAccProb* RAND_MAX);
00155 if(par_rndAccProb<0) {
00156 par_rndAccThr=0;
00157 par_rndAccProb=0.;
00158 } else if (par_rndAccProb>0.9999) {
00159 par_rndAccThr=RAND_MAX;
00160 par_rndAccProb=1.0;
00161 }
00162 if (mLogFile) {
00163 fprintf(mLogFile,"L2jet algorithm initRun(%d), compiled: %s , %s\n params:\n",run_number,__DATE__,__TIME__);
00164 fprintf(mLogFile," - use BTOW: East=%d West=%d, Endcap=%d L2ResOffset=%d\n", par_useBtowEast, par_useBtowWest,par_useEndcap ,mResultOffset);
00165 fprintf(mLogFile," - threshold: ADC-ped> %d \n", par_adcThr);
00166 fprintf(mLogFile," - min phi opening angle Jet1<->Jet2: %d in L2phiBins\n",par_minPhiBinDiff);
00167 fprintf(mLogFile," - diJet Et thrHigh= %.2f (GeV) thrLow= %.2f (GeV)\n", par_diJetThrHigh, par_diJetThrLow);
00168 fprintf(mLogFile," - oneJet Et thr = %.2f (GeV) ; rndAccProb=%f; cutTag=%d \n",par_oneJetThr,par_rndAccProb,par_cutTag);
00169 fprintf(mLogFile," - debug=%d, hot tower threshold: Et> %.1f GeV ( only monitoring)\n",par_dbg, monTwEtThr);
00170 }
00171
00172
00173 int kBad=0;
00174 kBad+=0x0001 * ( !par_useBtowEast & !par_useBtowWest & !par_useEndcap);
00175 kBad+=0x0002 * (par_adcThr<par_pedOff);
00176 kBad+=0x0004 * (par_adcThr>16);
00177 kBad+=0x0008 * (par_minPhiBinDiff<5);
00178 kBad+=0x0010 * (par_minPhiBinDiff>=15);
00179 kBad+=0x0020 * (par_oneJetThr<3.);
00180 kBad+=0x0040 * (par_oneJetThr>12.);
00181 kBad+=0x0080 * (par_diJetThrLow<2.9);
00182 kBad+=0x0100 * (par_diJetThrHigh<par_diJetThrLow);
00183 kBad+=0x0200 * (par_diJetThrHigh>12.);
00184 kBad+=0x0400 * (par_cutTag<=0 || par_cutTag>255);
00185 kBad+=0x0800 * (par_rndAccProb<0. || par_rndAccProb>1.);
00186 if (mLogFile) {
00187 fprintf(mLogFile,"L2jet initRun() params checked for consistency, Error flag=0x%04x\n",kBad);
00188 if(kBad) fprintf(mLogFile,"L2jet initRun() ABORT\n");
00189 }
00190
00191 if(kBad) {
00192 run_number=-66;
00193 if (mLogFile) {
00194 fprintf(mLogFile,"L2jet algorithm init: crashB in internal logic\n");
00195 fclose(mLogFile);
00196 return kBad;
00197 }
00198 }
00199
00200 char tit[100];
00201 sprintf(tit,"# BTOW towers>ped+%d (input); x: # of towers/event",par_adcThr);
00202 hA[47]->setTitle(tit);
00203
00204 sprintf(tit,"# ETOW towers>ped+%d (input); x: # of towers/event",par_adcThr);
00205 hA[48]->setTitle(tit);
00206
00207
00208
00209
00210 const float edgeEtaBinEtow[] = {
00211 2.0 ,
00212 1.9008 , 1.8065 , 1.7168 , 1.6317 , 1.5507 , 1.4738 ,
00213 1.4007 , 1.3312 , 1.2651 , 1.2023 , 1.1427 , 1.086 ,
00214 0.0
00215 };
00216
00217 const int mxEtaBinsE=12,mxEtaBinsB=40;
00218 float idealGainEtow[mxEtaBinsE], idealGainBtow[mxEtaBinsB];
00219 float coshEtow[mxEtaBinsE],coshBtow[mxEtaBinsB];
00220
00221 for(i=0;i<mxEtaBinsE;i++ ){
00222 float avrEta=(edgeEtaBinEtow[i]+edgeEtaBinEtow[i+1])/2.;
00223 coshEtow[i]=cosh(avrEta);
00224 idealGainEtow[i]=par_maxADC/par_maxEt/coshEtow[i];
00225
00226 }
00227
00228
00229 for(i=0;i<mxEtaBinsB;i++ ){
00230 float avrEta=-0.975 +i*0.05;
00231 coshBtow[i]=cosh(avrEta);
00232 idealGainBtow[i]=par_maxADC/par_maxEt/coshBtow[i];
00233
00234 }
00235
00236
00237
00238
00239 int etowEtaBin2Patch[mxEtaBinsE]={14,14,13,13,12,12,11,11,11,10,10,10};
00240
00241 int nB=0, nE=0;
00242 int nBg=0, nEg=0;
00243
00244 for(i=0; i<EmcDbIndexMax; i++) {
00245 const L2EmcDb::EmcCDbItem *x=mDb->getByIndex(i);
00246 if(mDb->isEmpty(x)) continue;
00247 if(x->fail) continue;
00248 if(x->gain<=0) continue;
00249
00250
00251
00252 int ietaP, iphiP;
00253 if (mDb->isBTOW(x) ) {
00254
00255 nB++;
00256 if(x->eta<0 || x->eta>mxEtaBinsB) goto crashIt_1;
00257 if(!par_useBtowEast && x->eta<=20) continue;
00258 if(!par_useBtowWest && x->eta>=21) continue;
00259 ietaP= (x->eta-1)/4;
00260 int iphiTw=(x->sec-1)*10 + x->sub-'a';
00261
00262 iphiTw--;
00263 if(iphiTw<0) iphiTw=119;
00264
00265 iphiP= iphiTw/4 ;
00266
00267
00268 int IgainCor=int(par_IgainCorrOne*idealGainBtow[x->eta-1]/x->gain);
00269
00270
00271 if(IgainCor <par_IgainCorrMin) continue;
00272 if(IgainCor >par_IgainCorrMax) continue;
00273 db_btowGainCorr[x->rdo]=IgainCor;
00274
00275 db_btowL2PhiBin[x->rdo]=iphiP;
00276 db_btowL2PatchBin[x->rdo]=ietaP+ iphiP*cl2jetMaxEtaBins;
00277 db_btowThr[x->rdo]=(int) (x->ped+par_adcThr);
00278 db_btowPedS[x->rdo]=(unsigned short) (par_pedOff-x->ped);
00279 nBg++;
00280 } else if(mDb->isETOW(x) && par_useEndcap) {
00281
00282 nE++;
00283 int iphiTw= (x->sec-1)*5 + x->sub-'A';
00284
00285 iphiTw--;
00286 if(iphiTw<0) iphiTw=59;
00287
00288 iphiP= iphiTw/2 ;
00289 if(x->eta<0 || x->eta>mxEtaBinsE) goto crashIt_1;
00290 ietaP=etowEtaBin2Patch[x->eta-1];
00291
00292 int IgainCor=int(par_IgainCorrOne*idealGainEtow[x->eta-1]/x->gain);
00293
00294
00295 if(IgainCor <par_IgainCorrMin) continue;
00296 if(IgainCor >par_IgainCorrMax) continue;
00297 db_etowGainCorr[x->rdo]=IgainCor;
00298
00299 db_etowL2PhiBin[x->rdo]=iphiP;
00300 db_etowL2PatchBin[x->rdo]=ietaP+ iphiP*cl2jetMaxEtaBins;
00301 db_etowThr[x->rdo]=(int) (x->ped+par_adcThr);
00302 db_etowPedS[x->rdo]=(unsigned short) (par_pedOff-x->ped);
00303 nEg++;
00304 }
00305
00306 }
00307
00308 if (mLogFile) {
00309 fprintf(mLogFile,"L2jet algorithm: found working/calibrated: %d/%d=ETOW & %d/%d=BTOW, based on ASCII DB\n",nE,nEg,nB,nBg);
00310 }
00311
00312 return 0;
00313
00314 crashIt_1:
00315 run_number=-55;
00316 if (mLogFile) {
00317 fprintf(mLogFile,"L2jet algorithm init: crashC in internal logic\n");
00318 fclose(mLogFile);
00319 }
00320 return -6;
00321
00322 }
00323
00324
00325
00326
00327 bool
00328 L2jetAlgo2006::doEvent(int L0trg, int inpEveId, TrgDataType* trgData,
00329 int bemcIn, ushort *bemcData,
00330 int eemcIn, ushort *eemcData){
00331
00332 rdtscl_macro(mEveTimeStart);
00333
00334 if(L0trg==1) hA[10]->fill(1);
00335 else if(L0trg==2) hA[10]->fill(2);
00336
00337 if(eve_ID!=inpEveId) {
00338
00339
00340
00341
00342
00343
00344
00345 eve_ID=inpEveId;
00346 mEventsInRun++;
00347 clearEvent();
00348 int runTimeSec=time(0)- run_startUnix;
00349 hA[10]->fill(0);
00350 hA[12]->fill(runTimeSec);
00351
00352 if(par_dbg>1) printf("\n......... in L2Jet_doEvent(ID=%d)... bIn=%d eIn=%d\n",eve_ID,bemcIn,eemcIn);
00353
00354 if (bemcIn || eemcIn){
00355
00356 eve_TrigData=(TrgDataType* )trgData;
00357
00358
00359
00360
00361 int nBtowTw=0, nEtowTw=0;
00362
00363 if(bemcIn==1 && (par_useBtowEast||par_useBtowWest) ) {
00364 nBtowTw=projectAdc( bemcData, MaxBtowRdo,
00365 db_btowThr, db_btowPedS, db_btowGainCorr,
00366 db_btowL2PhiBin, db_btowL2PatchBin,
00367 hA[20] );
00368 }
00369
00370
00371 if(eemcIn==1 && par_useEndcap ) {
00372 nEtowTw=projectAdc( eemcData, 720,
00373 db_etowThr, db_etowPedS, db_etowGainCorr,
00374 db_etowL2PhiBin, db_etowL2PatchBin,
00375 hA[30] );
00376 }
00377
00378
00379
00380 int itotEne=scanPhi();
00381 float totEneGeV=itotEne/par_energyScale;
00382 int itotEneGeV=(int)totEneGeV;
00383
00384
00385 int iJ;
00386 for(iJ=0; iJ< mxJ; iJ++) {
00387 scanEta(iJ);
00388 weightedPhi(iJ);
00389
00390 L2Jet *J=eve_Jet[iJ];
00391 J->eneGeV=J->iene/par_energyScale;
00392 J->phiRad=0.21*(6.0-J->fphiBin);
00393
00394
00395 while(J->phiRad<0) J->phiRad+=6.2832;
00396 while(J->phiRad>6.2832) J->phiRad-=6.2832;
00397 }
00398
00399 if(eve_Jet[0]->eneGeV <eve_Jet[1]->eneGeV) {
00400 L2Jet *Jx=eve_Jet[0];
00401 eve_Jet[0]=eve_Jet[1];
00402 eve_Jet[1]=Jx;
00403 }
00404
00405 if(par_dbg>2) printf("doEvent iphiBin1=%d iene1=%d , iphiBin2=%d iene2=%d\n",eve_Jet[0]->iphiBin,eve_Jet[0]->iene,eve_Jet[1]->iphiBin,eve_Jet[1]->iene);
00406
00407
00408 bool acceptDiJet=( eve_Jet[0]->eneGeV > par_diJetThrHigh) && ( eve_Jet[1]->eneGeV > par_diJetThrLow);
00409 bool acceptOneJet=( eve_Jet[0]->eneGeV> par_oneJetThr) ;
00410
00411 bool acceptRnd=rand()< par_rndAccThr;
00412 mAccept=acceptDiJet || acceptOneJet || acceptRnd;
00413
00414
00415
00416
00417 int iet1 =(int)eve_Jet[0]->eneGeV;
00418 int iet2 =(int)eve_Jet[1]->eneGeV;
00419 int ieta1=(int)eve_Jet[0]->fetaBin;
00420 int ieta2=(int)eve_Jet[1]->fetaBin;
00421 int iphi1=(int)eve_Jet[0]->fphiBin;
00422 int iphi2=(int)eve_Jet[1]->fphiBin;
00423
00424 hA[40]->fill(iet1,iet2);
00425
00426 hA[41]->fill(ieta1,iphi1);
00427 hA[42]->fill(ieta2,iphi2);
00428 hA[43]->fill(iphi1,iphi2);
00429 hA[44]->fill(iet1);
00430 hA[45]->fill(iet2);
00431 hA[46]->fill(itotEneGeV);
00432 hA[47]->fill(nBtowTw);
00433 hA[48]->fill(nEtowTw);
00434
00435
00436 int kphi1=int(eve_Jet[0]->phiRad*10.);
00437 int kphi2=int(eve_Jet[1]->phiRad*10.);
00438 int idelZeta=map_DelPhiJets[kphi1*MxPhiRad10 + kphi2];
00439
00440 if( mAccept) hA[10]->fill(8);
00441
00442 if(acceptOneJet ){
00443 hA[10]->fill(4);
00444 run_nEventOneJet++;
00445 hA[13]->fill(runTimeSec);
00446 hA[50]->fill(iet1);
00447 hA[51]->fill(ieta1,iphi1);
00448 hA[52]->fill(ieta1);
00449 hA[53]->fill(iphi1);
00450 }
00451
00452 if(acceptDiJet ){
00453 hA[10]->fill(5);
00454 run_nEventDiJet++;
00455 hA[14]->fill(runTimeSec);
00456 hA[60]->fill(iet1,iet2);
00457 hA[61]->fill(ieta1,iphi1);
00458 hA[62]->fill(ieta2,iphi2);
00459 hA[63]->fill(iphi1,iphi2);
00460 hA[64]->fill(iet1);
00461 hA[65]->fill(iet2);
00462 hA[66]->fill(ieta1);
00463 hA[67]->fill(ieta2);
00464 hA[68]->fill(iphi1);
00465 hA[69]->fill(iphi2);
00466 hA[70]->fill(idelZeta);
00467 hA[71]->fill(ieta1,idelZeta);
00468 hA[72]->fill(ieta1,ieta2);
00469 hA[73]->fill((iphi1+iphi2)/2,idelZeta);
00470 hA[74]->fill(itotEneGeV);
00471 }
00472 if(acceptRnd ){
00473 hA[10]->fill(6);
00474 run_nEventRnd++;
00475 hA[15]->fill(runTimeSec);
00476 }
00477
00478
00479
00480 L2jetResults2006 out;
00481 memset(&out,0,sizeof(out));
00482
00483 out.int0.version=L2JET_RESULTS_VERSION;
00484 out.int0.decision=
00485 ( par_useBtowEast <<0 ) +
00486 ( par_useBtowWest <<1 ) +
00487 ( par_useEndcap <<2 ) +
00488 ( (bemcIn>0) <<3 ) +
00489 ( (eemcIn>0) <<4 ) +
00490 ( acceptRnd <<5 ) +
00491 ( acceptOneJet <<6 ) +
00492 ( acceptDiJet <<7 ) ;
00493 out.int0.cutTag=par_cutTag;
00494
00495 out.int1.iTotEne=(unsigned short)(totEneGeV*100.);
00496 out.int2.nBtowTw=nBtowTw;
00497 out.int2.nEtowTw=nEtowTw;
00498
00499 out.jet1.jPhi=(int)(eve_Jet[0]->phiRad*28.65);
00500 out.jet1.jEta=(int)(eve_Jet[0]->fetaBin*10.);
00501 out.jet1.iEne=(unsigned short)(eve_Jet[0]->eneGeV*100.);
00502
00503 out.jet2.jPhi=(int)(eve_Jet[1]->phiRad*28.65);
00504 out.jet2.jEta=(int)(eve_Jet[1]->fetaBin*10.);
00505 out.jet2.iEne=(unsigned short)(eve_Jet[1]->eneGeV*100.);
00506
00507 rdtscl_macro(mEveTimeStop);
00508 mEveTimeDiff=mEveTimeStop-mEveTimeStart;
00509 int kTick=mEveTimeDiff/1000;
00510
00511 mhT->fill(kTick);
00512
00513 out.int0.kTick= kTick>255 ? 255 : kTick;
00514
00515
00516 out.int1.checkSum=-L2jetResults2006_doCheckSum(&out);
00517
00518
00519
00520 uint *outPlace=eve_TrigData->TrgSum.L2Result+mResultOffset;
00521 memcpy(outPlace,&out,sizeof( L2jetResults2006));
00522
00523
00524
00525 if(par_dbg){
00526 L2jetResults2006_print(&out);
00527 printf(" phiRad1=%f phiRad2=%f \n",eve_Jet[0]->phiRad,eve_Jet[1]->phiRad);
00528 printf("idelZeta=%d delZeta/deg=%.1f \n\n",idelZeta,idelZeta/31.416*180);
00529
00530
00531
00532 if( out.jet1.iEne+out.jet2.iEne > out.int1.iTotEne) {
00533 printf("L2jet-fatal error, eve=%d, iEtot=%d < iEJ1=%d + iEJ2=%d, continue\n",inpEveId, out.int1.iTotEne,out.jet1.iEne,out.jet2.iEne);
00534 }
00535 if(iphi1==iphi2) {
00536 printf("L2jet-fatal error,neveId=%d, phi1,2=%d,%d\n",mEventsInRun,iphi1,iphi2);
00537 dumpPatchEneA();
00538 }
00539
00540 if( L2jetResults2006_doCheckSum(&out)) {
00541 printf("L2jet-fatal error, wrong cSum=%d\n", L2jetResults2006_doCheckSum(&out));
00542 L2jetResults2006_print(&out);
00543 }
00544 }
00545 }
00546 }
00547
00548
00549 return mAccept;
00550 }
00551
00552
00553
00554
00555 void
00556 L2jetAlgo2006::finishRun() {
00557 if(run_number<0) return;
00558
00559 char Fname[1000];
00560 sprintf(Fname,"%s/run%d.l2jet.hist.bin",mOutDir,run_number);
00561 printf("L2jet::finishRun('%s') , save histo ...\n",Fname);
00562 mHistFile = fopen(Fname,"w");
00563
00564 if (mLogFile) {
00565 fprintf(mLogFile,"L2-jet algorithm finishRun(%d)\n",run_number);
00566 fprintf(mLogFile," - %d events seen by L2 di-jet\n",mEventsInRun);
00567 fprintf(mLogFile," - accepted: rnd=%d oneJet=%d diJet=%d \n", run_nEventRnd, run_nEventOneJet, run_nEventDiJet);
00568
00569
00570
00571 hA[10]->printCSV(mLogFile);
00572
00573 }
00574 finishRunHisto();
00575
00576 if( mHistFile==0) {
00577 printf(" L2jetAlgo2006: finishRun() UNABLE to open run summary log file, continue anyhow\n");
00578 if (mLogFile)
00579 fprintf(mLogFile,"L2 di-jet histos NOT saved, I/O error\n");
00580 } else {
00581 int j;
00582 int nh=0;
00583 for(j=0;j<mxHA;j++) {
00584 if(hA[j]==0) continue;
00585 hA[j]->write(mHistFile);
00586 nh++;
00587 }
00588 finishCommonHistos();
00589 fclose(mHistFile);
00590 mHistFile=0;
00591 if (mLogFile)
00592 fprintf(mLogFile,"L2 di-jet: %d histos saved to '%s'\n",nh,Fname);
00593 }
00594
00595 run_number=-2;
00596
00597
00598 if (mLogFile && mLogFile!=stdout) {
00599 fclose(mLogFile);
00600 mLogFile=0;
00601 }
00602
00603 }
00604
00605
00606
00607
00608 void
00609 L2jetAlgo2006::createHisto() {
00610 memset(hA,0,sizeof(hA));
00611
00612 hA[10]=new L2Histo(10, (char*)"total event counter; x=cases",9);
00613 hA[11]=new L2Histo(11, (char*)"L2 time used per input event; x: time (CPU kTics), range=100muSec; y: events ",160);
00614
00615 int mxRunDration=2500;
00616 hA[12]=new L2Histo(12, (char*)"rate of input events; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
00617
00618 hA[13]=new L2Histo(13, (char*)"rate of accepted one-Jet; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
00619 hA[14]=new L2Histo(14, (char*)"rate of accepted di-Jet ; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
00620 hA[15]=new L2Histo(15, (char*)"rate of random accepted ; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
00621
00622
00623 hA[20]=new L2Histo(20, (char*)"BTOW tower, Et>2.0 GeV (input); x: BTOW RDO index=chan*30+fiber; y: counts", 4800);
00624 hA[21]=new L2Histo(21, (char*)"BTOW tower, Et>2.0 GeV (input); x: BTOW softID", 4800);
00625 hA[22]=new L2Histo(22, (char*)"BTOW tower, Et>2.0 GeV (input); x: eta bin, [-1,+1]; y: phi bin ~sector",40,120);
00626
00627
00628 hA[30]=new L2Histo(30, (char*)"ETOW tower, Et>2.0 GeV (input); x: ETOW RDO index=chan*6+fiber; y: counts", 720 );
00629 hA[31]=new L2Histo(31, (char*)"ETOW tower, Et>2.0 GeV (input); x: i=chan+128*crate", 768);
00630 hA[32]=new L2Histo(32, (char*)"ETOW tower, Et>2.0 GeV (input); x: 12 - Endcap etaBin ,[+1,+2]; y: phi bin ~sector",12,60);
00631
00632
00633 hA[40]=new L2Histo(40, (char*)"Et Jet1-Jet2 (input); x: Jet1 Et/GeV ; Jet2 Et/GeV",12,12);
00634 hA[41]=new L2Histo(41, (char*)"diJet1 eta-phi (input); x: iEta [-1,+2] ; y: iPhi ~sector ",15,30);
00635 hA[42]=new L2Histo(42, (char*)"diJet2 eta-phi (input); x: iEta [-1,+2] ; y: iPhi ~sector",15,30);
00636
00637 hA[43]=new L2Histo(43, (char*)"diJet phi1-phi2 (input); x: iPhi1 ~sector ; y: iPhi2 ~sector ",30,30);
00638
00639 hA[44]=new L2Histo(44, (char*)"Jet1 Et (input); x: Et (GeV)", 60);
00640 hA[45]=new L2Histo(45, (char*)"Jet2 Et (input); x: Et (GeV)", 60);
00641 hA[46]=new L2Histo(46, (char*)"total Et (input); x: Et (GeV)", 60);
00642 hA[47]=new L2Histo(47, (char*)"# BTOW towers>thrXX (input); x: # of towers/event", 200);
00643 hA[48]=new L2Histo(48, (char*)"# ETOW towers>thrXX (input); x: # of towers/event", 100);
00644
00645
00646 hA[50]=new L2Histo(50, (char*)"one-Jet Et (accepted); x: jet Et (GeV)", 60);
00647 hA[51]=new L2Histo(51, (char*)"one-Jet eta-phi (accepted); x: iEta [-1,+2] ; y: iPhi ~sector ",15,30);
00648 hA[52]=new L2Histo(52, (char*)"one-Jet eta (accepted); x: iEta [-1,+2]", 15);
00649 hA[53]=new L2Histo(53, (char*)"one-Jet phi (accepted); x: iPhi ~sector", 30);
00650
00651
00652 hA[60]=new L2Histo(60, (char*)"Et of Jet1 vs. Jet2 (accepted); x: Jet1/GeV ; Jet2/GeV",12,12);
00653 hA[61]=new L2Histo(61, (char*)"diJet1 eta-phi (accepted); x: iEta [-1,+2] ; y: iPhi ~sector ",15,30);
00654 hA[62]=new L2Histo(62, (char*)"diJet2 eta-phi (accepted); x: iEta [-1,+2] ; y: iPhi ~sector",15,30);
00655
00656 hA[63]=new L2Histo(63, (char*)"diJet phi1-phi2 (accepted); x: iPhi1 ~sector ; y: iPhi2 ~sector ",30,30);
00657
00658 hA[64]=new L2Histo(64, (char*)"diJet1 Et (accepted); x: Et (GeV)", 60);
00659 hA[65]=new L2Histo(65, (char*)"diJet2 Et (accepted); x: Et (GeV)", 60);
00660
00661 hA[66]=new L2Histo(66, (char*)"diJet1 eta (accepted); x: i Eta [-1,+2]", 15);
00662 hA[67]=new L2Histo(67, (char*)"diJet2 eta (accepted); x: i Eta [-1,+2]", 15);
00663 hA[68]=new L2Histo(68, (char*)"diJet1 phi (accepted); x: iPhi ~sector", 30);
00664 hA[69]=new L2Histo(69, (char*)"diJet2 phi (accepted); x: iPhi ~sector", 30);
00665 hA[70]=new L2Histo(70, (char*)"diJet delZeta (accepted); x: delta zeta (rad*10)", MxPhiRad10);
00666 hA[71]=new L2Histo(71, (char*)"diJet delZeta vs. eta1 (accepted); x: iEta1 [-1,+2] ; y: delta zeta (rad*10)",15, MxPhiRad10);
00667 hA[72]=new L2Histo(72, (char*)"diJet eta2 vs. eta1 (accepted); x: iEta1 [-1,+2] ;x: iEta2 [-1,+2] ",15,15);
00668 hA[73]=new L2Histo(73, (char*)"diJet delZeta vs. avrPhi (accepted); x: (iphi1+iphi2)/2 (12 deg/bin); y: delta zeta (rad*10)",30, MxPhiRad10);
00669 hA[74]=new L2Histo(74, (char*)"total Et diJet (accepted); x: Et (GeV)", 60);
00670
00671 }
00672
00673
00674
00675 void
00676 L2jetAlgo2006::clearEvent(){
00677
00678
00679 eve_TrigData=0;
00680 mAccept=false;
00681 mEveTimeDiff=0;
00682 memset(eve_patchEne,0,sizeof(eve_patchEne));
00683 memset(eve_phiEne,0,sizeof(eve_phiEne));
00684 eve_Jet[0]->clear();
00685 eve_Jet[1]->clear();
00686 }
00687
00688
00689
00690
00691
00692 int
00693 L2jetAlgo2006::projectAdc( ushort *rawAdc, int nRdo,
00694 ushort *thr, ushort *pedS, ushort *gainCorr,
00695 ushort *phiBin, ushort *patchBin,
00696 L2Histo *hHot ){
00697
00698 int tmpNused=0;
00699
00700 short rdo;
00701 int adc,adc4;
00702 for(rdo=0; rdo<nRdo; rdo++){
00703 if(rawAdc[rdo]<thr[rdo])continue;
00704
00705 adc=(rawAdc[rdo]+pedS[rdo]) & par_adcMask ;
00706 adc4=adc*gainCorr[rdo];
00707 eve_patchEne[patchBin[rdo]]+=adc4;
00708 eve_phiEne[phiBin[rdo]]+=adc4;
00709 tmpNused++;
00710
00711
00712 if(adc4 >par_hotTwEtThr) hHot->fill(rdo);
00713 }
00714
00715 return tmpNused;
00716 }
00717
00718
00719
00720 int
00721 L2jetAlgo2006::scanPhi(){
00722
00723
00724
00725
00726
00727
00728
00729 int phiEneSum[cl2jetMaxPhiBins];
00730 memset(phiEneSum,0,sizeof(phiEneSum));
00731
00732
00733 eve_phiEne[cl2jetMaxPhiBins+0]=eve_phiEne[0];
00734 eve_phiEne[cl2jetMaxPhiBins+1]=eve_phiEne[1];
00735
00736 int i;
00737 int sum;
00738 int sumMax1=0, iMax1=-1;
00739 int sumTot=0;
00740
00741 int *phiEneA=eve_phiEne;
00742 for(i=0;i<cl2jetMaxPhiBins;i++,phiEneA++){
00743 sumTot+=phiEneA[0];
00744 sum=phiEneA[0]+phiEneA[1]+phiEneA[2];
00745 phiEneSum[i]=sum;
00746 if(sumMax1>sum) continue;
00747 sumMax1=sum;
00748 iMax1=i;
00749 }
00750
00751 if(par_dbg>2) printf("phiScan: sum1=%d, iphi1=%d\n",sumMax1,iMax1);
00752
00753
00754 int sumMax2=0, iMax2=-1;
00755 char doWrap=0;
00756 int k1=iMax1-par_minPhiBinDiff;
00757 int k2=iMax1+par_minPhiBinDiff;
00758 if (k1<0) { k1+=cl2jetMaxPhiBins; doWrap+=1; }
00759 if (k2>=cl2jetMaxPhiBins) { k2-=cl2jetMaxPhiBins; doWrap+=2; }
00760
00761 if (!doWrap) {
00762 for(i=0;i<cl2jetMaxPhiBins;i++){
00763
00764 if(i>=k1 && i<=k2) continue;
00765 if(sumMax2>phiEneSum[i]) continue;
00766 sumMax2=phiEneSum[i];
00767 iMax2=i;
00768 }
00769 } else {
00770 for(i=k2;i<k1;i++){
00771
00772 if(sumMax2>phiEneSum[i]) continue;
00773 sumMax2=phiEneSum[i];
00774 iMax2=i;
00775 }
00776 }
00777 if(par_dbg>2) printf("phiScan: sum2=%d, iphi2=%d\n",sumMax2,iMax2);
00778
00779
00780 eve_Jet[0]->iphiBin=iMax1;
00781 eve_Jet[1]->iphiBin=iMax2;
00782
00783 return sumTot;
00784
00785 }
00786
00787
00788
00789
00790
00791 void
00792 L2jetAlgo2006::scanEta(int iJ){
00793 L2Jet *J=eve_Jet[iJ];
00794
00795 int iphi0=J->iphiBin;
00796
00797
00798
00799
00800
00801
00802
00803 int eneA[cl2jetMaxEtaBins];
00804 memset(eneA,0,sizeof(eneA));
00805
00806 int sum;
00807 int sumMax=1;
00808 int iMax=0;
00809
00810
00811 int ix,iy;
00812 for(iy=0;iy<cl2jet_par_mxPhiBin;iy++) {
00813 int jy=(iphi0+iy)%cl2jetMaxPhiBins;
00814 int *patchEneA=eve_patchEne+(jy*cl2jetMaxEtaBins);
00815 for(ix=0;ix<cl2jetMaxEtaBins;ix++,patchEneA++){
00816 eneA[ix]+=*patchEneA;
00817 }
00818 }
00819
00820 int *eneAp=eneA;
00821 for(ix=0;ix<cl2jetMaxEtaBins-cl2jet_par_mxEtaBin+1;ix++,eneAp++) {
00822
00823
00824 sum=eneAp[0]+eneAp[1]+eneAp[2];
00825
00826 if(sumMax>sum) continue;
00827 sumMax=sum;
00828 iMax=ix;
00829 }
00830
00831
00832 int sumX=0;
00833 for(ix=iMax;ix<iMax+cl2jet_par_mxEtaBin;ix++){
00834 sumX+=ix*eneA[ix];
00835 }
00836 float fetaBin=0.5+1.*sumX/sumMax;
00837
00838
00839 if(par_dbg>2){
00840 if(par_dbg>3) {
00841 printf("scanEta iphi0=%d\n eta profile:\n L2eta-bin energy\n",iphi0);
00842 for(ix=0;ix<cl2jetMaxEtaBins;ix++){
00843 printf("%d %d\n",ix,eneA[ix]);
00844 }
00845 }
00846 printf("scanEta: sum=%d, ietaLeft=%d\n",sumMax,iMax);
00847 printf("sumX=%d fetaBinmax=%.1f \n",sumX,fetaBin);
00848 }
00849
00850 J->fetaBin=fetaBin;
00851 J->iene=sumMax;
00852 J->ietaBin=iMax;
00853 }
00854
00855
00856
00857 void
00858 L2jetAlgo2006:: dumpPatchEneA(){
00859
00860 int ix,iy;
00861 for(iy=0;iy<cl2jetMaxPhiBins;iy++) {
00862 int *patchEneA=eve_patchEne+(iy*cl2jetMaxEtaBins);
00863
00864 for(ix=0;ix<cl2jetMaxEtaBins;ix++,patchEneA++){
00865 printf(" %6d",*patchEneA);
00866 }
00867 printf(" iPhi=%d\n",iy);
00868 }
00869
00870 }
00871
00872
00873
00874 void
00875 L2jetAlgo2006::weightedPhi(int iJ){
00876 L2Jet *J=eve_Jet[iJ];
00877
00878
00879
00880 if(J->iene<=1) { J->fphiBin=J->iphiBin+.333; return;}
00881
00882 int iphi0=J->iphiBin;
00883 int ieta0=J->ietaBin;
00884
00885
00886
00887
00888
00889
00890 int sum=1, sumY=0;
00891 int sum1;
00892
00893 int ix,iy;
00894
00895 for(iy=iphi0;iy<iphi0+cl2jet_par_mxPhiBin;iy++) {
00896 int jy=iy % cl2jetMaxPhiBins;
00897 int *patchEneA=eve_patchEne+(jy*cl2jetMaxEtaBins);
00898 #if 0
00899 for(ix=0;ix<cl2jetMaxEtaBins;ix++){
00900 printf(" %6d",patchEneA[ix]);
00901 }
00902 printf(" iy=%d \n",iy);
00903 #endif
00904
00905 sum1=0;
00906 for(ix=ieta0;ix<ieta0+cl2jet_par_mxEtaBin;ix++){
00907 sum1+=patchEneA[ix];
00908 }
00909 sum+=sum1;
00910 sumY+=sum1*iy;
00911
00912 }
00913
00914 float fphiBinMax=0.5 + 1.*sumY/sum;
00915 if( fphiBinMax>cl2jetMaxPhiBins) fphiBinMax-=cl2jetMaxPhiBins;
00916 if(par_dbg>2) printf("weightedPhi() sum=%d sumY=%d fphiBin=%.2f\n",sum,sumY, fphiBinMax);
00917 J->fphiBin=fphiBinMax;
00918 }
00919
00920
00921
00922 void
00923 L2jetAlgo2006::finishRunHisto(){
00924
00925
00926 const int *data20=hA[20]->getData();
00927 const int *data30=hA[30]->getData();
00928
00929 int bHotSum=1,bHotId=-1;
00930 int eHotSum=1;
00931
00932 const L2EmcDb::EmcCDbItem *xE=mDb->getByIndex(402), *xB=mDb->getByIndex(402);
00933 int i;
00934 for(i=0; i<EmcDbIndexMax; i++) {
00935 const L2EmcDb::EmcCDbItem *x=mDb->getByIndex(i);
00936 if(mDb->isEmpty(x)) continue;
00937 if (mDb->isBTOW(x) ) {
00938 int softId=atoi(x->tube+2);
00939 int ieta= (x->eta-1);
00940 int iphi= (x->sec-1)*10 + x->sub-'a' ;
00941
00942 hA[21]->fillW(softId,data20[x->rdo]);
00943 hA[22]->fillW(ieta, iphi,data20[x->rdo]);
00944 if(bHotSum<data20[x->rdo]) {
00945 bHotSum=data20[x->rdo];
00946 bHotId=softId;
00947 xB=x;
00948 }
00949 }
00950 else if (mDb->isETOW(x) ) {
00951 int ihard=x->chan+(x->crate-1)*128;
00952 int ieta= 12-x->eta;
00953 int iphi= (x->sec-1)*5 + x->sub-'A' ;
00954 hA[31]->fillW(ihard,data30[x->rdo]);
00955 hA[32]->fillW(ieta, iphi,data30[x->rdo]);
00956 if(eHotSum<data30[x->rdo]) {
00957 eHotSum=data30[x->rdo];
00958 xE=x;
00959 }
00960
00961 }
00962 }
00963 if (mLogFile){
00964 fprintf(mLogFile,"L2jet::finishRun()\n");
00965 fprintf(mLogFile,"#BTOW_hot tower _candidate_ (bHotSum=%d) :, softID %d , crate %d , chan %d , name %s\n",bHotSum,bHotId,xB->crate,xB->chan,xB->name);
00966 fprintf(mLogFile,"#ETOW_hot tower _candidate_ (eHotSum=%d) :, name %s , crate %d , chan %d\n",eHotSum,xE->name,xE->crate,xE->chan);
00967 }
00968
00969
00970 }
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013