Belle II Software  release-08-01-10
BitStream.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 <iostream>
10 #include "trg/trg/Utilities.h"
11 #include "trg/trg/BitStream.h"
12 
13 using namespace std;
14 
15 namespace Belle2 {
21  TRGBitStream::TRGBitStream() :
22  _name("unknown"),
23  _sizeMax(0),
24  _size(0),
25  _stream(0)
26  {
27  }
28 
29  TRGBitStream::TRGBitStream(int size, const std::string& name) :
30  _name(name),
31  _sizeMax(size),
32  _size(0),
33  _stream(0)
34  {
35  }
36 
38  _name("CopyOf" + a._name),
39  _sizeMax(a._sizeMax),
40  _size(a._size),
41  _stream(0)
42  {
43  for (unsigned i = 0; i < a._stream.size(); i++)
44  _stream.push_back(new unsigned(* a._stream[i]));
45  }
46 
47  TRGBitStream::TRGBitStream(const char* const c, unsigned s) :
48  _name("unknown"),
49  _sizeMax(0),
50  _size(s),
51  _stream(0)
52  {
53  unsigned sz = s / 8;
54  if (s % 8) ++sz;
55  unsigned sz4 = sz / 4;
56  for (unsigned i = 0; i < sz4; i++) {
57 // unsigned j = * (unsigned *) c[i * 4];
58  unsigned j = * (unsigned*)(& (c[i * 4]));
59  _stream.push_back(new unsigned(j));
60  }
61  if (sz % 4) {
62  unsigned j = 0;
63  for (unsigned i = 0; i < sz % 4; i++)
64  j |= (c[sz4 + i] << i * 8);
65  _stream.push_back(new unsigned(j));
66  }
67  }
68 
70  {
71  for (unsigned i = 0; i < _stream.size(); i++)
72  delete _stream[i];
73  }
74 
75  void
76  TRGBitStream::dump(const string&,
77  const string& pre) const
78  {
79 
80  const string tab = " ";
81 
82  cout << pre << _name << ":size=" << dec << _size << endl;
83  for (unsigned i = 0; i < _stream.size(); i++) {
84  cout << pre << tab;
85  if (i == _stream.size() - 1) {
86  const unsigned last = _size % (sizeof(unsigned) * 8);
87 
88  if (last)
89  cout << TRGUtilities::streamDisplay(* _stream[i], 0, last - 1);
90  } else {
91  cout << TRGUtilities::streamDisplay(* _stream[i], 0, 31);
92  }
93  cout << endl;
94  }
95  }
96 
97  void
99  {
100  if (_sizeMax) {
101  if (_size >= _sizeMax) {
102  cout << "TRGBitStream::append !!! stream is full :current size="
103  << _size << ",max size=" << _sizeMax << endl;
104  return;
105  }
106  }
107 
108  const unsigned storageSize = sizeof(unsigned) * 8 * _stream.size();
109  ++_size;
110 
111  if (_size <= storageSize) {
112  if (a) {
113  // TODO This block does nothing, is it ok?
114  //const unsigned position = _size % (sizeof(unsigned) * 8) - 1;
115  //unsigned& last = * _stream.back();
116  //last |= (1 << position);
117  }
118  } else {
119  if (a)
120  _stream.push_back(new unsigned(1));
121  else
122  _stream.push_back(new unsigned(0));
123  }
124  }
125 
126  vector<TRGSignal>
128  int initialClockPosition,
129  vector<TRGBitStream*> stream)
130  {
131 
132  vector<TRGSignal> t;
133 
134  //...Check the size of stream...
135  if (stream.size() == 0) {
136  cout << " !!! TRGBitStream::TRGBitStream2TRGSignal: given stream "
137  << "has no data" << endl;
138  return t;
139  }
140 
141  //...Get bit size...
142  const unsigned bs = stream[0]->size();
143  const unsigned cs = stream.size();
144 
145  //...Preparation...
146  vector<TRGSignal*> s;
147  for (unsigned i = 0; i < bs; i++)
148  s.push_back(new TRGSignal());
149 
150  //...Bit stream loop...
151  for (unsigned i = 0; i < cs; i++) {
152 
153  //...Bit position loop...
154  for (unsigned j = 0; j < bs; j++) {
155  if (stream[i]->bit(j)) {
156  TRGTime r(int(initialClockPosition + i), true, clock);
157  TRGTime f = r;
158  f.shift(1).reverse();
159  (* s[j]) |= (r & f);
160  }
161  }
162  }
163 
164  //...Return value...
165  for (unsigned i = 0; i < s.size(); i++)
166  t.push_back(* s[i]);
167  return t;
168  }
169 
170 
172 } // namespace Belle2
A class to represent a bit stream.
Definition: BitStream.h:45
unsigned _size
Bit stream size.
Definition: BitStream.h:119
unsigned _sizeMax
Bit stream max size.
Definition: BitStream.h:116
std::string _name
Name.
Definition: BitStream.h:113
std::vector< unsigned * > _stream
Bit stream storage.
Definition: BitStream.h:122
A class to represent a digitized signal. Unit is nano second.
Definition: Clock.h:38
A class to represent a digitized signal. Unit is nano second.
Definition: Signal.h:23
A class to represent a signal timing in the trigger system.
Definition: Time.h:25
bool bit(unsigned positionInBit) const
returns true if given position is active.
Definition: BitStream.h:197
TRGBitStream()
Default constructor.
Definition: BitStream.cc:21
virtual ~TRGBitStream()
Destructor.
Definition: BitStream.cc:69
static std::vector< TRGSignal > TRGBitStream2TRGSignal(const TRGClock &clock, int initialClockPosition, std::vector< TRGBitStream * > stream)
Make trigger signals from bit stream.
Definition: BitStream.cc:127
char c(unsigned positionInChar) const
returns a pointer to char's.
Definition: BitStream.h:187
static std::string streamDisplay(unsigned)
Dumps bit stream in string.
Definition: Utilities.cc:136
void append(bool)
appends a bit to a stream.
Definition: BitStream.cc:98
void dump(const std::string &message="", const std::string &pre="") const
dumps contents. "message" is to select information to dump. "pre" will be printed in head of each lin...
Definition: BitStream.cc:76
Abstract base class for different kinds of events.