1 #ifndef artdaq_core_Data_Fragment_hh
2 #define artdaq_core_Data_Fragment_hh
18 #include "artdaq-core/Core/QuickVec.hh"
19 #include "artdaq-core/Data/detail/RawFragmentHeader.hh"
20 #include "artdaq-core/Data/detail/RawFragmentHeaderV0.hh"
21 #include "artdaq-core/Data/detail/RawFragmentHeaderV1.hh"
22 #include "artdaq-core/Data/dictionarycontrol.hh"
31 #define DATAVEC_T QuickVec<RawDataType>
264 void print(std::ostream& os)
const;
270 std::size_t
size()
const;
347 struct timespec
atime() const;
430 void resize(std::size_t sz);
505 T newpointer =
reinterpret_cast<T
>(in);
507 if (static_cast<const void*>(newpointer) !=
static_cast<const void*
>(in))
509 throw cet::exception(
"Error in Fragment.hh: reinterpret_cast failed to return expected address-- please contact John Freeman at jcfree@fnal.gov");
533 T newpointer =
reinterpret_cast<T
>(in);
535 if (static_cast<void*>(newpointer) !=
static_cast<void*
>(in))
537 throw cet::exception(
"Error in Fragment.hh: reinterpret_cast failed to return expected address-- please contact John Freeman at jcfree@fnal.gov");
598 return reinterpret_cast_checked<const byte_t*>(&*
dataBegin());
611 return reinterpret_cast_checked<const byte_t*>(&*
dataEnd());
627 return reinterpret_cast_checked<const byte_t*>(&*
headerBegin());
671 void swap(DATAVEC_T& other) noexcept { vals_.
swap(other); };
710 template<
class InputIterator>
718 std::copy(i, e, std::back_inserter(result->vals_));
719 result->updateFragmentHeaderWC_();
741 static std::size_t validatedMetadataSize_();
743 void updateFragmentHeaderWC_();
760 inline artdaq::Fragment& artdaq::Fragment::operator=(artdaq::Fragment&&) noexcept = default;
762 inline
bool constexpr artdaq::Fragment::
765 return fragmentType >= detail::RawFragmentHeader::FIRST_USER_TYPE &&
766 fragmentType <= detail::RawFragmentHeader::LAST_USER_TYPE;
778 validatedMetadataSize_()
783 static_assert(
sizeof(
size_t) >=
784 sizeof(decltype(std::numeric_limits<detail::RawFragmentHeader::metadata_word_count_t>::max())),
785 "metadata_word_count_t is too big!");
787 static size_t constexpr max_md_wc =
788 std::numeric_limits<detail::RawFragmentHeader::metadata_word_count_t>::max();
789 size_t requested_md_wc =
791 if (requested_md_wc > max_md_wc)
793 throw cet::exception(
"InvalidRequest")
794 <<
"The requested metadata structure is too large: "
795 <<
"requested word count = " << requested_md_wc
796 <<
", maximum word count = " << max_md_wc;
798 return requested_md_wc;
806 : vals_((artdaq::detail::RawFragmentHeader::num_words() +
807 validatedMetadataSize_<T>() +
819 updateFragmentHeaderWC_();
825 fragmentHeaderPtr()->
touch();
837 return fragmentHeader().word_count;
843 auto hdr = reinterpret_cast_checked<detail::RawFragmentHeader const*>(&vals_[0]);
850 return static_cast<type_t>(fragmentHeader().type);
862 return fragmentHeader().sequence_id;
868 return fragmentHeader().fragment_id;
874 return fragmentHeader().timestamp;
880 fragmentHeaderPtr()->setUserType(static_cast<uint8_t>(type));
886 fragmentHeaderPtr()->setSystemType(static_cast<uint8_t>(type));
893 fragmentHeaderPtr()->sequence_id = sequence_id;
899 fragmentHeaderPtr()->fragment_id = fragment_id;
905 fragmentHeaderPtr()->timestamp = timestamp;
910 fragmentHeaderPtr()->touch();
915 return fragmentHeader().atime();
920 return fragmentHeaderPtr()->getLatency(touch);
924 artdaq::Fragment::updateFragmentHeaderWC_()
928 assert(vals_.size() < (1ULL << 32));
929 TRACEN(
"Fragment", 50,
"Fragment::updateFragmentHeaderWC_ adjusting fragmentHeader()->word_count from %u to %zu", (
unsigned)(fragmentHeaderPtr()->word_count), vals_.size());
930 fragmentHeaderPtr()->word_count = vals_.size();
936 return vals_.size() - headerSizeWords() -
937 fragmentHeader().metadata_word_count;
943 return fragmentHeader().metadata_word_count != 0;
949 if (fragmentHeader().metadata_word_count == 0)
951 throw cet::exception(
"InvalidRequest")
952 <<
"No metadata has been stored in this Fragment.";
955 return reinterpret_cast_checked<T*>(&vals_[headerSizeWords()]);
962 if (fragmentHeader().metadata_word_count == 0)
964 throw cet::exception(
"InvalidRequest")
965 <<
"No metadata has been stored in this Fragment.";
967 return reinterpret_cast_checked<T const*>(&vals_[headerSizeWords()]);
973 if (fragmentHeader().metadata_word_count != 0)
975 throw cet::exception(
"InvalidRequest")
976 <<
"Metadata has already been stored in this Fragment.";
978 auto const mdSize = validatedMetadataSize_<T>();
979 vals_.insert(dataBegin(), mdSize, 0);
980 updateFragmentHeaderWC_();
981 fragmentHeaderPtr()->metadata_word_count = mdSize;
983 memcpy(metadataAddress(), &metadata,
sizeof(T));
989 if (fragmentHeader().metadata_word_count == 0)
991 throw cet::exception(
"InvalidRequest")
992 <<
"No metadata in fragment; please use Fragment::setMetadata instead of Fragment::updateMetadata";
995 auto const mdSize = validatedMetadataSize_<T>();
997 if (fragmentHeader().metadata_word_count != mdSize)
999 throw cet::exception(
"InvalidRequest")
1000 <<
"Mismatch between type of metadata struct passed to updateMetadata and existing metadata struct";
1003 memcpy(metadataAddress(), &metadata,
sizeof(T));
1009 vals_.resize(sz + fragmentHeaderPtr()->metadata_word_count +
1011 updateFragmentHeaderWC_();
1017 vals_.resize(sz + fragmentHeaderPtr()->metadata_word_count +
1020 updateFragmentHeaderWC_();
1034 vals_.resizeWithCushion(nwords + fragmentHeaderPtr()->metadata_word_count +
1037 updateFragmentHeaderWC_();
1044 byte_t* ptr = reinterpret_cast_checked<byte_t*>(&defaultval);
1054 resize(nwords, defaultval);
1060 vals_.resize(fragmentHeaderPtr()->word_count);
1061 updateFragmentHeaderWC_();
1067 return vals_.begin() + headerSizeWords() +
1068 fragmentHeader().metadata_word_count;
1080 return vals_.begin();
1086 return vals_.begin() + headerSizeWords() +
1087 fragmentHeader().metadata_word_count;
1099 return vals_.begin();
1105 vals_.erase(dataBegin(), dataEnd());
1106 updateFragmentHeaderWC_();
1112 return (vals_.size() - headerSizeWords() -
1113 fragmentHeader().metadata_word_count) == 0;
1119 vals_.reserve(cap + headerSizeWords() +
1120 fragmentHeader().metadata_word_count);
1126 vals_.swap(other.vals_);
1132 return &vals_[0] + headerSizeWords() +
1133 fragmentHeader().metadata_word_count;
1139 if (fragmentHeader().metadata_word_count == 0)
1141 throw cet::exception(
"InvalidRequest")
1142 <<
"No metadata has been stored in this Fragment.";
1144 return &vals_[0] + headerSizeWords();
1156 auto hdr = reinterpret_cast_checked<detail::RawFragmentHeader const*>(&vals_[0]);
1159 switch (hdr->version)
1162 TLOG(51,
"Fragment") <<
"Cannot get header size of InvalidVersion Fragment";
1166 TLOG(52,
"Fragment") <<
"Getting size of RawFragmentHeaderV0";
1167 auto old_hdr = reinterpret_cast_checked<detail::RawFragmentHeaderV0 const*>(&vals_[0]);
1168 return old_hdr->num_words();
1173 TLOG(52,
"Fragment") <<
"Getting size of RawFragmentHeaderV1";
1174 auto old_hdr = reinterpret_cast_checked<detail::RawFragmentHeaderV1 const*>(&vals_[0]);
1175 return old_hdr->num_words();
1179 throw cet::exception(
"Fragment") <<
"A Fragment with an unknown version (" << std::to_string(hdr->version) <<
") was received!";
1183 return hdr->num_words();
1187 artdaq::Fragment::fragmentHeaderPtr()
1189 auto hdr = reinterpret_cast_checked<detail::RawFragmentHeader*>(&vals_[0]);
1192 switch (hdr->version)
1195 TLOG(51,
"Fragment") <<
"Not upgrading InvalidVersion Fragment";
1199 TLOG(52,
"Fragment") <<
"Upgrading RawFragmentHeaderV0 (non const)";
1200 auto old_hdr = reinterpret_cast_checked<detail::RawFragmentHeaderV0*>(&vals_[0]);
1201 auto new_hdr = old_hdr->upgrade();
1203 auto szDiff = hdr->
num_words() - old_hdr->num_words();
1206 vals_.insert(vals_.begin(), szDiff, 0);
1207 new_hdr.word_count = vals_.size();
1209 memcpy(&vals_[0], &new_hdr, hdr->num_words() *
sizeof(
RawDataType));
1210 hdr = reinterpret_cast_checked<detail::RawFragmentHeader*>(&vals_[0]);
1215 TLOG(52,
"Fragment") <<
"Upgrading RawFragmentHeaderV1 (non const)";
1216 auto old_hdr = reinterpret_cast_checked<detail::RawFragmentHeaderV1*>(&vals_[0]);
1217 auto new_hdr = old_hdr->upgrade();
1219 auto szDiff = hdr->num_words() - old_hdr->num_words();
1222 vals_.insert(vals_.begin(), szDiff, 0);
1223 new_hdr.word_count = vals_.size();
1225 memcpy(&vals_[0], &new_hdr, hdr->num_words() *
sizeof(
RawDataType));
1226 hdr = reinterpret_cast_checked<detail::RawFragmentHeader*>(&vals_[0]);
1230 throw cet::exception(
"Fragment") <<
"A Fragment with an unknown version (" << std::to_string(hdr->version) <<
") was received!";
1238 artdaq::Fragment::fragmentHeader()
const
1240 auto hdr = *reinterpret_cast_checked<detail::RawFragmentHeader const*>(&vals_[0]);
1243 switch (hdr.version)
1246 TLOG(51,
"Fragment") <<
"Not upgrading InvalidVersion Fragment";
1250 TLOG(52,
"Fragment") <<
"Upgrading RawFragmentHeaderV0 (const)";
1251 auto old_hdr = reinterpret_cast_checked<detail::RawFragmentHeaderV0 const*>(&vals_[0]);
1252 hdr = old_hdr->upgrade();
1258 TLOG(52,
"Fragment") <<
"Upgrading RawFragmentHeaderV1 (const)";
1259 auto old_hdr = reinterpret_cast_checked<detail::RawFragmentHeaderV1 const*>(&vals_[0]);
1260 hdr = old_hdr->upgrade();
1265 throw cet::exception(
"Fragment") <<
"A Fragment with an unknown version (" << std::to_string(hdr.version) <<
") was received!";
1278 inline std::ostream&
std::unique_ptr< Fragment > FragmentPtr
A std::unique_ptr to a Fragment object.
void reserve(std::size_t cap)
Reserves enough memory to hold cap RawDataType words in the Fragment payload.
detail::RawFragmentHeader::timestamp_t timestamp_t
typedef for timestamp_t from RawFragmentHeader
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...
static constexpr type_t InvalidFragmentType
Copy InvalidFragmentType from RawFragmentHeader.
T reinterpret_cast_checked(const RawDataType *in) const
Wrapper around reinterpret_cast.
QuickVec< RawDataType >::difference_type difference_type
Alias difference_type type from QuickVec<RawDataType>
void setSequenceID(sequence_id_t sequence_id)
Sets the Sequence ID of the Fragment.
void setSystemType(type_t stype)
Sets the type of the Fragment, checking that it is a valid system type.
std::vector< Fragment > Fragments
A std::vector of Fragment objects.
std::size_t size() const
Gets the size of the Fragment, from the Fragment header.
static constexpr type_t EndOfDataFragmentType
Copy EndOfDataFragmentType from RawFragmentHeader.
static constexpr timestamp_t InvalidTimestamp
Copy InvalidTimestamp from RawFragmentHeader.
void updateMetadata(const T &md)
Updates existing metadata with a new metadata object.
static constexpr type_t EmptyFragmentType
Copy EmptyFragmentType from RawFragmentHeader.
bool fragmentSequenceIDCompare(Fragment i, Fragment j)
Comparator for Fragment objects, based on their sequence_id.
const byte_t * dataEndBytes() const
Return const Fragment::byte_t* pointing at the end of the payload.
struct timespec atime() const
Get the last access time of the Fragment.
static constexpr bool isUserFragmentType(type_t fragmentType)
Returns whether the given type is in the range of user types.
detail::RawFragmentHeader::fragment_id_t fragment_id_t
typedef for fragment_id_t from RawFragmentHeader
std::size_t sizeBytes() const
Size of vals_ vector ( header + (optional) metadata + payload) in bytes.
byte_t * dataBeginBytes()
Return Fragment::byte_t* pointing at the beginning of the payload.
detail::RawFragmentHeader::type_t type_t
typedef for type_t from RawFragmentHeader
void resizeBytes(std::size_t szbytes)
Resize the data payload to hold szbytes bytes (padded by the 8-byte RawDataTypes, so...
static FragmentPtr dataFrag(sequence_id_t sequenceID, fragment_id_t fragID, InputIterator i, InputIterator e)
Creates a Fragment, copying data from given location. 12-Apr-2013, KAB - this method is deprecated...
sequence_id_t sequenceID() const
Sequence ID of the Fragment, from the Fragment header.
timestamp_t timestamp() const
Timestamp of the Fragment, from the Fragment header.
static constexpr type_t DataFragmentType
Copy DataFragmentType from RawFragmentHeader.
QuickVec< RawDataType >::const_iterator const_iterator
Alias const_iterator type from QuickVec<RawDataType>
uint8_t byte_t
For byte representation.
void resize(std::size_t sz)
Resize the data payload to hold sz RawDataType words.
void autoResize()
Resize the fragment to hold the number of words indicated by the header.
static constexpr type_t EndOfRunFragmentType
Copy EndOfRunFragmentType from RawFragmentHeader.
static constexpr fragment_id_t InvalidFragmentID
Copy InvalidFragmentID from RawFragmentHeader.
size_t size() const
Accesses the current size of the QuickVec.
static constexpr type_t InitFragmentType
Copy InitFragmentType from RawFragmentHeader.
QuickVec< RawDataType >::reference reference
Alias reference type from QuickVec<RawDataType>
byte_t * headerBeginBytes()
Return a Fragment::byte_t pointer pointing to the beginning of the header.
static FragmentPtr FragmentBytes(std::size_t payload_size_in_bytes, sequence_id_t sequence_id, fragment_id_t fragment_id, type_t type, const T &metadata, timestamp_t timestamp=Fragment::InvalidTimestamp)
Create a Fragment with the given header values. Uses static factory function instead of constructor t...
static constexpr type_t ErrorFragmentType
Copy ErrorFragmentType from RawFragmentHeader.
struct timespec getLatency(bool touch)
Get the difference between the current time and the last access time of the Fragment.
detail::RawFragmentHeader::version_t version_t
typedef for version_t from RawFragmentHeader
void setTimestamp(timestamp_t timestamp)
Sets the Timestamp of the Fragment.
iterator headerBegin()
Return an iterator to the beginning of the header (should be used for serialization only: use setters...
T reinterpret_cast_checked(RawDataType *in)
Wrapper around reinterpret_cast.
byte_t * dataEndBytes()
Return Fragment::byte_t* pointing at the end of the payload.
static constexpr version_t InvalidVersion
Copy InvalidVersion from RawFragmentHeader.
iterator dataBegin()
Return an iterator to the beginning of the data payload (after header and metadata) ...
bool empty()
Determines if the Fragment contains no data.
static constexpr sequence_id_t InvalidSequenceID
Copy InvalidSequenceID from RawFragmentHeader.
QuickVec< RawDataType >::value_type value_type
Alias value_type type from QuickVec<RawDataType>
static FragmentPtr eodFrag(size_t nFragsToExpect)
Creates an EndOfData Fragment.
void setUserType(type_t utype)
Sets the type of the Fragment, checking that it is a valid user type.
void touch()
Update the access time of the Fragment.
static constexpr type_t ContainerFragmentType
Copy ContainerFragmentType from RawFragmentHeader.
static std::map< type_t, std::string > MakeSystemTypeMap()
Returns a map of the most commonly-used system types.
std::ostream & operator<<(std::ostream &os, Fragment const &f)
Prints the given Fragment to the stream.
void swap(QuickVec &other) noexcept
Exchanges references to two QuickVec objects.
void setMetadata(const T &md)
Set the metadata in the Fragment to the contents of the specified structure. This throws an exception...
const byte_t * dataBeginBytes() const
Return const Fragment::byte_t* pointing at the beginning of the payload.
type_t type() const
Type of the Fragment, from the Fragment header.
RawDataType * dataAddress()
Returns a RawDataType pointer to the beginning of the payload.
void print(std::ostream &os) const
Print out summary information for this Fragment to the given stream.
T * metadata()
Return a pointer to the metadata. This throws an exception if the Fragment contains no metadata...
iterator dataEnd()
Return an iterator to the end of the data payload.
static FragmentPtr FragmentBytes(std::size_t nbytes)
Create a Fragment using a static factory function rather than a constructor to allow for the function...
static constexpr type_t ShutdownFragmentType
Copy ShutdownFragmentType from RawFragmentHeader.
static constexpr type_t FirstUserFragmentType
Copy FIRST_USER_TYPE from RawFragmentHeader.
std::string typeString() const
Print the type of the Fragment.
detail::RawFragmentHeader::RawDataType RawDataType
The RawDataType (currently a 64-bit integer) is the basic unit of data representation within artdaq ...
RawDataType * metadataAddress()
Get the address of the metadata. For internal use only, use metadata() instead.
size_t headerSizeBytes() const
Get the size of this Fragment's header, in bytes.
std::list< FragmentPtr > FragmentPtrs
A std::list of FragmentPtrs.
Fragment()
Create a Fragment with all header values zeroed.
size_t headerSizeWords() const
Get the size of this Fragment's header, in RawDataType words.
void clear()
Removes all elements from the payload section of the Fragment.
static constexpr bool isSystemFragmentType(type_t fragmentType)
Returns whether the given type is in the range of system types.
QuickVec< RawDataType >::iterator iterator
Alias iterator type from QuickVec<RawDataType>
bool hasMetadata() const
Test whether this Fragment has metadata.
QuickVec< RawDataType >::size_type size_type
Alias size_type type from QuickVec<RawDataType>
const byte_t * headerBeginBytes() const
Return a const Fragment::byte_t pointer pointing to the beginning of the header.
void setFragmentID(fragment_id_t fragment_id)
Sets the Fragment ID of the Fragment.
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
void swap(Fragment &other) noexcept
Swaps two Fragment objects.
static constexpr type_t EndOfSubrunFragmentType
Copy EndOfSubrunFragmentType from RawFragmentHeader.
void resizeBytesWithCushion(std::size_t szbytes, double growthFactor=1.3)
Resize the data payload to hold szbytes bytes (padded by the 8-byte RawDataTypes, so...
std::size_t dataSize() const
Return the number of RawDataType words in the data payload. This does not include the number of words...
iterator begin()
Gets an iterator to the beginning of the QuickVec.
void swap(QuickVec< RawDataType > &other) noexcept
Swaps two Fragment data vectors.
detail::RawFragmentHeader::sequence_id_t sequence_id_t
typedef for sequence_id_t from RawFragmentHeader
fragment_id_t fragmentID() const
Fragment ID of the Fragment, from the Fragment header.
RawDataType * headerAddress()
Gets the address of the header.
version_t version() const
Version of the Fragment, from the Fragment header.