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 #ifdef IS_REAL_L2 //in l2-ana environment
00016 #include "../L2algoUtil/L2EmcDb2012.h"
00017 #include "../L2algoUtil/L2Histo.h"
00018 #else //full path needed for cvs'd code
00019 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb2012.h"
00020 #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
00021 #endif
00022
00023 #include "L2hienAlgo12.h"
00024 #include "L2hienResult2012.h"
00025
00026
00027
00028 L2hienAlgo12::L2hienAlgo12(const char* name, const char *uid, L2EmcDb2012* db, L2EmcGeom2012 *geoX, char* outDir, int resOff, L2VirtualAlgo2012::EmcSwitch beSwitch) : L2VirtualAlgo2012( name, uid, db, outDir, (beSwitch==kIsBtow),(beSwitch==kIsEtow), resOff ) {
00029
00030
00031
00032
00033
00034
00035 if(!(beSwitch==kIsBtow || beSwitch==kIsEtow))
00036 {
00037 criticalError("L2hienAlgo called with broken beSwitch. L2hien cannot run.\n");
00038 }
00039
00040 if (beSwitch==kIsBtow)
00041 {
00042 if (sizeof(L2hienResult2012)!= L2hienResult2012::mySizeChar)
00043 criticalError("L2hienAlgo12 (Barrel) has failed consistency check. sizeof(L2hienResult2012)!= L2hienResult2012::mySizeChar");
00044 }
00045 mSwitch=beSwitch;
00046 setMaxHist(16);
00047 createHisto();
00048
00049 }
00050
00051
00052
00053 int
00054 L2hienAlgo12::initRunUser( int runNo, int *rc_ints, float *rc_floats) {
00055
00056
00057 par_dbg = rc_ints[0];
00058 par_adcThres = rc_ints[1];
00059 par_maxList = rc_ints[2];
00060 par_RndAcceptPrescale = 0;
00061
00062
00063 int kBad=0;
00064 kBad+=0x00001 * (par_adcThres<10);
00065
00066
00067 if(par_maxList<10 ) par_maxList=10;
00068 if(par_maxList>L2hienList12::mxListSize ) par_maxList=L2hienList12::mxListSize;
00069
00070 if (mLogFile) {
00071 fprintf(mLogFile,"L2%s algorithm initRun(R=%d), compiled: %s , %s\n params for ",getName(),mRunNumber,__DATE__,__TIME__);
00072 if(mSwitch==kIsBtow) fprintf(mLogFile," BTOW setup:\n");
00073 if(mSwitch==kIsEtow) fprintf(mLogFile," ETOW setup:\n");
00074 fprintf(mLogFile," - use adcThresh =%d, maxList=%d debug=%d\n", par_adcThres , par_maxList, par_dbg);
00075 fprintf(mLogFile,"initRun() params checked for consistency, Error flag=0x%04x\n",kBad);
00076 }
00077 if(kBad) return -kBad;
00078
00079
00080 int i;
00081 for (i=0; i<mxHA;i++) if(hA[i])hA[i]->reset();
00082 memset(mHiEnTw,0,sizeof(mHiEnTw));
00083
00084
00085
00086 char txt[1000];
00087 sprintf(txt,"compute: # towers w/ ADC-ped>%d / event; x: # towers; y: counts",par_adcThres);
00088 hA[2]->setTitle(txt);
00089
00090 sprintf(txt,"accepted: # towers w/ ADC-ped>%d / event; x: # towers; y: counts",par_adcThres);
00091 hA[3]->setTitle(txt);
00092
00093 sprintf(txt,"accepted: # towers w/ ADC-ped>%d vs. eta ring;x: BTOW eta-bin, 0=eta=-1.0; y: counts",par_adcThres*2);
00094 if(mSwitch==kIsEtow)
00095 sprintf(txt,"accepted: # towers w/ ADC-ped>%d vs. eta ring;x: ETOW eta-bin, 0=eta=+2.0; y: counts",par_adcThres*2);
00096 hA[5]->setTitle(txt);
00097
00098 sprintf(txt,"accepted: # towers w/ ADC-ped>%d vs. phi ring;x: phi-bin, 0=12:30, follows TPC; y: counts",par_adcThres*2);
00099 hA[6]->setTitle(txt);
00100
00101 sprintf(txt,"accepted: ADC sum over towers w/ ADC-ped>%d ; ADC sum/16; y: counts",par_adcThres);
00102 hA[7]->setTitle(txt);
00103
00104
00105
00106
00107 memset(mRdo2towerID_B,0,sizeof(mRdo2towerID_B));
00108 memset(mTowerID2etaBin_B,0,sizeof(mTowerID2etaBin_B));
00109 memset(mTowerID2phiBin_B,0,sizeof(mTowerID2phiBin_B));
00110
00111 memset(mRdo2towerID_E,0,sizeof(mRdo2towerID_E));
00112 memset(mTowerID2etaBin_E,0,sizeof(mTowerID2etaBin_E));
00113 memset(mTowerID2phiBin_E,0,sizeof(mTowerID2phiBin_E));
00114
00115 if(mSwitch==kIsBtow)
00116 for ( int index=0; index<EmcDbIndexMax; index++ ) {
00117 const L2EmcDb2012::EmcCDbItem *x = mDb->getByIndex(index);
00118 if ( x==0 ) continue;
00119 if ( !mDb->isBTOW(x) ) continue;
00120 int sec = x->sec - 1;
00121 int sub = 8192;
00122 sub = x->sub - 'a';
00123 int eta = x->eta - 1;
00124 int phi = BtowGeom::mxSubs *sec + sub;
00125 int tow = BtowGeom::mxEtaBin *phi + eta;
00126 int rdo = x->rdo;
00127 if (tow<0 || tow>mxBtow || rdo<0 || rdo>mxBtow) return -101;
00128
00129 mRdo2towerID_B[ rdo ] = tow;
00130 mTowerID2etaBin_B[tow]=eta;
00131 mTowerID2phiBin_B[tow]=phi;
00132 }
00133
00134
00135 if(mSwitch==kIsEtow)
00136 for ( int index=0; index<EmcDbIndexMax; index++ ) {
00137 const L2EmcDb2012::EmcCDbItem *x = mDb->getByIndex(index);
00138 if ( x==0 ) continue;
00139 if ( !mDb->isETOW(x) ) continue;
00140 int sec = x->sec - 1;
00141 int sub = 8192;
00142 sub = x->sub - 'A';
00143 int eta = x->eta - 1;
00144 int phi = EtowGeom::mxSubs *sec + sub;
00145 int tow = EtowGeom::mxEtaBin *phi + eta;
00146 int rdo = x->rdo;
00147 if (tow<0 || tow>mxEtow || rdo<0 || rdo>mxEtow) return -101;
00148
00149 mRdo2towerID_E[ rdo ] = tow;
00150 mTowerID2etaBin_E[tow]=eta;
00151 mTowerID2phiBin_E[tow]=phi;
00152 }
00153
00154 return 0;
00155 }
00156
00157
00158
00159
00160
00161 void
00162 L2hienAlgo12::computeUser(int token){
00163
00164
00165
00166
00167 mHiEnTw[token].size=0;
00168 int i;
00169
00170 const HitTower1 *hit=0;
00171 int hitSize=0;
00172 int *mRdo2towerID=0;
00173
00174 if(mSwitch==kIsBtow) {
00175 hit =mEveStream_btow[token].get_hits();
00176 hitSize=mEveStream_btow[token].get_hitSize();
00177 mRdo2towerID=mRdo2towerID_B;
00178 } else {
00179 hit =mEveStream_etow[token].get_hits();
00180 hitSize=mEveStream_etow[token].get_hitSize();
00181 mRdo2towerID=mRdo2towerID_E;
00182 }
00183
00184
00185
00186 L2hienList12 *hiTwEve=mHiEnTw+token;
00187 unsigned int *value=hiTwEve->value;
00188 unsigned int tempValue=0;
00189 unsigned int totalADC=0;
00190 int resultMaxTowers=0;
00191 int softID=0;
00192 L2hienResult2012 *hienResult=mHienResult+token;
00193 if (mSwitch==kIsBtow)
00194 {
00195 resultMaxTowers=L2hienResult2012::maxTowers;
00196
00197 }
00198
00199 for(i=0;i< hitSize;i++,hit++) {
00200
00201 softID=mRdo2towerID[hit->rdo];
00202 totalADC+=hit->adc;
00203 tempValue=((hit->adc)<<16)+softID;
00204
00205 if ((mSwitch==kIsBtow) && (i<L2hienResult2012::maxTowers))
00206 hienResult->value[i]=tempValue;
00207 if (hiTwEve->size<par_maxList && hit->adc>=par_adcThres)
00208 {
00209
00210 (*value)=tempValue;
00211 hiTwEve->size++;
00212 value++;
00213 }
00214
00215 if(hiTwEve->size>=par_maxList && i>=L2hienResult2012::maxTowers) break;
00216 }
00217
00218 unsigned int nTow;
00219 if (hitSize>255) nTow=255;
00220 else nTow=hitSize;
00221
00222 if (totalADC>16777215) totalADC=16777215;
00223 hienResult->header=(nTow<<24)+totalADC;
00224
00225 hiTwEve->isFresh=L2hienList12::kDataFresh;
00226 hA[2]->fill(hiTwEve->size);
00227 }
00228
00229
00230
00231
00232 bool
00233 L2hienAlgo12::decisionUser(int token, int *myL2Result){
00234
00235
00236
00237
00238
00239
00240 L2hienList12 *hiTwEve=mHiEnTw+token;
00241 L2hienResult2012 *hienResult=mHienResult+token;
00242
00243
00244
00245 hA[3]->fill(hiTwEve->size);
00246 if(hiTwEve->size>= par_maxList) mhN->fill(5);
00247 if(hiTwEve->isFresh>L2hienList12::kDataFresh) mhN->fill(6);
00248 hiTwEve->isFresh++;
00249
00250
00251 int adc4QaThres=(par_adcThres/8);
00252 int * mTowerID2etaBin=0 , *mTowerID2phiBin=0;
00253 if(mSwitch==kIsBtow) {
00254 mTowerID2etaBin=mTowerID2etaBin_B;
00255 mTowerID2phiBin=mTowerID2phiBin_B;
00256 } else {
00257 mTowerID2etaBin=mTowerID2etaBin_E;
00258 mTowerID2phiBin=mTowerID2phiBin_E;
00259 }
00260
00261 unsigned int *value=hiTwEve->value;
00262 int ic;
00263 int adcSum4=0;
00264 for(ic=0;ic<hiTwEve->size;ic++,value++) {
00265 int adc4=(*value)>>(16+4);
00266 int softID=(*value)&0xffff;
00267 adcSum4+=adc4;
00268 hA[4]->fill(adc4);
00269 if(adc4<adc4QaThres) continue;
00270 hA[5]->fill(mTowerID2etaBin[softID]);
00271 hA[6]->fill(mTowerID2phiBin[softID]);
00272 }
00273 if(adcSum4>0) hA[7]->fill(adcSum4);
00274
00275
00276 if(par_dbg>0){
00277 print2(token);
00278
00279 }
00280
00281
00282
00283
00284
00285
00286 if(mSwitch==kIsBtow)
00287 {
00288 memcpy(myL2Result,hienResult,sizeof(L2hienResult2012));
00289 hienResult->header=0;
00290 }
00291
00292
00293
00294 return true;
00295
00296 }
00297
00298
00299
00300
00301 void
00302 L2hienAlgo12::finishRunUser() {
00303
00304
00305 if (mLogFile){
00306 fprintf(mLogFile,"finishRunUser-%s bhla bhla\n",getName());
00307 }
00308
00309 }
00310
00311
00312
00313
00314 void
00315 L2hienAlgo12::createHisto() {
00316 memset(hA,0,sizeof(hA));
00317
00318 hA[2]=new L2Histo(2,"compute: #towers w/ .....energy /event; x: # towers; y: counts", 35);
00319 hA[3]=new L2Histo(3,"decision: #towers w/ .....energy /event; x: # towers; y: counts", 35);
00320
00321 int nEtaBin=BtowGeom::mxEtaBin;
00322 int nPhiBin=BtowGeom::mxPhiBin;
00323 if(mSwitch==kIsEtow) {
00324 nEtaBin=EtowGeom::mxEtaBin;
00325 nPhiBin=EtowGeom::mxPhiBin;
00326 }
00327 hA[4]=new L2Histo(4,"accepted: towers ET (watch units!) ; x: (ADC-ped)/16", 150);
00328 hA[5]=new L2Histo(5,"accepted: #towers w/ ..... vs. eta ring",nEtaBin);
00329 hA[6]=new L2Histo(6,"accepted: #towers w/ .... vs. phi ring",nPhiBin);
00330 hA[7]=new L2Histo(7,"accepted: ADC sum ...",150);
00331
00332 }
00333
00334
00335
00336
00337 void
00338 L2hienAlgo12::print2(int token){
00339 if (!(token>0 && token<L2eventStream2012::mxToken))
00340 {
00341 printf("L2hienAlgo12::print2 called with token out of bounds!\n");
00342 return;
00343 }
00344
00345 int hiSize=getListSize(token);
00346 const unsigned int *value=getListData(token);
00347
00348 printf("pr2-%s: dump %d acceted towers for token=%d\n softID ADC-ped\n",getName(),hiSize, token);
00349 for(int ic=0;ic<hiSize;ic++,value++) {
00350 int adc=(*value)>>16;
00351 int softID=(*value)&0xffff;
00352 printf("%4d %d, \n",softID,adc);
00353 }
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394