artdaq_core  v3_01_05
ContainerFragment.hh
1 #ifndef artdaq_core_Data_ContainerFragment_hh
2 #define artdaq_core_Data_ContainerFragment_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 #include "cetlib/exception.h"
6 
7 //#include <ostream>
8 //#include <vector>
9 
10 // Implementation of "ContainerFragment", an artdaq::Fragment overlay class
11 
12 #ifndef CONTAINER_FRAGMENT_CAPACITY
13 #define CONTAINER_FRAGMENT_CAPACITY 100
15 #endif
16 
17 namespace artdaq
18 {
19  class ContainerFragment;
20 
24  static const int CONTAINER_FRAGMENT_COUNT_MAX = CONTAINER_FRAGMENT_CAPACITY;
25 }
26 
31 {
32 public:
33 
37  struct Metadata
38  {
39  typedef uint8_t data_t;
40  typedef uint64_t count_t;
41 
45 
48 
50  static size_t const size_words = 8ul + CONTAINER_FRAGMENT_COUNT_MAX * sizeof(size_t) / sizeof(data_t); // Units of Header::data_t
51  };
52 
53  static_assert (sizeof(Metadata) == Metadata::size_words * sizeof(Metadata::data_t), "ContainerFragment::Metadata size changed");
54 
61  explicit ContainerFragment(Fragment const& f) : artdaq_Fragment_(f) { }
62 
67  Metadata const* metadata() const { return artdaq_Fragment_.metadata<Metadata>(); }
68 
83  bool missing_data() const { return static_cast<bool>(metadata()->missing_data); }
84 
89  void const* dataBegin() const
90  {
91  return reinterpret_cast<void const *>(&*artdaq_Fragment_.dataBegin());
92  }
93 
98  void const* dataEnd() const
99  {
100  return reinterpret_cast<void const *>(reinterpret_cast<uint8_t const *>(dataBegin()) + lastFragmentIndex());
101  }
102 
109  FragmentPtr at(size_t index) const
110  {
111  if (index >= block_count() || block_count() == 0)
112  {
113  throw cet::exception("ArgumentOutOfRange") << "Buffer overrun detected! ContainerFragment::at was asked for a non-existent Fragment!";
114  }
116  memcpy(frag->headerAddress(), reinterpret_cast<uint8_t const *>(dataBegin()) + fragmentIndex(index), fragSize(index));
117  return frag;
118  }
119 
126  size_t fragSize(size_t index) const
127  {
128  if (index >= block_count() || block_count() == 0)
129  {
130  throw cet::exception("ArgumentOutOfRange") << "Buffer overrun detected! ContainerFragment::fragSize was asked for a non-existent Fragment!";
131  }
132  auto end = metadata()->index[index];
133  if (index == 0) return end;
134  return end - metadata()->index[index - 1];
135  }
136 
143  FragmentPtr operator[](size_t index) const
144  {
145  return this->at(index);
146  }
147 
154  size_t fragmentIndex(size_t index) const
155  {
156  if (index > block_count())
157  {
158  throw cet::exception("ArgumentOutOfRange") << "Buffer overrun detected! ContainerFragment::fragmentIndex was asked for a non-existent Fragment!";
159  }
160  if (index == 0) { return 0; }
161  return metadata()->index[index - 1];
162  }
163 
168  size_t lastFragmentIndex() const
169  {
170  return fragmentIndex(block_count());
171  }
172 
173 protected:
174 
179  static constexpr size_t words_per_frag_word_()
180  {
181  return sizeof(Fragment::value_type) / sizeof(Metadata::data_t);
182  }
183 
184 private:
185 
186  Fragment const& artdaq_Fragment_;
187 };
188 
189 #endif /* artdaq_core_Data_ContainerFragment_hh */
std::unique_ptr< Fragment > FragmentPtr
A std::unique_ptr to a Fragment object.
Definition: Fragment.hh:53
static constexpr size_t words_per_frag_word_()
Gets the ratio between the fundamental data storage type and the representation within the Fragment...
static size_t const size_words
Size of the Metadata object.
size_t fragmentIndex(size_t index) const
Get the offset of a Fragment within the ContainerFragment.
The artdaq::ContainerFragment class represents a Fragment which contains other Fragments.
uint64_t count_t
Size of block_count variables.
static constexpr std::size_t num_words()
Returns the number of RawDataType words present in the header.
detail::RawFragmentHeader::type_t type_t
typedef for type_t from RawFragmentHeader
Definition: Fragment.hh:136
bool missing_data() const
Gets the flag if the ContainerFragment knows that it is missing data.
Fragment::type_t fragment_type() const
Get the Fragment::type_t of stored Fragment objects.
static const int CONTAINER_FRAGMENT_COUNT_MAX
The maximum capacity of the ContainerFragment (in fragments)
count_t fragment_type
The Fragment::type_t of stored Fragment objects.
size_t lastFragmentIndex() const
Returns the offset of the last Fragment in the ContainerFragment.
count_t block_count
The number of Fragment objects stored in the ContainerFragment.
iterator dataBegin()
Return an iterator to the beginning of the data payload (after header and metadata) ...
Definition: Fragment.hh:1025
QuickVec< RawDataType >::value_type value_type
Alias value_type type from QuickVec&lt;RawDataType&gt;
Definition: Fragment.hh:183
void const * dataBegin() const
Gets the start of the data.
Contains the information necessary for retrieving Fragment objects from the ContainerFragment.
void const * dataEnd() const
Gets the last Fragment in the ContainerFragment.
count_t missing_data
Flag if the ContainerFragment knows that it is missing data.
ContainerFragment(Fragment const &f)
Metadata::count_t block_count() const
Gets the number of fragments stored in the ContainerFragment.
T * metadata()
Return a pointer to the metadata. This throws an exception if the Fragment contains no metadata...
Definition: Fragment.hh:909
Metadata const * metadata() const
const getter function for the Metadata
detail::RawFragmentHeader::RawDataType RawDataType
The RawDataType (currently a 64-bit integer) is the basic unit of data representation within artdaq ...
Definition: Fragment.hh:39
size_t index[CONTAINER_FRAGMENT_COUNT_MAX]
Offset of each Fragment within the ContainerFragment.
FragmentPtr operator[](size_t index) const
Alias to ContainerFragment::at()
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
Definition: Fragment.hh:84
FragmentPtr at(size_t index) const
Gets a specific Fragment from the ContainerFragment.
size_t fragSize(size_t index) const
Gets the size of the Fragment at the specified location in the ContainerFragment, in bytes...
uint8_t data_t
Basic unit of data-retrieval.