artdaq_core  v3_06_11
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 <map>
10 #include "artdaq-core/Data/dictionarycontrol.hh"
11 #include "artdaq-core/Utilities/TimeUtils.hh"
12 #include "cetlib_except/exception.h"
13 
14 extern "C" {
15 #include <stdint.h> // NOLINT(modernize-deprecated-headers)
16 }
17 
18 namespace artdaq {
19 namespace detail {
20 struct RawFragmentHeader;
21 }
22 } // namespace artdaq
23 
32 {
39  typedef unsigned long long RawDataType;
40 
41 #if HIDE_FROM_ROOT
42  typedef uint16_t version_t;
43  typedef uint64_t sequence_id_t;
44  typedef uint8_t type_t;
45  typedef uint16_t fragment_id_t;
46  typedef uint8_t metadata_word_count_t;
47  typedef uint64_t timestamp_t;
48 
49  // define special values for type_t
50  static constexpr type_t INVALID_TYPE = 0;
51  static constexpr type_t FIRST_USER_TYPE = 1;
52  static constexpr type_t LAST_USER_TYPE = 224;
53  static constexpr type_t FIRST_SYSTEM_TYPE = 225;
54  static constexpr type_t LAST_SYSTEM_TYPE = 255;
57  static constexpr type_t DataFragmentType = FIRST_SYSTEM_TYPE + 1;
58  static constexpr type_t InitFragmentType = FIRST_SYSTEM_TYPE + 2;
62  static constexpr type_t EmptyFragmentType = FIRST_SYSTEM_TYPE + 6;
64  static constexpr type_t ErrorFragmentType = FIRST_SYSTEM_TYPE + 8;
65 
70  static std::map<type_t, std::string> MakeSystemTypeMap()
71  {
72  return std::map<type_t, std::string>{
73  {type_t(DataFragmentType), "Data"},
74  {type_t(EmptyFragmentType), "Empty"},
75  {type_t(ErrorFragmentType), "Error"},
76  {type_t(InvalidFragmentType), "Invalid"},
77  {232, "Container"}};
78  }
79 
84  static std::map<type_t, std::string> MakeVerboseSystemTypeMap()
85  {
86  return std::map<type_t, std::string>{
87  {type_t(EndOfDataFragmentType), "EndOfData"},
88  {type_t(DataFragmentType), "Data"},
89  {type_t(InitFragmentType), "Init"},
90  {type_t(EndOfRunFragmentType), "EndOfRun"},
91  {type_t(EndOfSubrunFragmentType), "EndOfSubrun"},
92  {type_t(ShutdownFragmentType), "Shutdown"},
93  {type_t(EmptyFragmentType), "Empty"},
94  {type_t(ContainerFragmentType), "Container"}};
95  }
96 
102  static std::string SystemTypeToString(type_t type)
103  {
104  switch (type)
105  {
106  case INVALID_TYPE:
107  return "INVALID";
109  return "EndOfData";
110  case DataFragmentType:
111  return "Data";
112  case InitFragmentType:
113  return "Init";
115  return "EndOfRun";
117  return "EndOfSubrun";
119  return "Shutdown";
120  case EmptyFragmentType:
121  return "Empty";
123  return "Container";
124  default:
125  return "Unknown";
126  }
127  }
128 
129  // Each of the following invalid values is chosen based on the
130  // size of the bitfield in which the corresponding data are
131  // encoded; if any of the sizes are changed, the corresponding
132  // values must be updated.
133  static const version_t InvalidVersion = 0xFFFF;
134  static const version_t CurrentVersion = 0x2;
135  static const sequence_id_t InvalidSequenceID = 0xFFFFFFFFFFFF;
136  static const fragment_id_t InvalidFragmentID = 0xFFFF;
137  static const timestamp_t InvalidTimestamp = 0xFFFFFFFFFFFFFFFF;
138 
143 
146 
148 
153 
154  // ****************************************************
155  // New fields MUST be added to the END of this list!!!
156  // ****************************************************
157 
162  static constexpr std::size_t num_words();
163 
169  void setUserType(uint8_t utype);
170 
176  void setSystemType(uint8_t stype);
177 
181  void touch();
186  struct timespec atime() const;
192  struct timespec getLatency(bool touch);
193 
200  {
201  return word_count == other.word_count &&
202  version == other.version &&
203  type == other.type &&
205 
206  sequence_id == other.sequence_id &&
207  fragment_id == other.fragment_id &&
208 
209  timestamp == other.timestamp &&
210 
211  valid == other.valid &&
212  complete == other.complete &&
213  atime_ns == other.atime_ns &&
214  atime_s == other.atime_s;
215  }
216 #endif /* HIDE_FROM_ROOT */
217 };
218 
219 #if HIDE_FROM_ROOT
220 inline constexpr std::size_t
222 {
223  return sizeof(detail::RawFragmentHeader) / sizeof(RawDataType);
224 }
225 
226 // Compile-time check that the assumption made in num_words() above is
227 // actually true.
231  "sizeof(RawFragmentHeader) is not an integer "
232  "multiple of sizeof(RawDataType)!");
233 
234 inline void
236 {
237  if (utype < FIRST_USER_TYPE || utype > LAST_USER_TYPE)
238  {
239  throw cet::exception("InvalidValue") // NOLINT(cert-err60-cpp)
240  << "RawFragmentHeader user types must be in the range of "
241  << static_cast<int>(FIRST_USER_TYPE) << " to " << static_cast<int>(LAST_USER_TYPE)
242  << " (bad type is " << static_cast<int>(utype) << ").";
243  }
244  type = utype;
245 }
246 
247 inline void
249 {
250  if (stype < FIRST_SYSTEM_TYPE /*|| stype > LAST_SYSTEM_TYPE*/)
251  {
252  throw cet::exception("InvalidValue") // NOLINT(cert-err60-cpp)
253  << "RawFragmentHeader system types must be in the range of "
254  << static_cast<int>(FIRST_USER_TYPE) << " to " << static_cast<int>(LAST_USER_TYPE);
255  }
256  type = stype;
257 }
258 
260 {
262  atime_ns = time.tv_nsec;
263  atime_s = time.tv_sec;
264 }
265 
266 inline struct timespec artdaq::detail::RawFragmentHeader::atime() const
267 {
268  struct timespec ts;
269  ts.tv_nsec = atime_ns;
270  ts.tv_sec = atime_s;
271  return ts;
272 }
273 
274 inline struct timespec artdaq::detail::RawFragmentHeader::getLatency(bool touch)
275 {
276  auto a_time = atime();
278 
279  a_time.tv_sec = time.tv_sec - a_time.tv_sec;
280 
281  if (a_time.tv_nsec > time.tv_nsec)
282  {
283  a_time.tv_sec--;
284  a_time.tv_nsec = 1000000000 + time.tv_nsec - a_time.tv_nsec;
285  }
286  else
287  {
288  a_time.tv_nsec = time.tv_nsec - a_time.tv_nsec;
289  }
290 
291  if (touch)
292  {
293  atime_ns = time.tv_nsec;
294  atime_s = time.tv_sec;
295  }
296  return a_time;
297 }
298 #endif
299 
300 #endif /* artdaq_core_Data_detail_RawFragmentHeader_hh */
RawDataType metadata_word_count
The number of RawDataType words in the user-defined metadata.
RawDataType word_count
number of RawDataType words in this Fragment
static const sequence_id_t InvalidSequenceID
The sequence_id field is currently 48-bits.
static constexpr type_t FIRST_SYSTEM_TYPE
The first system type.
RawDataType type
The type of the fragment, either system or user-defined.
uint8_t type_t
type field is 8 bits
static std::map< type_t, std::string > MakeSystemTypeMap()
Returns a map of the most-commonly used system types.
void setSystemType(uint8_t stype)
Sets the type field to the specified system type.
static constexpr std::size_t num_words()
Returns the number of RawDataType words present in the header.
static constexpr type_t DataFragmentType
This Fragment holds data. Used for RawEvent Fragments sent from the EventBuilder to the Aggregator...
The RawFragmentHeader class contains the basic fields used by artdaq for routing Fragment objects thr...
RawDataType timestamp
The 64-bit timestamp field is the output of a user-defined clock used for building time-correlated ev...
static constexpr type_t InvalidFragmentType
Marks a Fragment as Invalid.
RawDataType sequence_id
The 48-bit sequence_id uniquely identifies events within the artdaq system.
RawDataType version
The version of the fragment.
RawDataType atime_s
Last access time of the Fragment, second part (measured from epoch)
static constexpr type_t ShutdownFragmentType
This Fragment indicates a system shutdown to art
static constexpr type_t LAST_USER_TYPE
The last user-accessible type (types above this number are system types.
RawDataType fragment_id
The fragment_id uniquely identifies a particular piece of hardware within the artdaq system...
bool operator==(const detail::RawFragmentHeader &other)
Check if two RawFragmentHeader objects are equal.
void touch()
Update the atime fields of the RawFragmentHeader to current time.
static const version_t InvalidVersion
The version field is currently 16-bits.
void setUserType(uint8_t utype)
Sets the type field to the specified user type.
struct timespec get_realtime_clock()
Get the current time of day as a pair of seconds and nanoseconds (from clock_gettime(CLOCK_REALTIME, ...) system call)
Definition: TimeUtils.cc:58
uint64_t timestamp_t
timestamp field is 32 bits
RawDataType valid
Flag for whether the Fragment has been transported correctly through the artdaq system.
static constexpr type_t EmptyFragmentType
This Fragment contains no data and serves as a placeholder for when no data from a FragmentGenerator ...
static constexpr type_t ErrorFragmentType
This Fragment has experienced some error, and no attempt should be made to read it.
static constexpr type_t FIRST_USER_TYPE
The first user-accessible type.
static std::map< type_t, std::string > MakeVerboseSystemTypeMap()
Returns a map of all system types.
static const timestamp_t InvalidTimestamp
The timestamp field is currently 64-bits.
unsigned long long RawDataType
The RawDataType (currently an unsigned long long) is the basic unit of data representation within art...
uint64_t sequence_id_t
sequence_id field is 48 bits
static const fragment_id_t InvalidFragmentID
The fragment_id field is currently 16-bits.
static const version_t CurrentVersion
The CurrentVersion field should be incremented whenever the RawFragmentHeader changes.
struct timespec atime() const
Get the last access time of this RawFragmentHeader.
static constexpr type_t ContainerFragmentType
This Fragment is a ContainerFragment and analysis code should unpack it.
static constexpr type_t EndOfDataFragmentType
This Fragment indicates the end of data to art
static std::string SystemTypeToString(type_t type)
Print a system type&#39;s string name.
uint16_t version_t
version field is 16 bits
RawDataType atime_ns
Last access time of the Fragment, nanosecond part.
static constexpr type_t LAST_SYSTEM_TYPE
The last system type.
static constexpr type_t EndOfSubrunFragmentType
This Fragment indicates the end of a subrun to art
struct timespec getLatency(bool touch)
Get the elapsed time between now and the last access time of the RawFragmentHeader, optionally resetting it.
uint16_t fragment_id_t
fragment_id field is 16 bits
static constexpr type_t EndOfRunFragmentType
This Fragment indicates the end of a run to art
uint8_t metadata_word_count_t
metadata_word_count field is 8 bits
static constexpr type_t InitFragmentType
This Fragment holds the necessary data for initializing art
static constexpr type_t INVALID_TYPE
Marks a Fragment as Invalid.
RawDataType complete
Flag for whether the Fragment completely represents an event for its hardware.