StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
L2bemcGamma2009.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: L2bemcGamma2009.cxx,v 1.1 2011/03/09 16:29:07 pibero Exp $
9  * \author Jan Balewski,MIT , 2008
10  *********************************************************************
11  * Descripion: see .h
12  *********************************************************************
13  */
14 
15 
16 #ifdef IS_REAL_L2 //in l2-ana environment
17  #include "../L2algoUtil/L2EmcDb.h"
18  #include "../L2algoUtil/L2Histo.h"
19 #else //full path needed for cvs'd code
20  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb.h"
21  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
22  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcGeom.h"
23 #endif
24 
25 #include "L2bemcGamma2009.h"
26 
27 
28 //=================================================
29 //=================================================
30 L2bemcGamma2009::L2bemcGamma2009(const char* name, L2EmcDb* db, L2EmcGeom *geoX, char* outDir, int resOff) : L2VirtualAlgo2009( name, db, outDir, true, false, resOff) {
31  /* called one per days
32  all memory allocation must be done here
33  */
34 
35  mGeom=geoX;
36  if (!mGeom)
37  criticalError("L2bemcGamma is broken -- can't find geom.");
38 
39  setMaxHist(16); // set upper range, I uses only 2^N -it is easier to remember
40  createHisto();
41 
42  //------- self-consistency checks, should never fail
43  // printf("ZZ %d %d\n", sizeof(L2gammaResult2008), L2gammaResult2008::mySizeChar);
44  if (sizeof(L2gammaResult2009)!= L2gammaResult2009::mySizeChar)
45  criticalError("L2bemcGamma has failed consistency check. sizeof(L2gammaResult2009)!= L2gammaResult2009::mySizeChar");
46 
47 }
48 
49 /* ========================================
50  ======================================== */
51 int
52 L2bemcGamma2009::initRunUser( int runNo, int *rc_ints, float *rc_floats) {
53 
54  // unpack params from run control GUI
55  par_dbg = rc_ints[0];
56  par_RndAcceptPrescale = rc_ints[1];
57  par_seedEtThres = rc_floats[0];
58  par_clusterEtThres = rc_floats[1];
59 
60  // verify consistency of input params
61  int kBad=0;
62  kBad+=0x00001 * (par_seedEtThres<1.0);
63  kBad+=0x00002 * (par_clusterEtThres<par_seedEtThres);
64 
65  // put notes about configuration into the log file
66  if (mLogFile) {
67  fprintf(mLogFile,"L2%s algorithm initRun(R=%d), compiled: %s , %s\n params:\n",
68  getName(),mRunNumber,__DATE__,__TIME__);
69  fprintf(mLogFile," - use seedThres=%.2f (GeV), debug=%d, prescale=%d (0=off,1=100proc, 2=50proc, etc)\n",
70  par_seedEtThres,par_dbg,par_RndAcceptPrescale);
71  fprintf(mLogFile," - accept event cluster Thres=%.2f (GeV)\n",
72  par_clusterEtThres);
73  fprintf(mLogFile,"initRun() params checked for consistency, Error flag=0x%04x\n",
74  kBad);
75  }
76 
77  // clear content of all histograms & token-dependent memory
78  int i;
79  for (i=0; i<mxHA;i++) if(hA[i])hA[i]->reset();
80  memset(mBtow,0,sizeof(mBtow));
81 
82  if(kBad>0) return -1*kBad;
83  else if(kBad<0) return kBad;
84 
85 
86  // update titles of histos
87  char txt[1000];
88 
89  sprintf(txt,"BTOW-decision: acc seed Tw ET>%.2f GeV; x:tower internal ID; y: counts",par_clusterEtThres);
90  hA[7]->setTitle(txt);
91  sprintf(txt,"#BTOW decision acc seed Tw, Et>%.2f GeV; eta bin [-1,+1]; y: phi bin ~ TCP sector",par_clusterEtThres);
92  hA[14]->setTitle(txt);
93 
94 
95  for ( int index=0; index<EmcDbIndexMax; index++ )
96  {
97  const L2EmcDb::EmcCDbItem *x = mDb->getByIndex(index);
98  if ( x==0 ) continue;
99  if ( !mDb->isBTOW(x) ) continue;
100  int sec = x->sec - 1;
101  int sub = 8192;
102  sub = x->sub - 'a';
103  int eta = x->eta - 1;
104  int phi = BtowGeom::mxSubs *sec + sub;
105  int tow = BtowGeom::mxEtaBin *phi + eta; // phi- changes faster
106  int rdo = x->rdo;
107  if (tow<0 || tow>mxBtow || rdo<0 || rdo>mxBtow) return -101;
108 
109  mTower2rdo[ tow ] = rdo; // returns rdo channel for given tower
110  mRdo2tower[ rdo ] = tow; // returns tower for given rdo channel
111  }
112  return 0; //OK
113 
114 }
115 
116 
117 
118 /* ========================================
119  ======================================== */
120 float
121 L2bemcGamma2009::sumET(int phi, int eta) {
122  int tow = BtowGeom::mxEtaBin *((phi+BtowGeom::mxPhiBin)%BtowGeom::mxPhiBin) + ((eta+BtowGeom::mxEtaBin)%BtowGeom::mxEtaBin); // phi- changes faster
123 
124  const int maxTowers = BtowGeom::mxEtaBin * BtowGeom::mxPhiBin;
125  int towPlusOne;
126  float sum;
127  sum=0;
128  sum=wrkBtow_et[tow];
129  towPlusOne = tow+1;
130  towPlusOne%= maxTowers;
131  sum+=wrkBtow_et[towPlusOne];
132 
133  //if(tow==0 || towPlusOne==0) {
134  //printf("tow : %d, %d --> %f %f \n",tow, towPlusOne,wrkBtow_et[tow],wrkBtow_et[towPlusOne]);
135  //}
136  tow+=BtowGeom::mxEtaBin;
137  tow%=maxTowers;
138 
139  sum+=wrkBtow_et[tow];
140 
141  towPlusOne = tow+1;
142  towPlusOne%= maxTowers;
143  sum+=wrkBtow_et[towPlusOne];
144 
145  //if(tow==0 || towPlusOne==0) {
146  //printf("tow : %d, %d --> %f %f \n",tow, towPlusOne,wrkBtow_et[tow],wrkBtow_et[towPlusOne]);
147  //}
148  // printf("B sumET=%f\n",sum);
149  return sum;
150 }
151 
152 
153 /* ========================================
154  ======================================== */
155 void
156 L2bemcGamma2009::computeUser(int token){
157  // token range is guaranteed by virtual08-class
158 
159  /* 2x2 cluster finder:
160  - can find clusters through the tower border in Phi
161  - sorts hit towers by Et and selects Highest Seed Towers first
162  */
163  clearEvent(token);
164 
165  // ------ PROJECT INPUT LIST TO 2D ARRAY AND SCAN FOR SEED TOWERS ----
166  int i;
167  // printf("L2-%s-compute: ---BTOW ADC list--- size=%d\n",getName(),*globEve_btow_hitSize);
168 
169  L2bemcGammaEvent2009 *btowEve=mBtow+token;
170  const HitTower1 *hit=mEveStream_btow[token].get_hits();
171  const int hitSize=mEveStream_btow[token].get_hitSize();
172  for(i=0;i< hitSize;i++,hit++) {
173  int tower=mRdo2tower[hit->rdo];
174  wrkBtow_et[tower]=hit->et;
175  if(hit->et<par_seedEtThres)continue;
176  wrkBtow_tower_seed[wrkBtow_tower_seed_size++]=tower;
177  //printf("A seed TWID=%d \n",tower);
178  }
179  hA[2]->fill(hitSize);
180  int seedTow=-1,seedEta=-1,seedPhi=-1;
181  float clustET=0;
182  btowEve->isFresh=L2bemcGammaEvent2009::kDataFresh;
183 
184  // printf("B nseed=%d\n",wrkBtow_tower_seed_size);
185  // ----------- FIND 2x2 CLUSTER AROUND EVERY SEED -----
186  for(i=0; i<wrkBtow_tower_seed_size;i++) {
187  seedTow=wrkBtow_tower_seed[i];
188  seedEta=seedTow%BtowGeom::mxEtaBin;
189  seedPhi=seedTow/BtowGeom::mxEtaBin;
190 
191  //.... find first 2x2 above cluster thresh
192  if (seedEta < BtowGeom::mxEtaBin) {
193  clustET = sumET(seedPhi,seedEta);
194  if(clustET>par_clusterEtThres) goto ACCEPT;
195  clustET = sumET(seedPhi-1,seedEta);
196  if(clustET>par_clusterEtThres) goto ACCEPT;
197  }
198  if (seedEta > 0 ) {
199  clustET = sumET(seedPhi-1,seedEta-1);
200  if(clustET>par_clusterEtThres) goto ACCEPT;
201  clustET = sumET(seedPhi,seedEta-1);
202  if(clustET>par_clusterEtThres) goto ACCEPT;
203  }
204  }
205  //.... ABORT
206  btowEve->resultBlob.clusterEt=0;
207  btowEve->resultBlob.meanEtaBin=0;
208  btowEve->resultBlob.meanPhiBin=0;
209  btowEve->resultBlob.trigger=0;
210  btowEve->seedTwID=-1;
211  btowEve->clusterET=0;
212  btowEve->seedET=0;
213  return;
214 
215  ACCEPT:
216  // printf("B acc clustET=%f\n",clustET);
217  btowEve->clusterET=clustET;
218  btowEve->seedET=wrkBtow_et[seedTow];
219  btowEve->resultBlob.clusterEt=(unsigned char)(clustET*256.0/60.0);
220  btowEve->resultBlob.meanEtaBin=seedEta;
221  btowEve->resultBlob.meanPhiBin=seedPhi;
222  btowEve->seedTwID=seedTow;
223  btowEve->resultBlob.trigger=2;
224  return;
225 }
226 
227 
228 
229 
230 /* ========================================
231  ======================================== */
232 bool
233 L2bemcGamma2009::decisionUser(int token, int *myL2Result){
234  // INPUT: token + comput() results stored internally
235  // OUTPUT: yes/now + pointer to L2Result
236 
237 
238  // get pointers to internal private event storage
239  L2bemcGammaEvent2009 *btowEve=mBtow+token;
240  bool triggerDecision=(btowEve->resultBlob.trigger>0);
241 
242  //prescaling is done in Virtual:
243  if (par_RndAcceptPrescale>0) btowEve->resultBlob.trigger+=mRandomAccept;
244 
245  //...... some histos just for fun
246  if(btowEve->isFresh>L2bemcGammaEvent2009::kDataFresh) mhN->fill(6); // stale data
247 
248  btowEve->isFresh++; // mark the data as stale
249 
250  if(btowEve->resultBlob.trigger&2) {
251  mhN->fill(15);
252  hA[6]->fill((int)btowEve->clusterET);
253  hA[7]->fill(btowEve->seedTwID);
254  hA[11]->fill((int)btowEve->seedET);
255  hA[12]->fill((int)(100.*btowEve->seedET/btowEve->clusterET));
256  }
257 
258  if(btowEve->resultBlob.trigger&1) mhN->fill(16);
259 
260  memcpy(myL2Result,&(btowEve->resultBlob),sizeof(L2gammaResult2009));
261  return triggerDecision;
262 }
263 
264 
265 /* ========================================
266  ======================================== */
267 void
268 L2bemcGamma2009::finishRunUser() { /* called once at the end of the run */
269  // do whatever you want, log-file & histo-file are still open
270 
271  if (mLogFile){
272  fprintf(mLogFile,"finishRunUser-%s bhla bhla\n",getName());
273  }
274 
275  const int *hist15Data = hA[7]->getData();
276  for (int i = 0; i < BtowGeom::mxEtaBin; i++) {
277  for (int j = 0; j < BtowGeom::mxPhiBin; j++) {
278  //printf("i %d, j %d, iXj*n %d, data %d\n", i, j, i+j*BtowGeom::mxEtaBin, hist15Data[i+j*BtowGeom::mxEtaBin]);
279  hA[14]->fillW(i,j, hist15Data[i+j*BtowGeom::mxEtaBin]);
280  }
281  }
282 
283 }
284 
285 
286 //=======================================
287 //=======================================
288 void
289 L2bemcGamma2009::createHisto() {
290  setMaxHist(16); // PMN added - histogram count does not seem to be initialiazed anywere.
291  //memset(hA,0,sizeof(hA));
292 
293  hA[2]=new L2Histo(2,"BTOW-compute: #towers w/ energy /event; x: # BTOW towers; y: counts", 100);
294  hA[6]=new L2Histo(6,"BTOW-decision: accepted clust ... ; x: ET(GeV)", 30);// title in initRun
295 
296  hA[7]=new L2Histo(7,"BTOW: accepted Seed Tower .....", 5000); // title in initRun
297  hA[11]=new L2Histo(11,"BTOW: decision Cluster Seed Et; ET GeV", 30); // title in initRun
298  hA[12]=new L2Histo(12,"BTOW: decision ;100*Seed Et/Cluster Et", 100); // title in initRun
299 
300  hA[14]=new L2Histo(14,"BTOW: hot tower projection", BtowGeom::mxEtaBin, BtowGeom::mxPhiBin); // title in initRun
301 
302  // printf("L2-%s::createHisto() done\n",getName());
303 }
304 
305 //=======================================
306 //=======================================
307 void
308 L2bemcGamma2009::clearEvent(int token){
309  memset(wrkBtow_et,0,sizeof(wrkBtow_et));
310  wrkBtow_tower_seed_size=0;
311  memset(&(mBtow[token].resultBlob),0, sizeof(L2gammaResult2009));
312 }
313 
314 /* ========================================
315  ======================================== */
316 void
317 L2bemcGamma2009::print2(){ // full , local ADC array
318  int i;
319  printf("pr2-%s: ---BTOW ADC 2D array, only non-zero\n",getName());
320 
321  for(i=0;i<mxBtow;i++) {
322  if(wrkBtow_et[i]<=0) continue;
323  int rdo=mTower2rdo[i];
324  float et=wrkBtow_et[i];
325  printf(" btow: tower=%4d rdo=%4d et=%.3f \n",i,rdo,et);
326  }
327 
328 }
329 
330 /* ========================================
331  ======================================== */
332 void
333 L2bemcGamma2009::print3(){ // seed list
334  int i;
335  printf("pr3-%s: ---seed list, size=%d\n",getName(),wrkBtow_tower_seed_size);
336 
337  for(i=0;i<wrkBtow_tower_seed_size;i++) {
338  int tower=wrkBtow_tower_seed[i];
339  float et=wrkBtow_et[tower];
340  printf(" btow: i=%4d tower=%4d et=%.3f \n",i,tower,et);
341  }
342 
343 }
344 
345 #if 0
346 
347 /* ========================================
348  ======================================== */
349 void
350 L2bemcGamma2009::print4(int token, int hitSize){ // L2-algo input list
351  int i;
352  printf("print4 IS NOT Fully FUNCTIONAL **********************\n");
353  printf("pr1-%s: ---BTOW Sorted ADC list--- size=%d\n",getName(),hitSize);
354  //const HitTower *hit=globEve_btow_hit;
355  for(i=0;i< hitSize;i++) {
356  int adc=0;//(mEveStream_btow[token].get_hits()[wrkBtow_tower_index[i]]).adc;
357  int rdo=0;//(mEveStream_btow[token].get_hits()[wrkBtow_tower_index[i]]).rdo;
358  float et=wrkBtow_et[wrkBtow_tower_index[i]];
359  float ene=0;//(mEveStream_btow[token].get_hits()[wrkBtow_tower_index[i]]).ene;
360  printf(" tower=%2d ",wrkBtow_tower_index[i]);
361  printf(" btow: i=%2d rdo=%4d adc=%d et=%.3f ene=%.3f\n",i,rdo,adc,et,ene);
362  }
363 }
364 
365 #endif
366 /**********************************************************************
367  $Log: L2bemcGamma2009.cxx,v $
368  Revision 1.1 2011/03/09 16:29:07 pibero
369  Added L2gamma2009
370 
371  Revision 1.6 2008/01/30 21:56:40 balewski
372  E+B high-enery-filter L2-algo fuly functional
373 
374  Revision 1.5 2008/01/30 00:47:17 balewski
375  Added L2-Etow-calib
376 
377  Revision 1.4 2008/01/18 23:29:13 balewski
378  now L2result is exported
379 
380  Revision 1.3 2008/01/17 23:15:51 balewski
381  bug in token-addressed memory fixed
382 
383  Revision 1.2 2008/01/16 23:32:35 balewski
384  toward token dependent compute()
385 
386  Revision 1.1 2007/12/19 02:30:18 balewski
387  new L2-btow-calib-2008
388 
389 
390 
391 */
392 
393