mu2e_artdaq_core  v1_02_09
 All Classes Functions
DetectorFragment.cc
1 #include "mu2e-artdaq-core/Overlays/DetectorFragment.hh"
2 
3 #if 0
4 namespace {
5  unsigned int pop_count (unsigned int n) {
6  unsigned int c;
7  for (c = 0; n; c++) n &= n - 1;
8  return c;
9  }
10 }
11 #endif
12 
13 void mu2e::DetectorFragment::checkADCData(int daq_adc_bits) const {
14  mu2e::DetectorFragment::adc_t const * adcPtr(findBadADC(daq_adc_bits));
15  if (adcPtr != dataEnd()) {
16  throw cet::exception("IllegalADCVal")
17  << "Illegal value of ADC word #"
18  << (adcPtr - dataBegin())
19  << ": 0x"
20  << std::hex
21  << *adcPtr
22  << ".";
23  }
24 }
25 
26 std::ostream & mu2e::operator << (std::ostream & os, DetectorFragment const & f) {
27  os << "DetectorFragment event size: "
28  << f.hdr_event_size()
29  << ", run number: "
30  << f.hdr_run_number()
31  << "\n";
32 
33  return os;
34 }
35 
36 //std::bitset<128> mu2e::DetectorFragment::bitArray(adc_t const * beginning) {
37 std::bitset<128> mu2e::DetectorFragment::bitArray(mu2e::DetectorFragment::adc_t const *beginning) const {
38  // Return 128 bit bitset filled with bits starting at the indicated position in the fragment
39  std::bitset<128> theArray;
40  for(int bitIdx=127, adcIdx = 0; adcIdx<8; adcIdx++) {
41  for(int offset = 0; offset<16; offset++) {
42  if( ( (*((adc_t const *)(beginning+adcIdx))) & (1<<offset) ) != 0) {
43  theArray.set(bitIdx);
44  } else {
45  theArray.reset(bitIdx);
46  }
47  bitIdx--;
48  }
49  }
50  return theArray;
51 }
52 
53 void mu2e::DetectorFragment::fillBitArray(std::bitset<128> &theArray, adc_t const * beginning) {
54  // Fill bitset using the 128 bits starting at the indicated position in the fragment
55  for(int bitIdx=127, adcIdx = 0; adcIdx<8; adcIdx++) {
56  for(int offset = 0; offset<16; offset++) {
57  if( ( (*((adc_t const *)(beginning+adcIdx))) & (1<<offset) ) != 0) {
58  theArray.set(bitIdx);
59  } else {
60  theArray.reset(bitIdx);
61  }
62  bitIdx--;
63  }
64  }
65 }
66 
67 void mu2e::DetectorFragment::printBitArray(std::bitset<128> theArray) {
68  // Print all 128 bits in the packet
69  std::cout << "\t\t" << "Packet Bits (128): ";
70  for(int i=0; i<128; i++) {
71  std::cout << theArray[i];
72  }
73  std::cout << std::endl;
74 }
75 
76 mu2e::DetectorFragment::adc_t mu2e::DetectorFragment::convertFromBinary(std::bitset<128> theArray, int minIdx, int maxIdx) const {
77  std::bitset<16> retVal;
78  for(int i=minIdx+1; i<=maxIdx; i++) {
79  retVal.set(maxIdx-i,theArray[i]);
80  }
81  return retVal.to_ulong();
82 }
83 
84 mu2e::DetectorFragment::adc_t mu2e::DetectorFragment::byteCount() {
85  return mu2e::DetectorFragment::convertFromBinary(bitArray(dataBlockBegin()),127-16,127-0);
86 }
87 
88 mu2e::DetectorFragment::adc_t mu2e::DetectorFragment::rocID() {
89  return mu2e::DetectorFragment::convertFromBinary(bitArray(dataBlockBegin()),127-20,127-16);
90 }
91 
92 mu2e::DetectorFragment::adc_t mu2e::DetectorFragment::packetType() {
93  return mu2e::DetectorFragment::convertFromBinary(bitArray(dataBlockBegin()),127-24,127-20);
94 }
95 
96 mu2e::DetectorFragment::adc_t mu2e::DetectorFragment::valid() {
97  return mu2e::DetectorFragment::convertFromBinary(bitArray(dataBlockBegin()),127-32,127-31);
98 }
99 
100 mu2e::DetectorFragment::adc_t mu2e::DetectorFragment::packetCount() {
101  return mu2e::DetectorFragment::convertFromBinary(bitArray(dataBlockBegin()),127-43,127-32);
102 }
103 
104 mu2e::DetectorFragment::adc_t mu2e::DetectorFragment::status() {
105  return mu2e::DetectorFragment::convertFromBinary(bitArray(dataBlockBegin()),127-104,127-96);
106 }
107 
108 std::vector<mu2e::DetectorFragment::adc_t> mu2e::DetectorFragment::timestampVector() {
109 
110  std::vector<adc_t> theVector;
111  std::bitset<128> bitarray;
112  fillBitArray(bitarray,dataBlockBegin());
113 
114  for(int i=0; i<6; i++) {
115  theVector.push_back(convertFromBinary(bitarray,127-8*(6+i + 1),127-8*(6+i)));
116  }
117 
118  return theVector;
119 }
120 
121 std::vector<mu2e::DetectorFragment::adc_t> mu2e::DetectorFragment::dataVector() {
122 
123  std::vector<adc_t> theVector;
124  std::bitset<128> bitarray;
125  fillBitArray(bitarray,dataBlockBegin());
126 
127  for(int i=0; i<3; i++) {
128  theVector.push_back(convertFromBinary(bitarray,127-8*(13+i + 1),127-8*(13+i)));
129  }
130 
131  return theVector;
132 }
133 
134 void mu2e::DetectorFragment::printDTCHeader() {
135  // std::cout << "\t\t" << "Binary Representation: ";
136  // printBitArray(bitArray(dataBlockBegin()));
137  std::cout << "\t\t" << "ROC ID: " << (int)rocID() << std::endl;
138  std::cout << "\t\t" << "Byte Count: " << (int)byteCount() << std::endl;
139  std::cout << "\t\t" << "Packet Type: " << (int)packetType() << std::endl;
140  std::cout << "\t\t" << "Valid: " << (int)valid() << std::endl;
141  std::cout << "\t\t" << "Packet Count: " << (int)packetCount() << std::endl;
142  std::cout << "\t\t" << "Status: " << (int)status() << std::endl;
143  std::cout << "\t\t" << "Timestamp: {[";
144 
145  std::vector<mu2e::DetectorFragment::adc_t> tsVec = timestampVector();
146  for(size_t i=0; i<tsVec.size(); i++) {
147  std::cout << (int)tsVec[i];
148  if(i<tsVec.size()-1) {
149  std::cout << ",";
150  }
151  }
152  std::cout << "]}" << std::endl;
153 
154  std::cout << "\t\t" << "Data: {[";
155  std::vector<mu2e::DetectorFragment::adc_t> dVec = dataVector();
156  for(size_t i=0; i<dVec.size(); i++) {
157  std::cout << (int)dVec[i];
158  if(i<dVec.size()-1) {
159  std::cout << ",";
160  }
161  }
162  std::cout << "]}" << std::endl;
163 
164 }
165 
166 
167 void mu2e::DetectorFragment::printAll() {
168  printDTCHeader();
169 }