Belle II Software  release-05-02-19
SignalVector.cc
1 //-----------------------------------------------------------------------------
2 // $Id$
3 //-----------------------------------------------------------------------------
4 // Filename : TRGSignalVector.cc
5 // Section : TRG
6 // Owner : Yoshihito Iwasaki
7 // Email : yoshihito.iwasaki@kek.jp
8 //-----------------------------------------------------------------------------
9 // Description : A class to represent a bundle of digitized signals.
10 //-----------------------------------------------------------------------------
11 // $Log$
12 //-----------------------------------------------------------------------------
13 
14 #define TRG_SHORT_NAMES
15 
16 #include <string>
17 #include <algorithm>
18 #include <limits>
19 #include <iostream>
20 #include "trg/trg/Utilities.h"
21 #include "trg/trg/Clock.h"
22 #include "trg/trg/Signal.h"
23 #include "trg/trg/SignalVector.h"
24 #include "trg/trg/State.h"
25 
26 using namespace std;
27 
28 namespace Belle2 {
34  TRGSignalVector::TRGSignalVector(const string& name,
35  const TRGClock& c,
36  unsigned size)
37  : _name(name),
38  _clock(& c)
39  {
40  for (unsigned i = 0; i < size; i++) {
41  const string n = _name + ":bit" + TRGUtilities::itostring(i);
42  TRGSignal t(n, c);
43  push_back(t);
44  }
45  }
46 
48  std::vector<TRGSignal>(),
49  _name("CopyOf" + t._name)
50  {
51  _clock = t._clock;
52  const unsigned n = t.size();
53  for (unsigned i = 0; i < n; i++)
54  push_back(t[i]);
55  }
56 
58  _name("VectorOf" + t.name())
59  {
60  _clock = & t.clock();
61  push_back(t);
62  }
63 
65  {
66  }
67 
68  void
69  TRGSignalVector::dump(const string& msg,
70  const string& pre) const
71  {
72 
73  cout << pre << _name << ":" << size() << " signal(s)" << endl;
74 
75  const bool det = msg.find("detail") != string::npos;
76  const bool clk = msg.find("clock") != string::npos;
77 
78  if (det || clk)
79  cout << pre << ":clock=" << _clock->name();
80 
81  if (det)
82  for (unsigned i = 0; i < size(); i++)
83  (* this)[i].dump(msg, " " + pre + "bit" +
84  TRGUtil::itostring(i) + ":");
85  else
86  for (unsigned i = 0; i < size(); i++)
87  if ((* this)[i].active())
88  (* this)[i].dump(msg, " " + pre + "bit" +
89  TRGUtil::itostring(i) + ":");
90  }
91 
92 // TRGSignalVector
93 // TRGSignalVector::operator+(const TRGSignalVector & left) const {
94 // TRGSignalVector t;
95 // t.push_back(this);
96 // t.push_back(& left);
97 // return t;
98 // }
99 
102  {
103  push_back(left);
104  return * this;
105  }
106 
109  {
110  for (unsigned i = 0; i < left.size(); i++)
111  push_back(left[i]);
112  return * this;
113  }
114 
115  bool
117  {
118  for (unsigned i = 0; i < size(); i++)
119  if ((* this)[i].active())
120  return true;
121  return false;
122  }
123 
124  bool
126  {
127  TRGState s = state(c);
128  if (s.active())
129  return true;
130  else
131  return false;
132  }
133 
134  std::vector<int>
136  {
137  std::vector<int> list;
138  const unsigned n = size();
139  for (unsigned i = 0; i < n; i++) {
140  vector<int> a = (* this)[i].stateChanges();
141  for (unsigned j = 0; j < a.size(); j++)
142  list.push_back(a[j]);
143  }
144 
145  //...Sorting...
146  std::sort(list.begin(), list.end());
147 
148  //...Remove duplicated clock...
149  std::vector<int> list2;
150  int last = numeric_limits<int>::min();
151  for (unsigned i = 0; i < list.size(); i++) {
152  const int j = list[i];
153  if (j != last) {
154  list2.push_back(j);
155  last = j;
156  }
157  }
158 
159  return list2;
160  }
161 
162  TRGState
163  TRGSignalVector::state(int clockPosition) const
164  {
165  std::vector<bool> list;
166  const unsigned n = size();
167  for (unsigned i = 0; i < n; i++)
168  list.push_back((* this)[i].state(clockPosition));
169  return TRGState(list);
170  }
171 
172  const TRGClock&
174  {
175  _clock = & c;
176 
177  for (unsigned i = 0; i < size(); i++) {
178  TRGSignal& t = (* this)[i];
179  t.clock(c);
180  }
181 
182  return * _clock;
183  }
184 
185  const TRGSignalVector&
186  TRGSignalVector::set(const TRGState& s, int cp)
187  {
188  const unsigned n = s.size();
189  for (unsigned i = 0; i < n; i++) {
190  TRGSignal& signal = (* this)[i];
191  signal.set(cp, cp + 1, s[i]);
192  }
193  return * this;
194  }
195 
196  bool
198  {
199  if (size() != a.size()) {
200  // cout << "TRGSignalVector::operator== : different size:"
201  // << size() << "," << a.size() << endl;
202  return false;
203  }
204  for (unsigned i = 0; i < size(); i++) {
205  if ((* this)[i] != a[i]) {
206  // cout << "TRGSignalVector::operator== : different signal:i="
207  // << i << endl;
208  // (* this)[i].dump();
209  // a[i].dump();
210  return false;
211  }
212  }
213 
214  return true;
215  }
216 
217 
219 } // namespace Belle2
Belle2::TRGSignalVector::~TRGSignalVector
virtual ~TRGSignalVector()
Destructor.
Definition: SignalVector.cc:64
Belle2::TRGSignal
A class to represent a digitized signal. Unit is nano second.
Definition: Signal.h:28
Belle2::TRGSignalVector::stateChanges
std::vector< int > stateChanges(void) const
returns a list of clock position of state change.
Definition: SignalVector.cc:135
Belle2::TRGState
A class to represent a state of multi bits.
Definition: State.h:29
Belle2::TRGSignalVector::_name
std::string _name
Name.
Definition: SignalVector.h:106
Belle2::TRGClock::name
const std::string & name(void) const
returns name.
Definition: Clock.h:162
Belle2::TRGSignalVector::active
bool active(void) const
returns true if there is a signal.
Definition: SignalVector.cc:116
Belle2::TRGSignalVector::dump
void dump(const std::string &message="", const std::string &pre="") const
dumps contents.
Definition: SignalVector.cc:69
Belle2::TRGSignalVector::clock
const TRGClock & clock(void) const
returns clock.
Definition: SignalVector.h:130
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TRGSignalVector::operator+=
TRGSignalVector & operator+=(const TRGSignal &)
appends TRGSignal.
Definition: SignalVector.cc:101
Belle2::TRGSignalVector::operator==
bool operator==(const TRGSignalVector &) const
compare two TRGSignalVectors.
Definition: SignalVector.cc:197
Belle2::TRGUtilities::itostring
static std::string itostring(int i)
converts int to string. (Use boost::lexical_cast)
Belle2::TRGSignalVector::TRGSignalVector
TRGSignalVector(const std::string &name, const TRGClock &, unsigned size=0)
Default constructor.
Definition: SignalVector.cc:34
Belle2::TRGSignalVector::_clock
const TRGClock * _clock
Clock.
Definition: SignalVector.h:109
Belle2::TRGSignalVector::state
TRGState state(int clockPosition) const
returns state at given clock position.
Definition: SignalVector.cc:163
Belle2::TRGClock
A class to represent a digitized signal. Unit is nano second.
Definition: Clock.h:43
Belle2::TRGSignalVector
A class to represent a bundle of digitized signals.
Definition: SignalVector.h:31
Belle2::TRGSignalVector::set
const TRGSignalVector & set(const TRGState &, int clockPosition)
sets state at given clock.
Definition: SignalVector.cc:186