read_packed.cxx
Go to the documentation of this file.
1 #include <fstream>
2 #include <iostream>
3 #include <vector>
4 #include <cstdio>
5 
6 int main(int argc, char** argv)
7 {
8  if(argc!=2){
9  std::cerr << "Usage: read_packed filename" << std::endl;
10  exit(1);
11  }
12  std::ifstream fin(argv[1], std::ios::in | std::ios::binary);
13  std::vector<std::vector<unsigned short>> samples(128);
14  uint32_t word=0;
15  size_t nwords=0;
16  uint32_t words[3]; // The shuffle pattern repeats every three words
17  size_t index=0; // The index within words for the next word
18  do{
19 
20  fin.read((char*)&word, sizeof(uint32_t));
21  ++nwords;
22  if(word==0xdeadbeef){
23  // We got start-of-frame. Next word is the tdc
24  fin.read((char*)&word, sizeof(uint32_t));
25  std::cout << "Got new frame with TDC " << word << std::endl;
26  if(index!=0){
27  std::cout << "Error: index at new frame is " << index << ", not 0" << std::endl;
28  }
29  ++nwords;
30  continue;
31  }
32  words[index]=word;
33  if(index==2){
34  // We've got our third word. Do the unpacking
35  unsigned short thisSample[8];
36  thisSample[0]=words[0] & 0xfff;
37  thisSample[1]=(words[0] >> 12) & 0xfff;
38  thisSample[2]=((words[0] >> 24) & 0xff) | ((words[1] << 8) & 0xf00);
39  thisSample[3]=(words[1] >> 4) & 0xfff;
40  thisSample[4]=(words[1] >> 16) & 0xfff;
41  thisSample[5]=((words[1] >> 28) & 0xf) | ((words[2] << 4) & 0xff0);
42  thisSample[6]=(words[2] >> 8) & 0xfff;
43  thisSample[7]=(words[2] >> 20) & 0xfff;
44 
45  printf("% 6d % 6d % 6d % 6d % 6d % 6d % 6d % 6d\n",
46  thisSample[0], thisSample[1],
47  thisSample[2], thisSample[3],
48  thisSample[4], thisSample[5],
49  thisSample[6], thisSample[7]);
50 
51  // Reset the index for the next set
52  index=0;
53  }
54  else{
55  ++index;
56  }
57  }
58  while(word!=0xffffffff && nwords<1000);
59  std::cout << "Exiting after reading " << nwords << " words" << std::endl;
60 }
union ptb::content::word::word word
QTextStream & endl(QTextStream &s)
int main(int argc, char **argv)
Definition: read_packed.cxx:6