artdaq_core  v3_01_05
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/Fragment.hh"
14 #include "artdaq-core/Data/ContainerFragment.hh"
15 
16 #undef TRACE_NAME
17 #define TRACE_NAME "ContainerFragmentLoader"
18 #include "tracemf.h"
19 
20 #include <iostream>
21 
22 
23 namespace artdaq
24 {
25  class ContainerFragmentLoader;
26 }
27 
28 
33 {
34 public:
35 
36 
43  explicit ContainerFragmentLoader(Fragment& f, Fragment::type_t expectedFragmentType);
44 
45  // ReSharper disable once CppMemberFunctionMayBeConst
51  {
52  assert(artdaq_Fragment_.hasMetadata());
53  return reinterpret_cast<Metadata *>(&*artdaq_Fragment_.metadataAddress());
54  }
55 
61  {
62  metadata()->fragment_type = type;
63  }
64 
73  void set_missing_data(bool isDataMissing)
74  {
75  metadata()->missing_data = isDataMissing;
76  }
77 
83  void addFragment(artdaq::Fragment& frag);
84 
89  void addFragment(artdaq::FragmentPtr& frag);
90 
96 
97 private:
98  // Note that this non-const reference hides the const reference in the base class
99  artdaq::Fragment& artdaq_Fragment_;
100 
101  static size_t words_to_frag_words_(size_t nWords);
102 
103  void addSpace_(size_t bytes);
104 
105  uint8_t* dataBegin_() { return reinterpret_cast<uint8_t*>(&*artdaq_Fragment_.dataBegin()); }
106  void* dataEnd_() { return reinterpret_cast<void*>(dataBegin_() + lastFragmentIndex()); }
107 };
108 
109 inline artdaq::ContainerFragmentLoader::ContainerFragmentLoader(artdaq::Fragment& f, artdaq::Fragment::type_t expectedFragmentType = Fragment::EmptyFragmentType) :
111  , artdaq_Fragment_(f)
112 {
114  Metadata m;
115  m.block_count = 0;
116  m.fragment_type = expectedFragmentType;
117  m.missing_data = false;
118  for (int ii = 0; ii < CONTAINER_FRAGMENT_COUNT_MAX; ++ii)
119  {
120  m.index[ii] = 0;
121  }
122  artdaq_Fragment_.setMetadata<Metadata>(m);
123 
124  if (artdaq_Fragment_.size() !=
126  words_to_frag_words_(Metadata::size_words))
127  {
128  TLOG(TLVL_ERROR) << "ContainerFragmentLoader: Raw artdaq::Fragment object size suggests it does not consist of its own header + the ContainerFragment::Metadata object" << TLOG_ENDL;
129  TLOG(TLVL_ERROR) << "artdaq_Fragment size: " << artdaq_Fragment_.size() << ", Expected size: " << artdaq::detail::RawFragmentHeader::num_words() + words_to_frag_words_(Metadata::size_words) << TLOG_ENDL;
130 
131  throw cet::exception("InvalidFragment") << "ContainerFragmentLoader: Raw artdaq::Fragment object size suggests it does not consist of its own header + the ContainerFragment::Metadata object";
132  }
133 }
134 
135 inline size_t artdaq::ContainerFragmentLoader::words_to_frag_words_(size_t nWords)
136 {
137  size_t mod = nWords % words_per_frag_word_();
138  return mod ?
139  nWords / words_per_frag_word_() + 1 :
140  nWords / words_per_frag_word_();
141 }
142 
143 inline void artdaq::ContainerFragmentLoader::addSpace_(size_t bytes)
144 {
145  auto currSize = sizeof(artdaq::Fragment::value_type) * artdaq_Fragment_.dataSize(); // Resize takes into account header and metadata size
146  artdaq_Fragment_.resizeBytes(bytes + currSize);
147  TLOG(4) << "addSpace_: dataEnd_ is now at " << (void*)dataEnd_();
148 }
149 
151 {
152  if (metadata()->block_count >= CONTAINER_FRAGMENT_COUNT_MAX)
153  {
154  TLOG(TLVL_ERROR) << "addFragment: Fragment is full, cannot add more fragments!" << TLOG_ENDL;
155  throw cet::exception("ContainerFull") << "ContainerFragmentLoader::addFragment: Fragment is full, cannot add more fragments!";
156  }
157 
158  TLOG(4) << "addFragment: Adding Fragment with payload size " << frag.dataSizeBytes() << " to Container";
159  if (metadata()->fragment_type == Fragment::EmptyFragmentType) metadata()->fragment_type = frag.type();
160  else if (frag.type() != metadata()->fragment_type)
161  {
162  TLOG(TLVL_ERROR) << "addFragment: Trying to add a fragment of different type than what's already been added!" << TLOG_ENDL;
163  throw cet::exception("WrongFragmentType") << "ContainerFragmentLoader::addFragment: Trying to add a fragment of different type than what's already been added!";
164  }
165  TLOG(4) << "addFragment: Payload Size is " << artdaq_Fragment_.dataSizeBytes() << ", lastFragmentIndex is " << lastFragmentIndex() << ", and frag.size is " << frag.sizeBytes();
166  if (artdaq_Fragment_.dataSizeBytes() < lastFragmentIndex() + frag.sizeBytes())
167  {
168  addSpace_(frag.sizeBytes());
169  }
170  frag.setSequenceID(artdaq_Fragment_.sequenceID());
171  TLOG(4) << "addFragment, copying " << frag.sizeBytes() << " bytes from " << (void*)frag.headerAddress() << " to " << (void*)dataEnd_();
172  memcpy(dataEnd_(), frag.headerAddress(), frag.sizeBytes());
173  metadata()->index[block_count()] = lastFragmentIndex() + frag.sizeBytes();
174  metadata()->block_count++;
175 }
176 
178 {
179  addFragment(*frag);
180 }
181 
183 {
184  for (auto& frag : frags)
185  {
186  addFragment((*frag));
187  }
188 }
189 
190 #endif /* artdaq_core_Data_ContainerFragmentLoader_hh */
std::unique_ptr< Fragment > FragmentPtr
A std::unique_ptr to a Fragment object.
Definition: Fragment.hh:53
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:355
void setSequenceID(sequence_id_t sequence_id)
Sets the Sequence ID of the Fragment.
Definition: Fragment.hh:861
void setSystemType(type_t stype)
Sets the type of the Fragment, checking that it is a valid system type.
Definition: Fragment.hh:854
std::size_t size() const
Gets the size of the Fragment, from the Fragment header.
Definition: Fragment.hh:798
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:154
std::size_t sizeBytes() const
Size of vals_ vector ( header + (optional) metadata + payload) in bytes.
Definition: Fragment.hh:340
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
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.
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:1025
QuickVec< RawDataType >::value_type value_type
Alias value_type type from QuickVec&lt;RawDataType&gt;
Definition: Fragment.hh:183
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:155
ContainerFragmentLoader(Fragment &f, Fragment::type_t expectedFragmentType)
Constructs the ContainerFragmentLoader.
void setMetadata(const T &md)
Set the metadata in the Fragment to the contents of the specified structure. This throws an exception...
Definition: Fragment.hh:936
type_t type() const
Type of the Fragment, from the Fragment header.
Definition: Fragment.hh:812
RawDataType * metadataAddress()
Get the address of the metadata. For internal use only, use metadata() instead.
Definition: Fragment.hh:1109
size_t index[CONTAINER_FRAGMENT_COUNT_MAX]
Offset of each Fragment within the ContainerFragment.
std::list< FragmentPtr > FragmentPtrs
A std::list of FragmentPtrs.
Definition: Fragment.hh:58
bool hasMetadata() const
Test whether this Fragment has metadata.
Definition: Fragment.hh:902
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
Definition: Fragment.hh:84
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.
RawDataType * headerAddress()
Gets the address of the header.
Definition: Fragment.hh:1121