Belle II Software development
RawPXD.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include "rawdata/dataobjects/RawPXD.h"
10#include "framework/logging/Logger.h"
11#include <framework/utilities/HTML.h>
12
13using namespace std;
14using namespace Belle2;
15
16RawPXD::RawPXD() : m_nwords(0), m_buffer(NULL)
17{
18}
19
20RawPXD::RawPXD(int* buffer, int length_in_Bytes)
21 : m_nwords(0), m_buffer(NULL)
22{
23 m_nwords = (length_in_Bytes + 3) / 4;
24 m_buffer = new int[m_nwords];
25 memcpy(m_buffer, buffer, m_nwords * sizeof(int));
26}
27
28unsigned int RawPXD::endian_swap(unsigned int x)
29{
30 x = (x >> 24) |
31 ((x << 8) & 0x00FF0000) |
32 ((x >> 8) & 0x0000FF00) |
33 (x << 24);
34 return x;
35}
36
37RawPXD::RawPXD(const std::vector <unsigned int>& header, const std::vector <std::vector <unsigned char>>& payload)
38 : m_nwords(0), m_buffer(NULL)
39{
40 // This function is only used by the PXDPacker in simulations. Does not make sense for other cases
41 // Now create header from payload , this can be done with less loops, but speed is not the issue here
42 int nr_frames = header.size();
43 int payload_size = 0; // in 32 bit words
44 for (auto& it : header) {
45 payload_size += (it + 3) / 4; // in 32 bit word, rounded up
46 }
47
48 m_nwords = 2 + nr_frames + payload_size; // 321 bit words
49
50 m_buffer = new int[m_nwords];
51
52 B2DEBUG(20, "RawPXD Frames " << header.size() << " Data " << payload_size << " (32 bitword)");
53
54 // now we know the actual payload length
55 int offset = 0;
56 m_buffer[offset++] = 0xBEBAFECA ; // 0xCAFEBABEu;
57 m_buffer[offset++] = endian_swap(nr_frames);
58 // and now append the frame length table
59 for (auto& it : payload) {
60 m_buffer[offset++] = endian_swap(it.size()); // in chars, rounded up to 32 bit boundary
61 }
62 // and now append the actual paylaod data
63 unsigned char* data = (unsigned char*) &m_buffer[offset];
64 for (auto& it : payload) {
65 memcpy(data, it.data(), it.size());
66 data += (it.size() + 3) & 0xFFFFFFFC; // in chars, rounded up to 32 bit boundary
67 }
68 // done
69}
70
72{
73 if (m_buffer != NULL) delete[] m_buffer;
74}
75
76int RawPXD::size() const
77{
78 return m_nwords;
79}
80
81int* RawPXD::data(void)
82{
83 int* ptr = &m_buffer[0];
84 return ptr;
85}
86
87std::string RawPXD::getInfoHTML() const
88{
89 std::string s;
90 if (m_nwords >= 2)
91 s += "Frames: " + std::to_string(endian_swap(m_buffer[1])) + ", ";
92
93 s += "Size (32bit words): " + std::to_string(m_nwords) + "<br /><br />";
95 return s;
96}
static unsigned int endian_swap(unsigned int x)
Endian swap a int32.
Definition: RawPXD.cc:28
virtual int * data(void)
get pointer to data
Definition: RawPXD.cc:81
int * m_buffer
Raw dump of ONSEN data. buffer of size m_nwords (32bit int)
Definition: RawPXD.h:60
int m_nwords
Number of (32bit) Words stored in the buffer.
Definition: RawPXD.h:58
virtual ~RawPXD()
Destructor.
Definition: RawPXD.cc:71
RawPXD()
Default constructor.
Definition: RawPXD.cc:16
std::string getInfoHTML() const
Return a short summary of this object's contents in HTML format.
Definition: RawPXD.cc:87
virtual int size() const
get size of buffer in 32 Bit words
Definition: RawPXD.cc:76
std::string getHexDump(const int *buf, int length)
Create hexdump of given buffer.
Definition: HTML.cc:121
Abstract base class for different kinds of events.
STL namespace.