Belle II Software  release-05-02-19
BitStream.h
1 //-----------------------------------------------------------------------------
2 // $Id$
3 //-----------------------------------------------------------------------------
4 // Filename : TRGBitStream.h
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 #ifndef TRGBitStream_FLAG_
15 #define TRGBitStream_FLAG_
16 
17 #include <vector>
18 #include <string>
19 #include "trg/trg/Signal.h"
20 
21 //...Data structure of TRGBitStreamFile...
22 // 4 byte : record type
23 // 4 byte : size in bit
24 // variable(size / 8) byte : data
25 
26 //...TRGBitStream record definition...
27 #define TRGBSRecord_Comment 0xffff0000
28 #define TRGBSRecord_BeginRun 0xffff00A0
29 #define TRGBSRecord_EndRun 0xffff00A1
30 #define TRGBSRecord_BeginEvent 0xffff00B0
31 #define TRGBSRecord_EndEvent 0xffff00B1
32 #define TRGBSRecord_Clock 0xffff00C0
33 #define TRGBSRecord_SegmentSL0 0xffff00F0
34 #define TRGBSRecord_SegmentSL1 0xffff00F1
35 #define TRGBSRecord_SegmentSL2 0xffff00F2
36 #define TRGBSRecord_SegmentSL3 0xffff00F3
37 #define TRGBSRecord_SegmentSL4 0xffff00F4
38 #define TRGBSRecord_SegmentSL5 0xffff00F5
39 #define TRGBSRecord_SegmentSL6 0xffff00F6
40 #define TRGBSRecord_SegmentSL7 0xffff00F7
41 #define TRGBSRecord_SegmentSL8 0xffff00F8
42 
43 namespace Belle2 {
49  class TRGBitStream {
51 
52  public:
53 
55  TRGBitStream();
56 
58  TRGBitStream(int size, const std::string& name = "unknown");
59 
61  TRGBitStream(const TRGBitStream&);
62 
64  TRGBitStream(const char* const, unsigned sizeInBit);
65 
67  virtual ~TRGBitStream();
68 
69  public:// Selectors
70 
72  const std::string& name(void) const;
73 
75  const std::string& name(const std::string& newName);
76 
78  void dump(const std::string& message = "",
79  const std::string& pre = "") const;
80 
82  unsigned size(void) const;
83 
85  unsigned sizeInChar(void) const;
86 
88  char c(unsigned positionInChar) const;
89 
91  bool bit(unsigned positionInBit) const;
92 
93  public:// Modifiers
94 
96  void clear(void);
97 
99  void append(bool);
100 
102  void append(int);
103 
105  void append(unsigned);
106 
107  public:// Utility functions
108 
110  static std::vector<TRGSignal> TRGBitStream2TRGSignal(
111  const TRGClock& clock,
112  int initialClockPosition,
113  std::vector<TRGBitStream*> stream);
114 
115  private:
116 
118  std::string _name;
119 
121  unsigned _sizeMax;
122 
124  unsigned _size;
125 
127  std::vector<unsigned* > _stream;
128  };
129 
130 //-----------------------------------------------------------------------------
131 
132  inline
133  const std::string&
134  TRGBitStream::name(void) const
135  {
136  return _name;
137  }
138 
139  inline
140  const std::string&
141  TRGBitStream::name(const std::string& newName)
142  {
143  return _name = newName;
144  }
145 
146  inline
147  void
149  {
150  _stream.clear();
151  }
152 
153  inline
154  unsigned
155  TRGBitStream::size(void) const
156  {
157  return _size;
158  }
159 
160  inline
161  void
163  {
164  if (a)
165  append(true);
166  else
167  append(false);
168  }
169 
170  inline
171  void
173  {
174  if (a)
175  append(true);
176  else
177  append(false);
178  }
179 
180  inline
181  unsigned
183  {
184  unsigned s = _size / 8;
185  if (_size % 8)
186  ++s;
187  return s;
188  }
189 
190  inline
191  char
192  TRGBitStream::c(unsigned a) const
193  {
194  unsigned p = a / sizeof(unsigned);
195  unsigned q = a % sizeof(unsigned);
196  unsigned v = * _stream[p];
197  return (v >> (q * 8)) & 0xff;
198  }
199 
200  inline
201  bool
202  TRGBitStream::bit(unsigned a) const
203  {
204  unsigned p = a / (sizeof(unsigned) * 8);
205  unsigned q = a % (sizeof(unsigned) * 8);
206  unsigned v = * _stream[p];
207  return (v & (1 << q));
208  }
209 
211 } // namespace Belle2
212 
213 #endif /* TRGBitStream_FLAG_ */
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::name
const std::string & name(void) const
returns name.
Definition: BitStream.h:134
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::size
unsigned size(void) const
returns size of stream in unit of bit.
Definition: BitStream.h:155
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::clear
void clear(void)
clears contents.
Definition: BitStream.h:148
Belle2::TRGBitStream::_stream
std::vector< unsigned * > _stream
Bit stream storage.
Definition: BitStream.h:127
Belle2::TRGBitStream::sizeInChar
unsigned sizeInChar(void) const
returns size in char's.
Definition: BitStream.h:182