00001 #ifndef mu2e_artdaq_core_Overlays_ArtFragment_hh
00002 #define mu2e_artdaq_core_Overlays_ArtFragment_hh
00003
00004 #include "artdaq-core/Data/Fragment.hh"
00005 #include "cetlib_except/exception.h"
00006
00007 #include <iostream>
00008
00009 #include <ostream>
00010 #include <vector>
00011
00012
00013
00014
00015 #define DATA_BLOCKS_PER_MU2E_FRAGMENT 2500
00016
00017 namespace mu2e {
00018 class ArtFragment;
00019
00020
00021 std::ostream &operator<<(std::ostream &, ArtFragment const &);
00022 }
00023
00024 class mu2e::ArtFragment {
00025 public:
00026 typedef uint16_t adc_t;
00027
00028 ArtFragment(artdaq::Fragment const &f) : artdaq_Fragment_(f) {
00029
00030 block_count_ = 0;
00031 size_t curIndex = 0;
00032 const adc_t *curPos = dataBegin();
00033 if (dataBegin() != dataEnd()) {
00034 while (curPos < dataEnd()) {
00035 block_count_++;
00036
00037 index[block_count_ - 1] = curIndex + *(curPos);
00038 curIndex += *(curPos);
00039 curPos = curPos + *(curPos) / 2;
00040 }
00041 }
00042
00043 }
00044
00045
00046 adc_t const *dataBegin() const {
00047 return reinterpret_cast<mu2e::ArtFragment::adc_t const *>(&*artdaq_Fragment_.dataBegin());
00048 }
00049
00050 adc_t const *dataEnd() const {
00051 return reinterpret_cast<mu2e::ArtFragment::adc_t const *>(&*artdaq_Fragment_.dataEnd());
00052 }
00053
00054
00055 size_t block_count() const { return block_count_; }
00056
00057 size_t byte_count() const { return index[block_count_ - 1]; }
00058
00059
00060 size_t blockIndexBytes(size_t offset) const {
00061 if (offset >= block_count()) {
00062 return 0;
00063 } else if (offset == 0) {
00064 return 0;
00065 }
00066 return index[offset - 1];
00067 }
00068
00069 size_t blockEndBytes(size_t offset) const {
00070 if (offset >= block_count()) {
00071 return byte_count();
00072 }
00073 return index[offset];
00074 }
00075
00076
00077 size_t blockSizeBytes(size_t offset) const {
00078 if (offset > block_count() - 1) {
00079 return 0;
00080 } else if (offset == 0) {
00081 return index[offset];
00082 }
00083 return index[offset] - index[offset - 1];
00084 }
00085
00086
00087 adc_t const *dataAtBytes(size_t offset) const { return dataBegin() + (offset / 2); }
00088
00089
00090 adc_t const *dataAtBlockIndex(size_t offset) const { return dataAtBytes(blockIndexBytes(offset)); }
00091
00092 void printPacketAtByte(size_t byteIdx) const {
00093 std::cout << "\t\t"
00094 << "Packet Bits (128): " << std::endl;
00095 for (int adcIdx = 0; adcIdx < 8; adcIdx++) {
00096 std::cout << "\t";
00097 for (int offset = 15; offset >= 0; offset--) {
00098 if (((*((adc_t const *)(dataAtBytes(byteIdx) + adcIdx))) & (1 << offset)) != 0) {
00099 std::cout << "1";
00100 } else {
00101 std::cout << "0";
00102 }
00103 if (offset == 7) {
00104 std::cout << " ";
00105 } else if (offset == 0) {
00106 std::cout << std::endl;
00107 }
00108 }
00109 }
00110 std::cout << std::endl;
00111 return;
00112 }
00113
00114 private:
00115 artdaq::Fragment const &artdaq_Fragment_;
00116 size_t block_count_;
00117 size_t index[DATA_BLOCKS_PER_MU2E_FRAGMENT];
00118 };
00119
00120 #endif