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 {
00026 public:
00027 typedef uint16_t adc_t;
00028
00029 ArtFragment(artdaq::Fragment const &f)
00030 : artdaq_Fragment_(f)
00031 {
00032
00033 block_count_ = 0;
00034 size_t curIndex = 0;
00035 const adc_t *curPos = dataBegin();
00036 if (dataBegin() != dataEnd()) {
00037 while (curPos < dataEnd()) {
00038 block_count_++;
00039
00040 index[block_count_ - 1] = curIndex + *(curPos);
00041 curIndex += *(curPos);
00042 curPos = curPos + *(curPos) / 2;
00043 }
00044 }
00045
00046 }
00047
00048
00049 adc_t const *dataBegin() const
00050 {
00051 return reinterpret_cast<mu2e::ArtFragment::adc_t const *>(&*artdaq_Fragment_.dataBegin());
00052 }
00053
00054 adc_t const *dataEnd() const
00055 {
00056 return reinterpret_cast<mu2e::ArtFragment::adc_t const *>(&*artdaq_Fragment_.dataEnd());
00057 }
00058
00059
00060 size_t block_count() const { return block_count_; }
00061
00062 size_t byte_count() const { return index[block_count_ - 1]; }
00063
00064
00065 size_t blockIndexBytes(size_t offset) const
00066 {
00067 if (offset >= block_count()) {
00068 return 0;
00069 }
00070 else if (offset == 0)
00071 {
00072 return 0;
00073 }
00074 return index[offset - 1];
00075 }
00076
00077 size_t blockEndBytes(size_t offset) const
00078 {
00079 if (offset >= block_count()) {
00080 return byte_count();
00081 }
00082 return index[offset];
00083 }
00084
00085
00086 size_t blockSizeBytes(size_t offset) const
00087 {
00088 if (offset > block_count() - 1) {
00089 return 0;
00090 }
00091 else if (offset == 0)
00092 {
00093 return index[offset];
00094 }
00095 return index[offset] - index[offset - 1];
00096 }
00097
00098
00099 adc_t const *dataAtBytes(size_t offset) const { return dataBegin() + (offset / 2); }
00100
00101
00102 adc_t const *dataAtBlockIndex(size_t offset) const { return dataAtBytes(blockIndexBytes(offset)); }
00103
00104 void printPacketAtByte(size_t byteIdx) const
00105 {
00106 std::cout << "\t\t"
00107 << "Packet Bits (128): " << std::endl;
00108 for (int adcIdx = 0; adcIdx < 8; adcIdx++) {
00109 std::cout << "\t";
00110 for (int offset = 15; offset >= 0; offset--) {
00111 if (((*((adc_t const *)(dataAtBytes(byteIdx) + adcIdx))) & (1 << offset)) != 0) {
00112 std::cout << "1";
00113 }
00114 else
00115 {
00116 std::cout << "0";
00117 }
00118 if (offset == 7) {
00119 std::cout << " ";
00120 }
00121 else if (offset == 0)
00122 {
00123 std::cout << std::endl;
00124 }
00125 }
00126 }
00127 std::cout << std::endl;
00128 return;
00129 }
00130
00131 private:
00132 artdaq::Fragment const &artdaq_Fragment_;
00133 size_t block_count_;
00134 size_t index[DATA_BLOCKS_PER_MU2E_FRAGMENT];
00135 };
00136
00137 #endif