Belle II Software  release-06-00-14
TOPUnpackerModule.h
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 #pragma once
10 
11 #include <framework/core/Module.h>
12 #include <top/geometry/TOPGeometryPar.h>
13 #include <framework/datastore/StoreArray.h>
14 #include <rawdata/dataobjects/RawTOP.h>
15 #include <top/RawDataTypes.h>
16 #include <top/dataobjects/TOPDigit.h>
17 #include <top/dataobjects/TOPRawWaveform.h>
18 #include <top/dataobjects/TOPRawDigit.h>
19 #include <top/dataobjects/TOPSlowData.h>
20 #include <top/dataobjects/TOPInterimFEInfo.h>
21 #include <top/dataobjects/TOPTemplateFitResult.h>
22 #include <top/dataobjects/TOPProductionEventDebug.h>
23 #include <top/dataobjects/TOPProductionHitDebug.h>
24 #include <string>
25 
26 namespace Belle2 {
32  namespace TOP {
33 
38  class DataArray {
39 
40  public:
41 
48  DataArray(const int* data, int size, bool swap = false)
49  {
50  m_data = data;
51  m_size = size;
52  m_i = -1;
53  m_swap = swap;
55  }
56 
63  int getWord()
64  {
65  m_i++;
66  if (m_i < m_size) {
67  m_checksumCounter += (m_data[m_i] & 0xFFFF) + ((m_data[m_i] >> 16) & 0xFFFF);
68  if (m_swap) return swap32(m_data[m_i]);
69  return m_data[m_i];
70  }
71  B2ERROR("Bug in data format: DataArray - index out of range");
72  return 0;
73  }
74 
79  int peekWord()
80  {
81  if (m_i + 1 < m_size) {
82  if (m_swap) return swap32(m_data[m_i + 1]);
83  return m_data[m_i + 1];
84  }
85 
86  return 0;
87  }
88 
94  {
95  if (m_size <= 0) return 0;
96  if (m_swap) return swap32(m_data[m_size - 1]);
97  return m_data[m_size - 1];
98  }
99 
104  unsigned int getIndex() const {return m_i;}
105 
110  int getRemainingWords() const {return m_size - m_i - 1;}
111 
117 
122  unsigned int foldChecksum()
123  {
124  unsigned int chk = m_checksumCounter;
125  while ((chk >> 16) > 0) {
126  chk = (chk & 0xFFFF) + (chk >> 16);
127  }
128  return chk;
129  };
130 
137  {
138  return (foldChecksum() == 0xFFFF);
139  }
140 
141  private:
142 
146  int swap32(int x)
147  {
148  return (((x << 24) & 0xFF000000) |
149  ((x << 8) & 0x00FF0000) |
150  ((x >> 8) & 0x0000FF00) |
151  ((x >> 24) & 0x000000FF));
152  }
153 
154  int m_size = 0;
155  const int* m_data = 0;
156  int m_i = 0;
157  bool m_swap = false;
158  unsigned int m_checksumCounter;
160  };
161 
162  } // TOP namespace
163 
164 
171  class TOPUnpackerModule : public Module {
172 
173  public:
174 
179 
183  virtual ~TOPUnpackerModule();
184 
189  virtual void initialize() override;
190 
195  virtual void beginRun() override;
196 
200  virtual void event() override;
201 
206  virtual void endRun() override;
207 
212  virtual void terminate() override;
213 
214  private:
215 
219  short expand13to16bits(unsigned short x) const
220  {
221  unsigned short signBit = x & 0x1000;
222  return ((x & 0x1FFF) | signBit << 1 | signBit << 2 | signBit << 3);
223  }
224 
225 
229  unsigned short sumShorts(unsigned int x) const
230  {
231  return x + (x >> 16);
232  }
233 
234 
240  void unpackProductionDraft(const int* buffer, int bufferSize);
241 
247  void unpackType0Ver16(const int* buffer, int bufferSize);
248 
256  int unpackInterimFEVer01(const int* buffer, int bufferSize, bool pedestalSubtracted);
257 
266  bool unpackHeadersInterimFEVer01(const int* buffer, int bufferSize, bool swapBytes);
267 
276  int unpackProdDebug(const int* buffer, int bufferSize, TOP::RawDataType dataFormat, bool pedestalSubtracted);
277 
278  std::string m_inputRawDataName;
279  std::string m_outputDigitsName;
280  std::string m_outputRawDigitsName;
281  std::string m_outputWaveformsName;
284  std::map<int, int> m_channelStatistics;
286  bool m_swapBytes = false;
287  int m_dataFormat = 0;
288  bool m_addRelations = false;
292  // collections
303  };
304 
306 } // Belle2 namespace
Base class for Modules.
Definition: Module.h:72
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
short expand13to16bits(unsigned short x) const
Expand 13-bit signed-word to 16-bit signed-word.
StoreArray< TOPRawWaveform > m_waveforms
collection of waveforms
StoreArray< TOPRawDigit > m_rawDigits
collection of raw digits
std::map< int, int > m_channelStatistics
counts how many different channels have been parsed in a given SCROD packet
StoreArray< TOPSlowData > m_slowData
collection of slow data
bool m_swapBytes
if true, swap bytes
StoreArray< TOPProductionEventDebug > m_productionEventDebugs
collection of event debug data
std::string m_outputRawDigitsName
name of TOPRawDigit store array
std::string m_outputWaveformsName
name of TOPRawWaveform store array
unsigned short sumShorts(unsigned int x) const
sum both 16-bit words of 32-bit integer
StoreArray< RawTOP > m_rawData
collection of raw data
StoreArray< TOPInterimFEInfo > m_interimFEInfos
collection of interim informations
StoreArray< TOPProductionHitDebug > m_productionHitDebugs
collection of hit debug data
std::string m_outputDigitsName
name of TOPDigit store array
StoreArray< TOPTemplateFitResult > m_templateFitResults
collection of template fit results
std::string m_inputRawDataName
name of RawTOP store array
StoreArray< TOPDigit > m_digits
collection of digits
bool m_addRelations
switch ON/OFF relations to TOPProductionHitDebugs
std::string m_templateFitResultName
name of TOPTemplateFitResult store array
TOP::TOPGeometryPar * m_topgp
geometry param
Helper class for getting data words from a finesse buffer Keeps checksum counter for each extracted d...
int getRemainingWords() const
Returns number of remaining words in the buffer.
int getWord()
Returns consecutive data word Updates internal checksum counter for each extracted word.
DataArray(const int *data, int size, bool swap=false)
Constructor.
void resetChecksum()
Resets internal checksum counter.
unsigned int getIndex() const
Returns index of last returned data word.
unsigned int foldChecksum()
Performs folding of carry bits into checksum until only 16 LSBs are populated.
const int * m_data
buffer data
bool m_swap
if true, swap bytes
int peekWord()
Returns next data word without incrementing the memory pointer and without modifying the checksum cou...
bool validateChecksum()
Validates current checksum counter status.
int getLastWord()
Returns last data word.
unsigned int m_checksumCounter
check sum counter
int swap32(int x)
Swap bytes of a 32-bit integer.
Singleton class for TOP Geometry Parameters.
static TOPGeometryPar * Instance()
Static method to obtain the pointer to its instance.
int unpackProdDebug(const int *buffer, int bufferSize, TOP::RawDataType dataFormat, bool pedestalSubtracted)
Unpack raw data given in production debugging format.
void unpackProductionDraft(const int *buffer, int bufferSize)
Unpack raw data given in a tentative production format (will vanish in future)
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processor.
virtual void endRun() override
End-of-run action.
void unpackType0Ver16(const int *buffer, int bufferSize)
Unpack raw data given in feature-extraction production format.
virtual void terminate() override
Termination action.
virtual void beginRun() override
Called when entering a new run.
bool unpackHeadersInterimFEVer01(const int *buffer, int bufferSize, bool swapBytes)
Tries to unpack raw data assuming it is in feature-extraction interim format.
virtual ~TOPUnpackerModule()
Destructor.
int unpackInterimFEVer01(const int *buffer, int bufferSize, bool pedestalSubtracted)
Unpack raw data given in feature-extraction interim format.
Abstract base class for different kinds of events.