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