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