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