StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
L2etowCalAlgo08.cxx
1 #include <stdio.h>
2 #include <assert.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <time.h>
6 #include <math.h>
7 
8 /*********************************************************
9  $Id: L2etowCalAlgo08.cxx,v 1.3 2008/02/01 00:16:40 balewski Exp $
10  \author Jan Balewski, MIT, 2008
11  *****************************************************
12  Descripion:
13  calibrates Endcap towers, result is used by other L2-algos
14  *****************************************************/
15 
16 
17 #ifdef IS_REAL_L2 //in l2-ana environment
18  #include "../L2algoUtil/L2EmcDb.h"
19  #include "../L2algoUtil/L2Histo.h"
20 #else
21  #include "L2EmcDb.h"
22  #include "L2Histo.h"
23  #include "L2EmcGeom.h"
24 #endif
25 
26 #include "L2etowCalAlgo08.h"
27 
28 
29 //=================================================
30 //=================================================
31 L2etowCalAlgo08::L2etowCalAlgo08(const char* name, L2EmcDb* db, L2EmcGeom *geoX, char* outDir) : L2VirtualAlgo2008( name, db, outDir) {
32  /* called once per days
33  all memory allocation must be done here
34  */
35 
36  mGeom=geoX; assert(mGeom);
37 
38  setMaxHist(32);
39  createHisto();
40 
41  // initilalize ETOW-Calibrated-data to zero
42  int k;
43  for(k=0;k<L2eventStream2008::mxToken;k++){
44  L2EtowCalibData08 & etowCalibData=globL2eventStream2008.etow[k];
45  etowCalibData.nInputBlock=0;
46  etowCalibData.hitSize=0;
47  }
48  }
49 
50 /* ========================================
51  ======================================== */
52 int
53 L2etowCalAlgo08::initRunUser( int runNo, int *rc_ints, float *rc_floats) {
54 
55 
56  // unpack params from run control GUI
57  par_dbg = rc_ints[0];
58  par_gainType = rc_ints[1];
59  par_nSigPed = rc_ints[2];
60 
61  par_twEneThres = rc_floats[0];
62  par_hotEtThres = rc_floats[1];;
63 
64  // verify consistency of input params
65  int kBad=0;
66  kBad+=0x00001 * (par_gainType<kGainZero || par_gainType>kGainOffline);
67  kBad+=0x00002 * (par_nSigPed<2 || par_nSigPed>5);
68  kBad+=0x00004 * (par_twEneThres<0.1 || par_twEneThres>1.5);
69 
70  if (mLogFile) {
71  fprintf(mLogFile,"L2%s algorithm initRun(R=%d), compiled: %s , %s\n params:\n",getName(),mRunNumber,__DATE__,__TIME__);
72  fprintf(mLogFile," - use ETOW=%d, gain Ideal=%d or Offline=%d, debug=%d\n",
73  par_gainType>=kGainIdeal, par_gainType==kGainIdeal, par_gainType==kGainOffline, par_dbg);
74  fprintf(mLogFile," - thresholds: ADC-ped> %d*sigPed .AND. energy>%.2f GeV \n", par_nSigPed, par_twEneThres);
75 
76  fprintf(mLogFile," - hot tower thresholds: ET/GeV=%.2f\n",par_hotEtThres);
77  fprintf(mLogFile,"initRun() params checked for consistency, Error flag=0x%04x\n",kBad);
78  }
79 
80  if(kBad) return kBad;
81 
82  // clear content of all histograms
83  int i;
84  for (i=0; i<mxHA;i++) if(hA[i])hA[i]->reset();
85 
86  // upadate title of histos
87  char txt[1000];
88  sprintf(txt,"ETOW tower, E_T>%.2f GeV (input); x: ETOW RDO index=chan*6+fiber; y: counts",par_hotEtThres);
89  hA[10]->setTitle(txt);
90 
91  sprintf(txt,"ETOW tower, Et>%.2f GeV (input); x: ETOW softID=i#phi+60*i#eta",par_hotEtThres);
92  hA[11]->setTitle(txt);
93  sprintf(txt,"ETOW tower, Et>%.2f GeV (input); x: eta bin, [-1,+1]; y: phi bin ~ TPC sector",par_hotEtThres);
94  hA[12] ->setTitle(txt);
95 
96  sprintf(txt,"#ETOW towers / event , Et>%.2f GeV; x: # ETOW towers; y: counts",par_hotEtThres);
97  hA[14] ->setTitle(txt);
98 
99  // re-caluclate geometry properties
100  mGeom->etow.clear();
101  int nT=0; /* counts # of unmasekd towers */
102  int nTg=0; /* counts # of reasonable calibrated towers */
103  int nEneThr=0, nPedThr=0; //ETOW count # of towers above & below threshold
104  if(par_gainType>=kGainIdeal) // this disables the whole loop below
105  for(i=0; i<EmcDbIndexMax; i++) {
106  const L2EmcDb::EmcCDbItem *x=mDb->getByIndex(i);
107  if(mDb->isEmpty(x)) continue; /* dropped not mapped channels */
108  /*....... E N D C A P .................*/
109  if (!mDb->isETOW(x) ) continue; /* drop if not ETOW */
110  if(x->fail) continue; /* dropped masked channels */
111  if(x->gain<=0) continue; /* dropped uncalibrated towers , tmp */
112  nT++;
113 
114  float adcThres=x->ped+par_nSigPed* fabs(x->sigPed);
115  float otherThr=x->ped+par_twEneThres*x->gain;
116 
117  if(adcThres<otherThr) { //use energy threshold if higher
118  adcThres=otherThr;
119  nEneThr++;
120  } else {
121  nPedThr++;
122  }
123 
124  /* use rdo index to match RDO order in the ADC data banks */
125  if(x->eta<=0 || x->eta>EtowGeom::mxEtaBin) return -90;
126  int ietaTw= (x->eta-1); /* correct */
127 
128  // use ideal gains for now, hardcoded
129  assert(par_gainType==kGainIdeal); // offline gains not implemented - should be changed here, Jan
130  mGeom->etow.gain2Ene_rdo[x->rdo]=mGeom->etow.idealGain2Ene[ietaTw];
131  mGeom->etow.gain2ET_rdo[x->rdo]=mGeom->getIdealAdc2ET();
132 
133  mGeom->etow.thr_rdo[x->rdo]=(int) (adcThres);
134  mGeom->etow.ped_rdo[x->rdo]=(int) (x->ped);
135  nTg++;
136  }
137 
138  if (mLogFile) {
139  fprintf(mLogFile," found towers working=%d calibrated=%d, based on ASCII DB\n",nT,nTg);
140  fprintf(mLogFile," thresh defined by energy=%d or NsigPed=%d \n",nEneThr, nPedThr);
141  }
142 
143  return 0; //OK
144 
145 
146 }
147 
148 /* ========================================
149  ======================================== */
150 void
151 L2etowCalAlgo08::calibrateEtow(int token, int eemcIn, unsigned short *rawAdc){
152  // Etow calibration is a special case, must have one exit at the end
153 
154  computeStart();
155  token&=L2eventStream2008::tokenMask; // only to protect against a bad token, Gerard's trick
156 
157  //...... now token is valid ........
158  L2EtowCalibData08 & etowCalibData=globL2eventStream2008.etow[token];
159  // clear data for this token from previous event
160  etowCalibData.nInputBlock++;
161  etowCalibData.hitSize=0;
162 
163  int nTower=0; /* counts mapped & used ADC channels */
164  int nHotTower=0;
165  if(eemcIn && par_gainType>kGainZero) { // EVEVEVEVEVE
166  // ............process this event ...............
167  short rdo;
168  int adc; // pedestal subtracted
169  float et;
170  unsigned short *thr=mGeom->etow.thr_rdo;
171  unsigned short *ped=mGeom->etow.ped_rdo;
172  float *gain2ET=mGeom->etow.gain2ET_rdo;
173  float *gain2Ene=mGeom->etow.gain2Ene_rdo;
174  HitTower1 *hit=etowCalibData.hit;
175  for(rdo=0; rdo<EtowGeom::mxRdo; rdo++){
176  if(rawAdc[rdo]<thr[rdo])continue;
177  if(nTower>=L2EtowCalibData08::mxListSize) break; // overflow protection
178  adc=rawAdc[rdo]-ped[rdo]; //did NOT correct for common pedestal noise - bad for the jet finder
179  et=adc/gain2ET[rdo];
180  hit->rdo=rdo;
181  hit->adc=adc;
182  hit->et=et;
183  hit->ene=adc/gain2Ene[rdo];
184  hit++;
185  nTower++;
186  // only monitoring
187  if(et >par_hotEtThres) {
188  hA[10]->fill(rdo);
189  nHotTower++;
190  }
191  }
192  etowCalibData.hitSize=nTower;
193 
194  // QA histos
195  hA[13]->fill(nTower);
196  hA[14]->fill(nHotTower);
197  if(nTower>=L2EtowCalibData08::mxListSize) mhN->fill(5); // was overflow
198  } // EVEVEVEVEVE
199 
200  // debugging should be off for any time critical computation
201  if(par_dbg>0){
202  printf("L2-%s-compute: set adcL size=%d\n",getName(),nTower);
203  printf("dbg=%s: found nTw=%d\n",getName(),nTower);
204  if(par_dbg>0) print0();
205  printCalibratedData(token);
206  }
207 
208  computeStop(token);
209 
210 }
211 
212 /* ========================================
213  ======================================== */
214 void
215 L2etowCalAlgo08::computeUser(int token ){
216 
217  printf("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());
218  assert(1==2);
219 }
220 
221 
222 /* ========================================
223  ======================================== */
224 void
225 L2etowCalAlgo08::finishRunUser() {
226  /* called once at the end of the run
227  write do whatever you want, log-file & histo-file are still open
228  Here it seraches for hot tower, re-project histos vs. other representations
229  */
230 
231  int eHotSum=1,eHotId=-1;
232  const int *data20=hA[10]->getData();
233  const L2EmcDb::EmcCDbItem *xE=0; //mDb->getByIndex(502); // some wired default?
234 
235  int i;
236  for(i=0; i<EmcDbIndexMax; i++) {
237  const L2EmcDb::EmcCDbItem *x=mDb->getByIndex(i);
238  if(mDb->isEmpty(x)) continue;
239  if (!mDb->isETOW(x) ) continue;
240  int ieta= (x->eta-1);
241  int iphi= (x->sec-1)*EtowGeom::mxSubs + x->sub-'A' ;
242  int softId= iphi+EtowGeom::mxPhiBin*ieta;
243  hA[11]->fillW(softId,data20[x->rdo]);
244  hA[12]->fillW(ieta, iphi,data20[x->rdo]);
245  if(eHotSum<data20[x->rdo]) {
246  eHotSum=data20[x->rdo];
247  eHotId=softId;
248  xE=x;
249  }
250  }
251 
252  int par_nHotThresh=20;
253  if (mLogFile && eHotSum>par_nHotThresh){
254  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);
255  }
256 
257  //...... QA tokens .....
258  int tkn1=99999, tkn2=0; // min/max token
259  int nTkn=0;
260  int tkn3=-1, nTkn3=-1; // most often used token
261 
262  int k;
263  for(k=0;k<L2eventStream2008::mxToken;k++){
264  L2EtowCalibData08 & etowCalibData=globL2eventStream2008.etow[k];
265  if(etowCalibData.nInputBlock==0) continue;
266  hA[1]->fillW(k,etowCalibData.nInputBlock);
267  if(nTkn3<etowCalibData.nInputBlock){
268  nTkn3=etowCalibData.nInputBlock;
269  tkn3=k;
270  }
271 
272  nTkn++;
273  if(tkn1>k) tkn1=k;
274  if(tkn2<k) tkn2=k;
275  }
276  if (mLogFile){
277  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);
278  }
279 
280 }
281 
282 
283 //=======================================
284 //=======================================
285 void
286 L2etowCalAlgo08::createHisto() {
287  memset(hA,0,sizeof(hA));
288  //token related spectra
289  hA[1]=new L2Histo(1,"L2-etow-calib: seen tokens; x: token value; y: events ",L2eventStream2008::mxToken);
290 
291  // ETOW raw spectra (zz 4 lines)
292  hA[10]=new L2Histo(10,"etow hot tower 1", EtowGeom::mxRdo); // title upadted in initRun
293  hA[11]=new L2Histo(11,"etow hot tower 2", EtowGeom::mxRdo); // title upadted in initRun
294  hA[12]=new L2Histo(12,"etow hot tower 3", EtowGeom::mxEtaBin,EtowGeom::mxPhiBin); // title upadted in initRun
295  hA[13]=new L2Histo(13,"ETOW #tower w/ energy /event; x: # ETOW towers; y: counts", 30);
296  hA[14]=new L2Histo(14,"# hot towers/event", 30);
297 
298 }
299 
300 
301 
302 /* ========================================
303  ======================================== */
304 void
305 L2etowCalAlgo08::print0(){ // full raw input ADC array
306  // empty
307  }
308 
309 
310 /**********************************************************************
311  $Log: L2etowCalAlgo08.cxx,v $
312  Revision 1.3 2008/02/01 00:16:40 balewski
313  add mxListSize to BTOW/ETOW calibration
314 
315  Revision 1.2 2008/01/30 21:56:40 balewski
316  E+B high-enery-filter L2-algo fuly functional
317 
318  Revision 1.1 2008/01/30 00:47:16 balewski
319  Added L2-Etow-calib
320 
321 
322 
323 */
324 
325