Belle II Software development
ROIpayload.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 <tracking/dataobjects/ROIpayload.h>
10#include <arpa/inet.h>
11#include <boost/crc.hpp>
12#include <stdio.h>
13#include <framework/logging/Logger.h>
14
15#define MAGIC_WORD (0xBE12DA7A)
16
17using namespace Belle2;
18using boost::crc_optimal;
19
21{
22 init(HEADER_SIZE_WITH_LENGTH + 2 * rois + 1); // HEADER + 2*ROIS + CHECKSUM
23};
24
25void ROIpayload::init(int length)
26{
27 m_index = 0;
28 m_length = length;
29 if (length == 0) {
30 m_rootdata = nullptr;
31 m_data32 = nullptr;
32 m_data64 = nullptr;
33 } else {
34 m_rootdata = new int[length];
35 m_data32 = reinterpret_cast<uint32_t*>(m_rootdata);
36 m_data64 = reinterpret_cast<ROIrawID::baseType*>(m_data32 + HEADER_SIZE_WITH_LENGTH);
37 }
38}
39
41{
42 m_data32[OFFSET_MAGIC] = htonl(MAGIC_WORD);
43 m_data32[OFFSET_LENGTH] = htonl(length);
44 m_packetLengthByte = length + 2 * sizeof(uint32_t);
45}
46
48{
49 unsigned int lengthInBytes =
50 sizeof(uint32_t) + // The header
51 sizeof(uint32_t) + // The trigger number
52 sizeof(uint32_t) + // Run Subrun Experiment Number
53 m_index * sizeof(uint64_t) + // The rois
54 sizeof(uint32_t); // The CRC
55
56 m_data32[OFFSET_MAGIC] = htonl(MAGIC_WORD);
57 m_data32[OFFSET_LENGTH] = htonl(lengthInBytes);
58 m_packetLengthByte = lengthInBytes + 2 * sizeof(uint32_t); // The space for the magic word and payload length
59}
60
61void ROIpayload::setHeader(bool Accepted, bool SendAll, bool SendROIs)
62{
63 unsigned int h = 0xCAFE0000; // Magic
64 if (Accepted) h |= 0x8000;
65 if (SendAll) h |=
66 0x4000;
67 if (SendROIs) h |=
68 0x2000;
69 m_data32[OFFSET_HEADER] = htonl(h);
70};
71
72void ROIpayload::setTriggerNumber(unsigned long int triggerNumber)
73{
74 m_data32[OFFSET_TRIGNR] = htonl(triggerNumber);
75};
76
77void ROIpayload::setRunSubrunExpNumber(int run, int subrun, int exp)
78{
79 int rseNumber;
80 rseNumber = (exp & 0x3FF) << 22 | (run & 0x3FFF) << 8 | (subrun & 0xFF) ;
81 m_data32[OFFSET_RUNNR] = htonl(rseNumber);
82};
83
84//void ROIpayload::addROIraw(ROIrawID roiraw)
85void ROIpayload::addROIraw(unsigned long int roiraw)
86{
87 if (reinterpret_cast<int*>(m_data64 + m_index) >= m_rootdata + m_length) {
88 B2ERROR("Adding too many ROIs to the ROIpayload." << std::endl <<
89 "Something really fishy is going on");
90 return;
91 }
92 m_data64[m_index++] = roiraw;
93}
94
96{
97
98 // assert((int*)(m_data64 + m_index) == m_rootdata + m_length - 1);
99
100 // TODO Pointer comparison is bad
101 if (reinterpret_cast<int*>(m_data32 + m_index * 2 + HEADER_SIZE_WITH_LENGTH) >= m_rootdata + m_length) {
102 B2ERROR("No space left on the ROI payload to write the CRC." << std::endl);
103 return;
104 }
105
106 crc_optimal<32, 0x04C11DB7, 0, 0, false, false> dhh_crc_32;
107
108 dhh_crc_32.process_bytes(m_rootdata + OFFSET_HEADER,
109 HEADER_SIZE_WO_LENGTH * sizeof(uint32_t) + m_index * sizeof(uint64_t));
110
111 m_data32[OFFSET_ROIS + m_index * 2] = htonl(dhh_crc_32.checksum()) ;
112}
int m_packetLengthByte
packet length in byte
Definition ROIpayload.h:52
void addROIraw(unsigned long int roiraw)
add a ROIrawID
Definition ROIpayload.cc:85
int m_length
packet length
Definition ROIpayload.h:53
void setCRC()
set CRC
Definition ROIpayload.cc:95
void setHeader(bool Accepted, bool SendAll, bool SendROIs)
set header
Definition ROIpayload.cc:61
uint32_t * m_data32
transient value
Definition ROIpayload.h:60
void setRunSubrunExpNumber(int run, int subrun, int exp)
set run/ subrun/exp number
Definition ROIpayload.cc:77
int * m_rootdata
pointer to data packet of m_length words
Definition ROIpayload.h:55
ROIpayload(int rois=0)
Default constructor.
Definition ROIpayload.cc:20
void setTriggerNumber(unsigned long int triggerNumber)
set trigger number
Definition ROIpayload.cc:72
void init(int length)
initializer
Definition ROIpayload.cc:25
void setPayloadLength()
set payload length
Definition ROIpayload.cc:47
int m_index
transient index
Definition ROIpayload.h:58
ROIrawID::baseType * m_data64
transient value
Definition ROIpayload.h:62
uint64_t baseType
base type
Definition ROIrawID.h:28
Abstract base class for different kinds of events.