9#include <pxd/modules/pxdUnpacking/PXDReadRawONSEN.h>
10#include <pxd/unpacking/PXDRawDataDefinitions.h>
12#include <framework/datastore/StoreArray.h>
14#include <boost/endian/arithmetic.hpp>
20using ulittle16_t = boost::endian::little_uint16_t;
21using ulittle32_t = boost::endian::little_uint32_t;
22using ubig16_t = boost::endian::big_uint16_t;
23using ubig32_t = boost::endian::big_uint32_t;
39 setDescription(
"Read a Raw PXD-Data Dump from ONSEN (or a simulator) and stores it as RawPXD in Data Store");
50 B2DEBUG(29,
"PXDReadRawONSENModule: Constructor done.");
65 B2INFO(
"Read Raw ONSEN Data from " <<
m_filename);
67 B2ERROR(
"Could not open Raw ONSEN Data: " <<
m_filename);
80 B2DEBUG(29,
"PXDReadRawONSENModule: initialized.");
86 if (
fh) l = fread(data, 1, len,
fh);
87 if (l != len)
return 0;
94 int len = MAXEVTSIZE *
sizeof(int);
96#define MAX_PXD_FRAMES 256
97 const int headerlen = 8;
100 int framenr = 0, tablelen = 0, datalen = 0;
102 if (br <= 0)
return br;
103 if (pxdheader[0] != 0xCAFEBABEu) {
104 B2FATAL(Form(
"pxdheader wrong : Magic %X , Frames %X \n", (
unsigned int) pxdheader[0], (
unsigned int) pxdheader[1]));
107 framenr = pxdheader[1];
108 if (framenr > MAX_PXD_FRAMES) {
109 B2FATAL(Form(
"MAX_PXD_FRAMES too small : %d(%d) \n", framenr, MAX_PXD_FRAMES));
112 tablelen = 4 * framenr;
113 br =
read_data((
char*)&data[headerlen], tablelen);
114 if (br <= 0)
return br;
115 for (
int i = 0; i < framenr; i++) {
116 datalen += (pxdheadertable[i] + 3) & 0xFFFFFFFC;
119 if (datalen + headerlen + tablelen > len) {
120 B2FATAL(Form(
"buffer too small : %d %d %d(%d) \n", headerlen, tablelen, datalen, len));
123 br =
read_data(data + headerlen + tablelen, datalen);
124 if (br <= 0)
return br;
125 return (headerlen + tablelen + br);
131 B2ERROR(
"Unexpected close of dump file.");
156 for (
auto& it : rawpxdary) {
179 if (px.
size() <= 0 || px.
size() > 16 * 1024 * 1024) {
180 B2ERROR(
"PXD Trigger Shifter --> invalid packet size (32bit words) " << hex << px.
size());
183 std::vector<unsigned int> data(px.
size());
184 fullsize = px.
size() * 4;
185 std::copy_n(px.
data(), px.
size(), data.begin());
189 B2ERROR(
"Data is to small to hold a valid Header! Will not unpack anything. Size:" << fullsize);
193 if (data[0] != 0xCAFEBABE && data[0] != 0xBEBAFECA) {
194 B2ERROR(
"Magic invalid: Will not unpack anything. Header corrupted! " << hex << data[0]);
198 Frames_in_event = ((
ubig32_t*)data.data())[1];
199 if (Frames_in_event < 1 || Frames_in_event > 250) {
200 B2ERROR(
"Number of Frames invalid: Will not unpack anything. Header corrupted! Frames in event: " << Frames_in_event);
204 unsigned int* tableptr;
207 unsigned int* dataptr;
208 dataptr = &tableptr[Frames_in_event];
209 datafullsize = fullsize - 2 * 4 - Frames_in_event * 4;
212 for (
int j = 0; j < Frames_in_event; j++) {
216 B2ERROR(
"size of frame invalid: " << j <<
"size " << lo <<
" at byte offset in dataptr " << ll);
219 if (ll + lo > datafullsize) {
220 B2ERROR(
"frames exceed packet size: " << j <<
" size " << lo <<
" at byte offset in dataptr " << ll <<
" of datafullsize " <<
221 datafullsize <<
" of fullsize " << fullsize);
225 B2ERROR(
"SKIP Frame with Data with not MOD 4 length " <<
" ( $" << hex << lo <<
" ) ");
226 ll += (lo + 3) & 0xFFFFFFFC;
237 switch (((*(
ubig16_t*)data) & 0x7800) >> 11) {
238 case EDHCFrameHeaderDataType::c_ONSEN_TRG: {
239 unsigned int trignr = ((
ubig32_t*)data)[2];
240 unsigned int tag = ((
ubig32_t*)data)[3];
242 B2INFO(
"Set event and exp/run from ONSEN: $" << hex << trignr <<
", $" << hex << tag);
250 case EDHCFrameHeaderDataType::c_DHC_START: {
251 unsigned int time_tag_lo_and_type = ((
ubig16_t*)data)[3];
252 unsigned int time_tag_mid = ((
ubig16_t*)data)[4];
253 unsigned int time_tag_hi = ((
ubig16_t*)data)[5];
254 B2INFO(
"Set time tag from DHC: $" << hex << time_tag_mid <<
", $" << hex << time_tag_lo_and_type);
255 uint32_t tt = ((time_tag_mid & 0x7FFF) << 12) | (time_tag_lo_and_type >> 4);
258 m_eventMetaDataPtr->setTime((
unsigned long long int)((time_tag_hi << 1) + ((time_tag_mid & 0x8000) ? 1 : 0)) * 1000000000 +
259 (int)std::round(tt / 0.127216));
void setDescription(const std::string &description)
Sets the description of the module.
A class to encode/decode an EvtMessage.
void initialize() override final
Initialize.
bool getTrigNr(RawPXD &px)
get the trigger number
int m_nread
No. of sent events.
int read_data(char *data, size_t len)
Read amount of data (len bytes) from file to ptr data.
PXDReadRawONSENModule()
Constructor / Destructor.
~PXDReadRawONSENModule()
Destructor.
bool m_setEvtMeta
Set Event Meta Info.
void terminate() override final
Terminate.
void event() override final
Event.
MsgHandler * m_msghandler
Message handler.
bool unpack_dhc_frame(void *data)
unpack the dhc frame
std::string m_filename
File Name.
int m_compressionLevel
Compression Level.
int readOneEvent(void)
Read data of one Event from File.
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
Event Meta Data.
virtual int * data(void)
get pointer to data
virtual int size() const
get size of buffer in 32 Bit words
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Accessor to arrays stored in the data store.
T * appendNew()
Construct a new T object at the end of the array.
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::big_uint32_t ubig32_t
define alias ubig32_t
boost::endian::big_uint16_t ubig16_t
define alias ubig16_t
Abstract base class for different kinds of events.