DABC (Data Acquisition Backbone Core)  2.9.9
Iterator.cxx
Go to the documentation of this file.
1 // $Id: Iterator.cxx 4575 2020-08-04 12:44:29Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #include "hadaq/Iterator.h"
17 
18 #include "dabc/logging.h"
19 
21  fFirstEvent(false),
22  fEvPtr(),
23  fSubPtr(),
24  fRawPtr(),
25  fBufType(dabc::mbt_Null)
26 {
27 }
28 
30  fFirstEvent(false),
31  fEvPtr(),
32  fSubPtr(),
33  fRawPtr(),
34  fBufType(dabc::mbt_Null)
35 {
36  Reset(buf);
37 }
38 
39 hadaq::ReadIterator::ReadIterator(const ReadIterator& src) :
40  fFirstEvent(src.fFirstEvent),
41  fEvPtr(src.fEvPtr),
42  fSubPtr(src.fSubPtr),
43  fRawPtr(src.fRawPtr),
44  fBufType(src.fBufType)
45 {
46 }
47 
49 {
50  fFirstEvent = src.fFirstEvent;
51  fEvPtr = src.fEvPtr;
52  fSubPtr = src.fSubPtr;
53  fRawPtr = src.fRawPtr;
54  fBufType = src.fBufType;
55 
56  return *this;
57 }
58 
60 {
61  Close();
62 
63  if (buf.null()) return false;
64 
65  fBufType = buf.GetTypeId();
66 
67  if ((fBufType != mbt_HadaqEvents) && (fBufType != mbt_HadaqTransportUnit) && (fBufType != mbt_HadaqSubevents)) {
68  EOUT("Buffer format %u not supported", (unsigned) fBufType);
69  return false;
70  }
71 
72  fEvPtr = buf;
73  fFirstEvent = true;
74 
75  return true;
76 }
77 
79 {
80  fEvPtr.reset();
81  fSubPtr.reset();
82  fRawPtr.reset();
83  fFirstEvent = false;
84  fBufType = dabc::mbt_Null;
85 }
86 
87 
89 {
90  if (fBufType != mbt_HadaqTransportUnit ) {
91  EOUT("NextHadTu only allowed for buffer type mbt_HadaqTransportUnit. Check your code!");
92  return false;
93  }
94 
95  if (fEvPtr.null()) return false;
96 
97  if (fFirstEvent)
98  fFirstEvent = false;
99  else
100  fEvPtr.shift(hadtu()->GetPaddedSize());
101 
102  if (fEvPtr.fullsize() < sizeof(hadaq::HadTu)) {
103  fEvPtr.reset();
104  return false;
105  }
106 
107  if (fEvPtr.rawsize() < sizeof(hadaq::HadTu)) {
108  EOUT("Raw size less than transport unit header - not supported !!!!");
109  fEvPtr.reset();
110  return false;
111  }
112 
113  if (fEvPtr.fullsize() < hadtu()->GetSize()) {
114  EOUT("Error in HADAQ format - declared event size %u smaller than actual portion in buffer %u",
115  (unsigned) hadtu()->GetSize(), (unsigned) fEvPtr.fullsize());
116  fEvPtr.reset();
117  return false;
118  }
119 
120  fSubPtr.reset();
121  fRawPtr.reset();
122 
123  return true;
124 }
125 
126 
128 {
129  if (fEvPtr.null()) return false;
130 
131  if (fBufType != mbt_HadaqEvents) {
132  EOUT("NextEvent only allowed for buffer type mbt_HadaqEvents. Check your code!");
133  return false;
134  }
135 
136  if (fFirstEvent)
137  fFirstEvent = false;
138  else
139  fEvPtr.shift(evnt()->GetPaddedSize());
140 
141  if (fEvPtr.fullsize() < sizeof(hadaq::RawEvent)) {
142  fEvPtr.reset();
143  return false;
144  }
145 
146  if (fEvPtr.rawsize() < sizeof(hadaq::RawEvent)) {
147  EOUT("Raw size less than event header - not supported !!!!");
148 
149  fEvPtr.reset();
150  return false;
151  }
152 
153  if (fEvPtr.fullsize() < evnt()->GetSize()) {
154  EOUT("Error in HADAQ format - declared event size %u bigger than actual portion in buffer %u",
155  (unsigned) evnt()->GetSize(), (unsigned) fEvPtr.fullsize());
156  fEvPtr.reset();
157  return false;
158  }
159 
160  fSubPtr.reset();
161  fRawPtr.reset();
162 
163  return true;
164 }
165 
167 {
168  if (fBufType == mbt_HadaqTransportUnit)
169  return NextHadTu();
170 
171  if (fBufType == mbt_HadaqEvents)
172  return NextEvent();
173 
174  if (fBufType == mbt_HadaqSubevents) {
175  // here only subevents
176  if (!fFirstEvent) fEvPtr.reset();
177  if (fEvPtr.null()) return false;
178  fFirstEvent = false;
179  fSubPtr.reset();
180  fRawPtr.reset();
181  return true;
182  }
183 
184  return false;
185 }
186 
187 
189 {
190  if (fEvPtr.null()) {
191  ptr.reset();
192  return false;
193  }
194  ptr.reset(fEvPtr, 0, evnt()->GetPaddedSize());
195  return true;
196 }
197 
198 
200 {
201  if (fSubPtr.null()) {
202  if (fEvPtr.null()) return false;
203  // this function is used both in hadtu and in event mode. Check out mode:
204  dabc::BufferSize_t headsize(0), containersize(0);
205  if (fBufType == mbt_HadaqEvents) {
206  headsize = sizeof(hadaq::RawEvent);
207  containersize = evnt()->GetPaddedSize();
208  } else if (fBufType == mbt_HadaqTransportUnit) {
209  headsize = sizeof(hadaq::HadTu);
210  containersize = hadtu()->GetPaddedSize();
211  } else if (fBufType == mbt_HadaqSubevents) {
212  headsize = 0;
213  containersize = fEvPtr.fullsize();
214  } else {
215  EOUT("NextSubEvent not allowed for buffer type %u. Check your code!", (unsigned) fBufType);
216  return false;
217  }
218 
219  if (containersize == 0) return false; // typical problem of artifical generated events
220 
221  if (containersize < headsize) {
222  EOUT("Hadaq format error - tu container fullsize %u too small", (unsigned) containersize);
223  return false;
224  }
225  fSubPtr.reset(fEvPtr, 0, containersize);
226  fSubPtr.shift(headsize);
227  } else {
228  fSubPtr.shift(subevnt()->GetPaddedSize());
229  }
230 
231  if (fSubPtr.fullsize() < sizeof(hadaq::RawSubevent)) {
232  fSubPtr.reset();
233  return false;
234  }
235 
236  if (subevnt()->GetSize() < sizeof(hadaq::RawSubevent)) {
237  EOUT("Hadaq format error - subevent fullsize %u too small", subevnt()->GetSize());
238  //char* ptr = (char*) subevnt();
239  //for(int i=0; i<20; ++i)
240  // printf("sub(%d)=0x%02x\n", i, (unsigned) *ptr++);
241 
242  fSubPtr.reset();
243  return false;
244  }
245 
246  fRawPtr.reset(fSubPtr, 0, subevnt()->GetPaddedSize());
247  fRawPtr.shift(sizeof(hadaq::RawSubevent));
248 
249  return true;
250 }
251 
253 {
254  unsigned sz0 = fEvPtr.rawsize(), sz1 = 0;
255  if (!fSubPtr.null()) sz1 = fEvPtr.distance_to(fSubPtr);
256  return sz0>sz1 ? sz0-sz1 : 0;
257 }
258 
259 unsigned hadaq::ReadIterator::NumEvents(const dabc::Buffer& buf)
260 {
261  ReadIterator iter(buf);
262  unsigned cnt(0);
263  while (iter.NextEvent()) cnt++;
264  return cnt;
265 }
266 
267 // ===================================================================
268 
270  fBuffer(),
271  fEvPtr(),
272  fSubPtr(),
273  fFullSize(0)
274 {
275 }
276 
278  fBuffer(),
279  fEvPtr(),
280  fSubPtr(),
281  fFullSize(0)
282 {
283  Reset(buf);
284 }
285 
287 {
288  Close();
289 }
290 
292 {
293  fBuffer.Release();
294  fEvPtr.reset();
295  fSubPtr.reset();
296  fFullSize = 0;
297 
298  if (buf.GetTotalSize() < sizeof(hadaq::RawEvent) + sizeof(hadaq::RawSubevent)) {
299  EOUT("Buffer too small for just empty HADAQ event");
300  return false;
301  }
302 
303  fBuffer = buf;
304 
305  fBuffer.SetTypeId(mbt_HadaqEvents);
306  return true;
307 }
308 
310 {
311  fEvPtr.reset();
312  fSubPtr.reset();
313  if ((fFullSize>0) && (fBuffer.GetTotalSize() >= fFullSize))
314  fBuffer.SetTotalSize(fFullSize);
315  fFullSize = 0;
316 
317  dabc::Buffer res;
318  res << fBuffer;
319  return res;
320 }
321 
322 bool hadaq::WriteIterator::IsPlaceForEvent(uint32_t subeventssize)
323 {
324  dabc::BufferSize_t availible = 0;
325 
326  if (!fEvPtr.null()) availible = fEvPtr.fullsize();
327  else availible = fBuffer.GetTotalSize();
328 
329  return availible >= (sizeof(hadaq::RawEvent) + subeventssize);
330 }
331 
332 
333 bool hadaq::WriteIterator::NewEvent(uint32_t evtSeqNr, uint32_t runNr, uint32_t minsubeventssize)
334 
335 {
336  // TODO: add arguments to set other event header fields
337  if (fBuffer.null()) return false;
338 
339  if (fEvPtr.null()) fEvPtr = fBuffer;
340 
341  fSubPtr.reset();
342 
343  if (fEvPtr.fullsize() < (sizeof(hadaq::RawEvent) + minsubeventssize)) {
344  fEvPtr.reset();
345  return false;
346  }
347 
348  evnt()->Init(evtSeqNr, runNr);
349 
350  return true;
351 }
352 
353 bool hadaq::WriteIterator::NewSubevent(uint32_t minrawsize, uint32_t trigger)
354 {
355  if (fEvPtr.null()) return false;
356 
357  if (fSubPtr.null())
358  fSubPtr.reset(fEvPtr, sizeof(hadaq::RawEvent));
359 
360  if (fSubPtr.fullsize() < (sizeof(hadaq::RawSubevent) + minrawsize)) return false;
361 
362  subevnt()->Init(trigger);
363 
364  return true;
365 }
366 
367 bool hadaq::WriteIterator::FinishSubEvent(uint32_t rawdatasz)
368 {
369  if (fSubPtr.null()) return false;
370 
371  if (rawdatasz > maxrawdatasize()) return false;
372 
373  subevnt()->SetSize(rawdatasz + sizeof(hadaq::RawSubevent));
374 
375  fSubPtr.shift(subevnt()->GetPaddedSize());
376 
377  return true;
378 }
379 
381 {
382  if (fEvPtr.null()) return false;
383 
384  if (fSubPtr.null())
385  fSubPtr.reset(fEvPtr, sizeof(hadaq::RawEvent));
386 
387  if (fSubPtr.fullsize() < source.fullsize()) return false;
388 
389  fSubPtr.copyfrom(source);
390 
391  fSubPtr.shift(source.fullsize());
392 
393  return true;
394 }
395 
396 bool hadaq::WriteIterator::AddSubevent(const void *ptr, unsigned len)
397 {
398  if (fEvPtr.null()) return false;
399 
400  if (fSubPtr.null())
401  fSubPtr.reset(fEvPtr, sizeof(hadaq::RawEvent));
402 
403  if (fSubPtr.fullsize() < len) return false;
404 
405  fSubPtr.copyfrom(ptr, len);
406 
407  fSubPtr.shift(len);
408 
409  return true;
410 }
411 
412 
414 {
415  if (fEvPtr.null()) return false;
416 
417  dabc::BufferSize_t dist = sizeof(hadaq::RawEvent);
418  if (!fSubPtr.null()) dist = fEvPtr.distance_to(fSubPtr);
419  evnt()->SetSize(dist);
420  dabc::BufferSize_t paddeddist = evnt()->GetPaddedSize();
421  fFullSize += paddeddist;
422  fEvPtr.shift(paddeddist);
423 
424  return true;
425 }
Reference on memory from memory pool.
Definition: Buffer.h:135
void SetTotalSize(BufferSize_t len)
Set total length of the buffer to specified value Size cannot be bigger than original size of the buf...
Definition: Buffer.cxx:99
unsigned GetTypeId() const
Definition: Buffer.h:152
BufferSize_t GetTotalSize() const
Return total size of all buffer segments.
Definition: Buffer.cxx:91
void SetTypeId(unsigned tid)
Definition: Buffer.h:151
Manipulator with dabc::Buffer class.
Definition: Pointer.h:34
void reset(void *buf, BufferSize_t sz)
Definition: Pointer.h:99
BufferSize_t fullsize() const
Definition: Pointer.h:149
bool null() const
Returns true if reference contains nullptr.
Definition: Reference.h:151
Read iterator for HADAQ events/subevents.
Definition: Iterator.h:39
static unsigned NumEvents(const dabc::Buffer &buf)
Definition: Iterator.cxx:259
bool Reset()
Reset iterator - forget pointer on buffer.
Definition: Iterator.h:63
bool NextEvent()
Used for ready HLD events.
Definition: Iterator.cxx:127
unsigned rawdata_maxsize() const
Try to define maximal length for the raw data.
Definition: Iterator.cxx:252
bool AssignEventPointer(dabc::Pointer &ptr)
Definition: Iterator.cxx:188
bool NextSubEvent()
Used for sub-events iteration inside current block.
Definition: Iterator.cxx:199
bool NextHadTu()
Used for raw data from TRBs.
Definition: Iterator.cxx:88
bool NextSubeventsBlock()
Depending from buffer type calls NextHadTu() or NextEvent()
Definition: Iterator.cxx:166
unsigned fBufType
Definition: Iterator.h:45
dabc::Pointer fEvPtr
Definition: Iterator.h:42
dabc::Pointer fRawPtr
Definition: Iterator.h:44
dabc::Pointer fSubPtr
Definition: Iterator.h:43
ReadIterator & operator=(const ReadIterator &src)
Definition: Iterator.cxx:48
bool NewEvent(uint32_t evtSeqNr=0, uint32_t runNr=0, uint32_t minsubeventsize=0)
Definition: Iterator.cxx:333
bool IsPlaceForEvent(uint32_t subeventsize)
Definition: Iterator.cxx:322
bool FinishSubEvent(uint32_t rawdatasz)
Definition: Iterator.cxx:367
dabc::Buffer Close()
Definition: Iterator.cxx:309
bool NewSubevent(uint32_t minrawsize=0, uint32_t trigger=0)
Definition: Iterator.cxx:353
bool AddSubevent(const dabc::Pointer &source)
Definition: Iterator.cxx:380
bool Reset(const dabc::Buffer &buf)
Definition: Iterator.cxx:291
#define EOUT(args ...)
Definition: logging.h:150
Event manipulation API.
Definition: api.h:23
uint32_t BufferSize_t
Definition: Buffer.h:32
@ mbt_Null
Definition: Buffer.h:39
@ mbt_HadaqSubevents
Definition: HadaqTypeDefs.h:26
@ mbt_HadaqTransportUnit
Definition: HadaqTypeDefs.h:25
@ mbt_HadaqEvents
Definition: HadaqTypeDefs.h:24
HADES transport unit header.
Definition: defines.h:138
Hadaq event structure.
Definition: defines.h:443
Hadaq subevent structure.
Definition: defines.h:262