EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prdfBuffer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file prdfBuffer.cc
1 #include <prdfBuffer.h>
2 #include <EventTypes.h>
3 #include <A_Event.h>
4 #include <Cframe.h>
5 #include <stdio.h>
6 
7 // the constructor first ----------------
8 
10 {
11  is_good =1;
12 }
13 
14 prdfBuffer::prdfBuffer (PHDWORD *array , const int length )
15 {
16  is_good =1;
17  bptr = (buffer_ptr) array;
18  data_ptr = &(bptr->data[0]);
20  current_index = 0;
21 
22  if (bptr->ID != BUFFERMARKER)
23  {
24  //COUT << " will swap the buffer " << std::endl;
25  unsigned int id = u4swap(bptr->ID);
26  if (id != BUFFERMARKER)
27  {
28  COUT << " wrong buffer" << std::endl;
29  is_good =0;
30  return;
31  }
32  if ( buffer_swap())
33  {
34  COUT << "problem in buffer swap" << std::endl;
35  is_good = 0;
36  }
37  }
38  buffer_size = array[0];
39  //COUT << " done... " << std::endl;
40 
41 }
42 
43 
45 {}
46 
48 {
49 
50  unsigned int i;
51  unsigned int evtindex, frameindex;
52  evtdata_ptr evtptr;
53  PHDWORD *frameptr;
54 
55  // swap the buffer header
56  bptr->Length = i4swap ( bptr->Length);
57  bptr->ID = i4swap ( bptr->ID);
58  bptr->Bufseq = i4swap ( bptr->Bufseq);
59  bptr->Runnr = i4swap ( bptr->Runnr);
60 
61  evtindex = 0;
62 
63  while (evtindex*4 < bptr->Length - BUFFERHEADERLENGTH)
64  {
65  // COUT << "evt index " << evtindex << " buffer length = " << bptr->Length << std::endl;
66 
67  // map event header on data
68  evtptr = ( evtdata_ptr ) &bptr->data[evtindex];
69 
70  evtptr->evt_length = i4swap(evtptr->evt_length);
71  evtptr->evt_type = i4swap(evtptr->evt_type);
72 
73  int written_length = (bptr->Length + 8191)/8192;
74  written_length *= 8192;
75 
76  if ( ( evtptr->evt_length + evtindex)*4 > written_length - BUFFERHEADERLENGTH )
77  {
78  std::cout << "Error: next event exceeds buffer length " << bptr->Length
79  << " current index " << evtindex
80  << "next event length " << evtptr->evt_length << std::endl;
81 
82  // shit. that's wrong. terminate the buffer here.
83  bptr->data[evtindex] = 2;
84  bptr->data[evtindex] = 0;
85  return -1;
86  }
87 
88  // see if we have got the end of buffer event
89  if (evtptr->evt_length == 2 && evtptr->evt_type ==0) break;
90 
91  // swap the rest of the event header
92  for (i=2; i<EVTHEADERLENGTH; i++)
93  bptr->data[evtindex+i] = i4swap(bptr->data[evtindex+i]);
94 
95  // mark first subevent
96  frameindex = 0;
97 
98  int fl;
99 
100  while (frameindex < evtptr->evt_length - EVTHEADERLENGTH)
101  {
102  // here we have a frame
103  frameptr = &evtptr->data[frameindex];
104  i = frame_swap(frameptr, evtptr->evt_length - EVTHEADERLENGTH ); /* byte swap if wrong endianism */
105  if ( i )
106  {
107  evtptr->evt_type |= CORRUPTEVENTMASK;
108  // COUT << "about to call getFrameLength " << std::endl;
109  break;
110  }
111  fl = getFrameLength(frameptr);
112  // COUT << "framelength " << fl << " frameindex = " << frameindex << " evt length = " << evtptr->evt_length << std::endl;
113 
114  if (fl <=0 )
115  {
116  break;
117  }
118  frameindex += fl;
119  }
120  evtindex += evtptr->evt_length;
121  if ( evtptr->evt_length ==0)
122  {
123  COUT << "0-length event found at " << evtindex << " " << bptr->Length << std::endl;
124  return -1;
125  }
126  }
127  return 0;
128 }
129 
130 
131 int prdfBuffer::frame_swap(PHDWORD * fp, const int eventlength)
132 {
133  int swapped_length = i4swap(*fp);
134  if ( swapped_length > eventlength || swapped_length <0 )
135  {
136  // std::cout << __FILE__ << " " << __LINE__ << " corrupt frame length " << std::endl;
137  return -1;
138  }
139  int i;
140  for ( i = 0; i < swapped_length; i++)
141  {
142  fp[i] = i4swap(fp[i]);
143  }
144 
145  return 0;
146 }
147 
148 
149 // ---------------------------------------------------------
151 
152 {
153 
154  if ( current_index < 0 ) return 0;
155  if ( ! is_good ) return 0;
156 
157  Event *evt;
158  evt = 0;
159 
160  // now is the new index pointing outside the allocated memory?
161  if (current_index < 0 || current_index > max_length)
162  {
163  //COUT << "end of buffer r0 " << current_index << std::endl;
164  current_index = -1;
165  return evt;
166  }
167 
168 
169  int len = bptr->data[current_index];
170 
171  // maybe there is something wrong with it?
172  if (len <= 0)
173  {
174  //COUT << "end of buffer r1 " << std::endl;
175  current_index = -1;
176  return evt;
177  }
178 
179  // current_index += len;
180 
181  // are we pointing beyond the logical end of buffer?
182  if (current_index >= (buffer_size/4) -6 ) //6 is 2 end-of-buffer + 4 buffer header
183  {
184  //COUT << "end of buffer r2 " << std::endl;
185  current_index = -1;
186  return evt;
187  }
188 
189  // are we pointing to an end-of-buffer event?
190  if (bptr->data[current_index] == 2 && bptr->data[current_index+1] == 0)
191  {
192  //COUT << "end of buffer r3 " << std::endl;
193  current_index = -1;
194  return evt;
195  }
196 
197  // none of the above, just return
198  evt = new A_Event( &bptr->data[current_index]);
199  current_index += len;
200  return evt;
201 
202 }
203 
204 // ---------------------------------------------------------
206 
207 {
208 
209  if ( current_index < 0 ) return 0;
210 
211 
212  int *evtData = 0;
213 
214  // now is the new index pointing outside the allocated memory?
215  if (current_index < 0 || current_index > max_length)
216  {
217  //COUT << "end of buffer r1 " << current_index << std::endl;
218  current_index = -1;
219  return evtData;
220  }
221 
222 
223  int len = bptr->data[current_index];
224 
225  // maybe there is something wrong with it?
226  if (len <= 0)
227  {
228  current_index = -1;
229  return evtData;
230  }
231 
232 
233  // are we pointing beyond the logical end of buffer?
234  if (current_index >= (buffer_size/4) -6 ) //6 is 2 end-of-buffer + 4 buffer header
235  {
236  //COUT << "end of buffer r2 " << std::endl;
237  current_index = -1;
238  return evtData;
239  }
240 
241  // are we pointing to an end-of-buffer event?
242  if (bptr->data[current_index] == 2 && bptr->data[current_index+1] == 0)
243  {
244  //COUT << "end of buffer r3 " << std::endl;
245  current_index = -1;
246  return evtData;
247  }
248 
249  // none of the above, just return
250  evtData = (int *) &bptr->data[current_index];
251  current_index += len;
252  return evtData;
253 
254 }
255 
256