artdaq_core  v1_07_01
 All Classes Namespaces Functions
RawFragmentHeader.hh
1 #ifndef artdaq_core_Data_detail_RawFragmentHeader_hh
2 #define artdaq_core_Data_detail_RawFragmentHeader_hh
3 // detail::RawFragmentHeader is an overlay that provides the user's view
4 // of the data contained within a Fragment. It is intended to be hidden
5 // from the user of Fragment, as an implementation detail. The interface
6 // of Fragment is intended to be used to access the data.
7 
8 //#include <cstddef>
9 #include "artdaq-core/Data/dictionarycontrol.hh"
10 #include "cetlib/exception.h"
11 #include <map>
12 
13 extern "C" {
14 #include <stdint.h>
15 }
16 
17 namespace artdaq {
18  namespace detail {
19  struct RawFragmentHeader;
20  }
21 }
22 
24  typedef unsigned long long RawDataType;
25 
26 #if HIDE_FROM_ROOT
27  typedef uint16_t version_t;
28  typedef uint64_t sequence_id_t;
29  typedef uint8_t type_t;
30  typedef uint16_t fragment_id_t;
31  typedef uint8_t metadata_word_count_t;
32  typedef uint32_t timestamp_t;
33 
34  // define special values for type_t
35  static constexpr type_t INVALID_TYPE = 0;
36  static constexpr type_t FIRST_USER_TYPE = 1;
37  static constexpr type_t LAST_USER_TYPE = 224;
38  static constexpr type_t FIRST_SYSTEM_TYPE = 225;
39  static constexpr type_t LAST_SYSTEM_TYPE = 255;
40  static constexpr type_t InvalidFragmentType = INVALID_TYPE;
41  static constexpr type_t EndOfDataFragmentType = FIRST_SYSTEM_TYPE;
42  static constexpr type_t DataFragmentType = FIRST_SYSTEM_TYPE + 1;
43  static constexpr type_t InitFragmentType = FIRST_SYSTEM_TYPE + 2;
44  static constexpr type_t EndOfRunFragmentType = FIRST_SYSTEM_TYPE + 3;
45  static constexpr type_t EndOfSubrunFragmentType = FIRST_SYSTEM_TYPE + 4;
46  static constexpr type_t ShutdownFragmentType = FIRST_SYSTEM_TYPE + 5;
47  static constexpr type_t EmptyFragmentType = FIRST_SYSTEM_TYPE + 6;
48  static constexpr type_t ContainerFragmentType = FIRST_SYSTEM_TYPE + 7;
49 
50  // These system types might actually show up in the data stream...
51  static std::map<type_t, std::string> MakeSystemTypeMap()
52  {
53  return std::map<type_t, std::string> {
54  { 226, "Data" },
55  { 231, "Empty" },
56  { 232, "Container" }
57  };
58  }
59  // All system types
60  static std::map<type_t, std::string> MakeVerboseSystemTypeMap()
61  {
62  return std::map<type_t, std::string> {
63  { 225, "EndOfData" },
64  { 226, "Data" },
65  { 227, "Init" },
66  { 228, "EndOfRun" },
67  { 229, "EndOfSubrun" },
68  { 230,"Shutdown" },
69  { 231, "Empty" },
70  { 232, "Container" }
71  };
72  }
73 
74  // Each of the following invalid values is chosen based on the
75  // size of the bitfield in which the corresponding data are
76  // encoded; if any of the sizes are changed, the corresponding
77  // values must be updated.
78  static const version_t InvalidVersion = 0xFFFF;
79  static const sequence_id_t InvalidSequenceID = 0xFFFFFFFFFFFF;
80  static const fragment_id_t InvalidFragmentID = 0xFFFF;
81  static const timestamp_t InvalidTimestamp = 0xFFFFFFFF;
82 
83  RawDataType word_count : 32; // number of RawDataTypes in this Fragment
84  RawDataType version : 16;
85  RawDataType type : 8;
86  RawDataType metadata_word_count : 8;
87 
88  RawDataType sequence_id : 48;
89  RawDataType fragment_id : 16;
90  RawDataType timestamp : 32;
91 
92  RawDataType unused1 : 16;
93  RawDataType unused2 : 16;
94 
95  constexpr static std::size_t num_words();
96 
97  void setUserType(uint8_t utype);
98  void setSystemType(uint8_t stype);
99 
100 #endif /* HIDE_FROM_ROOT */
101 
102 };
103 
104 #if HIDE_FROM_ROOT
105 inline
106 constexpr
107 std::size_t
108 artdaq::detail::RawFragmentHeader::num_words()
109 {
110  return sizeof(detail::RawFragmentHeader) / sizeof(RawDataType);
111 }
112 
113 // Compile-time check that the assumption made in num_words() above is
114 // actually true.
115 static_assert((artdaq::detail::RawFragmentHeader::num_words() *
116  sizeof(artdaq::detail::RawFragmentHeader::RawDataType)) ==
118  "sizeof(RawFragmentHeader) is not an integer "
119  "multiple of sizeof(RawDataType)!");
120 
121 inline
122 void
123 artdaq::detail::RawFragmentHeader::setUserType(uint8_t utype)
124 {
125  if (utype < FIRST_USER_TYPE || utype > LAST_USER_TYPE) {
126  throw cet::exception("InvalidValue")
127  << "RawFragmentHeader user types must be in the range of "
128  << ((int)FIRST_USER_TYPE) << " to " << ((int)LAST_USER_TYPE)
129  << " (bad type is " << ((int)utype) << ").";
130  }
131  type = utype;
132 }
133 
134 inline
135 void
136 artdaq::detail::RawFragmentHeader::setSystemType(uint8_t stype)
137 {
138  if (stype < FIRST_SYSTEM_TYPE /*|| stype > LAST_SYSTEM_TYPE*/) {
139  throw cet::exception("InvalidValue")
140  << "RawFragmentHeader system types must be in the range of "
141  << ((int)FIRST_SYSTEM_TYPE) << " to " << ((int)LAST_SYSTEM_TYPE);
142  }
143  type = stype;
144 }
145 #endif
146 
147 #endif /* artdaq_core_Data_detail_RawFragmentHeader_hh */