9#include <pxd/modules/pxdUnpacking/PXDReadRawBonnDAQ.h>
10#include <boost/endian/arithmetic.hpp>
11#include <boost/crc.hpp>
17using ulittle16_t = boost::endian::little_uint16_t;
18using ulittle32_t = boost::endian::little_uint32_t;
19using ubig16_t = boost::endian::big_uint16_t;
20using ubig32_t = boost::endian::big_uint32_t;
22using boost::crc_optimal;
23typedef crc_optimal<32, 0x04C11DB7, 0, 0, false, false> dhe_crc_32_type;
38 setDescription(
"Read a BonnDAQ file and stores it as RawPXD in Data Store");
47 B2DEBUG(29,
"PXDReadRawBonnDAQModule: Constructor done.");
61 B2INFO(
"Read BonnDAQ Data from " <<
m_filename);
63 B2ERROR(
"Could not open BonnDAQ Data: " <<
m_filename);
71 B2DEBUG(29,
"PXDReadRawBonnDAQModule: initialized.");
78 B2ERROR(
"BonnDAQ Data file is not open ");
81 unsigned int triggernr = 0xFFFFFFFF;
86 unsigned int get_size(
void) {
return (
unsigned int) size + (((
unsigned int)(header & 0x000F)) << 16);};
87 unsigned int get_size_group(
void) {
return (
unsigned int) size + (((
unsigned int)((header & 0x003F) ^ 0x0020)) << 16);};
88 unsigned int get_header12(
void) {
return (header & 0xFFF0);};
89 unsigned int get_header10(
void) {
return (header & 0xFFC0);};
90 unsigned int get_header8(
void) {
return (header & 0xFF00);};
101 if (br <= 0)
return br;
102 unsigned int chunk_size = 0;
103 if (evt->get_header8() == 0) {
104 B2DEBUG(29,
"Group Header $" << std::hex << evt->get_header10() <<
" Chunk size " << std::dec << evt->get_size());
105 chunk_size = evt->get_size_group();
107 B2DEBUG(29,
"Header $" << std::hex << evt->get_header12() <<
" Chunk size " << std::dec << evt->get_size());
108 chunk_size = evt->get_size();
110 if (chunk_size <= 1)
return 0;
112 if (br <= 0)
return br;
113 if (evt->get_header12() == 0xe230) {
114 B2DEBUG(29,
"File info " << std::hex << evt->get_header12() <<
" Events " << std::dec << data32[1]);
116 }
else if (evt->get_header12() == 0xe100) {
117 B2DEBUG(29,
"Info Event " << std::hex << evt->get_header12() <<
" RunNr $" << std::hex << data32[1]);
120 }
else if (evt->get_header10() == 0x0000) {
121 B2DEBUG(29,
"Run Event Group " << std::hex << evt->get_header10() <<
" Magic $" << std::hex << data32[1]);
123 }
else if (evt->get_header12() == 0xbb00) {
124 B2DEBUG(29,
"Run Event " << std::hex << evt->get_header12() <<
" Magic $" << std::hex << data32[1]);
126 }
else if (evt->get_header10() == 0x0080) {
127 int togo = chunk_size;
128 B2DEBUG(29,
"Data Event Group " << std::hex << evt->get_header10() <<
" TriggerNr $" << std::hex << data32[1]);
129 triggernr = data32[1];
134 B2DEBUG(29,
"TOGO: " << togo);
135 B2DEBUG(29,
" ............... " << std::hex << data32[0] <<
" TriggerNr $" << std::hex << data32[1]);
136 if (triggernr != data32[1]) B2ERROR(
"Trigger Nr does not match!");
137 B2DEBUG(29,
" ............... " << std::hex << data32[2]);
141 if ((data32[0] & 0xFFFF0000) == 0xCAFE0000) {
142 int frames = (data32[0] & 0x3FF);
143 B2DEBUG(29,
"Frames: " << frames);
145 bool nocrc = (data32[0] & 0x8000) != 0;
147 if ((data32[0] & 0x4000) == 0) B2FATAL(
"large data fields not supported");
153 std::vector <unsigned int> m_onsen_header;
156 std::vector <std::vector <unsigned char>> m_onsen_payload;
157 int offset = ((frames + 1) & ~1);
166 for (
int i = 0; i < frames; i++) {
171 std::vector <unsigned char> m_current_frame;
173 for (
int j = 0; j < (int)table16[i] * 2; j++) {
174 unsigned short w = data16[offset++];
175 m_current_frame.push_back((
unsigned char)(w >> 8));
176 m_current_frame.push_back((
unsigned char)(w));
180 dhe_crc_32_type current_crc;
181 current_crc.process_bytes(m_current_frame.data(), m_current_frame.size());
182 unsigned int w = current_crc.checksum();
183 m_current_frame.push_back((
unsigned char)(w >> 24));
184 m_current_frame.push_back((
unsigned char)(w >> 16));
185 m_current_frame.push_back((
unsigned char)(w >> 8));
186 m_current_frame.push_back((
unsigned char)(w));
189 m_onsen_header.push_back(m_current_frame.size());
190 m_onsen_payload.push_back(m_current_frame);
192 togo -= ((frames + 1) & ~1) / 2 + size;
193 data32 += ((frames + 1) & ~1) / 2 + size;
194 data16 += ((frames + 1) & ~1) + size * 2;
202 m_rawPXD.appendNew(m_onsen_header, m_onsen_payload);
207 B2INFO(
"Set Meta: Exp " <<
m_expNr <<
" Run " <<
m_runNr <<
" TrgNr " << triggernr);
217 B2ERROR(
"Undefine Header $" << std::hex << evt->get_header12());
229 B2ERROR(
"Unexpected close of dump file.");
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
void setDescription(const std::string &description)
Sets the description of the module.
A class to manage I/O for a chain of blocked files.
int read_data(char *data, size_t len)
Read a record from a file.
void initialize() override final
Initialize.
unsigned int m_expNr
set by Param
unsigned int m_subRunNr
set by Param
PXDReadRawBonnDAQModule()
Constructor.
void terminate() override final
Terminate.
void event() override final
Event.
~PXDReadRawBonnDAQModule() override final
Destructor.
std::string m_filename
File Name.
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
Event Meta Data.
PXDLocalDAQFile * fh
File handle.
unsigned int m_runNr
set by Param
StoreArray< RawPXD > m_rawPXD
DHH Data.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
boost::endian::little_uint16_t ulittle16_t
define alias ulittle16_t
boost::endian::little_uint32_t ulittle32_t
define alias ulittle32_t
Abstract base class for different kinds of events.