00001 #ifndef mu2e_artdaq_core_Overlays_mu2eFragmentReader_hh
00002 #define mu2e_artdaq_core_Overlays_mu2eFragmentReader_hh
00003
00005
00006
00007
00008
00009
00011
00012 #include "artdaq-core/Data/Fragment.hh"
00013 #include "artdaq-core/Data/Fragments.hh"
00014 #include "mu2e-artdaq-core/Overlays/mu2eFragment.hh"
00015
00016 #include <bitset>
00017
00018 #include <iostream>
00019
00020 namespace mu2e {
00021 class mu2eFragmentReader;
00022 }
00023
00024 class mu2e::mu2eFragmentReader : public mu2e::mu2eFragment {
00025 public:
00026 typedef uint16_t adc_t;
00027
00028 mu2eFragmentReader(artdaq::Fragment const &f) : mu2eFragment(f){};
00029
00030 size_t blockIndexBytes(size_t offset) const;
00031
00032 size_t blockSizeBytes() const;
00033 size_t blockSizeBytes(size_t offset) const;
00034 size_t blockEndBytes(size_t offset) const;
00035 Header::data_t const *dataAtBytes(size_t offset) const;
00036 Header::data_t const *dataAtBlockIndex(size_t offset) const;
00037
00038 void printPacketAtByte(size_t offset) const;
00039
00040
00041 adc_t DBH_ByteCount(adc_t const *pos);
00042 bool DBH_Valid(adc_t const *pos);
00043 adc_t DBH_ROCID(adc_t const *pos);
00044 adc_t DBH_PacketType(adc_t const *pos);
00045 adc_t DBH_PacketCount(adc_t const *pos);
00046 uint64_t DBH_Timestamp(adc_t const *pos);
00047 adc_t DBH_TimestampLow(adc_t const *pos);
00048 adc_t DBH_TimestampMedium(adc_t const *pos);
00049 adc_t DBH_TimestampHigh(adc_t const *pos);
00050 adc_t DBH_Status(adc_t const *pos);
00051 adc_t DBH_FormatVersion(adc_t const *pos);
00052 adc_t DBH_EVBMode(adc_t const *pos);
00053 adc_t DBH_SubsystemID(adc_t const *pos);
00054 adc_t DBH_DTCID(adc_t const *pos);
00055
00056
00057 adc_t DBT_StrawIndex(adc_t const *pos);
00058 uint32_t DBT_TDC0(adc_t const *pos);
00059 uint32_t DBT_TDC1(adc_t const *pos);
00060 uint32_t DBT_TOT0(adc_t const *pos);
00061 uint32_t DBT_TOT1(adc_t const *pos);
00062 std::array<adc_t, 15> DBT_Waveform(adc_t const *pos);
00063 adc_t DBT_Flags(adc_t const *pos);
00064
00065
00066 adc_t DBC_CrystalID(adc_t const *pos);
00067 adc_t DBC_apdID(adc_t const *pos);
00068 adc_t DBC_Time(adc_t const *pos);
00069 adc_t DBC_NumSamples(adc_t const *pos);
00070 adc_t DBC_PeakSampleIdx(adc_t const *pos);
00071 std::vector<adc_t> DBC_Waveform(adc_t const *pos);
00072
00073 protected:
00074
00075
00076
00077
00078
00079
00080
00081 std::bitset<128> bitArray(adc_t const *beginning) const;
00082
00083
00084
00085 void fillBitArray(std::bitset<128> &theArray, adc_t const *beginning);
00086
00087 void printBitArray(std::bitset<128> theArray);
00088
00089
00090
00091
00092 adc_t convertFromBinary(std::bitset<128> theArray, int minIdx, int maxIdx) const;
00093 };
00094
00095
00096 inline size_t mu2e::mu2eFragmentReader::blockIndexBytes(size_t offset) const {
00097 if (hdr_block_count() == 0) {
00098 return 0;
00099 } else if (offset >= hdr_block_count()) {
00100 return -1;
00101 } else if (offset == 0) {
00102 return 0;
00103 }
00104 return header_()->index[offset - 1];
00105 }
00106
00107
00108 inline size_t mu2e::mu2eFragmentReader::blockSizeBytes() const { return mu2eFragment::dataEndBytes(); }
00109
00110
00111 inline size_t mu2e::mu2eFragmentReader::blockSizeBytes(size_t offset) const {
00112 if (hdr_block_count() == 0) {
00113 return 0;
00114 } else if (offset > hdr_block_count() - 1) {
00115 return 0;
00116 } else if (offset == 0) {
00117 return header_()->index[offset];
00118 }
00119 return header_()->index[offset] - header_()->index[offset - 1];
00120 }
00121
00122
00123 inline size_t mu2e::mu2eFragmentReader::blockEndBytes(size_t offset) const {
00124 if (hdr_block_count() == 0) {
00125 return 0;
00126 } else if (offset > hdr_block_count() - 1) {
00127 return 0;
00128 }
00129 return header_()->index[offset];
00130 }
00131
00132
00133 inline mu2e::mu2eFragmentReader::Header::data_t const *mu2e::mu2eFragmentReader::dataAtBytes(size_t offset) const {
00134 return dataBegin() + (offset / sizeof(Header::data_t));
00135 }
00136
00137
00138 inline mu2e::mu2eFragmentReader::Header::data_t const *mu2e::mu2eFragmentReader::dataAtBlockIndex(size_t offset) const {
00139 return dataAtBytes(blockIndexBytes(offset));
00140 }
00141
00142 void mu2e::mu2eFragmentReader::printPacketAtByte(size_t offset) const {
00143 std::bitset<128> theArray = bitArray(reinterpret_cast<mu2e::mu2eFragmentReader::adc_t const *>(dataAtBytes(offset)));
00144 std::cout << "\t\t"
00145 << "Packet Bits (128): ";
00146 for (int i = 0; i < 128; i++) {
00147 std::cout << theArray[i];
00148 if (i != 0 && i < 128 - 1 && (i + 1) % 8 == 0) {
00149 std::cout << " ";
00150 }
00151 }
00152 std::cout << std::endl;
00153 return;
00154 }
00155
00156 std::bitset<128> mu2e::mu2eFragmentReader::bitArray(mu2e::mu2eFragmentReader::adc_t const *beginning) const {
00157
00158 std::bitset<128> theArray;
00159 for (int bitIdx = 127, adcIdx = 0; adcIdx < 8; adcIdx++) {
00160 for (int offset = 0; offset < 16; offset++) {
00161 if (((*((adc_t const *)(beginning + adcIdx))) & (1 << offset)) != 0) {
00162 theArray.set(bitIdx);
00163 } else {
00164 theArray.reset(bitIdx);
00165 }
00166 bitIdx--;
00167 }
00168 }
00169 return theArray;
00170 }
00171
00172 void mu2e::mu2eFragmentReader::fillBitArray(std::bitset<128> &theArray,
00173 mu2e::mu2eFragmentReader::adc_t const *beginning) {
00174
00175 for (int bitIdx = 127, adcIdx = 0; adcIdx < 8; adcIdx++) {
00176 for (int offset = 0; offset < 16; offset++) {
00177 if (((*((adc_t const *)(beginning + adcIdx))) & (1 << offset)) != 0) {
00178 theArray.set(bitIdx);
00179 } else {
00180 theArray.reset(bitIdx);
00181 }
00182 bitIdx--;
00183 }
00184 }
00185 }
00186
00187 void mu2e::mu2eFragmentReader::printBitArray(std::bitset<128> theArray) {
00188
00189 std::cout << "\t\t"
00190 << "Packet Bits (128): ";
00191 for (int i = 0; i < 128; i++) {
00192 std::cout << theArray[i];
00193 }
00194 std::cout << std::endl;
00195 }
00196
00197 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::convertFromBinary(std::bitset<128> theArray, int minIdx,
00198 int maxIdx) const {
00199 std::bitset<16> retVal;
00200 for (int i = minIdx + 1; i <= maxIdx; i++) {
00201 retVal.set(maxIdx - i, theArray[i]);
00202 }
00203 return retVal.to_ulong();
00204 }
00205
00207
00209
00210 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_ByteCount(adc_t const *pos) { return *(pos + 0); }
00211
00212 bool mu2e::mu2eFragmentReader::DBH_Valid(adc_t const *pos) { return (*(pos + 1) >> 15) & 0x0001; }
00213
00214 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_ROCID(adc_t const *pos) {
00215 return *(pos + 1) & 0x000F;
00216 }
00217
00218 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_PacketType(adc_t const *pos) {
00219 return (*(pos + 1) >> 4) & 0x000F;
00220 }
00221
00222 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_PacketCount(adc_t const *pos) {
00223 return *(pos + 2) & 0x07FF;
00224 }
00225
00226 uint64_t mu2e::mu2eFragmentReader::DBH_Timestamp(adc_t const *pos) {
00227 return uint64_t(*(pos + 3)) + (uint64_t(*(pos + 4)) << 16) + (uint64_t(*(pos + 5)) << 32);
00228 }
00229
00230 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_TimestampLow(adc_t const *pos) { return *(pos + 3); }
00231
00232 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_TimestampMedium(adc_t const *pos) { return *(pos + 4); }
00233
00234 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_TimestampHigh(adc_t const *pos) { return *(pos + 5); }
00235
00236 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_Status(adc_t const *pos) {
00237 return *(pos + 6) & 0x00FF;
00238 }
00239
00240 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_FormatVersion(adc_t const *pos) {
00241 return *(pos + 6) >> 8;
00242 }
00243
00244 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_EVBMode(adc_t const *pos) { return *(pos + 7) >> 8; }
00245
00246 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_SubsystemID(adc_t const *pos) {
00247 return (*(pos + 7) >> 6) & 0x0003;
00248 }
00249
00250 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBH_DTCID(adc_t const *pos) {
00251 return *(pos + 7) & 0x003F;
00252 }
00253
00255
00257 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBT_StrawIndex(adc_t const *pos) { return *(pos + 8 + 0); }
00258
00259 uint32_t mu2e::mu2eFragmentReader::DBT_TDC0(adc_t const *pos) { return (uint32_t(*(pos + 8 + 1)) & 0xFFFF); }
00260
00261 uint32_t mu2e::mu2eFragmentReader::DBT_TDC1(adc_t const *pos) { return (uint32_t(*(pos + 8 + 2)) & 0xFFFF); }
00262
00263 uint32_t mu2e::mu2eFragmentReader::DBT_TOT0(adc_t const *pos) { return (uint32_t(*(pos + 8 + 3)) & 0x00FF); }
00264
00265 uint32_t mu2e::mu2eFragmentReader::DBT_TOT1(adc_t const *pos) { return ((uint32_t(*(pos + 8 + 3)) >> 8) & 0x00FF); }
00266
00267 std::array<mu2e::mu2eFragmentReader::adc_t, 15> mu2e::mu2eFragmentReader::DBT_Waveform(adc_t const *pos) {
00268 std::array<adc_t, 15> waveform;
00269
00270
00271
00272
00273 for (size_t i = 0; i < 4; i += 1) {
00274 waveform[0 + i * 4] = *(pos + 8 + 4 + i * 3) & 0x0FFF;
00275 waveform[1 + i * 4] = ((*(pos + 8 + 4 + i * 3 + 1) & 0x00FF) << 4) | (*(pos + 8 + 4 + i * 3) >> 12);
00276 waveform[2 + i * 4] = ((*(pos + 8 + 4 + i * 3 + 2) & 0x000F) << 8) | (*(pos + 8 + 4 + i * 3 + 1) >> 8);
00277 if (i < 3) {
00278 waveform[3 + i * 4] = (*(pos + 8 + 4 + i * 3 + 2) >> 4);
00279 }
00280 }
00281
00282 return waveform;
00283 }
00284
00285 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBT_Flags(adc_t const *pos) { return (*(pos + 8 + 15) >> 8); }
00286
00288
00290
00291 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBC_CrystalID(adc_t const *pos) {
00292 return *(pos + 8 + 0) & 0x0FFF;
00293 }
00294
00295 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBC_apdID(adc_t const *pos) { return *(pos + 8 + 0) >> 12; }
00296
00297 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBC_Time(adc_t const *pos) { return *(pos + 8 + 1); }
00298
00299 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBC_NumSamples(adc_t const *pos) {
00300 return *(pos + 8 + 2) & 0x00FF;
00301 }
00302
00303 mu2e::mu2eFragmentReader::adc_t mu2e::mu2eFragmentReader::DBC_PeakSampleIdx(adc_t const *pos) {
00304 return *(pos + 8 + 2) >> 8;
00305 }
00306
00307 std::vector<mu2e::mu2eFragmentReader::adc_t> mu2e::mu2eFragmentReader::DBC_Waveform(adc_t const *pos) {
00308 std::vector<int> waveform(DBC_NumSamples(pos));
00309 for (size_t i = 0; i < waveform.size(); i++) {
00310 waveform[i] = *(pos + 8 + 3 + i);
00311 }
00312 return waveform;
00313 }
00314
00315 #endif