Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
intt_pool.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file intt_pool.cc
1 #include "intt_pool.h"
2 #include <string.h>
3 
4 using namespace std;
5 
6 #define coutfl std::cout << __FILE__<< " " << __LINE__ << " "
7 #define cerrfl std::cerr << __FILE__<< " " << __LINE__ << " "
8 
9 
10 enum ITEM
11 {
12  F_BCO = 1,
22 };
23 
24 
25 
26 
27 intt_pool::intt_pool( const unsigned int depth, const unsigned int low_mark )
28 {
29  _required_depth = depth;
30  _low_mark=low_mark;
31  //last_index.fill(0);
32  for ( int fee = 0; fee < MAX_FEECOUNT; fee++)
33  {
34  last_index[fee] = 0;
35  }
36 }
37 
39 {
40  int fee, i;
41 
42  if ( _myPacketid == -1)
43  {
44  _myPacketid = p->getIdentifier();
45  }
46  else
47  {
48  if (_myPacketid != p->getIdentifier())
49  {
50  cerrfl << " received packet " << p->getIdentifier() << " for pool for id " << _myPacketid << endl;
51  return -1;
52  }
53  }
54 
55 
56  // coutfl << " adding packet ";
57  // p->identify();
58 
59 
60 
61  for ( fee = 0; fee < MAX_FEECOUNT; fee++)
62  {
63  for ( i = 0; i < p->iValue(fee, "FEE_LENGTH"); i++)
64  {
65  //coutfl << " pushing back for FEE " << setw(2)<<i << " " << hex << p->iValue(fee,i, "") << dec <<endl;
66  fee_data[fee].push_back(p->iValue(fee,i, "") );
67  }
68  // coutfl << "fee " << fee << " size now " << fee_data[fee].size() << endl;
69  }
70 
71 
72 
73  return 0;
74 }
75 
76 unsigned int intt_pool::rawValue(const int fee, const int index)
77 {
78  if ( fee < 0 || fee >= MAX_FEECOUNT) return 0;
79  if ( index < 0 || (unsigned int) index >= fee_data[fee].size() ) return 0;
80  return fee_data[fee][index];
81 }
82 
83 int intt_pool::iValue(const int fee, const char *what)
84 {
85 
86  if ( strcmp(what,"FEE_LENGTH") == 0)
87  {
88  if ( fee < 0 || fee >= MAX_FEECOUNT) return 0;
89  return fee_data[fee].size();
90  }
91 
92 
93  intt_decode();
94  int hit = fee;
95 
96  if ( strcmp(what,"NR_HITS") == 0)
97  {
98  return intt_hits.size();
99  }
100 
101  if ( strcmp(what,"ADC") == 0)
102  {
103  return iValue(hit,F_ADC);
104  }
105 
106  else if ( strcmp(what,"AMPLITUDE") == 0)
107  {
108  return iValue(hit,F_AMPLITUDE);
109  }
110 
111  if ( strcmp(what,"CHIP_ID") == 0)
112  {
113  return iValue(hit,F_CHIP_ID);
114  }
115 
116  if ( strcmp(what,"CHANNEL_ID") == 0)
117  {
118  return iValue(hit,F_CHANNEL_ID);
119  }
120 
121  if ( strcmp(what,"FULL_FPHX") == 0)
122  {
123  return iValue(hit,F_FULL_FPHX);
124  }
125 
126  if ( strcmp(what,"FEE") == 0)
127  {
128  return iValue(hit,F_FEE);
129  }
130 
131  if ( strcmp(what,"FPHX_BCO") == 0)
132  {
133  return iValue(hit,F_FPHX_BCO);
134  }
135 
136  if ( strcmp(what,"FULL_FPHX") == 0)
137  {
138  return iValue(hit,F_FULL_FPHX);
139  }
140 
141  if ( strcmp(what,"FULL_ROC") == 0)
142  {
143  return iValue(hit,F_FULL_ROC);
144  }
145 
146  if ( strcmp(what,"DATAWORD") == 0)
147  {
148  return iValue(hit,F_DATAWORD);
149  }
150 
151  return 0;
152 }
153 
154 
155 long long intt_pool::lValue(const int hit, const int field)
156 {
157  intt_decode();
158  if ( hit < 0 || hit >= (int) intt_hits.size()) return 0;
159 
160  switch (field)
161  {
162  case F_BCO:
163  return intt_hits[hit]->bco;
164  break;
165  }
166 
167  return 0;
168 }
169 
170 long long intt_pool::lValue(const int hit, const char *what)
171 {
172  intt_decode();
173 
174  if ( strcmp(what,"BCO") == 0)
175  {
176  return lValue(hit,F_BCO);
177  }
178  return 0;
179 }
180 
181 
182 
183 unsigned int intt_pool::min_depth() const
184 {
185  unsigned int d = 0;
186 
187  for ( int fee = 0; fee < MAX_FEECOUNT ; fee++)
188  {
189  if ( fee_data[fee].size() > d) d = fee_data[fee].size();
190  }
191 
192  return d;
193 }
194 
195 int intt_pool::iValue(const int hit, const int field)
196 {
197  intt_decode();
198  if ( hit < 0 || hit >= (int) intt_hits.size()) return 0;
199 
200 
201  switch (field)
202  {
203  case F_FEE:
204  return intt_hits[hit]->fee;
205  break;
206 
207  case F_CHANNEL_ID:
208  return intt_hits[hit]->channel_id;
209  break;
210 
211  case F_CHIP_ID:
212  return intt_hits[hit]->chip_id;
213  break;
214 
215  case F_ADC:
216  return intt_hits[hit]->adc;
217  break;
218 
219  case F_FPHX_BCO:
220  return intt_hits[hit]->FPHX_BCO;
221  break;
222 
223  case F_FULL_FPHX:
224  return intt_hits[hit]->full_FPHX;
225  break;
226 
227  case F_FULL_ROC:
228  return intt_hits[hit]->full_ROC;
229  break;
230 
231  case F_AMPLITUDE:
232  return intt_hits[hit]->amplitude;
233  break;
234 
235  case F_DATAWORD:
236  return intt_hits[hit]->word;
237  break;
238 
239  }
240 
241  return 0;
242 }
243 
245 {
246  if (verbosity > 5)
247  {
248  std::cout << "current Pool depth " << min_depth()
249  << " required depth: " << _required_depth
250  << std::endl;
251  }
252  return ( min_depth() >= _required_depth);
253 }
254 
256 {
257  _is_decoded = 0;
258  std::vector<intt_hit*>::const_iterator hit_itr;
259 
260  // coutfl << "deleting " << intt_hits.size() << " hits" << endl;
261 
262  for ( hit_itr = intt_hits.begin(); hit_itr != intt_hits.end(); ++hit_itr)
263  {
264  // coutfl << "deleting 0x" << hex << (*hit_itr)->bco << dec << endl;
265  delete (*hit_itr);
266  }
267  intt_hits.clear();
268 
269  return 0;
270 }
271 
272 
274 {
275 
276  // coutfl << " pool depth too small still: " << min_depth() << " required " << _depth << endl;
277  if (! depth_ok() )
278  {
279  return 0;
280  }
281 
282 
283  if (_is_decoded) return 0;
284  _is_decoded = 1;
285 
286 
287 
288  for ( int fee = 0 ; fee < MAX_FEECOUNT ; fee++)
289  {
290 
291  unsigned int j = 0;
292 
293  // for ( j = 0; j < fee_data[fee].size(); j++)
294  // {
295  // coutfl << "fee " << fee << " " << j << " found code 0x" << hex << fee_data[fee][j] << dec << endl;
296  // }
297 
298 
299  // int go_on = 0;
300  int header_found = 0;
301 
302 
303  std::vector<unsigned int> hitlist;
304  j = 0;
305 
306 
307  unsigned int remaining = fee_data[fee].size() - _low_mark;
308  if ( fee_data[fee].size() < _low_mark) remaining = 0;
309 
310  while ( j < (remaining) )
311  {
312 
313  //skip until we have found the first header
314  if (! header_found && (fee_data[fee][j] & 0xff00ffff )!= 0xad00cade )
315  {
316  //coutfl<<"skip until header fee "<<fee<<" j="<<j<<" "<<hex<<fee_data[fee][j]<<dec<<endl;
317 
318  j++;
319  last_index[fee] = j;
320  if ( j > fee_data[fee].size()) coutfl << "Warning " << j << " " << fee_data[fee].size() << endl;
321  continue;
322  }
323  header_found = 1;
324 
325  // here j points to a "cade" word
326 
327  // push back the cdae word, the BCO, and event counter
328  if ( fee_data[fee].size() -j >=3 )
329  {
330  for ( int k = 0; k < 3; k++) hitlist.push_back(fee_data[fee][j++]);
331  last_index[fee] = j;
332  }
333  else
334  {
335  coutfl << " Warning - size is " << fee_data[fee].size() << " probably cut off" << endl;
336  break;
337  }
338  last_index[fee] = j;
339  if ( j > fee_data[fee].size()) coutfl << "Warning " << j << " " << fee_data[fee].size() << endl;
340  // ok, now let's go until we hit the end, or hit the next header, or a footer
341 
342  while ( j < fee_data[fee].size() ) // note we don't stop at the "leftover" amount here
343  {
344 
345  // we break here if find the next header or a footer
346  if ( ( fee_data[fee][j] & 0xff00ffff ) == 0xad00cade )
347  {
348  header_found = 0;
349  j--;
350  last_index[fee] = j;
351  if ( j > fee_data[fee].size()) coutfl << "Warning " << j << " " << fee_data[fee].size() << endl;
352  // we have a full hitlist in the vector here
353  coutfl << "calling decode with size " << hitlist.size() << endl;
354  intt_decode_hitlist (hitlist, fee);
355  hitlist.clear();
356  break;
357  }
358 
359 
360  if ( fee_data[fee][j] == 0xcafeff80 )
361  {
362  // we have a full hitlist in the vector here
363  //x coutfl << "calling decode with size " << hitlist.size() << endl;
364  //coutfl << "calling decode for FEE " << fee << " with size " << hitlist.size() << endl;
365  intt_decode_hitlist (hitlist, fee);
366  hitlist.clear();
367  j++;
368  last_index[fee] = j;
369  if ( j > fee_data[fee].size()) coutfl << "Warning " << j << " " << fee_data[fee].size() << endl;
370 
371  break;
372  }
373 
374  hitlist.push_back(fee_data[fee][j]);
375 
376  j++;
377  last_index[fee] = j;
378 
379  }
380  last_index[fee] = j;
381 
382 
383  remaining = fee_data[fee].size() - _low_mark;
384  if ( fee_data[fee].size() < _low_mark) remaining = 0;
385 
386 
387  }
388 
389  //--coutfl<<"fee "<<fee
390  //-- <<", remaining "<<remaining
391  //-- <<", datasize "<<fee_data[fee].size()
392  //-- <<", lmark "<< _low_mark
393  //-- <<", last "<<last_index[fee]<< " "
394  //-- <<( fee_data[fee].size()>0&& last_index[fee]>=fee_data[fee].size() ? "WARNING last index exceeds fee_data_size" : "")
395  //-- <<endl;
396 
397 
398  //--for(unsigned int ii=last_index[fee]; ii<fee_data[fee].size(); ii++){
399  //-- coutfl<<" data:"<<ii<<" "<<hex<<fee_data[fee][ii]<<dec<<endl;
400  //--}
401 
402  // all data is not in this pool. need to wait next pool data.
403  // remaining data should be decoded in the next pool (after all data comes)
404  // to do this, last_index goes back to the last header
405  if(hitlist.size()>0&&fee_data[fee].size()>0&&last_index[fee]>=fee_data[fee].size()){
406  last_index[fee] -= hitlist.size();
407  hitlist.clear();
408  //coutfl<<" last_index changed : fee "<<fee<<" "<<last_index[fee]
409  // <<" "<<hex<<fee_data[fee][last_index[fee]]<<" "<<fee_data[fee][last_index[fee]+1]<<dec<<endl;
410  }
411 
412  if ( hitlist.size() )
413  {
414  //coutfl << "calling decode for FEE " << fee << " with size " << hitlist.size() << endl;
415  intt_decode_hitlist (hitlist, fee);
416  hitlist.clear();
417  }
418 
419  }
420 
421  for ( int fee = 0 ; fee < MAX_FEECOUNT ; fee++)
422  {
423  //coutfl << "FEE " << fee << " erasing " << last_index[fee] << " words, size is " << fee_data[fee].size() << endl;
424  for ( unsigned int j = 0; j < last_index[fee]; j++)
425  {
426  if ( fee_data[fee].size() ) fee_data[fee].erase(fee_data[fee].begin());
427  }
428  //coutfl << "FEE " << fee << " size is now " << fee_data[fee].size() << endl;
429 
430  }
431 
432  //--for ( int fee = 0 ; fee < MAX_FEECOUNT ; fee++)
433  //-- {
434  //-- coutfl<< "decode end : "<<fee<<" size "<<fee_data[fee].size()<<endl;
435  //-- }
436 
437 
438  return 0;
439 }
440 
441 
442  //coutfl << "next fee: " << fee << " j = " << j << endl;
443 
444  // while ( j < fee_data[fee].size() )
445  // {
446 
447  // //skip until we have found the first header
448  // if (! header_found && (fee_data[fee][j] & 0xff00ffff )!= 0xad00cade )
449  // {
450  // j++;
451  // continue;
452  // }
453  // header_found = 1;
454  // last_index = j; // remember that we found a header here
455 
456  // // coutfl << "fee " << fee << " found code 0x" << hex << fee_data[fee][j] << dec << " last_index " << last_index << endl;
457 
458  // unsigned long long l = 0;
459 
460  // // 1st word --- cade add9 87ea 0fe3 cade add9
461  // l = fee_data[fee][j];
462  // // coutfl << "fee " << i << " BCO MSB " << hex << l << dec << endl;
463  // BCO |= ( ((l >> 16 ) & 0xff) << 32);
464  // l = fee_data[fee][j+1];
465  // BCO |= ( (l & 0xffff) << 16);
466  // BCO |= ( (l >> 16) & 0xffff);
467  // //coutfl << "BCO for fee " << setw(3) << fee << " : " << hex << BCO << dec << endl;
468 
469  // if ( !old_BCO ) old_BCO = BCO;
470 
471  // if ( old_BCO && BCO != old_BCO) // ok, we have reached a new BCO here
472  // {
473  // // coutfl << "found a new BCO for fee " << setw(3) << fee << " : " << hex << old_BCO << " - " << BCO << dec << endl;
474  // old_BCO = BCO;
475  // break;
476  // }
477 
478  // // here j points to a "cade" word
479 
480  // // push back the cdae word, the BCO, and event counter
481  // for ( int k = 0; k < 3; k++) hitlist.push_back(fee_data[fee][j++]);
482 
483  // int go_on = 1;
484  // // ok, now let's go until we hit the end, or hit the next header, or a footer
485  // while ( j < fee_data[fee].size() && go_on)
486  // {
487 
488  // // we break here if find the next header or a footer
489  // if ( ( fee_data[fee][j] & 0xff00ffff ) == 0xad00cade )
490  // {
491  // header_found = 0;
492  // j--;
493  // // we have a full hitlist in the vector here
494  // coutfl << "calling intt_decode_hitlist with size " << hitlist.size() << endl;
495  // intt_decode_hitlist (hitlist, fee);
496  // hitlist.clear();
497  // go_on = 0;
498  // }
499 
500 
501  // if ( fee_data[fee][j] == 0xcafeff80 )
502  // {
503  // // we have a full hitlist in the vector here
504  // //coutfl << "calling intt_decode_hitlist with size " << hitlist.size() << endl;
505  // intt_decode_hitlist (hitlist, fee);
506  // hitlist.clear();
507  // j++;
508  // go_on = 0;
509  // }
510 
511  // hitlist.push_back(fee_data[fee][j]);
512  // // coutfl << "pos " << j << " fee length " << fee_data[fee].size()
513  // // << " hit length now " << hitlist.size() << " 0x" << hex << fee_data[fee][j] << dec << endl;
514  // j++;
515  // }
516 
517  // }
518 
519  // // coutfl << " erasing the first " << last_index << " entries from fee " << fee << endl;
520  // for ( int j = 0; j < last_index; j++)
521  // {
522  // fee_data[fee].erase(fee_data[fee].begin());
523  // }
524 
525 
526  // coutfl << "done with BCO 0x" << hex << BCO << dec << " at index " << last_index << " fee size is " << fee_data[fee].size() << endl;
527  // coutfl << "calling intt_decode_hitlist with size " << hitlist.size() << endl;
528  // intt_decode_hitlist (hitlist, fee);
529  // hitlist.clear();
530 
531  // coutfl << " size is now " << fee_data[i].size() << endl;
532 
533 
534 // }
535 // return 0;
536 // }
537 
538 int intt_pool::intt_decode_hitlist (std::vector<unsigned int> &hitlist , const int fee)
539 {
540 
541  // coutfl << " next hitlist, size " << hitlist.size() << " :" << endl;
542 
543  // for ( unsigned int i = 0; i < hitlist.size(); i++)
544  // {
545  // coutfl << i << " " << hex << hitlist[i] << dec << endl;
546  // }
547  // cout << endl;
548 
549  if ( hitlist.size() < 3)
550  {
551  coutfl << "hitlist too short " << endl;
552  return 1;
553  }
554 
555  unsigned long long BCO = 0;
556  unsigned long long l = 0;
557 
558  l = hitlist[0];
559  BCO |= ( ((l >> 16 ) & 0xff) << 32);
560  l = hitlist[1];
561  BCO |= ( (l & 0xffff) << 16);
562  BCO |= ( (l >> 16) & 0xffff);
563  unsigned int event_counter =hitlist[2];
564 
565  int count = 0;
566  for (unsigned int i = 3; i < hitlist.size(); i++)
567  {
568  unsigned int x = hitlist[i];
569  intt_hit * hit= new intt_hit;
570  hit->event_counter = event_counter;
571  hit->fee = fee;
572  hit->bco = BCO;
573  hit->channel_id = (x >> 16) & 0x7f; // 7bits
574  hit->chip_id = (x >> 23) & 0x3f; // 6
575  hit->adc = (x >> 29) & 0x7; // 3
576 
577  hit->FPHX_BCO = x & 0x7f;
578  hit->full_FPHX = (x >> 7) & 0x1; // 1
579  hit->full_ROC = (x >> 8) & 0x1; // 1
580  hit->amplitude = (x >> 9) & 0x3f; // 1
581  hit->word = x;
582  if (verbosity > 1)
583  {
584  if (last_bco[fee] > BCO)
585  {
586  coutfl << "fee " << fee << " old bco : 0x" << std::hex
587  << last_bco[fee] << ", current: 0x" << BCO
588  << std::dec << std::endl;
589  }
590  cout << Name() << " pushing back hit for FEE " << fee << " with BCO 0x" << hex << BCO << dec
591  << " chip " << hit->chip_id<< " channel " << hit->channel_id << " hit length now " << intt_hits.size() << ", last bco: 0x" << hex << last_bco[fee] << dec << endl;
592  last_bco[fee] = BCO;
593  }
594  intt_hits.push_back(hit);
595  count++;
596  }
597  // coutfl << "pushed back " << count << " hits for FEE " << fee << " with BCO 0x" << hex << BCO << dec
598  // << " size of hitlist now " << intt_hits.size() << endl;
599 
600  return 0;
601 }
602 
604 {
605  // os << "number_of_hits: " << iValue(0, "NR_HITS") << endl;
606  intt_decode();
607  // identify(os);
608 
609  os << " Number of hits: " << iValue(0, "NR_HITS") << endl;
610 
611 // std::vector::<intt_hit*>::const_iterator hit_itr;
612 
613  os << " # FEE BCO chip_BCO chip_id channel_id ADC full_phx full_ROC Ampl." << endl;
614 
615  for ( int i = 0; i < iValue(0, "NR_HITS"); i++)
616  {
617  os << setw(4) << i << " "
618  << setw(5) << iValue(i, F_FEE) << " "
619  << hex << setw(11) << lValue(i, F_BCO) << dec << " "
620  << hex << setw(2) << "0x" << iValue(i,F_FPHX_BCO) << dec << " "
621  << setw(5) << iValue(i,F_CHIP_ID) << " "
622  << setw(9) << iValue(i,F_CHANNEL_ID) << " "
623  << setw(5) << iValue(i,F_ADC) << " "
624  << setw(5) << iValue(i,F_FULL_FPHX) << " "
625  << setw(9) << iValue(i,F_FULL_ROC)
626  << setw(8) << iValue(i,F_AMPLITUDE)
627  << " "
628  << "0x" << setw(8) << hex << setfill('0') << iValue(i,F_DATAWORD)
629  << setfill(' ') << dec << endl;
630 
631  }
632 
633 }
634