EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oncsBuffer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file oncsBuffer.cc
1 #include "oncsBuffer.h"
2 #include "oncsStructures.h"
3 #include "oncsSubConstants.h"
4 
5 // the constructor first ----------------
7 {
8  bptr = (buffer_ptr) array;
9  data_ptr = &(bptr->data[0]);
11  current_index = 0;
12 
13  if (bptr->ID != ONCSBUFFERID && bptr->ID != PRDFBUFFERID) // PRDFBUFFERID is for legacy data
14  {
15  // COUT << " will swap the buffer " << std::endl;
16  unsigned int id = i4swap(bptr->ID);
17  if (id != ONCSBUFFERID && id != PRDFBUFFERID )
18  {
19  COUT << " wrong buffer" << std::endl;
20  return;
21  }
22  if ( buffer_swap())
23  {
24  COUT << "problem in buffer swap" << std::endl;
25  }
26  }
27  buffer_size = array[0];
28 
29 }
30 
32 {
33 
34  unsigned int i;
35  unsigned int evtindex, sevtindex;
36  oncsevtdata_ptr evtptr;
37  subevtdata_ptr sevtptr;
38 
39  // swap the buffer header
40  bptr->Length = i4swap ( bptr->Length);
41  bptr->ID = i4swap ( bptr->ID);
42  bptr->Bufseq = i4swap ( bptr->Bufseq);
43  bptr->Runnr = i4swap ( bptr->Runnr);
44 
45  evtindex = 0;
46 
47  while (evtindex < bptr->Length - BUFFERHEADERLENGTH)
48  {
49  // COUT << "evt index " << evtindex << std::endl;
50 
51  // map event header on data
52  evtptr = ( oncsevtdata_ptr ) &bptr->data[evtindex];
53 
54  evtptr->evt_length = i4swap(evtptr->evt_length);
55  evtptr->evt_type = i4swap(evtptr->evt_type);
56 
57  // see if we have got the end of buffer event
58  if (evtptr->evt_length == 2 && evtptr->evt_type ==0) break;
59 
60  // swap the rest of the event header
61  for (i=2; i<EVTHEADERLENGTH; i++)
62  bptr->data[evtindex+i] = i4swap(bptr->data[evtindex+i]);
63 
64  // mark first subevent
65  sevtindex = 0;
66 
67  while (sevtindex < evtptr->evt_length - EVTHEADERLENGTH)
68  {
69  // map subevent structure on data
70  sevtptr = (subevtdata_ptr) &evtptr->data[sevtindex];
71 
72  // swap buffer header
73  sevtptr->sub_length = i4swap(sevtptr->sub_length);
74  sevtptr->sub_id = i2swap(sevtptr->sub_id);
75  sevtptr->sub_type = i2swap(sevtptr->sub_type);
76  sevtptr->sub_decoding = i2swap(sevtptr->sub_decoding);
77  sevtptr->sub_padding = i2swap(sevtptr->sub_padding);
78  sevtptr->reserved[0] = i2swap(sevtptr->reserved[0]);
79  sevtptr->reserved[1] = i2swap(sevtptr->reserved[1]);
80 
81  // now swap the data depending on the type
82  int *p = &sevtptr->data;
83 
84  switch (sevtptr->sub_type)
85  {
86  case 1: break;
87 
88  case 2:
89  for (i=0; i<sevtptr->sub_length - SEVTHEADERLENGTH; i++)
90  {
91  *p = i22swap(*p);
92  p++;
93  }
94  break;
95 
96  case 4:
97  for (i=0; i<sevtptr->sub_length - SEVTHEADERLENGTH; i++)
98  {
99  *p = i4swap(*p);
100  p++;
101  }
102  break;
103 
104  default:
105  COUT << "unknown data type " << sevtptr->sub_type << std::endl;
106  break;
107  }
108  sevtindex += sevtptr->sub_length;
109  }
110  evtindex += evtptr->evt_length;
111  }
112  return 0;
113 }
114 
115 // ---------------------------------------------------------
116 int oncsBuffer::i4swap(const int in)
117 {
118  union
119  {
120  int i4;
121  char c[4];
122  } i,o;
123 
124  i.i4 = in;
125  o.c[0] = i.c[3];
126  o.c[1] = i.c[2];
127  o.c[2] = i.c[1];
128  o.c[3] = i.c[0];
129  return o.i4;
130 }
131 // ---------------------------------------------------------
132 int oncsBuffer::i22swap(const int in)
133 {
134  union
135  {
136  int i4;
137  char c[4];
138  } i,o;
139 
140  i.i4 = in;
141  o.c[0] = i.c[1];
142  o.c[1] = i.c[0];
143  o.c[2] = i.c[3];
144  o.c[3] = i.c[2];
145  return o.i4;
146 }
147 
148 // ---------------------------------------------------------
149 short oncsBuffer::i2swap(const short in)
150 {
151  union
152  {
153  short i2;
154  char c[2];
155  } i,o;
156 
157  i.i2 = in;
158  o.c[0] = i.c[1];
159  o.c[1] = i.c[0];
160 
161  return o.i2;
162 }
163 
164 
165 // ---------------------------------------------------------
167 {
168  if ( current_index < 0 ) return 0;
169 
170  Event *evt;
171  evt = new oncsEvent( &bptr->data[current_index]);
172 
173 
174  int l = evt->getEvtLength();
175  if ( l<= 0) return 0;
176  current_index += evt->getEvtLength();
177 
178  // now is the new index pointing outside the allocated memory?
179  // if (current_index < 0 || current_index > BUFFERSIZE)
180  if (current_index < 0 || current_index >= buffer_size)
181  {
182  //COUT << "end of buffer r1" << current_index << std::endl;
183  current_index = -1;
184  return evt;
185  }
186 
187  // are we pointing beyond the logical end of buffer?
188  if (current_index > buffer_size/4 )
189  {
190  //COUT << "end of buffer r2" << std::endl;
191  current_index = -1;
192  return evt;
193  }
194 
195  // are we pointing to an end-of-buffer event?
196  if (bptr->data[current_index] == 2 && bptr->data[current_index+1] == 0)
197  {
198  //COUT << "end of buffer r3" << std::endl;
199  current_index = -1;
200  return evt;
201  }
202 
203  // none of the above, just return
204  return evt;
205 
206 }
207 
208