16 #ifdef IS_REAL_L2 //in l2-ana environment
17 #include "../L2algoUtil/L2EmcDb.h"
18 #include "../L2algoUtil/L2Histo.h"
22 #include "L2EmcGeom.h"
25 #include "L2btowCalAlgo09.h"
35 L2btowCalAlgo09::L2btowCalAlgo09(
const char* name,
L2EmcDb* db,
L2EmcGeom *geoX,
char* outDir,
int resOff) :
L2VirtualAlgo2009( name, db, outDir, true, false, resOff) {
42 criticalError(
"L2btowCalAlgo is broken -- can't find geom.");
49 for(k=0;k<L2eventStream2009::mxToken;k++){
51 btowCalibData.nInputBlock=0;
52 btowCalibData.hitSize=0;
59 L2btowCalAlgo09::initRunUser(
int runNo,
int *rc_ints,
float *rc_floats) {
61 par_adcMask=(
unsigned short)(-0x10);
66 par_gainType = rc_ints[1];
67 par_nSigPed = rc_ints[2];
69 par_twEneThres = rc_floats[0];
70 par_hotEtThres = rc_floats[1];;
74 kBad+=0x00001 * (par_gainType<kGainZero || par_gainType>kGainOffline);
75 kBad+=0x00002 * (par_nSigPed<2 || par_nSigPed>5);
76 kBad+=0x00004 * (par_twEneThres<0.1 || par_twEneThres>1.5);
79 fprintf(mLogFile,
"L2%s algorithm initRun(R=%d), compiled: %s , %s\n params:\n",getName(),mRunNumber,__DATE__,__TIME__);
80 fprintf(mLogFile,
" - use BTOW=%d, gain Ideal=%d or Offline=%d, debug=%d\n",
81 par_gainType>=kGainIdeal, par_gainType==kGainIdeal, par_gainType==kGainOffline, par_dbg);
82 fprintf(mLogFile,
" - thresholds: ADC-ped> %d*sigPed .AND. energy>%.2f GeV \n", par_nSigPed, par_twEneThres);
84 fprintf(mLogFile,
" - hot tower thresholds: ET/GeV=%.2f\n",par_hotEtThres);
85 fprintf(mLogFile,
"initRun() params checked for consistency, Error flag=0x%04x\n",kBad);
90 for(k=0;k<L2eventStream2009::mxToken;k++){
92 btowCalibData.nInputBlock=0;
93 btowCalibData.hitSize=0;
97 if(kBad)
return -kBad;
101 for (i=0; i<mxHA;i++)
if(hA[i])hA[i]->reset();
105 sprintf(txt,
"BTOW tower, E_T>%.2f GeV (input); x: BTOW RDO index=chan*30+fiber; y: counts",par_hotEtThres);
106 hA[10]->setTitle(txt);
108 sprintf(txt,
"BTOW tower, Et>%.2f GeV (input); x: BTOW softID",par_hotEtThres);
109 hA[11]->setTitle(txt);
110 sprintf(txt,
"BTOW tower, Et>%.2f GeV (input); x: eta bin, [-1,+1]; y: phi bin ~ TPC sector",par_hotEtThres);
111 hA[12] ->setTitle(txt);
113 sprintf(txt,
"#BTOW towers / event , Et>%.2f GeV; x: # BTOW towers; y: counts",par_hotEtThres);
114 hA[14] ->setTitle(txt);
120 int nEneThr=0, nPedThr=0;
121 if(par_gainType>=kGainIdeal)
122 for(i=0; i<EmcDbIndexMax; i++) {
124 if(mDb->isEmpty(x))
continue;
126 if (!mDb->isBTOW(x) )
continue;
127 if(x->fail)
continue;
128 if(x->gain<=0)
continue;
131 float adcThres=x->ped+par_nSigPed* fabs(x->sigPed);
132 float otherThr=x->ped+par_twEneThres*x->gain;
135 if(adcThres<otherThr) {
143 if(x->eta<=0 || x->eta>BtowGeom::mxEtaBin)
return -90;
144 int ietaTw= (x->eta-1);
148 if (par_gainType!=kGainIdeal)
return -102;
149 geom->btow.gain2Ene_rdo[x->rdo]=geom->btow.idealGain2Ene[ietaTw];
150 geom->btow.gain2ET_rdo[x->rdo]=geom->getIdealAdc2ET();
152 geom->btow.thr_rdo[x->rdo]=(int) (adcThres);
153 geom->btow.ped_rdo[x->rdo]=(int) (x->ped);
154 geom->btow.ped_shifted_rdo[x->rdo]=(
unsigned short)(par_pedOff - x->ped);
159 fprintf(mLogFile,
" found towers working=%d calibrated=%d, based on ASCII DB\n",nB,nBg);
160 fprintf(mLogFile,
" thresh defined by energy=%d or NsigPed=%d \n",nEneThr, nPedThr);
171 L2btowCalAlgo09::calibrateBtow(
int token,
int bemcIn,
unsigned short *rawAdc){
174 token&=L2eventStream2009::tokenMask;
178 btowCalibData.nInputBlock++;
181 btowCalibData.hitSize=0;
185 if(bemcIn && par_gainType>kGainZero) {
192 unsigned short *thr=geom->btow.thr_rdo;
193 unsigned short *ped=geom->btow.ped_rdo;
194 unsigned short *ped_shifted=geom->btow.ped_shifted_rdo;
195 float *gain2ET=geom->btow.gain2ET_rdo;
196 float *gain2Ene=geom->btow.gain2Ene_rdo;
198 for(rdo=0; rdo<BtowGeom::mxRdo; rdo++){
199 if(rawAdc[rdo]<thr[rdo])
continue;
200 if(nTower>=L2BtowCalibData09::mxListSize)
break;
201 adc=rawAdc[rdo]-ped[rdo];
203 low_noise_adc=(rawAdc[rdo]+ped_shifted[rdo]) & par_adcMask;
204 low_noise_et=low_noise_adc/gain2ET[rdo];
208 hit->low_noise_et=low_noise_et;
209 hit->ene=adc/gain2Ene[rdo];
213 if(et >par_hotEtThres) {
218 btowCalibData.hitSize=nTower;
221 hA[13]->fill(nTower);
222 hA[14]->fill(nHotTower);
223 if(nTower>=L2BtowCalibData09::mxListSize) mhN->fill(5);
229 printf(
"L2-%s-compute: set adcL size=%d, get=%d\n",getName(),nTower,999);
230 printf(
"dbg=%s: found nTw=%d\n",getName(),nTower);
231 if(par_dbg>0) print0();
232 printCalibratedData(token);
243 L2btowCalAlgo09::clear(
int token){
244 token&=L2eventStream2009::tokenMask;
247 btowCalibData.hitSize=0;
254 L2btowCalAlgo09::computeUser(
int token ){
256 printf(
"computeUser-%s FATAL CRASH\n If you see this message it means l2new is very badly misconfigured \n and L2-btow-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());
257 criticalError(
"L2btowCalAlgo09::computeUser has been called and should not have been. Serious problem in L2");
264 L2btowCalAlgo09::finishRunUser() {
269 hA[13]->findMean(&mean,&RMS);
271 fprintf(mLogFile,
"#BTOW_nonzero_towers_per_event: mean=%d, rms=%d\n",mean,RMS);
274 int bHotSum=1,bHotId=-1;
275 const int *data20=hA[10]->getData();
279 for(i=0; i<EmcDbIndexMax; i++) {
281 if(mDb->isEmpty(x))
continue;
282 if (!mDb->isBTOW(x) )
continue;
283 int softId=atoi(x->tube+2);
284 int ieta= (x->eta-1);
285 int iphi= (x->sec-1)*BtowGeom::mxSubs + x->sub-
'a' ;
286 hA[11]->fillW(softId,data20[x->rdo]);
287 hA[12]->fillW(ieta, iphi,data20[x->rdo]);
288 if(bHotSum<data20[x->rdo]) {
289 bHotSum=data20[x->rdo];
296 fprintf(mLogFile,
"#BTOW_hot tower _candidate_ (bHotSum=%d of %d eve) :, softID %d , crate %d , chan %d , name %s\n",bHotSum,mEventsInRun,bHotId,xB->crate,xB->chan,xB->name);
300 int tkn1=99999, tkn2=0;
302 int tkn3=-1, nTkn3=-1;
305 for(k=0;k<L2eventStream2009::mxToken;k++){
307 if(btowCalibData.nInputBlock==0)
continue;
308 hA[1]->fillW(k,btowCalibData.nInputBlock);
309 if(nTkn3<btowCalibData.nInputBlock){
310 nTkn3=btowCalibData.nInputBlock;
319 fprintf(mLogFile,
"#BTOW_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);
328 L2btowCalAlgo09::createHisto() {
329 memset(hA,0,mxHA*
sizeof(
L2Histo*));
331 hA[1]=
new L2Histo(1,
"L2-btow-calib: seen tokens; x: token value; y: events ",L2eventStream2009::mxToken);
334 hA[10]=
new L2Histo(10,
"btow hot tower 1", BtowGeom::mxRdo);
335 hA[11]=
new L2Histo(11,
"btow hot tower 2", BtowGeom::mxRdo);
336 hA[12]=
new L2Histo(12,
"btow hot tower 3", BtowGeom::mxEtaBin,BtowGeom::mxPhiBin);
337 hA[13]=
new L2Histo(13,
"BTOW #tower w/ energy /event; x: # BTOW towers; y: counts", 200);
338 hA[14]=
new L2Histo(14,
"# hot towers/event", 100);
347 L2btowCalAlgo09::print0(){