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