StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
L2bemcGamma2012.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <math.h>
6 #include <fakeRtsLog.h>
7 
8 /*********************************************************************
9  * $Id: L2bemcGamma2012.cxx,v 1.5 2012/03/21 18:18:03 jml Exp $
10  * \author Jan Balewski,MIT , 2008
11  *********************************************************************
12  * Descripion: see .h
13  *********************************************************************
14  */
15 
16 
17 #ifdef IS_REAL_L2 //in l2-ana environment
18  #include "../L2algoUtil/L2EmcDb2012.h"
19  #include "../L2algoUtil/L2Histo.h"
20 #else //full path needed for cvs'd code
21  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb2012.h"
22  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
23  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcGeom2012.h"
24 #endif
25 
26 #include "L2bemcGamma2012.h"
27 
28 
29 //=================================================
30 //=================================================
31 L2bemcGamma2012::L2bemcGamma2012(const char* name, const char *uid, L2EmcDb2012* db, L2EmcGeom2012 *geoX, char* outDir, int resOff) : L2VirtualAlgo2012( name, uid, db, outDir, true, false, resOff) {
32  /* called one per days
33  all memory allocation must be done here
34  */
35 
36  mGeom=geoX;
37  if (!mGeom)
38  criticalError("L2bemcGamma is broken -- can't find geom.");
39 
40  setMaxHist(16); // set upper range, I uses only 2^N -it is easier to remember
41  createHisto();
42 
43  //------- self-consistency checks, should never fail
44  if (sizeof(L2gammaResult2012)!= L2gammaResult2012::mySizeChar)
45  criticalError("L2bemcGamma has failed consistency check. sizeof(L2gammaResult2012)!= L2gammaResult2012::mySizeChar");
46 
47 }
48 
49 /* ========================================
50  ======================================== */
51 int
52 L2bemcGamma2012::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 L2EmcDb2012::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 L2bemcGamma2012::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  tow+=BtowGeom::mxEtaBin;
134  tow%=maxTowers;
135 
136  sum+=wrkBtow_et[tow];
137 
138  towPlusOne = tow+1;
139  towPlusOne%= maxTowers;
140  sum+=wrkBtow_et[towPlusOne];
141 
142  return sum;
143 }
144 
145 
146 /* ========================================
147  ======================================== */
148 void
149 L2bemcGamma2012::computeUser(int token){
150  // token range is guaranteed by virtual08-class
151 
152  /* 2x2 cluster finder:
153  - can find clusters through the tower border in Phi
154  - sorts hit towers by Et and selects Highest Seed Towers first
155  */
156  clearEvent(token);
157 
158  // ------ PROJECT INPUT LIST TO 2D ARRAY AND SCAN FOR SEED TOWERS ----
159  int i;
160 
161  L2bemcGammaEvent2012 *btowEve=mBtow+token;
162 
163  LOG(DBG, "token = %d mBtow = 0x%x btowEve=0x%x",token, mBtow, btowEve);
164 
165  const HitTower1 *hit=mEveStream_btow[token].get_hits();
166  const int hitSize=mEveStream_btow[token].get_hitSize();
167 
168  LOG(DBG, "Hits = %d",hitSize);
169 
170  for(i=0;i< hitSize;i++,hit++) {
171  int tower=mRdo2tower[hit->rdo];
172  wrkBtow_et[tower]=hit->et;
173  if(hit->et<par_seedEtThres)continue;
174  wrkBtow_tower_seed[wrkBtow_tower_seed_size++]=tower;
175  }
176  hA[2]->fill(hitSize);
177  int seedTow=-1,seedEta=-1,seedPhi=-1;
178  float clustET=0;
179  btowEve->isFresh=L2bemcGammaEvent2012::kDataFresh;
180 
181  LOG(DBG, "seed size = %d",wrkBtow_tower_seed_size);
182  // ----------- FIND 2x2 CLUSTER AROUND EVERY SEED -----
183  for(i=0; i<wrkBtow_tower_seed_size;i++) {
184  seedTow=wrkBtow_tower_seed[i];
185  seedEta=seedTow%BtowGeom::mxEtaBin;
186  seedPhi=seedTow/BtowGeom::mxEtaBin;
187 
188  //.... find first 2x2 above cluster thresh
189  if (seedEta < BtowGeom::mxEtaBin) {
190  clustET = sumET(seedPhi,seedEta);
191  LOG(DBG, "clustET1=%f, thresh=%f",clustET, par_clusterEtThres);
192 
193  if(clustET>par_clusterEtThres) goto ACCEPT;
194  clustET = sumET(seedPhi-1,seedEta);
195 
196  LOG(DBG, "clustET2=%f, thresh=%f",clustET, par_clusterEtThres);
197 
198  if(clustET>par_clusterEtThres) goto ACCEPT;
199  }
200  if (seedEta > 0 ) {
201  clustET = sumET(seedPhi-1,seedEta-1);
202 
203  LOG(DBG, "clustET3=%f, thresh=%f",clustET, par_clusterEtThres);
204 
205  if(clustET>par_clusterEtThres) goto ACCEPT;
206  clustET = sumET(seedPhi,seedEta-1);
207 
208  LOG(DBG, "clustET4=%f, thresh=%f",clustET, par_clusterEtThres);
209 
210  if(clustET>par_clusterEtThres) goto ACCEPT;
211  }
212  }
213  //.... ABORT
214  btowEve->resultBlob.clusterEt=0;
215  btowEve->resultBlob.meanEtaBin=0;
216  btowEve->resultBlob.meanPhiBin=0;
217  btowEve->resultBlob.trigger=0;
218  btowEve->seedTwID=-1;
219  btowEve->clusterET=0;
220  btowEve->seedET=0;
221  return;
222 
223  ACCEPT:
224  btowEve->clusterET=clustET;
225  btowEve->seedET=wrkBtow_et[seedTow];
226  btowEve->resultBlob.clusterEt=(unsigned char)(clustET*256.0/60.0);
227  btowEve->resultBlob.meanEtaBin=seedEta;
228  btowEve->resultBlob.meanPhiBin=seedPhi;
229  btowEve->seedTwID=seedTow;
230  btowEve->resultBlob.trigger=2;
231 
232  LOG(DBG, "trigger = %d",btowEve->resultBlob.trigger);
233  return;
234 }
235 
236 
237 
238 
239 /* ========================================
240  ======================================== */
241 bool
242 L2bemcGamma2012::decisionUser(int token, int *myL2Result){
243  // INPUT: token + comput() results stored internally
244  // OUTPUT: yes/now + pointer to L2Result
245 
246 
247  // get pointers to internal private event storage
248  L2bemcGammaEvent2012 *btowEve=mBtow+token;
249  LOG(DBG, "token = %d mBtow = 0x%x btowEve=0x%x",token, mBtow, btowEve);
250 
251  LOG(DBG, "trigger = %d",btowEve->resultBlob.trigger);
252 
253  bool triggerDecision=(btowEve->resultBlob.trigger>0);
254 
255  //prescaling is done in Virtual:
256  if (par_RndAcceptPrescale>0) btowEve->resultBlob.trigger+=mRandomAccept;
257 
258  //...... some histos just for fun
259  if(btowEve->isFresh>L2bemcGammaEvent2012::kDataFresh) mhN->fill(6); // stale data
260 
261  btowEve->isFresh++; // mark the data as stale
262 
263 
264 
265 
266  if(btowEve->resultBlob.trigger&2) {
267  mhN->fill(15);
268  hA[6]->fill((int)btowEve->clusterET);
269  hA[7]->fill(btowEve->seedTwID);
270  hA[11]->fill((int)btowEve->seedET);
271  hA[12]->fill((int)(100.*btowEve->seedET/btowEve->clusterET));
272  }
273 
274  if(btowEve->resultBlob.trigger&1) mhN->fill(16);
275 
276  memcpy(myL2Result,&(btowEve->resultBlob),sizeof(L2gammaResult2012));
277 
278  return triggerDecision;
279 }
280 
281 
282 /* ========================================
283  ======================================== */
284 void
285 L2bemcGamma2012::finishRunUser() { /* called once at the end of the run */
286  // do whatever you want, log-file & histo-file are still open
287 
288  if (mLogFile){
289  fprintf(mLogFile,"finishRunUser-%s bhla bhla\n",getName());
290  }
291 
292  const int *hist15Data = hA[7]->getData();
293  for (int i = 0; i < BtowGeom::mxEtaBin; i++) {
294  for (int j = 0; j < BtowGeom::mxPhiBin; j++) {
295  hA[14]->fillW(i,j, hist15Data[i+j*BtowGeom::mxEtaBin]);
296  }
297  }
298 
299 }
300 
301 
302 //=======================================
303 //=======================================
304 void
305 L2bemcGamma2012::createHisto() {
306  setMaxHist(16); // PMN added - histogram count does not seem to be initialiazed anywere.
307  //memset(hA,0,sizeof(hA));
308 
309  hA[2]=new L2Histo(2,"BTOW-compute: #towers w/ energy /event; x: # BTOW towers; y: counts", 100);
310  hA[6]=new L2Histo(6,"BTOW-decision: accepted clust ... ; x: ET(GeV)", 30);// title in initRun
311 
312  hA[7]=new L2Histo(7,"BTOW: accepted Seed Tower .....", 5000); // title in initRun
313  hA[11]=new L2Histo(11,"BTOW: decision Cluster Seed Et; ET GeV", 30); // title in initRun
314  hA[12]=new L2Histo(12,"BTOW: decision ;100*Seed Et/Cluster Et", 100); // title in initRun
315 
316  hA[14]=new L2Histo(14,"BTOW: hot tower projection", BtowGeom::mxEtaBin, BtowGeom::mxPhiBin); // title in initRun
317 
318 }
319 
320 //=======================================
321 //=======================================
322 void
323 L2bemcGamma2012::clearEvent(int token){
324  memset(wrkBtow_et,0,sizeof(wrkBtow_et));
325  wrkBtow_tower_seed_size=0;
326  memset(&(mBtow[token].resultBlob),0, sizeof(L2gammaResult2012));
327 }
328 
329 /* ========================================
330  ======================================== */
331 void
332 L2bemcGamma2012::print2(){ // full , local ADC array
333  int i;
334  printf("pr2-%s: ---BTOW ADC 2D array, only non-zero\n",getName());
335 
336  for(i=0;i<mxBtow;i++) {
337  if(wrkBtow_et[i]<=0) continue;
338  int rdo=mTower2rdo[i];
339  float et=wrkBtow_et[i];
340  printf(" btow: tower=%4d rdo=%4d et=%.3f \n",i,rdo,et);
341  }
342 
343 }
344 
345 /* ========================================
346  ======================================== */
347 void
348 L2bemcGamma2012::print3(){ // seed list
349  int i;
350  printf("pr3-%s: ---seed list, size=%d\n",getName(),wrkBtow_tower_seed_size);
351 
352  for(i=0;i<wrkBtow_tower_seed_size;i++) {
353  int tower=wrkBtow_tower_seed[i];
354  float et=wrkBtow_et[tower];
355  printf(" btow: i=%4d tower=%4d et=%.3f \n",i,tower,et);
356  }
357 
358 }
359 
360 #if 0
361 
362 /* ========================================
363  ======================================== */
364 void
365 L2bemcGamma2012::print4(int token, int hitSize){ // L2-algo input list
366  int i;
367  printf("print4 IS NOT Fully FUNCTIONAL **********************\n");
368  printf("pr1-%s: ---BTOW Sorted ADC list--- size=%d\n",getName(),hitSize);
369  //const HitTower *hit=globEve_btow_hit;
370  for(i=0;i< hitSize;i++) {
371  int adc=0;//(mEveStream_btow[token].get_hits()[wrkBtow_tower_index[i]]).adc;
372  int rdo=0;//(mEveStream_btow[token].get_hits()[wrkBtow_tower_index[i]]).rdo;
373  float et=wrkBtow_et[wrkBtow_tower_index[i]];
374  float ene=0;//(mEveStream_btow[token].get_hits()[wrkBtow_tower_index[i]]).ene;
375  printf(" tower=%2d ",wrkBtow_tower_index[i]);
376  printf(" btow: i=%2d rdo=%4d adc=%d et=%.3f ene=%.3f\n",i,rdo,adc,et,ene);
377  }
378 }
379 
380 #endif
381 /**********************************************************************
382  $Log: L2bemcGamma2012.cxx,v $
383  Revision 1.5 2012/03/21 18:18:03 jml
384  got rid of printfs from 2012 files
385 
386  Revision 1.4 2011/10/19 16:12:11 jml
387  more 2012 stuff
388 
389  Revision 1.3 2011/10/19 15:39:43 jml
390  2012
391 
392  Revision 1.2 2011/10/19 14:34:23 jml
393  added fakeRtsLog.h to turn log statements into printfs
394 
395  Revision 1.1 2011/10/18 15:11:42 jml
396  adding 2012 algorithms
397 
398  Revision 1.1 2011/03/09 16:29:07 pibero
399  Added L2gamma2009
400 
401  Revision 1.6 2008/01/30 21:56:40 balewski
402  E+B high-enery-filter L2-algo fuly functional
403 
404  Revision 1.5 2008/01/30 00:47:17 balewski
405  Added L2-Etow-calib
406 
407  Revision 1.4 2008/01/18 23:29:13 balewski
408  now L2result is exported
409 
410  Revision 1.3 2008/01/17 23:15:51 balewski
411  bug in token-addressed memory fixed
412 
413  Revision 1.2 2008/01/16 23:32:35 balewski
414  toward token dependent compute()
415 
416  Revision 1.1 2007/12/19 02:30:18 balewski
417  new L2-btow-calib-2008
418 
419 
420 
421 */
422 
423