DABC (Data Acquisition Backbone Core)  2.9.9
defines.h
Go to the documentation of this file.
1 // $Id: defines.h 4721 2021-03-13 13:52:01Z 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 #ifndef HADAQ_defines
17 #define HADAQ_defines
18 
19 #include <cstdint>
20 #include <iostream>
21 #include <cstring>
22 
23 /*
24 //Description of the Event Structure
25 //
26 //An event consists of an event header and of varying number of subevents, each with a subevent header. The size of the event header is fixed to 0x20 bytes
27 //
28 //Event header
29 //evtHeader
30 //evtSize evtDecoding evtId evtSeqNr evtDate evtTime runNr expId
31 //
32 // * evtSize - total size of the event including the event header, it is measured in bytes.
33 // * evtDecoding - event decoding type: Tells the analysis the binary format of the event data, so that it can be handed to a corresponding routine for unpacking into a software usable data structure. For easier decoding of this word, the meaning of some bits is already predefined:
34 // evtDecoding
35 // msB --- --- lsB
36 // 0 alignment decoding type nonzero
37 // o The first (most significant) byte is always zero.
38 // o The second byte contains the alignment of the subevents in the event. 0 = byte, 1 = 16 bit word, 2 = 32 bit word...
39 // o The remaining two bytes form the actual decoding type, e.g. fixed length, zero suppressed etc.
40 // o The last byte must not be zero, so the whole evtDecoding can be used to check for correct or swapped byte order.
41 //
42 //It is stated again, that the whole evtDecoding is one 32bit word. The above bit assignments are merely a rule how to select this 32bit numbers.
43 //
44 // * evtId - event identifier: Tells the analysis the semantics of the event data, e.g. if this is a run start event, data event, simulated event, slow control data, end file event, etc..
45 // evtId
46 // 31 30 - 16 15 - 12 11 - 8 5 - 7 4 3- 0
47 // error bit reserved version reserved MU decision DS flag ID
48 // o error bit - set if one of the subsystems has set the error bit
49 // o version - 0 meaning of event ID before SEP03; 1 event ID after SEP03
50 // o MU decision - 0 = negative LVL2 decision; >0 positive LVL2 decision
51 // MU trigger algo result
52 // 1 negative decision
53 // 2 positive decision
54 // 3 positive decision due to too many leptons or dileptons
55 // 4 reserved
56 // 5 reserved
57 // o DS flag - LVL1 downscaling flag; 1 = this event is written to the tape independent on the LVL2 trigger decision
58 // o ID - defines the trigger code
59 // ID before SEP03 description
60 // 0 simulation
61 // 1 real
62 // 2,3,4,5,6,7,8,9 calibration
63 // 13 beginrun
64 // 14 endrun
65 //
66 // ID after SEP03 description
67 // 0 simulation
68 // 1,2,3,4,5 real
69 // 7,9 calibration
70 // 1 real1
71 // 2 real2
72 // 3 real3
73 // 4 real4
74 // 5 real5
75 // 6 special1
76 // 7 offspill
77 // 8 special3
78 // 9 MDCcalibration
79 // 10 special5
80 // 13 beginrun
81 // 14 endrun
82 // * evtSeqNr - event number: This is the sequence number of the event in the file. The pair evtFileNr/evtSeqNr
83 //
84 //is unique throughout all events ever acquired by the system.
85 //
86 // * evtDate - date of event assembly (filled by the event builder, rough precision):
87 // evtDate ISO-C date format
88 // msB --- --- lsB
89 // 0 year month day
90 //
91 // 1. The first (most significant) byte is zero
92 // 2. The second byte contains the years since 1900
93 // 3. The third the months since January [0-11]
94 // 4. The last the day of the month [1-31]
95 //
96 // * evtTime - time of assembly (filled by the event builder, rough precision):
97 // evtTime ISO-C time format
98 // msB --- --- lsB
99 // 0 hour minute second
100 //
101 // 1. The first (most significant) byte is zero
102 // 2. The second byte contains the hours since midnight [0-23]
103 // 3. The third the minutes after the hour [0-59]
104 // 4. The last the seconds after the minute [0-60]
105 //
106 // * runNr - file number: A unique number assigned to the file. The runNr is used as key for the RTDB.
107 // * evtPad - padding: Makes the event header a multiple of 64 bits long.
108 */
109 
110 #pragma pack(push, 1)
111 
112 namespace hadaq {
113 
114  enum {
115  HADAQ_TIMEOFFSET = 1200000000 /* needed to reconstruct time from runId */
116  };
117 
118  enum EvtId {
119  EvtId_data = 0x00000001,
120  EvtId_DABC = 0x00002001, // hades DAQVERSION=2 (same as evtbuild.c uses DAQVERSION=2)
121  EvtId_runStart = 0x00010002,
122  EvtId_runStop = 0x00010003
123  };
124 
125  enum EvtDecoding {
127  EvtDecoding_AloneSubevt = 0x8, // subevent contains the only subsubevent, taking same id
128  EvtDecoding_64bitAligned = (0x03 << 16) | 0x0001
129  };
130 
131 
138  struct HadTu {
139  protected:
140  uint32_t tuSize;
141  uint32_t tuDecoding;
142 
143  public:
144 
145  HadTu() : tuSize(0), tuDecoding(0) {}
146 
148  inline bool IsSwapped() const { return tuDecoding > 0xffffff; }
149 
150  void SetDecodingDirect(uint32_t dec) { tuDecoding = dec; }
151 
152  inline uint32_t Value(const uint32_t *member) const
153  {
154  return IsSwapped() ? ((*member & 0xFF) << 24) |
155  ((*member & 0xFF00) << 8) |
156  ((*member & 0xFF0000) >> 8) |
157  ((*member & 0xFF000000) >> 24) : *member;
158 
159  //return IsSwapped() ? ((((uint8_t *) member)[0] << 24) |
160  // (((uint8_t *) member)[1] << 16) |
161  // (((uint8_t *) member)[2] << 8) |
162  // (((uint8_t *) member)[3])) : *member;
163  }
164 
166  inline void SetValue(uint32_t *member, uint32_t val)
167  {
168  *member = IsSwapped() ? ((val & 0xFF) << 24) |
169  ((val & 0xFF00) << 8) |
170  ((val & 0xFF0000) >> 8) |
171  ((val & 0xFF000000) >> 24) : val;
172 
173  // *member = IsSwapped() ?
174  // ((((uint8_t *) &val)[0] << 24) |
175  // (((uint8_t *) &val)[1] << 16) |
176  // (((uint8_t *) &val)[2] << 8) |
177  // (((uint8_t *) &val)[3])) : val;
178  }
179 
180 
181  uint32_t GetDecoding() const { return Value(&tuDecoding); }
182  inline uint32_t GetSize() const { return Value(&tuSize); }
183 
184  inline uint32_t GetPaddedSize() const
185  {
186  uint32_t hedsize = GetSize();
187  uint32_t rest = hedsize % 8;
188  return (rest==0) ? hedsize : (hedsize + 8 - rest);
189  }
190 
191  void SetSize(uint32_t bytes) { SetValue(&tuSize, bytes); }
192 
193  void SetDecoding(uint32_t decod) { SetValue(&tuDecoding, decod); }
194  };
195 
196  // ======================================================================
197 
200  struct HadTuId : public HadTu {
201  protected:
202  uint32_t tuId;
203 
204  public:
205 
206  HadTuId() : HadTu(), tuId(0) {}
207 
208  inline uint32_t GetId() const { return Value(&tuId); }
209  void SetId(uint32_t id) { SetValue(&tuId, id); }
210 
211  inline bool GetDataError() const { return (GetId() & 0x80000000) != 0; }
212 
213  void SetDataError(bool on)
214  {
215  if(on)
216  SetId(GetId() | 0x80000000);
217  else
218  SetId(GetId() & ~0x80000000);
219  }
220  };
221 
222  // =================================================================================
223 
262  struct RawSubevent : public HadTuId {
263 
264  protected:
265 
266  uint32_t subEvtTrigNr;
267 
268  public:
269 
271 
272  unsigned Alignment() const { return 1 << ( GetDecoding() >> 16 & 0xff); }
273 
274  uint32_t GetTrigNr() const { return Value(&subEvtTrigNr); }
275  void SetTrigNr(uint32_t trigger) { SetValue(&subEvtTrigNr, trigger); }
276 
277  /* for trb3: each subevent contains trigger type in decoding word*/
278  uint8_t GetTrigTypeTrb3() const { return (GetDecoding() & 0xF0) >> 4; }
279 
280  void SetTrigTypeTrb3(uint8_t typ)
281  {
282  uint32_t decod = GetDecoding();
283  SetDecoding((decod & ~0xF0) | (typ << 4));
284  }
285 
286  void Init(uint32_t trigger = 0)
287  {
288  SetTrigNr(trigger);
289  }
290 
292  uint32_t* GetDataPtr(unsigned indx) const
293  {
294  return (uint32_t*) (this) + sizeof(RawSubevent)/sizeof(uint32_t) + indx;
295  }
296 
297  /* returns number of payload data words, not maximum index!*/
298  unsigned GetNrOfDataWords() const
299  {
300  unsigned datasize = GetSize() - sizeof(hadaq::RawSubevent);
301  switch (Alignment()) {
302  case 4: return datasize / sizeof(uint32_t);
303  case 2: return datasize / sizeof(uint16_t);
304  }
305  return datasize / sizeof(uint8_t);
306  }
307 
309  uint32_t Data(unsigned idx) const
310  {
311  const void* my = (char*) (this) + sizeof(hadaq::RawSubevent);
312 
313  switch (Alignment()) {
314  case 4:
315  return Value((uint32_t *) my + idx);
316 
317  case 2: {
318  uint16_t tmp = ((uint16_t *) my)[idx];
319 
320  if (IsSwapped()) tmp = ((tmp >> 8) & 0xff) | ((tmp << 8) & 0xff00);
321 
322  return tmp;
323  }
324  }
325 
326  return ((uint8_t*) my)[idx];
327  }
328 
329  uint32_t GetErrBits()
330  {
331  return Data(GetNrOfDataWords()-1);
332  }
333 
334  void CopyDataTo(void* buf, unsigned indx, unsigned datalen)
335  {
336  if (buf==0) return;
337  while (datalen-- > 0) {
338  *((uint32_t*) buf) = Data(indx++);
339  buf = ((uint32_t*) buf) + 1;
340  }
341  }
342 
344  void* RawData() const { return (char*) (this) + sizeof(hadaq::RawSubevent); }
345 
347  void Dump(bool print_raw_data = false);
348 
350  void PrintRawData(unsigned ix = 0, unsigned len = 0xffffffff, unsigned prefix = 6);
351  };
352 
353  // =================================================================================
354 
443  struct RawEvent : public HadTuId {
444 
445  protected:
446  uint32_t evtSeqNr;
447  uint32_t evtDate;
448  uint32_t evtTime;
449  uint32_t evtRunNr;
450  uint32_t evtPad;
451 
453  void InitHeader(uint32_t evid);
454 
455 
456  public:
457 
458  RawEvent() : HadTuId(), evtSeqNr(0), evtDate(0), evtTime(0), evtRunNr(0), evtPad(0) {}
459 
460  uint32_t GetSeqNr() const { return Value(&evtSeqNr); }
461  void SetSeqNr(uint32_t n) { SetValue(&evtSeqNr, n); }
462 
463  int32_t GetRunNr() const { return Value(&evtRunNr); }
464  void SetRunNr(uint32_t n) { SetValue(&evtRunNr, n); }
465 
466  int32_t GetDate() const { return Value(&evtDate); }
467  void SetDate(uint32_t d) { SetValue(&evtDate, d); }
468 
469  int32_t GetTime() const { return Value(&evtTime); }
470  void SetTime(uint32_t t) { SetValue(&evtTime, t); }
471 
472  void Init(uint32_t evnt, uint32_t run=0, uint32_t id=EvtId_DABC)
473  {
474  InitHeader(id);
475  SetSeqNr(evnt);
476  SetRunNr(run);
477  evtPad = 0;
478  }
479 
480  void Dump();
481 
483 
485 
486  uint32_t AllSubeventsSize();
487  };
488 
489 }
490 
491 #pragma pack(pop)
492 
493 #endif
Support for HADAQ - HADES DAQ
Definition: api.h:27
EvtId
Definition: defines.h:118
@ EvtId_runStop
Definition: defines.h:122
@ EvtId_data
Definition: defines.h:119
@ EvtId_runStart
Definition: defines.h:121
@ EvtId_DABC
Definition: defines.h:120
@ HADAQ_TIMEOFFSET
Definition: defines.h:115
EvtDecoding
Definition: defines.h:125
@ EvtDecoding_AloneSubevt
Definition: defines.h:127
@ EvtDecoding_64bitAligned
Definition: defines.h:128
@ EvtDecoding_default
Definition: defines.h:126
Intermediate hierarchy class as common base for event and subevent.
Definition: defines.h:200
bool GetDataError() const
Definition: defines.h:211
void SetDataError(bool on)
Definition: defines.h:213
void SetId(uint32_t id)
Definition: defines.h:209
uint32_t tuId
Definition: defines.h:202
uint32_t GetId() const
Definition: defines.h:208
HADES transport unit header.
Definition: defines.h:138
uint32_t Value(const uint32_t *member) const
Definition: defines.h:152
void SetDecoding(uint32_t decod)
Definition: defines.h:193
bool IsSwapped() const
msb of decode word is always non zero...?
Definition: defines.h:148
uint32_t GetSize() const
Definition: defines.h:182
uint32_t tuSize
Definition: defines.h:140
void SetValue(uint32_t *member, uint32_t val)
swapsave method to set value stolen from hadtu.h
Definition: defines.h:166
uint32_t GetDecoding() const
Definition: defines.h:181
void SetSize(uint32_t bytes)
Definition: defines.h:191
void SetDecodingDirect(uint32_t dec)
Definition: defines.h:150
uint32_t tuDecoding
Definition: defines.h:141
uint32_t GetPaddedSize() const
Definition: defines.h:184
Hadaq event structure.
Definition: defines.h:443
RawSubevent * NextSubevent(RawSubevent *prev=0)
Definition: defines.cxx:66
void Init(uint32_t evnt, uint32_t run=0, uint32_t id=EvtId_DABC)
Definition: defines.h:472
RawSubevent * FirstSubevent()
Definition: defines.cxx:53
uint32_t evtTime
Definition: defines.h:448
void SetRunNr(uint32_t n)
Definition: defines.h:464
void InitHeader(uint32_t evid)
Method to set initial header value like decoding and date/time.
Definition: defines.cxx:34
uint32_t evtRunNr
Definition: defines.h:449
int32_t GetTime() const
Definition: defines.h:469
int32_t GetRunNr() const
Definition: defines.h:463
void SetSeqNr(uint32_t n)
Definition: defines.h:461
uint32_t GetSeqNr() const
Definition: defines.h:460
uint32_t evtSeqNr
Definition: defines.h:446
int32_t GetDate() const
Definition: defines.h:466
void SetTime(uint32_t t)
Definition: defines.h:470
uint32_t evtDate
Definition: defines.h:447
uint32_t evtPad
Definition: defines.h:450
void SetDate(uint32_t d)
Definition: defines.h:467
uint32_t AllSubeventsSize()
Definition: defines.cxx:60
Hadaq subevent structure.
Definition: defines.h:262
uint32_t Data(unsigned idx) const
swap-save access to any data.
Definition: defines.h:309
uint32_t subEvtTrigNr
Definition: defines.h:266
unsigned GetNrOfDataWords() const
Definition: defines.h:298
void Init(uint32_t trigger=0)
Definition: defines.h:286
void Dump(bool print_raw_data=false)
Print subevent header and optionally raw data.
Definition: defines.cxx:97
void * RawData() const
Return pointer where raw data should starts.
Definition: defines.h:344
uint32_t GetErrBits()
Definition: defines.h:329
void CopyDataTo(void *buf, unsigned indx, unsigned datalen)
Definition: defines.h:334
uint32_t * GetDataPtr(unsigned indx) const
Return pointer on data by index - user should care itself about swapping.
Definition: defines.h:292
void SetTrigNr(uint32_t trigger)
Definition: defines.h:275
unsigned Alignment() const
Definition: defines.h:272
void SetTrigTypeTrb3(uint8_t typ)
Definition: defines.h:280
void PrintRawData(unsigned ix=0, unsigned len=0xffffffff, unsigned prefix=6)
Print raw data, optionally one can position and portion to print.
Definition: defines.cxx:79
uint32_t GetTrigNr() const
Definition: defines.h:274
uint8_t GetTrigTypeTrb3() const
Definition: defines.h:278