StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
L2hienAlgo12.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: L2hienAlgo12.cxx,v 1.4 2012/03/21 18:18:03 jml 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/L2EmcDb2012.h"
17  #include "../L2algoUtil/L2Histo.h"
18 #else //full path needed for cvs'd code
19  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb2012.h"
20  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
21 #endif
22 
23 #include "L2hienAlgo12.h"
24 #include "L2hienResult2012.h"
25 
26 //=================================================
27 //=================================================
28 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 ) {
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(L2hienResult2012)!= L2hienResult2012::mySizeChar)
43  criticalError("L2hienAlgo12 (Barrel) has failed consistency check. sizeof(L2hienResult2012)!= L2hienResult2012::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 L2hienAlgo12::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>L2hienList12::mxListSize ) par_maxList=L2hienList12::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 L2EmcDb2012::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 L2EmcDb2012::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 L2hienAlgo12::computeUser(int token){
163  // token range is guaranteed by virtual12-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  // get pointers to internal, token indexed, output event storage
185  // output is just one per instance, same format for B & ETOW
186  L2hienList12 *hiTwEve=mHiEnTw+token;
187  unsigned int *value=hiTwEve->value;
188  unsigned int tempValue=0;
189  unsigned int totalADC=0;
190  int resultMaxTowers=0;
191  int softID=0;
192  L2hienResult2012 *hienResult=mHienResult+token;
193  if (mSwitch==kIsBtow)
194  {
195  resultMaxTowers=L2hienResult2012::maxTowers; //only grab the barrel towers.
196  //memset to zero the Result?
197  }
198 
199  for(i=0;i< hitSize;i++,hit++) {
200 
201  softID=mRdo2towerID[hit->rdo];
202  totalADC+=hit->adc;
203  tempValue=((hit->adc)<<16)+softID;
204 
205  if ((mSwitch==kIsBtow) && (i<L2hienResult2012::maxTowers))
206  hienResult->value[i]=tempValue;
207  if (hiTwEve->size<par_maxList && hit->adc>=par_adcThres)
208  {
209  //(*value)= ((hit->adc)<<16 )+ softID; // store composite value
210  (*value)=tempValue; //same as the commented line, but no need to do same computation twice.
211  hiTwEve->size++;
212  value++;
213  }
214 
215  if(hiTwEve->size>=par_maxList && i>=L2hienResult2012::maxTowers) break; // overflow protection
216  }
217 
218  unsigned int nTow;
219  if (hitSize>255) nTow=255;
220  else nTow=hitSize;
221 
222  if (totalADC>16777215) totalADC=16777215; //16777215=256*256*256=24 bits.
223  hienResult->header=(nTow<<24)+totalADC;
224 
225  hiTwEve->isFresh=L2hienList12::kDataFresh;
226  hA[2]->fill(hiTwEve->size);
227 }
228 
229 
230 /* ========================================
231  ======================================== */
232 bool
233 L2hienAlgo12::decisionUser(int token, int *myL2Result){
234  // INPUT: token + comput() results stored internally
235  // OUTPUT: always YES & put first ten towers above threshold into myL2Result.
236 
237  //don't unless you have something useful to say, and then talk to Ross first: (*myL2Result)=0;// empty
238 
239  // get pointers to internal private event storage
240  L2hienList12 *hiTwEve=mHiEnTw+token;
241  L2hienResult2012 *hienResult=mHienResult+token;
242 
243  //...... some histos for QA
244 
245  hA[3]->fill(hiTwEve->size);
246  if(hiTwEve->size>= par_maxList) mhN->fill(5); // was overflow
247  if(hiTwEve->isFresh>L2hienList12::kDataFresh) mhN->fill(6); // stale data
248  hiTwEve->isFresh++; // mark local data as stale
249 
250  // scan for very hot towers
251  int adc4QaThres=(par_adcThres/8); // it is 2x higher than minThres
252  int * mTowerID2etaBin=0 , *mTowerID2phiBin=0;
253  if(mSwitch==kIsBtow) { //...... map pointers to Barrel
254  mTowerID2etaBin=mTowerID2etaBin_B;
255  mTowerID2phiBin=mTowerID2phiBin_B;
256  } else { //...... map pointers to Endcap
257  mTowerID2etaBin=mTowerID2etaBin_E;
258  mTowerID2phiBin=mTowerID2phiBin_E;
259  }
260 
261  unsigned int *value=hiTwEve->value;
262  int ic;
263  int adcSum4=0;
264  for(ic=0;ic<hiTwEve->size;ic++,value++) {
265  int adc4=(*value)>>(16+4); // reduced resolution
266  int softID=(*value)&0xffff;
267  adcSum4+=adc4;
268  hA[4]->fill(adc4);
269  if(adc4<adc4QaThres) continue;
270  hA[5]->fill(mTowerID2etaBin[softID]);
271  hA[6]->fill(mTowerID2phiBin[softID]);
272  }
273  if(adcSum4>0) hA[7]->fill(adcSum4);
274 
275  // debugging should be off for any time critical computation
276  if(par_dbg>0){
277  print2(token);
278 
279  }
280 
281  //end of histogramming code
282 
283 
284 
285  //set L2Result:
286  if(mSwitch==kIsBtow) //only if we're the barrel instance of this, otherwise we'd clobber.
287  {
288  memcpy(myL2Result,hienResult,sizeof(L2hienResult2012));
289  hienResult->header=0; //mark it as used by setting totalADC=nTowers=0;
290  }
291  /****************/
292  /* accept always */
293  /****************/
294  return true;
295 
296 }
297 
298 
299 /* ========================================
300  ======================================== */
301 void
302 L2hienAlgo12::finishRunUser() { /* called once at the end of the run */
303  // do whatever you want, log-file & histo-file are still open
304 
305  if (mLogFile){
306  fprintf(mLogFile,"finishRunUser-%s bhla bhla\n",getName());
307  }
308 
309 }
310 
311 
312 //=======================================
313 //=======================================
314 void
315 L2hienAlgo12::createHisto() {
316  memset(hA,0,sizeof(hA));
317 
318  hA[2]=new L2Histo(2,"compute: #towers w/ .....energy /event; x: # towers; y: counts", 35); // title set in initRun
319  hA[3]=new L2Histo(3,"decision: #towers w/ .....energy /event; x: # towers; y: counts", 35); // title set in initRun
320 
321  int nEtaBin=BtowGeom::mxEtaBin;
322  int nPhiBin=BtowGeom::mxPhiBin;
323  if(mSwitch==kIsEtow) {
324  nEtaBin=EtowGeom::mxEtaBin;
325  nPhiBin=EtowGeom::mxPhiBin;
326  }
327  hA[4]=new L2Histo(4,"accepted: towers ET (watch units!) ; x: (ADC-ped)/16", 150);
328  hA[5]=new L2Histo(5,"accepted: #towers w/ ..... vs. eta ring",nEtaBin); // title set in initRun
329  hA[6]=new L2Histo(6,"accepted: #towers w/ .... vs. phi ring",nPhiBin); // title set in initRun
330  hA[7]=new L2Histo(7,"accepted: ADC sum ...",150); // title set in initRun
331 
332 }
333 
334 
335 /* ========================================
336  ======================================== */
337 void
338 L2hienAlgo12::print2(int token){ // full , local ADC array
339  if (!(token>0 && token<L2eventStream2012::mxToken))
340  {
341  printf("L2hienAlgo12::print2 called with token out of bounds!\n");
342  return;
343  }
344 
345  int hiSize=getListSize(token);
346  const unsigned int *value=getListData(token);
347 
348  printf("pr2-%s: dump %d acceted towers for token=%d\n softID ADC-ped\n",getName(),hiSize, token);
349  for(int ic=0;ic<hiSize;ic++,value++) {
350  int adc=(*value)>>16;
351  int softID=(*value)&0xffff;
352  printf("%4d %d, \n",softID,adc);
353  }
354 }
355 
356 
357 /**********************************************************************
358  $Log: L2hienAlgo12.cxx,v $
359  Revision 1.4 2012/03/21 18:18:03 jml
360  got rid of printfs from 2012 files
361 
362  Revision 1.3 2011/10/19 16:12:11 jml
363  more 2012 stuff
364 
365  Revision 1.2 2011/10/19 15:39:43 jml
366  2012
367 
368  Revision 1.1 2011/10/18 15:11:42 jml
369  adding 2012 algorithms
370 
371  Revision 1.1 2011/09/22 20:44:10 pibero
372  *** empty log message ***
373 
374  Revision 1.5 2008/02/01 00:16:43 balewski
375  add mxListSize to BTOW/ETOW calibration
376 
377  Revision 1.4 2008/01/31 00:51:34 balewski
378  bug fix
379 
380  Revision 1.3 2008/01/30 21:56:43 balewski
381  E+B high-enery-filter L2-algo fuly functional
382 
383  Revision 1.2 2008/01/30 00:47:23 balewski
384  Added L2-Etow-calib
385 
386  Revision 1.1 2008/01/29 00:17:13 balewski
387  new algo filtering high-energy towers
388 
389 
390 
391 
392 */
393 
394