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
00021 #include "L2EmcDb2012.h"
00022 #include "L2Histo.h"
00023 #include "L2EmcGeom2012.h"
00024 #endif
00025
00026 #include "L2etowCalAlgo12.h"
00027
00028
00029
00030
00031 L2etowCalAlgo12::L2etowCalAlgo12(const char* name, const char *uid, L2EmcDb2012* db, L2EmcGeom2012 *geoX, char* outDir, int resOff) : L2VirtualAlgo2012( name, uid, db, outDir, false, true, resOff) {
00032
00033
00034
00035
00036 mGeom=geoX;
00037 if (!mGeom)
00038 criticalError("L2etowCalAlgo is broken -- can't find geom.");
00039
00040 setMaxHist(32);
00041 createHisto();
00042
00043
00044 int k;
00045 for(k=0;k<L2eventStream2012::mxToken;k++){
00046 L2EtowCalibData12 & etowCalibData=globL2eventStream2012.etow[k];
00047 etowCalibData.nInputBlock=0;
00048 etowCalibData.hitSize=0;
00049 }
00050 }
00051
00052
00053
00054 int
00055 L2etowCalAlgo12::initRunUser( int runNo, int *rc_ints, float *rc_floats) {
00056
00057 par_adcMask=(unsigned short)(-0x10);
00058 par_pedOff=0x10/2;
00059
00060
00061 par_dbg = rc_ints[0];
00062 par_gainType = rc_ints[1];
00063 par_nSigPed = rc_ints[2];
00064
00065 par_twEneThres = rc_floats[0];
00066 par_hotEtThres = rc_floats[1];;
00067
00068
00069 int kBad=0;
00070 kBad+=0x00001 * (par_gainType<kGainZero || par_gainType>kGainOffline);
00071 kBad+=0x00002 * (par_nSigPed<2 || par_nSigPed>5);
00072 kBad+=0x00004 * (par_twEneThres<0.1 || par_twEneThres>1.5);
00073
00074 if (mLogFile) {
00075 fprintf(mLogFile,"L2%s algorithm initRun(R=%d), compiled: %s , %s\n params:\n",getName(),mRunNumber,__DATE__,__TIME__);
00076 fprintf(mLogFile," - use ETOW=%d, gain Ideal=%d or Offline=%d, debug=%d\n",
00077 par_gainType>=kGainIdeal, par_gainType==kGainIdeal, par_gainType==kGainOffline, par_dbg);
00078 fprintf(mLogFile," - thresholds: ADC-ped> %d*sigPed .AND. energy>%.2f GeV \n", par_nSigPed, par_twEneThres);
00079
00080 fprintf(mLogFile," - hot tower thresholds: ET/GeV=%.2f\n",par_hotEtThres);
00081 fprintf(mLogFile,"initRun() params checked for consistency, Error flag=0x%04x\n",kBad);
00082 }
00083
00084
00085 int k;
00086 for(k=0;k<L2eventStream2012::mxToken;k++){
00087 L2EtowCalibData12 & etowCalibData=globL2eventStream2012.etow[k];
00088 etowCalibData.nInputBlock=0;
00089 etowCalibData.hitSize=0;
00090 }
00091
00092 if(kBad) return kBad;
00093
00094
00095 int i;
00096 for (i=0; i<mxHA;i++) if(hA[i])hA[i]->reset();
00097
00098
00099 char txt[1000];
00100 sprintf(txt,"ETOW tower, E_T>%.2f GeV (input); x: ETOW RDO index=chan*6+fiber; y: counts",par_hotEtThres);
00101 hA[10]->setTitle(txt);
00102
00103 sprintf(txt,"ETOW tower, Et>%.2f GeV (input); x: ETOW softID=i#phi+60*i#eta",par_hotEtThres);
00104 hA[11]->setTitle(txt);
00105 sprintf(txt,"ETOW tower, Et>%.2f GeV (input); x: eta bin, [+2,+1]; y: phi bin ~ TPC sector",par_hotEtThres);
00106 hA[12] ->setTitle(txt);
00107
00108 sprintf(txt,"#ETOW towers / event , Et>%.2f GeV; x: # ETOW towers; y: counts",par_hotEtThres);
00109 hA[14] ->setTitle(txt);
00110
00111
00112 mGeom->etow.clear();
00113 int nT=0;
00114 int nTg=0;
00115 int nEneThr=0, nPedThr=0;
00116 if(par_gainType>=kGainIdeal)
00117 for(i=0; i<EmcDbIndexMax; i++) {
00118 const L2EmcDb2012::EmcCDbItem *x=mDb->getByIndex(i);
00119 if(mDb->isEmpty(x)) continue;
00120
00121 if (!mDb->isETOW(x) ) continue;
00122 if(x->fail) continue;
00123 if(x->gain<=0) continue;
00124 nT++;
00125
00126 float adcThres=x->ped+par_nSigPed* fabs(x->sigPed);
00127 float otherThr=x->ped+par_twEneThres*x->gain;
00128
00129 if(adcThres<otherThr) {
00130 adcThres=otherThr;
00131 nEneThr++;
00132 } else {
00133 nPedThr++;
00134 }
00135
00136
00137 if(x->eta<=0 || x->eta>EtowGeom::mxEtaBin) return -90;
00138 int ietaTw= (x->eta-1);
00139
00140
00141 if (par_gainType!=kGainIdeal) return -102;
00142 mGeom->etow.gain2Ene_rdo[x->rdo]=mGeom->etow.idealGain2Ene[ietaTw];
00143 mGeom->etow.gain2ET_rdo[x->rdo]=mGeom->getIdealAdc2ET();
00144
00145 mGeom->etow.thr_rdo[x->rdo]=(int) (adcThres);
00146 mGeom->etow.ped_rdo[x->rdo]=(int) (x->ped);
00147 mGeom->etow.ped_shifted_rdo[x->rdo]=(unsigned short)(par_pedOff - x->ped);
00148 nTg++;
00149 }
00150
00151 if (mLogFile) {
00152 fprintf(mLogFile," found towers working=%d calibrated=%d, based on ASCII DB\n",nT,nTg);
00153 fprintf(mLogFile," thresh defined by energy=%d or NsigPed=%d \n",nEneThr, nPedThr);
00154 }
00155
00156 return 0;
00157
00158
00159 }
00160
00161
00162
00163 void
00164 L2etowCalAlgo12::calibrateEtow(int token, int eemcIn, ushort *rawAdc){
00165
00166
00167 computeStart();
00168 token&=L2eventStream2012::tokenMask;
00169
00170
00171 L2EtowCalibData12 & etowCalibData=globL2eventStream2012.etow[token];
00172
00173 etowCalibData.nInputBlock++;
00174 etowCalibData.hitSize=0;
00175
00176 int nTower=0;
00177 int nHotTower=0;
00178 if(eemcIn && par_gainType>kGainZero) {
00179
00180 short rdo;
00181 int adc;
00182 int low_noise_adc;
00183 float et;
00184 float low_noise_et;
00185 ushort *thr=mGeom->etow.thr_rdo;
00186 ushort *ped=mGeom->etow.ped_rdo;
00187 ushort *ped_shifted=mGeom->etow.ped_shifted_rdo;
00188 float *gain2ET=mGeom->etow.gain2ET_rdo;
00189 float *gain2Ene=mGeom->etow.gain2Ene_rdo;
00190 HitTower1 *hit=etowCalibData.hit;
00191 for(rdo=0; rdo<EtowGeom::mxRdo; rdo++){
00192 if(rawAdc[rdo]<thr[rdo])continue;
00193 if(nTower>=L2EtowCalibData12::mxListSize) break;
00194 adc=rawAdc[rdo]-ped[rdo];
00195 et=adc/gain2ET[rdo];
00196 low_noise_adc=(rawAdc[rdo]+ped_shifted[rdo]) & par_adcMask;
00197 low_noise_et=low_noise_adc/gain2ET[rdo];
00198 hit->rdo=rdo;
00199 hit->adc=adc;
00200 hit->et=et;
00201 hit->low_noise_et=low_noise_et;
00202 hit->ene=adc/gain2Ene[rdo];
00203 hit++;
00204 nTower++;
00205
00206 if(et >par_hotEtThres) {
00207 hA[10]->fill(rdo);
00208 nHotTower++;
00209 }
00210 }
00211 etowCalibData.hitSize=nTower;
00212
00213
00214 hA[13]->fill(nTower);
00215 hA[14]->fill(nHotTower);
00216 if(nTower>=L2EtowCalibData12::mxListSize) mhN->fill(5);
00217 }
00218
00219
00220 if(par_dbg>0){
00221 LOG(DBG,"L2-%s-compute: set adcL size=%d\n",getName(),nTower);
00222 LOG(DBG,"dbg=%s: found nTw=%d\n",getName(),nTower);
00223 if(par_dbg>0) print0();
00224 printCalibratedData(token);
00225 }
00226
00227 computeStop(token);
00228
00229 }
00230
00231
00232
00233 void
00234 L2etowCalAlgo12::clear(int token){
00235 token&=L2eventStream2012::tokenMask;
00236
00237 L2EtowCalibData12 & etowCalibData=globL2eventStream2012.etow[token];
00238 etowCalibData.hitSize=0;
00239 return;
00240 }
00241
00242
00243
00244
00245 void
00246 L2etowCalAlgo12::computeUser(int token ){
00247
00248 LOG(CRIT,"computeUser-%s FATAL CRASH\n If you see this message it means l2new is very badly misconfigured \n and L2-etow-calib algo was not executed properly\n before calling other individual L2-algos. \n\n l2new will aborted now - fix the code, Jan B.\n",getName());
00249 criticalError("L2etowCalAlgo12::computeUser has been called and should not have been. Serious problem in L2");
00250
00251 }
00252
00253
00254
00255
00256 void
00257 L2etowCalAlgo12::finishRunUser() {
00258
00259
00260
00261
00262
00263
00264 int mean=0, RMS=0;
00265 hA[13]->findMean(&mean,&RMS);
00266 if (mLogFile){
00267 fprintf(mLogFile,"#ETOW_nonzero_towers_per_event: mean=%d, rms=%d\n",mean,RMS);
00268 }
00269
00270
00271 int eHotSum=1,eHotId=-1;
00272 const int *data20=hA[10]->getData();
00273 const L2EmcDb2012::EmcCDbItem *xE=0;
00274
00275 int i;
00276 for(i=0; i<EmcDbIndexMax; i++) {
00277 const L2EmcDb2012::EmcCDbItem *x=mDb->getByIndex(i);
00278 if(mDb->isEmpty(x)) continue;
00279 if (!mDb->isETOW(x) ) continue;
00280 int ieta= (x->eta-1);
00281 int iphi= (x->sec-1)*EtowGeom::mxSubs + x->sub-'A' ;
00282 int softId= iphi+EtowGeom::mxPhiBin*ieta;
00283 hA[11]->fillW(softId,data20[x->rdo]);
00284 hA[12]->fillW(ieta, iphi,data20[x->rdo]);
00285 if(eHotSum<data20[x->rdo]) {
00286 eHotSum=data20[x->rdo];
00287 eHotId=softId;
00288 xE=x;
00289 }
00290 }
00291
00292 int par_nHotThresh=20;
00293 if (mLogFile && eHotSum>par_nHotThresh){
00294 fprintf(mLogFile,"#ETOW_hot tower _candidate_ (eHotSum=%d of %d eve) :, softID %d , crate %d , chan %d , name %s\n",eHotSum,mEventsInRun,eHotId,xE->crate,xE->chan,xE->name);
00295 }
00296
00297
00298 int tkn1=99999, tkn2=0;
00299 int nTkn=0;
00300 int tkn3=-1, nTkn3=-1;
00301
00302 int k;
00303 for(k=0;k<L2eventStream2012::mxToken;k++){
00304 L2EtowCalibData12 & etowCalibData=globL2eventStream2012.etow[k];
00305 if(etowCalibData.nInputBlock==0) continue;
00306 hA[1]->fillW(k,etowCalibData.nInputBlock);
00307 if(nTkn3<etowCalibData.nInputBlock){
00308 nTkn3=etowCalibData.nInputBlock;
00309 tkn3=k;
00310 }
00311
00312 nTkn++;
00313 if(tkn1>k) tkn1=k;
00314 if(tkn2<k) tkn2=k;
00315 }
00316 if (mLogFile){
00317 fprintf(mLogFile,"#ETOW_token_QA: _candidate_ hot token=%d used %d for %d events, token range [%d, %d], used %d tokens\n",tkn3,nTkn3,mEventsInRun,tkn1,tkn2,nTkn);
00318 }
00319
00320 }
00321
00322
00323
00324
00325 void
00326 L2etowCalAlgo12::createHisto() {
00327 memset(hA,0,mxHA*sizeof(L2Histo*));
00328
00329 hA[1]=new L2Histo(1,"L2-etow-calib: seen tokens; x: token value; y: events ",L2eventStream2012::mxToken);
00330
00331
00332 hA[10]=new L2Histo(10,"etow hot tower 1", EtowGeom::mxRdo);
00333 hA[11]=new L2Histo(11,"etow hot tower 2", EtowGeom::mxRdo);
00334 hA[12]=new L2Histo(12,"etow hot tower 3", EtowGeom::mxEtaBin,EtowGeom::mxPhiBin);
00335 hA[13]=new L2Histo(13,"ETOW #tower w/ energy /event; x: # ETOW towers; y: counts", 100);
00336 hA[14]=new L2Histo(14,"# hot towers/event", 30);
00337
00338 }
00339
00340
00341
00342
00343
00344 void
00345 L2etowCalAlgo12::print0(){
00346
00347 }
00348
00349
00350
00351
00352
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