StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
L2pedAlgo12.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <fakeRtsLog.h>
5 
6 /**********************************************************
7  * $Id: L2pedAlgo12.cxx,v 1.4 2012/03/21 18:18:03 jml Exp $
8  * \author Jan Balewski, IUCF, 2006
9  **********************************************************
10  * Descripion:
11  * pedestal algo in L2 , for BTOW & ETOW
12  **********************************************************
13  */
14 
15 
16 #ifdef IS_REAL_L2 //in l2-ana environment
17  #include "../L2algoUtil/L2EmcDb2012.h"
18  #include "../L2algoUtil/L2EmcGeom2012.h"
19  #include "../L2algoUtil/L2Histo.h"
20 #else
21  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb2012.h"
22  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
23 #endif
24 
25 #include "L2pedAlgo12.h"
26 #include "L2pedResults2012.h"
27 
28 //=================================================
29 //=================================================
30 L2pedAlgo12::L2pedAlgo12(const char* name, const char *uid, L2EmcDb2012* db, char* outDir, int resOff)
31  : L2VirtualAlgo2012(name, uid, db, outDir, false, false, resOff) {
32  /* called one per days
33  all memory allocation must be done here
34  */
35 
36  par_pedSubtr=false;
37  par_saveBinary=false;
38  par_dbg=0;
39  par_prescAccept=0;
40 
41  int i;
42  char tit[1000];
43  for(i=0;i<BtowGeom::mxRdo;i++) {
44  sprintf(tit,"BTOW ADC for rdo=%d; ADC+%d",i,-minAdc);
45  btowAdc[i]=new L2Histo(20000+i,tit,maxAdc-minAdc+1);
46  }
47 
48  for(i=0;i<EtowGeom::mxRdo;i++) {
49  sprintf(tit,"ETOW ADC for rdo=%d; ADC ",i);
50  etowAdc[i]=new L2Histo(10000+i,tit,maxAdc-minAdc+1);
51  }
52 
53  // aux histos
54  par_maxMatt=160;
55  setMaxHist(64); // set upper range, I uses only 2^N -it is easier to remember
56 
57  //j memset(hA,0,sizeof(hA));
58  hA[10]=new L2Histo(10,"total event counter; x=cases",6);
59  hA[11]=new L2Histo(11,"L2 time used per input event; x: time (CPU 20*kTics); y: events ",500);
60 
61  // BTOW raw spectra
62  hA[20]=new L2Histo(20,"BTOW pedRes Y=ADC-DBped ; x: chan + 160*crate", 4800);
63  hA[21]=new L2Histo(21,"BTOW pedRes Z=ADC-DBped, saturated @ |3|; x: etaBin ,[-1,+1]; y: phi bin ~sector",40,120);
64  sprintf(tit,"BTOW pedRes ; x: chan + 160*crate ;y: ADC+%d",-minAdc);
65 
66  hA[22]=new L2Histo(22,tit,4800,par_maxMatt);
67 
68  // ETOW raw spectra
69  hA[30]=new L2Histo(30,"ETOW pedRes Y=ADC-DBped ; x: chan + 128*crate", 768);
70  hA[31]=new L2Histo(31,"ETOW pedRes Z=ADC-DBped, saturated @ |3|; x: 12 - Endcap etaBin ,[+1,+2]; y: phi bin ~sector",12,60);
71  sprintf(tit,"ETOW pedRes ; x: chan + 128*crate ;y: ADC+%d",-minAdc);
72 
73  hA[33]=new L2Histo(33,tit,768,par_maxMatt);
74 
75 
76  //------- self-consistency checks, should never fail
77  if (sizeof(L2pedResults2012)!= L2pedResults2012::mySizeChar)
78  criticalError("L2pedAlgo12 has failed consistency check. sizeof(L2pedAlgo12)!= L2pedResults2012::mySizeChar");
79 }
80 
81 /*========================================
82  ======================================== */
83 int
84 L2pedAlgo12::initRunUser(int runNo, int *rc_ints, float *rc_floats) {
85  //myName is not used.
86  // update DB if run # has changed
87  //OLD APPROACH, abandoned on Feb 4, 2008, JanB if(mDb->initRun(runNo)) return -27;
88  // DB must be initialized prior to lookup tables
89 
90  if(mDb->getRun()!=runNo) return -700; // L2EmcDb not initialized properly
91  // unpack input params
92 
93  par_pedSubtr =rc_ints[0]!=0;
94  par_speedFact =rc_ints[1];
95  par_saveBinary=rc_ints[2]!=0;
96  par_dbg =rc_ints[3];
97  par_prescAccept=rc_ints[4];
98 
99  if(par_prescAccept<0) par_prescAccept=0; // prescale can't be negative
100  //note speedFactor can be only powers of 2, range [1-256]
101  if(par_speedFact<1) par_speedFact=1;
102  if(par_speedFact>2) {// ASSURE ONLY POWERS OF 2, round down
103  if(par_speedFact<4) par_speedFact=2;
104  else if(par_speedFact<8) par_speedFact=4;
105  else if(par_speedFact<16) par_speedFact=8;
106  else if(par_speedFact<32) par_speedFact=16;
107  else if(par_speedFact<64) par_speedFact=32;
108  else if(par_speedFact<192) par_speedFact=64;
109  else par_speedFact=192;
110  }
111  // can't be bigger, since there is only 6 etow crates
112 
113  s_lastB=s_lastE=0;
114  s_stepB=BtowGeom::mxRdo/ par_speedFact ;
115  s_stepE=EtowGeom::mxRdo/ par_speedFact ;
116 
117 
118  /* .... clear content */
119  memset(db_btowPed, 0,sizeof(db_btowPed));
120  memset(db_etowPed, 0,sizeof(db_etowPed));
121 
122 
123  int i;
124  int nBtowOk=0, nEtowOk=0;
125  for(i=0; i<EmcDbIndexMax; i++) {
126  const L2EmcDb2012::EmcCDbItem *x=mDb->getByIndex(i);
127  if(mDb->isEmpty(x)) continue; // dropped not mapped channels
128  if (mDb->isBTOW(x) ) {
129  db_btowPed[x->rdo]=(int) (x->ped);
130  nBtowOk++;
131  } else if (mDb->isETOW(x) ) {
132  db_etowPed[x->rdo]=(int) (x->ped);
133  nEtowOk++;
134  }
135  }
136 
137  for(i=0;i<BtowGeom::mxRdo;i++) btowAdc[i]->reset();
138  for(i=0;i<EtowGeom::mxRdo;i++) etowAdc[i]->reset();
139  for(i=0;i<mxHA;i++) if(hA[i]) hA[i]->reset();
140 
141  nInp=0;
142  run_number=runNo;
143  return 0; // OK
144 }
145 
146 
147 /*========================================
148  ======================================== */
149 bool
150 L2pedAlgo12::doPedestals(int inpEveId, int* L2Result,
151  int bemcIn, unsigned short *bemcData,
152  int eemcIn, unsigned short *eemcData){
153  /* STRICT TIME BUDGET START ...., well a bit relaxed for this algo */
154  unsigned long mEveTimeStart;
155  rdtscl_macro(mEveTimeStart);
156  //computeStart();//swapped out previous line for this one.
157  nInp++;
158  hA[10]->fill(0);
159  if(par_prescAccept>0) { // value=1 accepts 100% at maximal speed
160  if((rand()>>4) % par_prescAccept ) return false;
161  hA[10]->fill(5);
162  return true;
163  }
164 
165 
166  /* *****************************************
167  the code below is NOT optimized for speed
168  one would need to make loop indexed by RDO to speed it up
169  not worth for pedestal calculation
170  This version takes 2,500 kTicks/eve --> ~1.5msec
171  ******************************************** */
172 
173  short rdo;
174  if( bemcIn ) {
175  short first=s_lastB%BtowGeom::mxRdo;
176  s_lastB=first+s_stepB;
177  if(first==0) hA[10]->fill(1);
178 
179  for(rdo=first; rdo<s_lastB; rdo++){
180  int adc=bemcData[rdo];
181  if(par_pedSubtr) adc-=db_btowPed[rdo];
182  btowAdc[rdo]->fill(adc-minAdc);
183  }
184  }
185 
186 
187  if( eemcIn ) {
188  short first=s_lastE%EtowGeom::mxRdo;
189  s_lastE=first+s_stepE;
190  if(first==0) hA[10]->fill(2);
191 
192  for(rdo=first; rdo<s_lastE; rdo++){
193  int adc=eemcData[rdo];
194  if(par_pedSubtr) adc-=db_etowPed[rdo];
195  etowAdc[rdo]->fill(adc-minAdc);
196  }
197  }
198 
199  //...this event will be accepted
200  L2pedResults2012 out; // all output bits lump together
201  memset(&out,0,sizeof(out));
202  out.int0.decision=
203  ( (bemcIn>0) <<3 ) +
204  ( (eemcIn>0) <<4 ) +
205 
206  ( par_pedSubtr <<6 ) ;
207 
208  unsigned long mEveTimeStop;
209  rdtscl_macro(mEveTimeStop);
210  unsigned long mEveTimeDiff=mEveTimeStop-mEveTimeStart;
211  int kTick=mEveTimeDiff/1000;
212  hA[11]->fill(kTick/20);
213 
214  const unsigned short maxKT=30000;
215  out.int0.kTick= kTick>maxKT ? maxKT : (int)kTick;
216 
217  int *outPlace=L2Result+ mResultOffset;
218  memcpy(outPlace,&out,sizeof( L2pedResults2012));
219 
220  if(par_dbg) L2pedResults2012_print(&out);
221 
222  // computeStop(token);
223 
224  return true;
225 }
226 
227 
228 /* ========================================
229  ======================================== */
230 void
231 L2pedAlgo12::computeUser(int token ){
232 
233  criticalError("L2pedAlgo12::computeUser has been called and should not have been. Serious problem in L2");
234 
235 }
236 
237 /*========================================
238  ======================================== */
239 void
240 L2pedAlgo12::finishRunUser() {/* called once at the end of the run */
241  if(run_number<=0) return; // algo not used for current run
242 
243  int nBtowLow=0, nBtowHigh=0 ,nEtowLow=0, nEtowHigh=0 ;
244 
245  if(mLogFile==0) { LOG(ERR,"no open output log file,skip ped_finish()\n"); return;}
246  fprintf(mLogFile,"#L2-ped algorithm finishRun(%d), compiled: %s , %s\n",run_number,__DATE__,__TIME__);
247  fprintf(mLogFile,"#params: pedSubtr=%d speedFact=%d saveBin=%d debug=%d prescAccept=%d\n",par_pedSubtr,par_speedFact,par_saveBinary,par_dbg,par_prescAccept);
248  hA[10]->printCSV(mLogFile); // event accumulated
249  int iMax=-3, iFWHM=-4;
250  hA[11]->findMax( &iMax, &iFWHM);
251  fprintf(mLogFile,"#L2ped CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",iMax, iFWHM,nInp);
252  if(par_saveBinary) fprintf(mLogFile,"#L2ped will save full spectra for all towers\n");
253 
254  int par_topAdc=85;
255  int maxPedDeviation=5;
256  int iadcHigh=maxAdc - maxPedDeviation;
257  int iadcLow =minAdc + maxPedDeviation;
258 
259  char xAxis[100];
260  sprintf(xAxis,"raw ADC + %d",-minAdc);
261  if(par_pedSubtr) sprintf(xAxis,"ADC - ped + %d",-minAdc);
262 
263 
264  fprintf(mLogFile,"# L2ped-Adc spectra, run=%d, Z-scale is ln(yield), only first digit shown; maxPedDev=%d table format:\n# name, ped, sigPed, crate, chan, softID-m-s-e, RDO_ID;\n# ADC spectrum: [%d ... <=-10 ... *=0 ... >=+10 ... :=+20 ... %d], Xaxis=%s\n",run_number,maxPedDeviation,minAdc,par_topAdc,xAxis);
265 
266  int i;
267  int nB=0;
268  for(i=0; i<EmcDbIndexMax; i++) {
269  const L2EmcDb2012::EmcCDbItem *x=mDb->getByIndex(i);
270  if(mDb->isEmpty(x)) continue; /* dropped not mapped channels */
271  if (mDb->isBTOW(x) ||mDb->isETOW(x) ) { //
272  nB++;
273  /* if(nB>30) return; */
274  int iMax=-3, iFWHM=-4;
275  char pedQA='?';
276  L2Histo *h=0;
277  if(mDb->isBTOW(x)) h= btowAdc[x->rdo];
278  else if(mDb->isETOW(x)) h= etowAdc[x->rdo];
279  else continue;
280 
281 
282  int pedRes=999;
283  int maxRes=3;
284  if(h->findMax( &iMax, &iFWHM)) {
285  pedQA='0';
286  if(iMax<iadcLow) pedQA='-';
287  else if(iMax>iadcHigh) pedQA='+';
288  pedRes=iMax+minAdc;
289  if(!par_pedSubtr) pedRes=int(pedRes - x->ped); // this looks funny but is right
290  }
291 
292  if(mDb->isBTOW(x)) { //BTOW ...............
293  if(pedQA=='-' ) nBtowLow++;
294  else if(pedQA=='+') nBtowHigh++;
295  //........ residual monito histos ....
296  int ieta= (x->eta-1);
297  int iphi= (x->sec-1)*10 + x->sub-'a' ;
298  int ihard=x->chan+(x->crate-1)*160;
299  if(x->fail) pedRes =0;
300  hA[20]->fillW(ihard,pedRes);
301  if(x->fail) {
302  pedRes =-100;
303  } else {
304  if(pedRes<-maxRes) pedRes=-maxRes;
305  if(pedRes>maxRes) pedRes=maxRes;
306  }
307  hA[21]->fillW(ieta, iphi,pedRes);
308 
309  // copy fraction of 1Dped histos to 2D histos
310  const int *Data=h->getData();
311  for(int k=0;k<par_maxMatt;k++)
312  hA[22]->fillW(ihard,k,Data[k]);
313 
314  } else { // ETOW ...........
315  if(pedQA=='-' ) nEtowLow++;
316  else if(pedQA=='+') nEtowHigh++;
317  //........ residual monito histos ....
318  int ieta= 12-x->eta;
319  int iphi= (x->sec-1)*5 + x->sub-'A' ;
320  int ihard=x->chan+(x->crate-1)*128;
321  if(x->fail) pedRes =0;
322  hA[30]->fillW(ihard,pedRes);
323  if(x->fail) {
324  pedRes =-100;
325  } else {
326  if(pedRes<-maxRes) pedRes=-maxRes;
327  if(pedRes>maxRes) pedRes=maxRes;
328  }
329  hA[31]->fillW(ieta, iphi,pedRes);
330 
331  // copy fraction of 1Dped histos to 2D histos
332  const int *Data=h->getData();
333  for(int k=0;k<par_maxMatt;k++)
334  hA[33]->fillW(ihard,k,Data[k]);
335 
336  }
337 
338  char okC=' ';
339  if(x->fail) okC='#';
340  fprintf(mLogFile,"%c%s %3d %4.1f 0x%02x 0x%02x %15s %4d ",okC,x->name,iMax+minAdc,iFWHM/2.3,x->crate, x->chan,x->tube, x->rdo);
341  h->printPed(mLogFile,minAdc,par_topAdc,' ');
342  fprintf(mLogFile,"qa=%c\n",pedQA);
343  // exit(1);
344  } /* end of BTOW & ETOW */
345  } /* end of pixels */
346 
347 
348  fprintf(mLogFile,"#L2ped_finishRun() # of towers with |ped-pedDB| >10 chan\n# BTOW: nLow=%d nHigh=%d ; ETOW nLow=%d, nHigh=%d\n",nBtowLow, nBtowHigh,nEtowLow, nEtowHigh);
349  fprintf(mLogFile,"# found peds for nB+E=%d , seen events=%d\n",nB,nInp);
350 
351  // sprintf(fname,"%s/run%d.l2ped.hist.bin",mOutDir1.c_str(),run_number); // full spectra in binary form
352 
353  if(mHistFile==0) {
354  LOG(ERR,"Can't open .hist.bin file,skip ped_finish()\n");
355  goto end;
356  }
357 
358 
359  if(par_saveBinary) {
360  LOG(DBG,"l2ped_finish() , save FULL spectra binary...\n");
361 
362  // .................. SAVE FULL SPECTRA BINARY ...........
363  for(i=0; i<EmcDbIndexMax; i++) {
364  const L2EmcDb2012::EmcCDbItem *x=mDb->getByIndex(i);
365  if(mDb->isEmpty(x)) continue; /* dropped not mapped channels */
366  L2Histo *h=0;
367  char tit[400];
368  if(mDb->isBTOW(x) ){
369  h= btowAdc[x->rdo];
370  //L2EmcDb2012::printItem(x);
371  sprintf(tit,"BTOW=%s cr/ch=%03d/%03d stat/0x=%04x+%04x soft=%s; %s",x->name, x->crate, x->chan, x->stat, x->fail,x->tube,xAxis);
372  } else if(mDb->isETOW(x) ) {
373  h= etowAdc[x->rdo];
374  //L2EmcDb2012::printItem(x);
375  sprintf(tit,"ETOW=%s cr/ch=%03d/%03d stat/0x=%04x+%04x pname=%s; %s",x->name, x->crate, x->chan, x->stat, x->fail,x->tube,xAxis);
376  }
377  if(h==0) continue; //just in case
378  h->setTitle(tit);
379  h->write(mHistFile); // change title, add cr/chan/name, pedSubtrFlag
380  // break;
381  }
382  }
383 
384  end:
385  run_number=-2;
386 }
387 
388 
389 
390 /**********************************************************************
391  $Log: L2pedAlgo12.cxx,v $
392  Revision 1.4 2012/03/21 18:18:03 jml
393  got rid of printfs from 2012 files
394 
395  Revision 1.3 2011/10/19 16:12:12 jml
396  more 2012 stuff
397 
398  Revision 1.2 2011/10/19 15:39:44 jml
399  2012
400 
401  Revision 1.1 2011/10/18 15:11:43 jml
402  adding 2012 algorithms
403 
404  Revision 1.1 2010/04/17 17:14:37 pibero
405  *** empty log message ***
406 
407  Revision 1.10 2007/11/19 22:18:31 balewski
408  most L2algos provide triggerID's
409 
410  Revision 1.9 2007/11/18 21:58:58 balewski
411  L2algos triggerId list fixed
412 
413  Revision 1.8 2007/11/13 23:06:09 balewski
414  toward more unified L2-algos
415 
416  Revision 1.7 2007/11/13 00:12:38 balewski
417  added offline triggerID, take1
418 
419  Revision 1.6 2007/11/08 04:02:33 balewski
420  run on l2ana as well
421 
422  Revision 1.5 2007/11/02 20:44:54 balewski
423  cleanup
424 
425  Revision 1.4 2007/11/02 17:43:11 balewski
426  cleanup & it started to work w/ L2upsilon
427 
428  Revision 1.3 2007/11/02 03:03:50 balewski
429  modified L2VirtualAlgo
430 
431  Revision 1.2 2007/10/25 02:07:06 balewski
432  added L2upsilon & binary event dump
433 
434  Revision 1.1 2007/10/11 00:33:24 balewski
435  L2algo added
436 
437  Revision 1.5 2006/03/28 19:46:51 balewski
438  ver16b, in l2new
439 
440  Revision 1.4 2006/03/11 17:08:35 balewski
441  now CVS comments should work
442 
443 */
444