ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oncsSub_idsrs_v01.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file oncsSub_idsrs_v01.cc
1 #include "oncsSub_idsrs_v01.h"
2 #include <cstring>
3 
4 #include <arpa/inet.h>
5 
6 using namespace std;
7 
9  :oncsSubevent_w4 (data)
10 {
11  // samples = 0;
12  // nsamples = 0;
13  // hybrid = 0;
14  nhybrids = 0;
15 
16 }
17 
19 {
20  // if ( samples )
21  // {
22  // delete [] samples;
23  // samples = 0;
24  // nsamples = 0;
25  // }
26 
27  std::vector<hybriddata*>::iterator it;
28  std::vector<report *>::iterator reportit;
29 
30 
31  for ( it = hybridlist.begin(); it != hybridlist.end(); ++it)
32  {
33  for ( reportit = (*it)->rowdata.begin(); reportit != (*it)->rowdata.end(); ++reportit)
34  {
35  delete (*reportit);
36  }
37  delete (*it);
38  }
39 
40 }
41 
42 
43 int *oncsSub_idsrs_v01::decode ( int *nwout)
44 {
45  int i;
46 
47  unsigned int *d = (unsigned int *) &SubeventHdr->data; // here begins the payload
48 
49  int dlength = getLength()-4 - getPadding();
50 
51  for ( i = 0; i< dlength; i++)
52  {
53  hybriddata *hd = new hybriddata;
54  if ( d[i] == 0xfafafafa ) break; // end of payload
55  unsigned int dh = ntohl(d[i]);
56  int framecounter = (dh & 0xff);
57  //cout << "framecounter: " << framecounter << endl;
58 
59  hd->framecounter = framecounter;
60 
61  dh = ntohl(d[i+1]);
62  int header = dh;
63 
64  unsigned char c = (header >>24) & 0xff;
65  hd->desc[0] = c;
66  c = (header >>16) & 0xff;
67  hd->desc[1] = c;
68  c = (header >>8) & 0xff;
69  hd->desc[2] = c;
70  hd->desc[3] = 0;
71 
72  unsigned int channel = header & 0xff;
73  //cout << " channel nr: " << channel << endl;
74  hd->hdmi = channel;
75 
76  unsigned int w = ntohl(d[i+2]);
77  int words = w & 0xffff;
78  //cout << " words: " << words << endl;
79 
80  int j = 0; // here begin the samples;
81 
82  while ( d[j + i +3] != 0xf000f000 && j < words )
83  {
84  hd->adc.push_back(d[j+i+3] & 0x0fff);
85  hd->adc.push_back( (d[j+i+3] >> 16) & 0x0fff);
86  j++;
87  }
88 
89 
90  //cout << " EOP marker " << hex << d[j + i +3] << dec << endl;
91  hd->words = hd->adc.size();
92  //cout << " words: " << hd->words << " " << words << endl;
93 
94  analyze ( hd);
95 
96  hybridlist.push_back(hd);
97  nhybrids++;
98  i+= 3+j;
99  }
100 
101  *nwout = 0;
102  return 0;
103 }
104 
105 
107 {
108  int i;
109 
110  int firstfound = 0;
111  int onecount = 0;
112 
113  int current_report = 0;
114 
115  for ( i = 0; i < hd->words-( 128+8); i++)
116  {
117 
118  if ( firstfound)
119  {
120  add_report ( hd, i, current_report++);
121  i += 11+128;
122  }
123  else
124  {
125  if ( hd->adc[i] < 1220) onecount++;
126  if ( onecount == 3)
127  {
128  // cout << " sequence start at " << i -2 << endl;
129  add_report ( hd, i-2, current_report++);
130  onecount = 0;
131 
132  i += ( 9 + 128);
133  firstfound = 1;
134 
135  }
136  if ( onecount && hd->adc[i] >= 1220)
137  {
138  onecount = 0;
139  }
140  }
141  }
142  return 0;
143 }
144 
145 int oncsSub_idsrs_v01::add_report ( hybriddata * hd, const int start, const int nreport)
146 {
147  int i;
148 
149  report *r = new report;
150 
151  r->error = 1;
152  r->n = nreport;
153 
154  if ( hd->adc[start + 11] < 1220)
155  {
156  r->error = 0;
157  }
158  else if ( hd->adc[start + 11] > 3000)
159  {
160  r->error = 1;
161  }
162  else
163  {
164  r->error = -1; // funny bit
165  }
166 
167  int acode = 0;
168  for ( i = 0; i < 8; i++)
169  {
170  if (hd->adc[start + 3 + i] < 1220)
171  {
172  acode = ( acode << 1)+1;
173  }
174  else if (hd->adc[ start + 3 + i] > 2980)
175  {
176  acode = ( acode << 1);
177  }
178  else
179  {
180  // cout << "bit expected but got " << hd->adc[start + 3 + i] << " as bit " << i << endl;
181  acode = ( acode << 1);
182  }
183  }
184  r->address = acode;
185 
186 
187  for (i = 0; i < 128; i++)
188  {
189  int channel = 32 * ( i % 4 ) + 8 * ( i >> 2) - 31 * ( i >> 4);
190  r->adc[channel] = hd->adc[start + 12+i];
191  }
192  hd->rowdata.push_back(r);
193 
194  return 0;
195 }
196 
197 
198 
199 int oncsSub_idsrs_v01::iValue(const int hybrid,const char *what)
200 {
201 
202  std::vector<hybriddata*>::iterator it;
203  if ( nhybrids == 0 ) decoded_data1 = decode(&data1_length);
204 
205  if ( strcmp(what,"NHYBRIDS") == 0 )
206  {
207  return hybridlist.size();
208  }
209 
210  else if ( strcmp(what,"NSAMPLES") == 0 )
211  {
212 
213  for ( it = hybridlist.begin(); it != hybridlist.end(); ++it)
214  {
215  if ( (*it)->hdmi == hybrid )
216  {
217  return (*it)->rowdata.size();;
218  }
219  }
220 
221 
222  }
223 
224  return 0;
225 
226 }
227 
228 
229 int oncsSub_idsrs_v01::iValue(const int ich,const int hybrid, const char *what)
230 {
231 
232  if ( nhybrids == 0 ) decoded_data1 = decode(&data1_length);
233 
234  std::vector<hybriddata*>::iterator it;
235 
236  if ( ich < 0) return 0;
237 
238  if ( strcmp(what,"RAWSAMPLES") == 0 )
239  {
240 
241  for ( it = hybridlist.begin(); it != hybridlist.end(); ++it)
242  {
243  if ( (*it)->hdmi == hybrid )
244  {
245  if ( ich >= (*it)->words) return 0;
246  return (*it)->adc[ich];
247  }
248  }
249  }
250 
251  return 0;
252 }
253 
254 int oncsSub_idsrs_v01::iValue(const int ich, const int tsample, const int hybrid)
255 {
256 
257  if ( ich < 0 || ich > 127) return 0;
258  if ( nhybrids == 0 ) decoded_data1 = decode(&data1_length);
259 
260  std::vector<hybriddata*>::iterator it;
261 // std::vector<report *>::iterator reportit;
262 
263  for ( it = hybridlist.begin(); it != hybridlist.end(); ++it)
264  {
265 
266  if ( (*it)->hdmi == hybrid)
267  {
268 
269  //cout << __LINE__ << " " << __FILE__ << " tsample " << tsample << " " << (*it)->rowdata.size() << endl;
270 
271  if ( (*it)->rowdata.size() < (unsigned int)(tsample +1)) return 0;
272 
273  report *r = (*it)->rowdata[tsample];
274  return r->adc[ich];
275 
276  }
277  }
278  return 0;
279 }
280 
281 
282 
284 {
285 
286  identify(os);
287 
288  int i;
289  int is = iValue(0,"NHYBRIDS");
290 
291 
292  os << "Number of Hybrids: " << is << std::endl;
293 
294  std::vector<hybriddata*>::iterator it;
295  std::vector<report *>::iterator reportit;
296 
297  for ( it = hybridlist.begin(); it != hybridlist.end(); ++it)
298  {
299  os << std::endl;
300  os << "Framecounter: " << (*it)->framecounter << std::endl;
301  os << "HDMI Channel: " << (*it)->hdmi << std::endl;
302  os << "Description: " << (*it)->desc << std::endl;
303  os << "Words: " << (*it)->words << std::endl;
304  os << "Time samples: " << (*it)->rowdata.size() << std::endl;
305 
306 // for ( i = 0; i < (*it)->words; i++)
307 // {
308 // os << setw(6) << i << setw(6) << (*it)->adc[i];
309 // if ( (*it)->adc[i] < 1200 ) cout << " D1";
310 // else if ( (*it)->adc[i] > 3000 ) cout << " D0";
311 // os << endl;
312 // }
313 
314  if ( (*it)->rowdata.size() > 0)
315  {
316  os << "Sample ";
317  for ( reportit = (*it)->rowdata.begin(); reportit != (*it)->rowdata.end(); ++reportit)
318  {
319  os << setw(5) << (*reportit)->n;
320  }
321  os << endl;
322  os << "Addr ";
323 
324  for ( reportit = (*it)->rowdata.begin(); reportit != (*it)->rowdata.end(); ++reportit)
325  {
326  os << setw(5) << (*reportit)->address;
327  }
328  os << endl;
329  os << "Error ";
330 
331  for ( reportit = (*it)->rowdata.begin(); reportit != (*it)->rowdata.end(); ++reportit)
332  {
333  os << setw(5) << (*reportit)->error;
334  }
335  os << endl;
336  os << " ----------------------------------------------------------------------------------------" << endl;
337 
338 
339  for ( i = 0; i < 128; i++)
340  {
341  os << setw(4) << i << " | ";
342 
343  for ( reportit = (*it)->rowdata.begin(); reportit != (*it)->rowdata.end(); ++reportit)
344  {
345  os << setw(5) << (*reportit)->adc[i];
346  }
347  os << endl;
348  }
349  }
350  }
351 
352 }
353