Line data Source code
1 : #include <cstdint>
2 : #include <math.h>
3 : #include <bitset>
4 : #include <fstream>
5 : #include <iostream>
6 : #include <string>
7 : #include <vector>
8 :
9 0 : int main()
10 : {
11 0 : auto verbose = true;
12 :
13 0 : std::string packetType = "TRK";
14 : // std::string packetType = "CAL";
15 :
16 : // Number of tracker adc samples
17 : // size_t numADCSamples = 8;
18 0 : size_t numADCSamples = 12;
19 :
20 0 : std::string inputFile = "TRK_packets.bin";
21 0 : if (packetType == "CAL")
22 : {
23 0 : inputFile = "CAL_packets.bin";
24 : }
25 :
26 0 : std::ifstream binFile;
27 0 : binFile.open(inputFile, std::ios::in | std::ios::binary | std::ios::ate);
28 :
29 : typedef uint16_t adc_t;
30 :
31 0 : std::vector<adc_t> masterVector;
32 0 : std::vector<std::vector<std::vector<adc_t>>> timeStampVector;
33 0 : if (binFile.is_open())
34 : {
35 0 : auto size = binFile.tellg();
36 0 : auto memblock = new char[static_cast<unsigned>(size)];
37 0 : binFile.seekg(0, std::ios::beg);
38 0 : binFile.read(memblock, size);
39 0 : binFile.close();
40 :
41 : // Read input file into master adc_t vector
42 0 : for (auto curPos = reinterpret_cast<adc_t *>(memblock);
43 0 : curPos != reinterpret_cast<adc_t *>(memblock + sizeof(char) * size); curPos++)
44 : {
45 0 : masterVector.push_back(static_cast<adc_t>(*curPos));
46 : }
47 0 : std::cout << "Number of adc_t entries in input dataset: " << masterVector.size() << std::endl;
48 :
49 0 : auto exitLoop = false;
50 0 : size_t curPos = 0;
51 :
52 0 : std::vector<std::vector<adc_t>> dataBlockVector; // Vector of adc_t vectors containing DataBlocks
53 0 : while (!exitLoop && curPos < masterVector.size())
54 : {
55 0 : std::bitset<64> byteCount = 0;
56 0 : std::bitset<16> byteCount0 = masterVector[curPos + 0];
57 0 : std::bitset<16> byteCount1 = masterVector[curPos + 1];
58 0 : std::bitset<16> byteCount2 = masterVector[curPos + 2];
59 0 : std::bitset<16> byteCount3 = masterVector[curPos + 3];
60 0 : for (auto i = 0; i < 16; i++)
61 : {
62 0 : byteCount[i + 16 * 0] = byteCount0[i];
63 0 : byteCount[i + 16 * 1] = byteCount1[i];
64 0 : byteCount[i + 16 * 2] = byteCount2[i];
65 0 : byteCount[i + 16 * 3] = byteCount3[i];
66 : }
67 0 : size_t theCount = byteCount.to_ulong();
68 :
69 0 : if (verbose)
70 : {
71 0 : std::cout << "Number of bytes in DMABlock: " << theCount << std::endl;
72 : }
73 :
74 0 : auto blockStartIdx = curPos;
75 0 : auto blockEndIdx = curPos + theCount / 2;
76 0 : size_t posInBlock = 4;
77 :
78 0 : std::vector<adc_t> curDataBlock;
79 0 : while (posInBlock < blockEndIdx - blockStartIdx)
80 : {
81 0 : size_t numDataPackets = masterVector[blockStartIdx + posInBlock + 2];
82 0 : for (size_t i = 0; i < 8 + numDataPackets * 8; i++)
83 : {
84 0 : curDataBlock.push_back(masterVector[blockStartIdx + posInBlock + i]);
85 : }
86 0 : dataBlockVector.push_back(curDataBlock);
87 0 : curDataBlock.clear();
88 0 : posInBlock += 8 + numDataPackets * 8;
89 : }
90 : // Skip over the 4*16 bit DMABlock header and skip theCount/2 adc_t values to
91 : // get to the next DMABlock
92 0 : curPos += theCount / 2;
93 0 : }
94 :
95 0 : std::cout << "DataBlock vector size: " << dataBlockVector.size() << std::endl;
96 0 : if (verbose)
97 : {
98 0 : for (size_t eventNum = 0; eventNum < dataBlockVector.size(); eventNum++)
99 : {
100 0 : auto packetVector = dataBlockVector[eventNum];
101 0 : std::cout << "================================================" << std::endl;
102 0 : std::cout << "Hit num: " << eventNum << std::endl;
103 :
104 : // Print out header info
105 0 : std::bitset<48> timestamp;
106 0 : std::bitset<16> timestamp0 = packetVector[3];
107 0 : std::bitset<16> timestamp1 = packetVector[4];
108 0 : std::bitset<16> timestamp2 = packetVector[5];
109 0 : for (auto i = 0; i < 16; i++)
110 : {
111 0 : timestamp[i + 16 * 0] = timestamp0[i];
112 0 : timestamp[i + 16 * 1] = timestamp1[i];
113 0 : timestamp[i + 16 * 2] = timestamp2[i];
114 : }
115 0 : size_t ts = timestamp.to_ulong();
116 0 : std::cout << "\tTimestamp: " << ts << std::endl;
117 :
118 0 : std::bitset<16> roclink = packetVector[1];
119 0 : std::bitset<4> link;
120 0 : std::bitset<4> roc;
121 0 : for (size_t i = 0; i < 4; i++)
122 : {
123 0 : roc[i] = roclink[i];
124 0 : link[i] = roclink[i + 8];
125 : }
126 0 : size_t ROC_ID = roc.to_ulong();
127 0 : size_t Link_ID = link.to_ulong();
128 0 : std::cout << "\tROC ID: " << ROC_ID << std::endl;
129 0 : std::cout << "\tRing ID: " << Link_ID << std::endl;
130 :
131 0 : std::cout << "\tNumber of non-header data packets: " << packetVector.size() / 8 - 1 << std::endl;
132 :
133 : // Print out payload packet info
134 :
135 0 : if (packetType == "TRK")
136 : {
137 0 : std::cout << "\tNumber of ADC samples: " << numADCSamples << std::endl;
138 0 : std::cout << "\tscaledNoisyVector: {";
139 0 : for (size_t i = 8 + 4; i < 8 + 4 + numADCSamples; i++)
140 : {
141 0 : double curVal = packetVector[i];
142 0 : if (i > 8 + 4)
143 : {
144 0 : std::cout << ",";
145 : }
146 0 : std::cout << curVal;
147 : }
148 0 : std::cout << "}" << std::endl;
149 : }
150 0 : else if (packetType == "CAL")
151 : {
152 0 : if (packetVector.size() / 8 - 1 > 0)
153 : { // At least 1 data packet following the header packet
154 0 : std::bitset<16> IDNum = packetVector[8];
155 0 : std::bitset<12> crystalID;
156 0 : for (auto i = 11; i >= 0; i--)
157 : {
158 0 : crystalID[i] = IDNum[i];
159 : }
160 0 : std::cout << "\tCrystalID: " << crystalID.to_ulong() << std::endl;
161 :
162 0 : adc_t apdID = adc_t(packetVector[8]) >> 12;
163 0 : std::cout << "\tapdID: " << apdID << std::endl;
164 :
165 0 : auto numSamples = packetVector[8 + 2];
166 0 : std::cout << "\tNumber of waveform samples: " << numSamples << std::endl;
167 0 : std::cout << "\tscaledNoisyVector: {";
168 0 : for (auto i = 8U + 3U; i < 8U + 3U + packetVector[8 + 2]; i++)
169 : {
170 0 : double curVal = packetVector[i];
171 0 : if (i > 8 + 3)
172 : {
173 0 : std::cout << ",";
174 : }
175 0 : std::cout << curVal;
176 : }
177 0 : std::cout << "}" << std::endl;
178 : }
179 : }
180 :
181 : // Print out raw datablock contents, including the header packet
182 0 : std::bitset<16> curEntry;
183 0 : std::cout << std::endl;
184 0 : std::cout << "\tRaw Packets:" << std::endl;
185 0 : for (size_t i = 0; i < packetVector.size(); i++)
186 : {
187 0 : curEntry = packetVector[i];
188 0 : std::cout << "\t\t" << curEntry.to_string() << " " << curEntry.to_ulong() << std::endl;
189 0 : if (i > 0 && (i + 1) % 8 == 0)
190 : {
191 0 : std::cout << std::endl;
192 : }
193 : }
194 0 : }
195 : }
196 :
197 0 : delete[] memblock;
198 0 : }
199 : else
200 : {
201 0 : std::cout << "ERROR: Could not open input file.";
202 0 : return 1;
203 : }
204 :
205 0 : return 0;
206 0 : }
|