StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
L2VirtualAlgo2008.cxx
1 #include <string.h>
2 #include <stdio.h>
3 
4 #ifdef IS_REAL_L2 //in l2-ana environment
5  #include "trgStructures.h"
6  #include "../L2algoUtil/L2Histo.h"
7  #include "../L2algoUtil/L2EmcDb.h"
8  #else
9  #include "StDaqLib/TRG/trgStructures.h"
10  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2Histo.h"
11  #include "StTriggerUtilities/L2Emulator/L2algoUtil/L2EmcDb.h"
12 #endif
13 
14 //#define ADD_HARDCODED_DELAY // take it off for real on-line
15 
16 #include "L2VirtualAlgo2008.h"
17 //=============================================
18 L2VirtualAlgo2008::L2VirtualAlgo2008(const char* name, L2EmcDb* db, char* outDir) : mDb(db) {
19  mxHA=0;// initialy no user defined histos
20  mName1=name;
21  mOutDir1=outDir;
22 
23  // map L2event variables for _read_
24  mEveStream_btow=globL2eventStream2008.get_btow();
25  mEveStream_etow=globL2eventStream2008.get_etow();
26 
27  setOflTrigID(0); // relevant only for offline analysis
28  mhN =new L2Histo(900,"total events 0=anyInput, 10=anyAccept; x=cases",19);
29  mhTc=new L2Histo(901,"L2 COMPUTE time per input event; x: COMPUTE time (CPU kTics); y: events ",180);
30  mhTd=new L2Histo(902,"L2 DECISION time per input event; x: DECISION time (CPU kTics); y: events ",36);
31  mhTcd=new L2Histo(903,"L2 COMP+DECI time per input event; x: COMP+DECIS time (CPU kTics); y: events ",180);
32 
33  int mxRunDration=200;
34  mhRc= new L2Histo(905,"rate of COMPUTE; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
35  mhRd= new L2Histo(906,"rate of DECISION; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
36  mhRa= new L2Histo(907,"rate of ACCEPT; x: time in this run (seconds); y: rate (Hz)", mxRunDration);
37  printf("L2-%s instantiated, logPath='%s'\n",getName(),mOutDir1.c_str());
38 
39  // consistency checks, should never fail
40  assert(L2eventStream2008::mxToken == L2eventStream2008::tokenMask+1);
41 }
42 
43 /*========================================
44  ======================================== */
45 int
46 L2VirtualAlgo2008::initRun( int runNo, int *rc_ints, float *rc_floats) {
47  if(mDb->getRun()!=runNo) return -700; // L2EmcDb not initialized properly
48 
49  if(mRunNumber==runNo) {
50  if (mLogFile) fprintf(mLogFile,"L2-%s::initRun(%d)=ghost already initilized, Abort run\n",getName(), runNo);
51  return -701;
52  }
53 
54  unsigned int high,low;
55  rdtsc_macro(low,high); // needs also high to get tim in seconds
56  mRunStartTicks=high; mRunStartTicks <<= 32; mRunStartTicks += low;
57 
58  mRunNumber =runNo; // serves as a flag this run is initialized
59 
60  char Fname[1000];
61  sprintf(Fname,"%s/run%d.l2%s.log",mOutDir1.c_str(),mRunNumber,getName());
62  printf("L2-%s::initRun('%s') ...\n",getName(),Fname);
63 
64  mLogFile = fopen(Fname,"w");
65  if( mLogFile==0) printf(" L2-%s() UNABLE to open run summary log file, continue anyhow\n",getName());
66 
67  int kBad=initRunUser( runNo, rc_ints, rc_floats);
68 
69  if (mLogFile) {
70  fprintf(mLogFile,"L2-%s initRun() params checked for consistency, Error flag=0x%04x\n",getName(),kBad);
71  }
72 
73  if(kBad) {
74  if (mLogFile) {
75  fprintf(mLogFile,"L2-%s initRun() ABORT due to internal logic\n",getName());
76  fclose(mLogFile);
77  }
78  mRunNumber=-55;
79  }
80  if (mLogFile) {
81  fprintf(mLogFile,"L2-%s initRun succesfull\n",getName());
82 
83 #ifdef ADD_HARDCODED_DELAY
84  fprintf(mLogFile,"WARN: HARDCODED_DELAY in compute() & decision() is ON\n");
85 #endif
86 
87  }
88 
89  // printf("L2initRaunVirtual2008-%s kBad=%d\n",getName(),kBad);
90 
91  return kBad;
92 }
93 
94 /*========================================
95  ======================================== */
96 void
97 L2VirtualAlgo2008::finishRun() { /* called once at the end of the run */
98 
99  if(mRunNumber<0) return; // already finished
100  if(mLogFile)fprintf(mLogFile,"L2-%s: finishRun(%d) called after %d seconds\n",getName(),mRunNumber ,mSecondsInRun);
101 
102 
103  // save run summary histos
104  char Fname[1000];
105  sprintf(Fname,"%s/run%d.l2%s.hist.bin",mOutDir1.c_str(),mRunNumber,getName());
106  printf("\nL2:%s::finishRun('%s') , save histo ...\n",getName(),Fname);
107  mHistFile = fopen(Fname,"w");
108 
109 
110  if( mHistFile==0) {
111  printf(" L2-%s: finishRun() UNABLE to open run summary log file, continue anyhow\n",getName());
112  if (mLogFile)
113  fprintf(mLogFile,"L2-%s histos NOT saved, I/O error\n",getName());
114  } else { // save histos
115 
116  finishRunUser();
117  int nh=finishCommonHistos();
118 
119  if (mLogFile)
120  fprintf(mLogFile,"L2-%s: %d histos saved to '%s'\n",getName(),nh,Fname);
121  }
122 
123  mRunNumber=-2; // clear run #
124 
125  /* close the output file if it is open */
126  if (mLogFile) {
127  fclose(mLogFile);
128  mLogFile=0;
129  }
130 
131  if ( mHistFile) {
132  fclose(mHistFile);
133  mHistFile=0;
134  }
135 
136 }
137 
138 
139 //=============================================
140 int
141 L2VirtualAlgo2008::finishCommonHistos() {
142  int j;
143  int nh=0;
144  for(j=0;j<mxHA;j++) {
145  if(hA[j]==0) continue;
146  hA[j]->write(mHistFile);
147  nh++;
148  }
149 
150  const int nHt=3;
151  L2Histo *hT[nHt]={mhTc,mhTd,mhTcd};
152  const char *text[nHt]={"Compute ","Decision ","Deci+Comp"};
153  int ih;
154  for(ih=0;ih<nHt;ih++) {
155  int iMax=-3, iFWHM=-4;
156  hT[ih]->findMax( &iMax, &iFWHM);
157  printf("L2-%s %s CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",getName(),text[ih],iMax, iFWHM,mEventsInRun);
158 
159  if (mLogFile){
160  fprintf(mLogFile,"L2:%s %s CPU/eve MPV %d kTicks, FWHM=%d, seen eve=%d\n",getName(),text[ih],iMax, iFWHM,mEventsInRun);
161  // hT[ih] ->print(0,mLogFile); // mhT->printCSV(mLogFile);
162  }
163  if (mHistFile) {hT[ih]->write(mHistFile); nh++;}
164  }
165 
166  if (mHistFile) {
167  mhN->write(mHistFile); nh++;
168  mhRc->write(mHistFile);nh++;
169  mhRd->write(mHistFile);nh++;
170  mhRa->write(mHistFile);nh++;
171  }
172  return nh;
173 }
174 
175 //=============================================
176 
177 L2VirtualAlgo2008::~L2VirtualAlgo2008(){};
178 
179 //=============================================
180 int
181 L2VirtualAlgo2008::readParams(const char *fileN, int mxPar, int *iPar, float *fPar) {
182  /* return:
183  <0 : error in input
184  >=0 : # of valid params : int+float
185  */
186 
187  memset(iPar,0,mxPar*sizeof(int));
188  memset(fPar,0,mxPar*sizeof(int));
189  FILE *fd=fopen(fileN,"r");
190  if(fd==0) { printf(" L2VirtualAlgo2008::readParams failed to open =%s=\n",fileN); return -2222;}
191 
192  int nVal=0; // sum of read in ints & floats
193  int nInt=0, nFloat=0; // # of read in values
194 
195  const int mx=1000;
196  char buf[mx];
197  int mode=0; // 1=int, 2=float
198 
199  for(;;) {
200  char * ret=fgets(buf,mx,fd);
201  //printf("xx1=%p\n",ret);
202  if(ret==0) break;
203  if(buf[0]==0) continue;
204  if(buf[0]=='#') continue;
205  if(buf[0]=='\n') continue;
206 
207  if (mode==0 && strstr(buf,"INTS")) { mode=1; continue; }
208  if (mode==1 && strstr(buf,"FLOATS")) { mode=2; continue; }
209  // printf("AA %d =%s= %p %p \n",mode,buf,strstr(buf,"INTS"),strstr("FLOATS",buf));
210  if(mode==1) { // ints[]
211  if(nInt>=mxPar) {nVal=-501; break;} // too many int-params
212  int ret1=sscanf(buf,"%i",iPar+nInt);
213  if(ret1!=1) {nVal=-100*mode -nInt; break;} // wrong input file for this int-par
214  nInt++; nVal++;
215  }
216  else if(mode==2) { // floats[]
217  if(nFloat>=mxPar) {nVal=-502; break;} // too many float-params
218  int ret1=sscanf(buf,"%f",fPar+nFloat);
219  if(ret1!=1) {nVal= -100*mode -nFloat; break;} // wrong input file for this float-par
220  nFloat++; nVal++;
221  }
222 
223  }
224 
225  fclose(fd);
226  printf(" L2VirtualAlgo2008::readParams %d from '%s'\n",nVal,fileN);
227  return nVal;
228 }
229 
230 
231 //=============================================
232 void
233 L2VirtualAlgo2008::compute(int token){
234  /* STRICT TIME BUDGET START ....*/
235  computeStart();
236  mhN->fill(1);
237  token&=L2eventStream2008::tokenMask; // only protect against bad token, Gerard's trick
238 
239 #ifdef ADD_HARDCODED_DELAY
240  /* for testing of histos,
241  adds 3kTicks delay, - to see sth in the spectra
242  even if algo is very fast */
243  for(int i=0;i<3*100;i++) { float x=i*i; x=x;}
244 #endif
245 
246  computeUser( token );
247  computeStop( token);
248 
249 }
250 
251 //=============================================
252 void
253 L2VirtualAlgo2008::computeStart(){
254 
255  /* STRICT TIME BUDGET START ....*/
256  unsigned int high,low;
257  rdtsc_macro(low,high); // needs also high to get tim in seconds
258 
259  mComputeTimeStart=low;
260  unsigned long long ticks = high;
261  ticks <<= 32;
262  ticks += low;
263 
264  // the line below costs ~100 ticks, wherase use of time(0) costs ~10,000 ticks
265  mSecondsInRun=(ticks- mRunStartTicks)/par_cpuTicksPerSecond;
266 
267  mhRc->fill(mSecondsInRun);
268 
269  mAccept=true; // by default accept every event
270  mEventsInRun++;
271 
272  mhN->fill(0);
273 }
274 
275 //=============================================
276 void
277 L2VirtualAlgo2008::computeStop(int token){
278 
279  rdtscl_macro(mComputeTimeStop);
280  unsigned long xxx=mComputeTimeStop-mComputeTimeStart;
281  mComputeTimeDiff[token]=xxx;
282  int kTick=xxx/1000;
283  //printf("jj delT/kTick=%f t1=%d t2=%d \n",mComputeTimeDiff/1000.,mComputeTimeStart,mComputeTimeStop);
284  mhTc->fill(kTick);
285 }
286 
287 
288 //=============================================
289 bool
290 L2VirtualAlgo2008::decision(int token, void **myL2Result){
291  /* STRICT TIME BUDGET START ....*/
292  /*
293  Chris doesn't want us to write out anything
294  during event processing ...
295  */
296 
297  rdtscl_macro(mDecisionTimeStart);
298  token&=L2eventStream2008::tokenMask; // only protect against bad token, Gerard's trick
299  mDecisionTimeDiff=0;
300 
301  mhRd->fill(mSecondsInRun);
302 
303 #ifdef ADD_HARDCODED_DELAY
304  /* for testing of histos,
305  adds 3kTicks delay, - to see sth in the spectra
306  even if algo is very fast */
307  for(int i=0;i<3*100;i++) { float x=i*i; x=x;}
308 #endif
309 
310  mhN->fill(2);
311  mAccept=decisionUser(token, myL2Result);
312  //printf("compuDDDD tkn=%d dec=%d myRes=%p\n",token,mAccept, *myL2Result);
313 
314  if(mAccept) {
315  mhN->fill(10);
316  mhRa->fill(mSecondsInRun);
317  }
318  rdtscl_macro(mDecisionTimeStop);
319  mDecisionTimeDiff=mDecisionTimeStop-mDecisionTimeStart;
320  int kTick=mDecisionTimeDiff/1000;
321 
322  // printf("deci-%s compT=%d; deciT=%d kTick=%d\n", getName(),mComputeTimeDiff,mDecisionTimeDiff,kTick);
323  mhTd->fill(kTick);
324  kTick=(mDecisionTimeDiff+mComputeTimeDiff[token])/1000;
325  mhTcd->fill(kTick);
326  return mAccept;
327 }
328 
329 
330 
331 /* ========================================
332  ======================================== */
333 void
334 L2VirtualAlgo2008::printCalibratedData(int token){ //
335  // now print always BARREL - fix it later
336  int i;
337  const int hitSize=mEveStream_btow[token].get_hitSize();
338  printf("printCalibratedData-%s: ---BTOW ADC list--- size=%d\n",getName(),hitSize);
339  const HitTower1 *hit=mEveStream_btow[token].get_hits();
340  for(i=0;i< hitSize;i++,hit++) {
341  int adc=hit->adc;
342  int rdo=hit->rdo;
343  float et=hit->et;
344  float ene=hit->ene;
345  printf(" btow: i=%2d rdo=%4d adc=%d et=%.3f ene=%.3f\n",i,rdo,adc,et,ene);
346  }
347 }
348 
349 
350 /******************************************************
351  $Log: L2VirtualAlgo2008.cxx,v $
352  Revision 1.7 2010/04/18 06:05:32 pibero
353  Address compiler warnings.
354 
355  Revision 1.6 2008/01/30 21:56:40 balewski
356  E+B high-enery-filter L2-algo fuly functional
357 
358  Revision 1.5 2008/01/30 00:47:15 balewski
359  Added L2-Etow-calib
360 
361  Revision 1.4 2008/01/18 23:29:12 balewski
362  now L2result is exported
363 
364  Revision 1.3 2008/01/17 23:15:51 balewski
365  bug in token-addressed memory fixed
366 
367  Revision 1.2 2008/01/16 23:32:33 balewski
368  toward token dependent compute()
369 
370 
371 
372 */