Skip to content

Commit 7c27096

Browse files
author
Milo Vermeulen
committed
Update timestamp and split frame into parts.
The Frame timestamp now uses chrono’s high_resolution_clock and increments by 500 ns at each Frame created. Frames now consist of a header struct and four COLDATA structs. These can be accessed and changed indirectly or directly. The idle words were removed from the Frame class so that it is now 464 bytes long instead of 480.
1 parent 670c34f commit 7c27096

106 files changed

Lines changed: 131 additions & 52 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.CMakeLists.txt.swp

-12 KB
Binary file not shown.

FrameGen.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace framegen {
2121
return false;
2222
}
2323
ifile.seekg(0,std::ios_base::end);
24-
if(ifile.tellg()<(frameNum+1)*480) {
25-
std::cout << "Error (Frame::load): file " << filename << " contains fewer than " << frameNum << " frames." << std::endl;
24+
if(ifile.tellg()<(frameNum+1)*464) {
25+
std::cout << "Error (Frame::load): file " << filename << " contains fewer than " << frameNum+1 << " frames." << std::endl << "Be sure to start counting at 0." << std::endl;
2626
return false;
2727
}
2828

@@ -33,9 +33,9 @@ namespace framegen {
3333
}
3434

3535
void Frame::load(std::ifstream& strm, int frameNum) {
36-
strm.seekg(frameNum*480);
37-
for(int i=0; i<120; i++)
38-
_binaryData[i] = (uint32_t)strm.get() | strm.get()<<8 | strm.get()<<16 | strm.get()<<24;
36+
strm.seekg(frameNum*464);
37+
for(int i=0; i<116; i++)
38+
*_binaryData[i] = (uint32_t)strm.get() | strm.get()<<8 | strm.get()<<16 | strm.get()<<24;
3939
}
4040

4141
bool Frame::print(const std::string& filename) {
@@ -55,8 +55,9 @@ namespace framegen {
5555
bool Frame::print(std::ofstream& strm) {
5656
if(!strm)
5757
return false;
58-
for(int i=0; i<120; i++)
59-
strm << (char)_binaryData[i] << (char)(_binaryData[i]>>8) << (char)(_binaryData[i]>>16) << (char)(_binaryData[i]>>24);
58+
for(int i=0; i<116; i++)
59+
strm << (char)(*_binaryData[i]) << (char)(*_binaryData[i]>>8) << (char)(*_binaryData[i]>>16) << (char)(*_binaryData[i]>>24);
60+
// strm << std::hex << std::setfill('0') << "0x" << std::setw(8) << _binaryData[i] << "," << std::endl;
6061
return true;
6162
}
6263

@@ -69,10 +70,10 @@ namespace framegen {
6970
uint8_t result = init;
7071
for(unsigned int i=4+blockNum*28; i<4+blockNum*28+27; i++) {
7172
// Divide 32-bit into 8-bit and XOR.
72-
result ^= (_binaryData[i]<<24)>>24;
73-
result ^= (_binaryData[i]<<16)>>24;
74-
result ^= (_binaryData[i]<<8)>>24;
75-
result ^= _binaryData[i]>>24;
73+
result ^= (*_binaryData[i]<<24)>>24;
74+
result ^= (*_binaryData[i]<<16)>>24;
75+
result ^= (*_binaryData[i]<<8)>>24;
76+
result ^= *_binaryData[i]>>24;
7677
}
7778
return result;
7879
}
@@ -86,23 +87,23 @@ namespace framegen {
8687
uint8_t result = init;
8788
for(unsigned int i=4+blockNum*28; i<4+blockNum*28+27; i++) {
8889
// Divide 32-bit into 8-bit and add.
89-
result += (_binaryData[i]<<24)>>24;
90-
result += (_binaryData[i]<<16)>>24;
91-
result += (_binaryData[i]<<8)>>24;
92-
result += _binaryData[i]>>24;
90+
result += (*_binaryData[i]<<24)>>24;
91+
result += (*_binaryData[i]<<16)>>24;
92+
result += (*_binaryData[i]<<8)>>24;
93+
result += *_binaryData[i]>>24;
9394
}
9495
return -result;
9596
}
9697

9798
// Cyclic redundancy check (32-bit).
9899
uint32_t Frame::CRC32(uint32_t padding, uint64_t CRC32_Polynomial) {
99-
uint64_t shiftReg = (uint64_t)_binaryData[0]<<1 | ((_binaryData[1]>>31)&1); // Shifting register.
100+
uint64_t shiftReg = (uint64_t)(*_binaryData[0])<<1 | ((*_binaryData[1]>>31)&1); // Shifting register.
100101
// Shift through the data.
101102
for(int i=0; i<114*32; i++) { // The register shifts through 115 32-bit words and is 33 bits long.
102103
// Perform XOR on the shifting register if the leading bit is 1 and shift.
103104
if(shiftReg & (1UL<<33))
104105
shiftReg ^= CRC32_Polynomial;
105-
shiftReg = shiftReg<<1 | ((_binaryData[i/32+1]>>(31-(i%32)))&1);
106+
shiftReg = shiftReg<<1 | ((*_binaryData[i/32+1]>>(31-(i%32)))&1);
106107
}
107108
// Shift through padding.
108109
for(int i=0; i<32; i++) {
@@ -124,9 +125,14 @@ namespace framegen {
124125
void FrameGen::fill(std::ofstream& ofile) {
125126
// Header.
126127
_frame.setStreamID(rand()%8);
127-
_frame.setResetCount(rand()%(1<<24));
128+
_frame.setResetCount(numberOfFrames>>16);
128129

129-
_frame.setWIBTimestamp(time(nullptr));
130+
{ // Small timestamp scope.
131+
using namespace std::chrono;
132+
static nanoseconds time = duration_cast<nanoseconds>(high_resolution_clock::now().time_since_epoch());
133+
_frame.setWIBTimestamp(duration_cast<nanoseconds>(time).count());
134+
time += nanoseconds(500);
135+
}
130136

131137
_frame.setCrateNo(rand()%32);
132138
_frame.setSlotNo(rand()%8);
@@ -159,6 +165,9 @@ namespace framegen {
159165

160166
// Print the generated data to frame.
161167
_frame.print(ofile);
168+
169+
// Increment the number of frames.
170+
numberOfFrames++;
162171
}
163172

164173
// Main generator function: builds frames and calls the fill function.
@@ -306,11 +315,11 @@ namespace framegen {
306315

307316
// Get number of frames and check whether this is an integer.
308317
ifile.seekg(0,ifile.end);
309-
if(ifile.tellg()%480) {
318+
if(ifile.tellg()%464) {
310319
std::cout << "Error: file " << filename << " contains unreadable frames." << std::endl;
311320
return false;
312321
}
313-
int numberOfFrames = ifile.tellg()/480;
322+
int numberOfFrames = ifile.tellg()/464;
314323
ifile.seekg(0,ifile.beg);
315324

316325
Frame frame;

FrameGen.hpp

Lines changed: 95 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,35 @@
1010
#define FRAMEGEN_HPP_
1111

1212
#include <iostream>
13+
#include <iomanip>
1314
#include <fstream>
1415
#include <string>
1516
#include <cstring>
1617
#include <ctime>
1718
#include <random>
19+
#include <chrono>
1820

1921
#include "zlib.h"
2022

2123
#define CRC32_POLYNOMIAL 6186310019 // Polynomial to compute the CRC32 checksum (random 33-bit). Needs to be >=2^32.
2224

2325
namespace framegen {
2426

25-
class Frame {
26-
// Frame structure from Eric Hazen's preliminary WIB->FELIX frame format:
27-
// http://docs.dunescience.org/cgi-bin/RetrieveFile?docid=1701&filename=ProtoDUNE_to_FELIX.pdf&version=1
28-
private:
29-
uint32_t _binaryData[120]; // Container for all data of a single frame.
30-
// uint64_t _CRC32_Polynomial = 6186310019; // Polynomial to compute the CRC32 checksum (random 33-bit). Needs to be >=2^32.
31-
public:
27+
static uint64_t numberOfFrames = 0;
28+
29+
struct WIB_header {
3230
// Header/footer accessors.
33-
const uint8_t getStreamID() { return _binaryData[0]; } // Data type (trigger, calibration, unbiased, etc.) to route data.
34-
const uint32_t getResetCount() { return _binaryData[0]>>8; } // Increments by one every 2^16 frames.
35-
const uint32_t getWIBTimestamp() { return _binaryData[1]; } // Local timestamp.
36-
const uint8_t getCrateNo() { return _binaryData[2]&0x1F; } // Crate number.
37-
const uint8_t getSlotNo() { return (_binaryData[2]>>5)&0x7; } // Slot number.
38-
const uint8_t getFiberNo() { return (_binaryData[2]>>8)&0x7; } // Fiber number.
39-
const uint8_t getCapture() { return (_binaryData[2]>>16)&0xF; } // Error bit set if an error occurred during data capture.
40-
const uint8_t getASIC() { return (_binaryData[2]>>20)&0xF; } // Error bit set if an error was logged in ASIC data.
41-
const uint8_t getWIBErrors() { return _binaryData[2]>>16; } // Error bit set by the WIB.
42-
const uint32_t getCRC32() { return _binaryData[115]; } // Complete checksum of frame.
43-
// COLDATA blocks accessors.
44-
const uint8_t getChecksumA(int blockNum) { return _binaryData[3+28*blockNum]; } // Checksum of individual COLDATA blocks.
45-
const uint8_t getChecksumB(int blockNum) { return _binaryData[3+28*blockNum]>>8; } // Checksum of individual COLDATA blocks.
46-
const uint8_t getS1Err(int blockNum) { return (_binaryData[3+28*blockNum]>>16)&0xF; } // Indicate errors in capturing streams from COLDATA ASICS.
47-
const uint8_t getS2Err(int blockNum) { return (_binaryData[3+28*blockNum]>>20)&0xF; } // Indicate errors in capturing streams from COLDATA ASICS.
48-
const uint16_t getCOLDATA(int blockNum, int num) { return _binaryData[4+28*blockNum+num/2]>>(16*(1-num%2)); } // Raw data.
31+
uint32_t _binaryData[4];
32+
const uint8_t getStreamID() { return _binaryData[0]; } // Data type (trigger, calibration, unbiased, etc.) to route data.
33+
const uint32_t getResetCount() { return _binaryData[0]>>8; } // Increments by one every 2^16 frames.
34+
const uint32_t getWIBTimestamp() { return _binaryData[1]; } // Local timestamp.
35+
const uint8_t getCrateNo() { return _binaryData[2]&0x1F; } // Crate number.
36+
const uint8_t getSlotNo() { return (_binaryData[2]>>5)&0x7; } // Slot number.
37+
const uint8_t getFiberNo() { return (_binaryData[2]>>8)&0x7; } // Fiber number.
38+
const uint8_t getCapture() { return (_binaryData[2]>>16)&0xF; } // Error bit set if an error occurred during data capture.
39+
const uint8_t getASIC() { return (_binaryData[2]>>20)&0xF; } // Error bit set if an error was logged in ASIC data.
40+
const uint8_t getWIBErrors() { return _binaryData[2]>>16; } // Error bit set by the WIB.
41+
const uint32_t getCRC32() { return _binaryData[3]; } // Complete checksum of frame.
4942

5043
// Header/footer mutators.
5144
void setStreamID(uint8_t newStreamID) { _binaryData[0] = (_binaryData[0]&~(0xFF)) | newStreamID; }
@@ -57,13 +50,85 @@ namespace framegen {
5750
void setCapture(uint8_t newCapture) { _binaryData[2] = (_binaryData[2]&~(0xF<<16)) | (newCapture&0xF)<<16; }
5851
void setASIC(uint8_t newASIC) { _binaryData[2] = (_binaryData[2]&~(0xF<<20)) | (newASIC&0xF)<<20; }
5952
void setWIBErrors(uint8_t newWIBErrors) { _binaryData[2] = (_binaryData[2]&~(0xFF<<24)) | newWIBErrors<<24; }
60-
void setCRC32(uint32_t newCRC32) { _binaryData[115] = newCRC32; }
53+
void setCRC32(uint32_t newCRC32) { _binaryData[3] = newCRC32; }
54+
};
55+
56+
struct COLDATA_block {
57+
uint32_t _binaryData[28];
58+
// COLDATA blocks accessors.
59+
const uint8_t getChecksumA() { return _binaryData[0]; } // Checksum of individual COLDATA blocks.
60+
const uint8_t getChecksumB() { return _binaryData[0]>>8; } // Checksum of individual COLDATA blocks.
61+
const uint8_t getS1Err() { return (_binaryData[0]>>16)&0xF; } // Indicate errors in capturing streams from COLDATA ASICS.
62+
const uint8_t getS2Err() { return (_binaryData[0]>>20)&0xF; } // Indicate errors in capturing streams from COLDATA ASICS.
63+
const uint16_t getCOLDATA(int num) { return _binaryData[1+num/2]>>(16*(1-num%2)); } // Raw data.
64+
65+
// COLDATA blocks mutators.
66+
void setChecksumA(uint8_t newChecksumA) { _binaryData[0] = (_binaryData[0]&~(0xFF)) | newChecksumA; }
67+
void setChecksumB(uint8_t newChecksumB) { _binaryData[0] = (_binaryData[0]&~(0xFF<<8)) | newChecksumB<<8; }
68+
void setS1Err(uint8_t newS1Err) { _binaryData[0] = (_binaryData[0]&~(0xF<<16)) | (newS1Err&0xF)<<16; }
69+
void setS2Err(uint8_t newS2Err) { _binaryData[0] = (_binaryData[0]&~(0xF<<20)) | (newS2Err&0xF)<<20; }
70+
void setCOLDATA(int num, uint16_t newCOLDATA) { _binaryData[1+num/2] = (_binaryData[1+num/2]&~(0xFFFF<<(16*(num%2)))) | (uint32_t)newCOLDATA<<(16*(num%2)); }
71+
};
72+
73+
class Frame {
74+
// Frame structure from Eric Hazen's preliminary WIB->FELIX frame format:
75+
// http://docs.dunescience.org/cgi-bin/RetrieveFile?docid=1701&filename=ProtoDUNE_to_FELIX.pdf&version=1
76+
private:
77+
WIB_header head;
78+
COLDATA_block block[4];
79+
uint32_t* _binaryData[116]; // Pointers to all data of a single frame for easy access.
80+
public:
81+
// Frame constructor links the binary data from the header and block structs.
82+
Frame() {
83+
for(int i=0; i<3; i++)
84+
_binaryData[i] = &head._binaryData[i];
85+
_binaryData[115] = &head._binaryData[3];
86+
87+
for(int i=0; i<4*28; i++)
88+
_binaryData[3+(i/28)*28+i%28] = &block[i/28]._binaryData[i%28];
89+
}
90+
91+
// Header/footer accessors.
92+
const uint8_t getStreamID() { return head.getStreamID(); } // Data type (trigger, calibration, unbiased, etc.) to route data.
93+
const uint32_t getResetCount() { return head.getResetCount(); } // Increments by one every 2^16 frames.
94+
const uint32_t getWIBTimestamp() { return head.getWIBTimestamp(); } // Local timestamp.
95+
const uint8_t getCrateNo() { return head.getCrateNo(); } // Crate number.
96+
const uint8_t getSlotNo() { return head.getSlotNo(); } // Slot number.
97+
const uint8_t getFiberNo() { return head.getFiberNo(); } // Fiber number.
98+
const uint8_t getCapture() { return head.getCapture(); } // Error bit set if an error occurred during data capture.
99+
const uint8_t getASIC() { return head.getASIC(); } // Error bit set if an error was logged in ASIC data.
100+
const uint8_t getWIBErrors() { return head.getWIBErrors(); } // Error bit set by the WIB.
101+
const uint32_t getCRC32() { return head.getCRC32(); } // Complete checksum of frame.
102+
// COLDATA blocks accessors.
103+
const uint8_t getChecksumA(int blockNum) { return block[blockNum].getChecksumA(); } // Checksum of individual COLDATA blocks.
104+
const uint8_t getChecksumB(int blockNum) { return block[blockNum].getChecksumB(); } // Checksum of individual COLDATA blocks.
105+
const uint8_t getS1Err(int blockNum) { return block[blockNum].getS1Err(); } // Indicate errors in capturing streams from COLDATA ASICS.
106+
const uint8_t getS2Err(int blockNum) { return block[blockNum].getS2Err(); } // Indicate errors in capturing streams from COLDATA ASICS.
107+
const uint16_t getCOLDATA(int blockNum, int num) { return block[blockNum].getCOLDATA(num); } // Raw data.
108+
// Struct accessors.
109+
const WIB_header getWIBHeader() { return head; }
110+
const COLDATA_block getCOLDATABlock(int blockNum) { return block[blockNum]; }
111+
112+
// Header/footer mutators.
113+
void setStreamID(uint8_t newStreamID) { head.setStreamID(newStreamID); }
114+
void setResetCount(uint32_t newResetCount) { head.setResetCount(newResetCount); }
115+
void setWIBTimestamp(uint32_t newWIBTimestamp) { head.setWIBTimestamp(newWIBTimestamp); }
116+
void setCrateNo(uint8_t newCrateNo) { head.setCrateNo(newCrateNo); }
117+
void setSlotNo(uint8_t newSlotNo) { head.setSlotNo(newSlotNo); }
118+
void setFiberNo(uint8_t newFiberNo) { head.setFiberNo(newFiberNo); }
119+
void setCapture(uint8_t newCapture) { head.setCapture(newCapture); }
120+
void setASIC(uint8_t newASIC) { head.setASIC(newASIC); }
121+
void setWIBErrors(uint8_t newWIBErrors) { head.setWIBErrors(newWIBErrors); }
122+
void setCRC32(uint32_t newCRC32) { head.setCRC32(newCRC32); }
61123
// COLDATA blocks mutators.
62-
void setChecksumA(int blockNum, uint8_t newChecksumA) { _binaryData[3+28*blockNum] = (_binaryData[3+28*blockNum]&~(0xFF)) | newChecksumA; }
63-
void setChecksumB(int blockNum, uint8_t newChecksumB) { _binaryData[3+28*blockNum] = (_binaryData[3+28*blockNum]&~(0xFF<<8)) | newChecksumB<<8; }
64-
void setS1Err(int blockNum, uint8_t newS1Err) { _binaryData[3+28*blockNum] = (_binaryData[3+28*blockNum]&~(0xF<<16)) | (newS1Err&0xF)<<16; }
65-
void setS2Err(int blockNum, uint8_t newS2Err) { _binaryData[3+28*blockNum] = (_binaryData[3+28*blockNum]&~(0xF<<20)) | (newS2Err&0xF)<<20; }
66-
void setCOLDATA(int blockNum, int num, uint16_t newCOLDATA) { _binaryData[4+28*blockNum+num/2] = (_binaryData[4+28*blockNum+num/2]&~(0xFFFF<<(16*num%2))) | newCOLDATA<<(16*num%2); }
124+
void setChecksumA(int blockNum, uint8_t newChecksumA) { block[blockNum].setChecksumA(newChecksumA); }
125+
void setChecksumB(int blockNum, uint8_t newChecksumB) { block[blockNum].setChecksumB(newChecksumB); }
126+
void setS1Err(int blockNum, uint8_t newS1Err) { block[blockNum].setS1Err(newS1Err); }
127+
void setS2Err(int blockNum, uint8_t newS2Err) { block[blockNum].setS2Err(newS2Err); }
128+
void setCOLDATA(int blockNum, int num, uint16_t newCOLDATA) { block[blockNum].setCOLDATA(num, newCOLDATA); }
129+
// Struct mutators.
130+
void setWIBHeader(WIB_header newWIBHeader) { head = newWIBHeader; }
131+
void setCOLDATABlock(int blockNum, COLDATA_block newCOLDATABlock) { block[blockNum] = newCOLDATABlock; }
67132

68133
bool load(const std::string& filename, int frameNum = 0);
69134
void load(std::ifstream& strm, int frameNum = 0);
@@ -90,7 +155,7 @@ namespace framegen {
90155
class FrameGen {
91156
private:
92157
// File data.
93-
std::string _path = "frames/";
158+
std::string _path = "exampleframes/";
94159
std::string _prefix = "test";
95160
std::string _suffix = "";
96161
std::string _extension = ".frame";
-480 Bytes
Binary file not shown.
-480 Bytes
Binary file not shown.
-480 Bytes
Binary file not shown.
-480 Bytes
Binary file not shown.
-480 Bytes
Binary file not shown.
-480 Bytes
Binary file not shown.
-480 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)