StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
L2hienAlgo09.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <math.h>
6 
7 /***********************************************************
8  * $Id: L2hienAlgo09.cxx,v 1.1 2011/09/22 20:44:10 pibero Exp $
9  * \author Jan Balewski, MIT, 2008
10  ***********************************************************
11  * Descripion: see .h
12  **********************************************************
13  */
14 
15 #ifdef IS_REAL_L2 //in l2-ana environment
16  #include "../L2algoUtil/L2EmcDb.h"
17  #include "../L2algoUtil/L2Histo.h"
18 #else //full path needed for cvs'd code
19  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb.h"
20  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
21 #endif
22 
23 #include "L2hienAlgo09.h"
24 #include "L2hienResult2009.h"
25 
26 //=================================================
27 //=================================================
28 L2hienAlgo09::L2hienAlgo09(const char* name, L2EmcDb* db, L2EmcGeom *geoX, char* outDir, int resOff, L2VirtualAlgo2009::EmcSwitch beSwitch) : L2VirtualAlgo2009( name, db, outDir, (beSwitch==kIsBtow),(beSwitch==kIsEtow), resOff ) {
29  /* called one per days
30  all memory allocation must be done here
31  */
32 
33  // geoX is not used, ignore
34 
35  if(!(beSwitch==kIsBtow || beSwitch==kIsEtow))
36  {
37  criticalError("L2hienAlgo called with broken beSwitch. L2hien cannot run.\n");
38  }
39 
40  if (beSwitch==kIsBtow)
41  {
42  if (sizeof(L2hienResult2009)!= L2hienResult2009::mySizeChar)
43  criticalError("L2hienAlgo09 (Barrel) has failed consistency check. sizeof(L2hienResult2009)!= L2hienResult2009::mySizeChar");
44  }
45  mSwitch=beSwitch;
46  setMaxHist(16); // set upper range, I uses only 2^N -it is easier to remember
47  createHisto(); // identical for B or EEMC
48 
49 }
50 
51 /* ========================================
52  ======================================== */
53 int
54 L2hienAlgo09::initRunUser( int runNo, int *rc_ints, float *rc_floats) {
55 
56  // unpack params from run control GUI
57  par_dbg = rc_ints[0];
58  par_adcThres = rc_ints[1];
59  par_maxList = rc_ints[2];
60  par_RndAcceptPrescale = 0;//rc_ints[3]; Isn't currently in RC, so can't have it here.
61 
62  // verify consistency of input params
63  int kBad=0;
64  kBad+=0x00001 * (par_adcThres<10);
65 
66  // fix unreasonable params
67  if(par_maxList<10 ) par_maxList=10;
68  if(par_maxList>L2hienList09::mxListSize ) par_maxList=L2hienList09::mxListSize;
69 
70  if (mLogFile) {
71  fprintf(mLogFile,"L2%s algorithm initRun(R=%d), compiled: %s , %s\n params for ",getName(),mRunNumber,__DATE__,__TIME__);
72  if(mSwitch==kIsBtow) fprintf(mLogFile," BTOW setup:\n");
73  if(mSwitch==kIsEtow) fprintf(mLogFile," ETOW setup:\n");
74  fprintf(mLogFile," - use adcThresh =%d, maxList=%d debug=%d\n", par_adcThres , par_maxList, par_dbg);
75  fprintf(mLogFile,"initRun() params checked for consistency, Error flag=0x%04x\n",kBad);
76  }
77  if(kBad) return -kBad;
78 
79  // clear content of all histograms & token-dependet memory
80  int i;
81  for (i=0; i<mxHA;i++) if(hA[i])hA[i]->reset();
82  memset(mHiEnTw,0,sizeof(mHiEnTw));
83 
84 
85  // update titles of histos, add values of params
86  char txt[1000];
87  sprintf(txt,"compute: # towers w/ ADC-ped>%d / event; x: # towers; y: counts",par_adcThres);
88  hA[2]->setTitle(txt);
89 
90  sprintf(txt,"accepted: # towers w/ ADC-ped>%d / event; x: # towers; y: counts",par_adcThres);
91  hA[3]->setTitle(txt);
92 
93  sprintf(txt,"accepted: # towers w/ ADC-ped>%d vs. eta ring;x: BTOW eta-bin, 0=eta=-1.0; y: counts",par_adcThres*2);
94  if(mSwitch==kIsEtow)
95  sprintf(txt,"accepted: # towers w/ ADC-ped>%d vs. eta ring;x: ETOW eta-bin, 0=eta=+2.0; y: counts",par_adcThres*2);
96  hA[5]->setTitle(txt);
97 
98  sprintf(txt,"accepted: # towers w/ ADC-ped>%d vs. phi ring;x: phi-bin, 0=12:30, follows TPC; y: counts",par_adcThres*2);
99  hA[6]->setTitle(txt);
100 
101  sprintf(txt,"accepted: ADC sum over towers w/ ADC-ped>%d ; ADC sum/16; y: counts",par_adcThres);
102  hA[7]->setTitle(txt);
103 
104  // only one set is instantiated , but memory is reserved for both
105 
106  // clear lookup tables for every new run
107  memset(mRdo2towerID_B,0,sizeof(mRdo2towerID_B));
108  memset(mTowerID2etaBin_B,0,sizeof(mTowerID2etaBin_B));
109  memset(mTowerID2phiBin_B,0,sizeof(mTowerID2phiBin_B));
110 
111  memset(mRdo2towerID_E,0,sizeof(mRdo2towerID_E));
112  memset(mTowerID2etaBin_E,0,sizeof(mTowerID2etaBin_E));
113  memset(mTowerID2phiBin_E,0,sizeof(mTowerID2phiBin_E));
114 
115  if(mSwitch==kIsBtow) // prepare BTOW lookup tables
116  for ( int index=0; index<EmcDbIndexMax; index++ ) {
117  const L2EmcDb::EmcCDbItem *x = mDb->getByIndex(index);
118  if ( x==0 ) continue;
119  if ( !mDb->isBTOW(x) ) continue;
120  int sec = x->sec - 1;
121  int sub = 8192;
122  sub = x->sub - 'a';
123  int eta = x->eta - 1;
124  int phi = BtowGeom::mxSubs *sec + sub;
125  int tow = BtowGeom::mxEtaBin *phi + eta; // phi- changes faster
126  int rdo = x->rdo;
127  if (tow<0 || tow>mxBtow || rdo<0 || rdo>mxBtow) return -101;
128 
129  mRdo2towerID_B[ rdo ] = tow; // returns towerID vs.rdo
130  mTowerID2etaBin_B[tow]=eta; // range [0..39]
131  mTowerID2phiBin_B[tow]=phi; // range [0..119]
132  }
133 
134 
135  if(mSwitch==kIsEtow) // prepare ETOW lookup tables
136  for ( int index=0; index<EmcDbIndexMax; index++ ) {
137  const L2EmcDb::EmcCDbItem *x = mDb->getByIndex(index);
138  if ( x==0 ) continue;
139  if ( !mDb->isETOW(x) ) continue;
140  int sec = x->sec - 1;
141  int sub = 8192;
142  sub = x->sub - 'A';
143  int eta = x->eta - 1;
144  int phi = EtowGeom::mxSubs *sec + sub;
145  int tow = EtowGeom::mxEtaBin *phi + eta; // phi- changes faster
146  int rdo = x->rdo;
147  if (tow<0 || tow>mxEtow || rdo<0 || rdo>mxEtow) return -101;
148 
149  mRdo2towerID_E[ rdo ] = tow; // returns towerID vs.rdo
150  mTowerID2etaBin_E[tow]=eta; // range [0..11]
151  mTowerID2phiBin_E[tow]=phi; // range [0..59]
152  }
153 
154  return 0; //OK
155 }
156 
157 
158 
159 /* ========================================
160  ======================================== */
161 void
162 L2hienAlgo09::computeUser(int token){
163  // token range is guaranteed by virtual09-class
164  // ----------- SCAN FOR HIGH TOWERS ----
165 
166  // reset # of towers for this token
167  mHiEnTw[token].size=0;// do not set 'fresh-flag here, only after compute() finishes
168  int i;
169  // get pointer to input list towers from calib-algo
170  const HitTower1 *hit=0;
171  int hitSize=0;
172  int *mRdo2towerID=0;
173 
174  if(mSwitch==kIsBtow) { //...... map pointers to Barrel
175  hit =mEveStream_btow[token].get_hits();
176  hitSize=mEveStream_btow[token].get_hitSize();
177  mRdo2towerID=mRdo2towerID_B;
178  } else { //...... map pointers to Endcap
179  hit =mEveStream_etow[token].get_hits();
180  hitSize=mEveStream_etow[token].get_hitSize();
181  mRdo2towerID=mRdo2towerID_E;
182  }
183 
184  // printf("L2-%s-compute: ---EMC ADC list---token=%d input size=%d\n",getName(),token,hitSize);
185  // get pointers to internal, token indexed, output event storage
186  // output is just one per instance, same format for B & ETOW
187  L2hienList09 *hiTwEve=mHiEnTw+token;
188  unsigned int *value=hiTwEve->value;
189  unsigned int tempValue=0;
190  unsigned int totalADC=0;
191  int resultMaxTowers=0;
192  int softID=0;
193  L2hienResult2009 *hienResult=mHienResult+token;
194  if (mSwitch==kIsBtow)
195  {
196  resultMaxTowers=L2hienResult2009::maxTowers; //only grab the barrel towers.
197  //memset to zero the Result?
198  }
199 
200  for(i=0;i< hitSize;i++,hit++) {
201 
202  softID=mRdo2towerID[hit->rdo];
203  totalADC+=hit->adc;
204  tempValue=((hit->adc)<<16)+softID;
205 
206  if ((mSwitch==kIsBtow) && (i<L2hienResult2009::maxTowers))
207  hienResult->value[i]=tempValue;
208  if (hiTwEve->size<par_maxList && hit->adc>=par_adcThres)
209  {
210  //(*value)= ((hit->adc)<<16 )+ softID; // store composite value
211  (*value)=tempValue; //same as the commented line, but no need to do same computation twice.
212  hiTwEve->size++;
213  value++;
214  }
215 
216  // printf(" adc in=%d ",hit->adc);
217  if(hiTwEve->size>=par_maxList && i>=L2hienResult2009::maxTowers) break; // overflow protection
218  // printf("A %p %x adc=%d softID=%d \n", value, *value, hit->adc,softID);
219  }
220 
221  unsigned int nTow;
222  if (hitSize>255) nTow=255;
223  else nTow=hitSize;
224 
225  if (totalADC>16777215) totalADC=16777215; //16777215=256*256*256=24 bits.
226  hienResult->header=(nTow<<24)+totalADC;
227 
228  // printf("L2-%s-compute: saved: size%d val[0]=0x%x p=%p\n",getName(),hiTwEve->size,hiTwEve->value[0],hiTwEve->value);
229 
230  hiTwEve->isFresh=L2hienList09::kDataFresh;
231  hA[2]->fill(hiTwEve->size);
232 }
233 
234 
235 /* ========================================
236  ======================================== */
237 bool
238 L2hienAlgo09::decisionUser(int token, int *myL2Result){
239  // INPUT: token + comput() results stored internally
240  // OUTPUT: always YES & put first ten towers above threshold into myL2Result.
241 
242  //don't unless you have something useful to say, and then talk to Ross first: (*myL2Result)=0;// empty
243 
244  // get pointers to internal private event storage
245  L2hienList09 *hiTwEve=mHiEnTw+token;
246  L2hienResult2009 *hienResult=mHienResult+token;
247  //printf("L2-%s-decision: ---EMC ADC list---token=%d size=%d val[0]=0x%x p=%p\n",getName(),token,hiTwEve->size,hiTwEve->value[0],hiTwEve->value);
248 
249  //...... some histos for QA
250 
251  hA[3]->fill(hiTwEve->size);
252  if(hiTwEve->size>= par_maxList) mhN->fill(5); // was overflow
253  if(hiTwEve->isFresh>L2hienList09::kDataFresh) mhN->fill(6); // stale data
254  hiTwEve->isFresh++; // mark local data as stale
255 
256  // scan for very hot towers
257  int adc4QaThres=(par_adcThres/8); // it is 2x higher than minThres
258  int * mTowerID2etaBin=0 , *mTowerID2phiBin=0;
259  if(mSwitch==kIsBtow) { //...... map pointers to Barrel
260  mTowerID2etaBin=mTowerID2etaBin_B;
261  mTowerID2phiBin=mTowerID2phiBin_B;
262  } else { //...... map pointers to Endcap
263  mTowerID2etaBin=mTowerID2etaBin_E;
264  mTowerID2phiBin=mTowerID2phiBin_E;
265  }
266 
267  unsigned int *value=hiTwEve->value;
268  int ic;
269  int adcSum4=0;
270  for(ic=0;ic<hiTwEve->size;ic++,value++) {
271  int adc4=(*value)>>(16+4); // reduced resolution
272  int softID=(*value)&0xffff;
273  adcSum4+=adc4;
274  //printf(" got adc=%d softID=%d, ",adc,softID);
275  hA[4]->fill(adc4);
276  if(adc4<adc4QaThres) continue;
277  hA[5]->fill(mTowerID2etaBin[softID]);
278  hA[6]->fill(mTowerID2phiBin[softID]);
279  }
280  if(adcSum4>0) hA[7]->fill(adcSum4);
281 
282  // printf("\n");
283 
284 
285  // debugging should be off for any time critical computation
286  if(par_dbg>0){
287  print2(token);
288 
289  }
290 
291  //end of histogramming code
292 
293 
294 
295  //set L2Result:
296  if(mSwitch==kIsBtow) //only if we're the barrel instance of this, otherwise we'd clobber.
297  {
298  memcpy(myL2Result,hienResult,sizeof(L2hienResult2009));
299  hienResult->header=0; //mark it as used by setting totalADC=nTowers=0;
300  }
301  /****************/
302  /* accept always */
303  /****************/
304  return true;
305 
306 }
307 
308 
309 /* ========================================
310  ======================================== */
311 void
312 L2hienAlgo09::finishRunUser() { /* called once at the end of the run */
313  // do whatever you want, log-file & histo-file are still open
314 
315  if (mLogFile){
316  fprintf(mLogFile,"finishRunUser-%s bhla bhla\n",getName());
317  }
318 
319 }
320 
321 
322 //=======================================
323 //=======================================
324 void
325 L2hienAlgo09::createHisto() {
326  memset(hA,0,sizeof(hA));
327 
328  hA[2]=new L2Histo(2,"compute: #towers w/ .....energy /event; x: # towers; y: counts", 35); // title set in initRun
329  hA[3]=new L2Histo(3,"decision: #towers w/ .....energy /event; x: # towers; y: counts", 35); // title set in initRun
330 
331  int nEtaBin=BtowGeom::mxEtaBin;
332  int nPhiBin=BtowGeom::mxPhiBin;
333  if(mSwitch==kIsEtow) {
334  nEtaBin=EtowGeom::mxEtaBin;
335  nPhiBin=EtowGeom::mxPhiBin;
336  }
337  hA[4]=new L2Histo(4,"accepted: towers ET (watch units!) ; x: (ADC-ped)/16", 150);
338  hA[5]=new L2Histo(5,"accepted: #towers w/ ..... vs. eta ring",nEtaBin); // title set in initRun
339  hA[6]=new L2Histo(6,"accepted: #towers w/ .... vs. phi ring",nPhiBin); // title set in initRun
340  hA[7]=new L2Histo(7,"accepted: ADC sum ...",150); // title set in initRun
341 
342  // printf("L2-%s::createHisto() done\n",getName());
343 }
344 
345 
346 /* ========================================
347  ======================================== */
348 void
349 L2hienAlgo09::print2(int token){ // full , local ADC array
350  if (!(token>0 && token<L2eventStream2009::mxToken))
351  {
352  printf("L2hienAlgo09::print2 called with token out of bounds!\n");
353  return;
354  }
355 
356  int hiSize=getListSize(token);
357  const unsigned int *value=getListData(token);
358 
359  printf("pr2-%s: dump %d acceted towers for token=%d\n softID ADC-ped\n",getName(),hiSize, token);
360  for(int ic=0;ic<hiSize;ic++,value++) {
361  int adc=(*value)>>16;
362  int softID=(*value)&0xffff;
363  printf("%4d %d, \n",softID,adc);
364  }
365 }
366 
367 
368 /**********************************************************************
369  $Log: L2hienAlgo09.cxx,v $
370  Revision 1.1 2011/09/22 20:44:10 pibero
371  *** empty log message ***
372 
373  Revision 1.5 2008/02/01 00:16:43 balewski
374  add mxListSize to BTOW/ETOW calibration
375 
376  Revision 1.4 2008/01/31 00:51:34 balewski
377  bug fix
378 
379  Revision 1.3 2008/01/30 21:56:43 balewski
380  E+B high-enery-filter L2-algo fuly functional
381 
382  Revision 1.2 2008/01/30 00:47:23 balewski
383  Added L2-Etow-calib
384 
385  Revision 1.1 2008/01/29 00:17:13 balewski
386  new algo filtering high-energy towers
387 
388 
389 
390 
391 */
392 
393