Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oncsSub_idinttv0.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file oncsSub_idinttv0.cc
1 #include "oncsSub_idinttv0.h"
2 
3 #include <string.h>
4 #include <stdint.h>
5 
6 #define coutfl cout << __FILE__<< " " << __LINE__ << " "
7 #define cerrfl cerr << __FILE__<< " " << __LINE__ << " "
8 
9 using namespace std;
10 
12  :oncsSubevent_w2 (data)
13 {
14 
15  _is_decoded = 0;
16 
17 }
18 
19 
21 {
22  std::vector<intt_hit*>::const_iterator hit_itr;
23 
24  for ( hit_itr = intt_hits.begin(); hit_itr != intt_hits.end(); ++hit_itr)
25  {
26  delete (*hit_itr);
27  }
28  intt_hits.clear();
29 
30 }
31 
32 
33 int oncsSub_idinttv0::intt_decode_hitlist (std::vector<unsigned int> &hitlist , const int fee)
34 {
35 
36  // coutfl << " next hitlist, size " << hitlist.size() << " :" << endl;
37 
38  // for ( unsigned int i = 0; i < hitlist.size(); i++)
39  // {
40  // coutfl << i << " " << hex << hitlist[i] << dec << endl;
41  // }
42  // cout << endl;
43 
44  if ( hitlist.size() < 3)
45  {
46  // coutfl << "hitlist too short " << endl;
47  return 1;
48  }
49 
50  unsigned long long BCO = 0;
51  unsigned long long l = 0;
52 
53  l = hitlist[0];
54  BCO |= ( ((l >> 16 ) & 0xff) << 32);
55  l = hitlist[1];
56  BCO |= ( (l & 0xffff) << 16);
57  BCO |= ( (l >> 16) & 0xffff);
58  unsigned int event_counter =hitlist[2];
59 
60  for (unsigned int i = 3; i < hitlist.size(); i++)
61  {
62  unsigned int x = hitlist[i];
63  intt_hit * hit= new intt_hit;
64  hit->event_counter = event_counter;
65  hit->fee = fee;
66  hit->bco = BCO;
67  hit->channel_id = (x >> 16) & 0x7f; // 7bits
68  hit->chip_id = (x >> 23) & 0x3f; // 6
69  hit->adc = (x >> 29) & 0x7; // 3
70 
71  hit->FPHX_BCO = x & 0x7f;
72  hit->full_FPHX = (x >> 7) & 0x1; // 1
73  hit->full_ROC = (x >> 8) & 0x1; // 1
74  hit->amplitude = (x >> 9) & 0x3f; // 1
75  hit->word = x;
76 
77  intt_hits.push_back(hit);
78  }
79 
80  return 0;
81 }
82 
84 {
85 
86  if (_is_decoded ) return 0;
87  _is_decoded = 1;
88 
89  unsigned int payload_length = ( getLength() - SEVTHEADERLENGTH) - getPadding() ;
90 
91  unsigned int index = 0;
92 
93 
94  unsigned int *buffer = ( unsigned int *) &SubeventHdr->data;
95 
96  while ( index < payload_length)
97  {
98  // find the "ba" index
99  while ( (buffer[index] & 0xff00ffff ) != 0xf000caf0 )
100  {
101  coutfl << "skipping at " << index << " values " << hex << buffer[index] << dec << endl;
102  index++;
103  if (index >= payload_length)
104  {
105  coutfl << " reached end at " << index << " values " << hex << buffer[index] << dec << endl;
106  _broken = 1;
107  return -1;
108  }
109  }
110 
111 
112  unsigned short fee = ( buffer[index] >> 20 ) & 0xf;
113  unsigned short len = ( (buffer[index] >> 16) & 0xf) >>1;
114  //coutfl << "found start at index " << index << " values " << hex << buffer[index] << dec << " fee: " << fee << " len: " << len << endl;
115  index++;
116 
117  for ( int i = 0; i < len ; i++)
118  {
119  // coutfl << "adding to ladder " << fee << " " << hex << buffer[index] << dec << endl;
120  fee_data[fee].push_back(buffer[index++]);
121  }
122  }
123 
124  // for ( int fee = 0; fee < MAX_FEECOUNT; fee++)
125  // {
126  // for ( unsigned int i = 0; i < fee_data[fee].size(); i++)
127  // {
128  // cout << setw(3) << fee << " " << hex << fee_data[fee][i] << dec << endl;
129  // }
130  // cout << endl;
131  // }
132 
133 
134 
135  for ( int fee = 0 ; fee < MAX_FEECOUNT ; fee++)
136  {
137 
138  unsigned int j;
139 
140  // for ( j = 0; j < fee_data[fee].size(); j++)
141  // {
142  // coutfl << "fee " << fee << " " << j << " found code 0x" << hex << fee_data[fee][j] << dec << endl;
143  // }
144 
145 
146  // int go_on = 0;
147  int header_found = 0;
148 
149  std::vector<unsigned int> hitlist;
150  j = 0;
151 
152  while ( j < fee_data[fee].size() )
153  {
154 
155  //skip until we have found the first header
156  if (! header_found && (fee_data[fee][j] & 0xff00ffff )!= 0xad00cade )
157  {
158  j++;
159  continue;
160  }
161  header_found = 1;
162 
163  // here j points to a "cade" word
164 
165  // push back the cdae word, the BCO, and event counter
166  if ( fee_data[fee].size() >=3 )
167  {
168  for ( int k = 0; k < 3; k++) hitlist.push_back(fee_data[fee][j++]);
169  }
170  else
171  {
172  coutfl << " Warning - size is " << fee_data[fee].size() << endl;
173  }
174 
175 
176  // ok, now let's go until we hit the end, or hit the next header, or a footer
177  while ( j < fee_data[fee].size() )
178  {
179 
180  // we break here if find the next header or a footer
181  if ( ( fee_data[fee][j] & 0xff00ffff ) == 0xad00cade )
182  {
183  header_found = 0;
184  j--;
185  // we have a full hitlist in the vector here
186  //coutfl << "calling decode with size " << hitlist.size() << endl;
187  intt_decode_hitlist (hitlist, fee);
188  hitlist.clear();
189  break;
190  }
191 
192 
193  if ( fee_data[fee][j] == 0xcafeff80 )
194  {
195  // we have a full hitlist in the vector here
196  // coutfl << "calling decode with size " << hitlist.size() << endl;
197  intt_decode_hitlist (hitlist, fee);
198  hitlist.clear();
199  j++;
200  break;
201  }
202 
203  hitlist.push_back(fee_data[fee][j]);
204 
205  j++;
206  }
207  }
208 
209  // coutfl << "calling decode with size " << hitlist.size() << endl;
210  intt_decode_hitlist (hitlist, fee);
211  hitlist.clear();
212 
213  }
214  return 0;
215 }
216 
217 enum ITEM
218 {
219  F_BCO = 1,
229 };
230 
231 
232 int oncsSub_idinttv0::iValue(const int hit, const int field)
233 {
234  intt_decode();
235  if ( hit < 0 || hit >= (int) intt_hits.size()) return 0;
236 
237  switch (field)
238  {
239  case F_FEE:
240  return intt_hits[hit]->fee;
241  break;
242 
243  case F_CHANNEL_ID:
244  return intt_hits[hit]->channel_id;
245  break;
246 
247  case F_CHIP_ID:
248  return intt_hits[hit]->chip_id;
249  break;
250 
251  case F_ADC:
252  return intt_hits[hit]->adc;
253  break;
254 
255  case F_FPHX_BCO:
256  return intt_hits[hit]->FPHX_BCO;
257  break;
258 
259  case F_FULL_FPHX:
260  return intt_hits[hit]->full_FPHX;
261  break;
262 
263  case F_FULL_ROC:
264  return intt_hits[hit]->full_ROC;
265  break;
266 
267  case F_AMPLITUDE:
268  return intt_hits[hit]->amplitude;
269  break;
270 
271  case F_DATAWORD:
272  return intt_hits[hit]->word;
273  break;
274 
275  }
276 
277  return 0;
278 }
279 
280 int oncsSub_idinttv0::iValue(const int fee, const int index, const char * what)
281 {
282  if ( fee < 0 || fee >= MAX_FEECOUNT) return 0;
283 
284  if ( index < 0 || (unsigned int) index >= fee_data[fee].size() ) return 0;
285  intt_decode();
286  return fee_data[fee][index];
287 }
288 
289 long long oncsSub_idinttv0::lValue(const int hit, const int field)
290 {
291  intt_decode();
292  if ( hit < 0 || hit >= (int) intt_hits.size()) return 0;
293 
294  switch (field)
295  {
296  case F_BCO:
297  return intt_hits[hit]->bco;
298  break;
299  }
300 
301  return 0;
302 }
303 
304 
305 int oncsSub_idinttv0::iValue(const int hit, const char *what)
306 {
307  intt_decode();
308 
309  if ( strcmp(what,"NR_HITS") == 0)
310  {
311  return intt_hits.size();
312  }
313 
314  if ( strcmp(what,"FEE_LENGTH") == 0)
315  {
316  if ( hit < 0 || hit >= MAX_FEECOUNT) return 0;
317  return fee_data[hit].size();
318  }
319 
320 
321 
322 
323  else if ( strcmp(what,"ADC") == 0)
324  {
325  return iValue(hit,F_ADC);
326  }
327 
328  else if ( strcmp(what,"AMPLITUDE") == 0)
329  {
330  return iValue(hit,F_AMPLITUDE);
331  }
332 
333  if ( strcmp(what,"CHIP_ID") == 0)
334  {
335  return iValue(hit,F_CHIP_ID);
336  }
337 
338  if ( strcmp(what,"CHANNEL_ID") == 0)
339  {
340  return iValue(hit,F_CHANNEL_ID);
341  }
342 
343  if ( strcmp(what,"FULL_FPHX") == 0)
344  {
345  return iValue(hit,F_FULL_FPHX);
346  }
347 
348  if ( strcmp(what,"FEE") == 0)
349  {
350  return iValue(hit,F_FEE);
351  }
352 
353  if ( strcmp(what,"FPHX_BCO") == 0)
354  {
355  return iValue(hit,F_FPHX_BCO);
356  }
357 
358  if ( strcmp(what,"FULL_FPHX") == 0)
359  {
360  return iValue(hit,F_FULL_FPHX);
361  }
362 
363  if ( strcmp(what,"FULL_ROC") == 0)
364  {
365  return iValue(hit,F_FULL_ROC);
366  }
367 
368  if ( strcmp(what,"DATAWORD") == 0)
369  {
370  return iValue(hit,F_DATAWORD);
371  }
372 
373  return 0;
374 }
375 
376 long long oncsSub_idinttv0::lValue(const int hit, const char *what)
377 {
378  intt_decode();
379 
380  if ( strcmp(what,"BCO") == 0)
381  {
382  return lValue(hit,F_BCO);
383  }
384  return 0;
385 }
386 
387 
389 {
390  // os << "number_of_hits: " << iValue(0, "NR_HITS") << endl;
391  intt_decode();
392  identify(os);
393 
394  os << " Number of hits: " << iValue(0, "NR_HITS") << endl;
395 
396 // std::vector<intt_hit*>::const_iterator hit_itr;
397 
398  os << " # FEE BCO chip_BCO chip_id channel_id ADC full_phx full_ROC Ampl." << endl;
399 
400  for ( int i = 0; i < iValue(0, "NR_HITS"); i++)
401  {
402  os << setw(4) << i << " "
403  << setw(5) << iValue(i, F_FEE) << " "
404  << hex << setw(11) << lValue(i, F_BCO) << dec << " "
405  << hex << setw(2) << "0x" << iValue(i,F_FPHX_BCO) << dec << " "
406  << setw(5) << iValue(i,F_CHIP_ID) << " "
407  << setw(9) << iValue(i,F_CHANNEL_ID) << " "
408  << setw(5) << iValue(i,F_ADC) << " "
409  << setw(5) << iValue(i,F_FULL_FPHX) << " "
410  << setw(9) << iValue(i,F_FULL_ROC)
411  << setw(8) << iValue(i,F_AMPLITUDE)
412  << " "
413  << "0x" << setw(8) << hex << setfill('0') << iValue(i,F_DATAWORD)
414  << setfill(' ') << dec << endl;
415 
416  }
417 
418 }
419 
420 
421 unsigned short oncsSub_idinttv0::reverseBits(const unsigned short x) const
422 {
423  unsigned short n = x;
424  n = ((n >> 1) & 0x55555555) | ((n << 1) & 0xaaaaaaaa);
425  n = ((n >> 2) & 0x33333333) | ((n << 2) & 0xcccccccc);
426  n = ((n >> 4) & 0x0f0f0f0f) | ((n << 4) & 0xf0f0f0f0);
427  n = ((n >> 8) & 0x00ff00ff) | ((n << 8) & 0xff00ff00);
428  //n = (n >> 16) & 0x0000ffff | (n << 16) & 0xffff0000;
429  return n;
430 }
431 
432 
433 
434 unsigned short oncsSub_idinttv0::crc16(const unsigned int fee, const unsigned int index, const int l) const
435 {
436  int len = l;
437  int i = index;
438  unsigned short crc = 0xffff;
439 
440  while (len--)
441  {
442  unsigned short x = fee_data[fee].at(i++);
443  // cout << "in crc " << hex << x << dec << endl;
444  crc ^= reverseBits( x);
445  for (unsigned short k = 0; k < 16; k++)
446  {
447  crc = crc & 1 ? (crc >> 1) ^ 0xa001 : crc >> 1;
448  }
449  }
450  crc = reverseBits(crc);
451  return crc;
452 }
453