artdaq_core  v3_06_00
ContainerFragmentLoader.hh
1 #ifndef artdaq_core_Data_ContainerFragmentLoader_hh
2 #define artdaq_core_Data_ContainerFragmentLoader_hh
3 
5 // ContainerFragmentLoader
6 //
7 // This class gives write access to a ContainerFragment. It should be
8 // used when multiple fragments are generated by one BoardReader for a
9 // single event.
10 //
12 
13 #include "artdaq-core/Data/ContainerFragment.hh"
14 #include "artdaq-core/Data/Fragment.hh"
15 
16 #include "tracemf.h"
17 
18 #include <iostream>
19 
20 namespace artdaq {
21 class ContainerFragmentLoader;
22 }
23 
28 {
29 public:
36  explicit ContainerFragmentLoader(Fragment& f, Fragment::type_t expectedFragmentType);
37 
38  // ReSharper disable once CppMemberFunctionMayBeConst
44  {
45  assert(artdaq_Fragment_.hasMetadata());
46  return reinterpret_cast<Metadata*>(&*artdaq_Fragment_.metadataAddress()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
47  }
48 
54  {
55  metadata()->fragment_type = type;
56  }
57 
66  void set_missing_data(bool isDataMissing)
67  {
68  metadata()->missing_data = isDataMissing;
69  }
70 
76  void addFragment(artdaq::Fragment& frag);
77 
82  void addFragment(artdaq::FragmentPtr& frag);
83 
89 
90 private:
91  // Note that this non-const reference hides the const reference in the base class
92  artdaq::Fragment& artdaq_Fragment_;
93 
94  static size_t words_to_frag_words_(size_t nWords);
95 
96  void addSpace_(size_t bytes);
97 
98  uint8_t* dataBegin_() { return reinterpret_cast<uint8_t*>(&*artdaq_Fragment_.dataBegin()); } // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
99  void* dataEnd_() { return static_cast<void*>(dataBegin_() + lastFragmentIndex()); } // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
100 };
101 
102 inline artdaq::ContainerFragmentLoader::ContainerFragmentLoader(artdaq::Fragment& f, artdaq::Fragment::type_t expectedFragmentType = Fragment::EmptyFragmentType)
103  : ContainerFragment(f)
104  , artdaq_Fragment_(f)
105 {
107  Metadata m;
108  m.block_count = 0;
109  m.fragment_type = expectedFragmentType;
110  m.missing_data = false;
111  m.has_index = true;
113  m.index_offset = 0;
114  artdaq_Fragment_.setMetadata<Metadata>(m);
115 
116  if (artdaq_Fragment_.size() !=
118  words_to_frag_words_(Metadata::size_words))
119  {
120  TLOG(TLVL_ERROR, "ContainerFragmentLoader") << "ContainerFragmentLoader: Raw artdaq::Fragment object size suggests it does not consist of its own header + the ContainerFragment::Metadata object";
121  TLOG(TLVL_ERROR, "ContainerFragmentLoader") << "artdaq_Fragment size: " << artdaq_Fragment_.size() << ", Expected size: " << artdaq::detail::RawFragmentHeader::num_words() + words_to_frag_words_(Metadata::size_words);
122 
123  throw cet::exception("InvalidFragment") << "ContainerFragmentLoader: Raw artdaq::Fragment object size suggests it does not consist of its own header + the ContainerFragment::Metadata object"; // NOLINT(cert-err60-cpp)
124  }
125 
126  artdaq_Fragment_.resize(1);
127  *artdaq_Fragment_.dataBegin() = CONTAINER_MAGIC;
128 }
129 
130 inline size_t artdaq::ContainerFragmentLoader::words_to_frag_words_(size_t nWords)
131 {
132  size_t mod = nWords % words_per_frag_word_();
133  return mod ? nWords / words_per_frag_word_() + 1 : nWords / words_per_frag_word_();
134 }
135 
136 inline void artdaq::ContainerFragmentLoader::addSpace_(size_t bytes)
137 {
138  auto currSize = sizeof(artdaq::Fragment::value_type) * artdaq_Fragment_.dataSize(); // Resize takes into account header and metadata size
139  artdaq_Fragment_.resizeBytesWithCushion(bytes + currSize, 1.3);
140  reset_index_ptr_(); // Must reset index_ptr after resize operation!
141 
142  TLOG(TLVL_TRACE, "ContainerFragmentLoader") << "addSpace_: dataEnd_ is now at " << static_cast<void*>(dataEnd_()) << " (oldSizeBytes/deltaBytes: " << currSize << "/" << bytes << ")";
143 }
144 
146 {
147  TLOG(TLVL_TRACE, "ContainerFragmentLoader") << "addFragment: Adding Fragment with payload size " << frag.dataSizeBytes() << " to Container";
148  if (metadata()->fragment_type == Fragment::EmptyFragmentType)
149  metadata()->fragment_type = frag.type();
150  else if (frag.type() != metadata()->fragment_type)
151  {
152  TLOG(TLVL_ERROR, "ContainerFragmentLoader") << "addFragment: Trying to add a fragment of different type than what's already been added!";
153  throw cet::exception("WrongFragmentType") << "ContainerFragmentLoader::addFragment: Trying to add a fragment of different type than what's already been added!"; // NOLINT(cert-err60-cpp)
154  }
155 
156  TLOG(TLVL_TRACE, "ContainerFragmentLoader") << "addFragment: Payload Size is " << artdaq_Fragment_.dataSizeBytes() << ", lastFragmentIndex is " << lastFragmentIndex() << ", and frag.size is " << frag.sizeBytes();
157  if (artdaq_Fragment_.dataSizeBytes() < (lastFragmentIndex() + frag.sizeBytes() + sizeof(size_t) * (metadata()->block_count + 2)))
158  {
159  addSpace_((lastFragmentIndex() + frag.sizeBytes() + sizeof(size_t) * (metadata()->block_count + 2)) - artdaq_Fragment_.dataSizeBytes());
160  }
161  frag.setSequenceID(artdaq_Fragment_.sequenceID());
162  TLOG(TLVL_TRACE, "ContainerFragmentLoader") << "addFragment, copying " << frag.sizeBytes() << " bytes from " << static_cast<void*>(frag.headerAddress()) << " to " << static_cast<void*>(dataEnd_());
163  memcpy(dataEnd_(), frag.headerAddress(), frag.sizeBytes());
164  metadata()->has_index = 0;
165 
166  metadata()->block_count++;
167 
168  auto index = create_index_();
169  metadata()->index_offset = index[metadata()->block_count - 1]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
170  memcpy(dataBegin_() + metadata()->index_offset, index, sizeof(size_t) * (metadata()->block_count + 1)); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
171 
172  metadata()->has_index = 1;
173  reset_index_ptr_();
174 }
175 
177 {
178  addFragment(*frag);
179 }
180 
182 {
183  for (auto& frag : frags)
184  {
185  addFragment((*frag));
186  }
187 }
188 
189 #endif /* artdaq_core_Data_ContainerFragmentLoader_hh */
std::unique_ptr< Fragment > FragmentPtr
A std::unique_ptr to a Fragment object.
Definition: Fragment.hh:54
static size_t const size_words
Size of the Metadata object.
The artdaq::ContainerFragment class represents a Fragment which contains other Fragments.
std::size_t dataSizeBytes() const
Return the number of bytes in the data payload. This does not include the number of bytes in the head...
Definition: Fragment.hh:374
void setSequenceID(sequence_id_t sequence_id)
Sets the Sequence ID of the Fragment.
Definition: Fragment.hh:895
void setSystemType(type_t stype)
Sets the type of the Fragment, checking that it is a valid system type.
Definition: Fragment.hh:889
std::size_t size() const
Gets the size of the Fragment, from the Fragment header.
Definition: Fragment.hh:840
void set_fragment_type(Fragment::type_t type)
Sets the type of Fragment which this ContainerFragment should contain.
A Read-Write version of the ContainerFragment, used for filling ContainerFragment objects with other ...
static constexpr type_t EmptyFragmentType
Copy EmptyFragmentType from RawFragmentHeader.
Definition: Fragment.hh:155
std::size_t sizeBytes() const
Size of vals_ vector ( header + (optional) metadata + payload) in bytes.
Definition: Fragment.hh:360
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:137
count_t has_index
Whether the ContainerFragment has an index at the end of the payload.
void resize(std::size_t sz)
Resize the data payload to hold sz RawDataType words.
Definition: Fragment.hh:1012
static constexpr size_t CONTAINER_MAGIC
Marker word used in index.
count_t fragment_type
The Fragment::type_t of stored Fragment objects.
void setMetadata(const T &metadata)
Set the metadata in the Fragment to the contents of the specified structure. This throws an exception...
Definition: Fragment.hh:976
size_t lastFragmentIndex() const
Returns the offset of the last Fragment in the ContainerFragment.
Metadata * metadata()
Get the ContainerFragment metadata (includes information about the location of Fragment objects withi...
void addFragment(artdaq::Fragment &frag)
Add a Fragment to the ContainerFragment by reference.
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:1070
QuickVec< RawDataType >::value_type value_type
Alias value_type type from QuickVec&lt;RawDataType&gt;
Definition: Fragment.hh:185
Contains the information necessary for retrieving Fragment objects from the ContainerFragment.
count_t missing_data
Flag if the ContainerFragment knows that it is missing data.
static constexpr type_t ContainerFragmentType
Copy ContainerFragmentType from RawFragmentHeader.
Definition: Fragment.hh:156
ContainerFragmentLoader(Fragment &f, Fragment::type_t expectedFragmentType)
Constructs the ContainerFragmentLoader.
type_t type() const
Type of the Fragment, from the Fragment header.
Definition: Fragment.hh:853
static constexpr uint8_t CURRENT_VERSION
The current version of the ContainerFragmentHeader.
RawDataType * metadataAddress()
Get the address of the metadata. For internal use only, use metadata() instead.
Definition: Fragment.hh:1142
std::list< FragmentPtr > FragmentPtrs
A std::list of FragmentPtrs.
Definition: Fragment.hh:59
bool hasMetadata() const
Test whether this Fragment has metadata.
Definition: Fragment.hh:946
count_t version
Version number of ContainerFragment.
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
Definition: Fragment.hh:85
void set_missing_data(bool isDataMissing)
Sets the missing_data flag.
void addFragments(artdaq::FragmentPtrs &frags)
Add a collection of Fragment objects to the ContainerFragment.
uint64_t index_offset
Index starts this many bytes after the beginning of the payload (is also the total size of contained ...
RawDataType * headerAddress()
Gets the address of the header.
Definition: Fragment.hh:1153