Belle II Software  release-08-01-10
Signal.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 #ifndef TRGSignal_FLAG_
10 #define TRGSignal_FLAG_
11 
12 #include <vector>
13 #include "trg/trg/Clock.h"
14 #include "trg/trg/Time.h"
15 
16 namespace Belle2 {
23  class TRGSignal {
24 
25  public:
26 
28  TRGSignal(const TRGClock& = Belle2_GDL::GDLSystemClock);
29 
31  TRGSignal(const TRGTime& t0, const TRGTime& t1);
32 
34  TRGSignal(const TRGClock& c, int t0, int t1);
35 
38  TRGSignal(const TRGClock& c, double t0, double t1);
39 
41  explicit TRGSignal(const std::string& name,
42  const TRGClock& = Belle2_GDL::GDLSystemClock);
43 
45  TRGSignal(const TRGSignal&);
46 
48  explicit TRGSignal(const TRGTime&);
49 
51  TRGSignal& operator=(const TRGSignal&) = default;
52 
54  virtual ~TRGSignal();
55 
56  public:// Selectors
57 
59  const std::string& name(void) const;
60 
62  const TRGClock& clock(void) const;
63 
65  unsigned nSignals(void) const;
66 
68  unsigned nEdges(void) const;
69 
71  unsigned width(unsigned i = 0) const;
72 
74  bool active(void) const;
75 
77  bool active(int clk0, int clk1) const;
78 
80  bool state(int clockPosition) const;
81 
84  bool riseEdge(int clockPosition) const;
85 
87  std::vector<int> stateChanges(void) const;
88 
91  void dump(const std::string& message = "",
92  const std::string& pre = "") const;
93 
94  public:// Modifiers
95 
97  const std::string& name(const std::string& newName);
98 
100  void clear(void);
101 
103  const TRGClock& clock(const TRGClock&);
104 
107  const TRGSignal& set(double t0, double t1);
108 
111  const TRGSignal& set(int t0, int t1, bool state = true);
112 
115  const TRGSignal& unset(int t0, int t1);
116 
118  const TRGSignal& invert(void);
119 
120  public:// Operators
121 
123  TRGSignal operator&(const TRGSignal&) const;
124 
126  TRGSignal operator&(const TRGTime&) const;
127 
129  TRGSignal& operator&=(const TRGSignal&);
130 
132  TRGSignal& operator&=(const TRGTime&);
133 
135  TRGSignal operator|(const TRGSignal&) const;
136 
138  TRGSignal operator|(const TRGTime&) const;
139 
141  TRGSignal& operator|=(const TRGSignal&);
142 
144  TRGSignal& operator|=(const TRGTime&);
145 
147  TRGSignal& widen(unsigned width);
148 
150  const TRGTime* operator[](unsigned i) const;
151 
153  bool operator==(const TRGSignal&) const;
154 
156  bool operator!=(const TRGSignal&) const;
157 
158  private:
159 
160  // /// And operation.
161  // static std::vector<TRGTime> andOperation(const std::vector<TRGTime>&);
162 
164  static std::vector<TRGTime> orOperation(const std::vector<TRGTime>&);
165 
167  void sort(void);
168 
170  bool consistencyCheck(void) const;
171 
172  private:
173 
175  std::string _name;
176 
178  const TRGClock* _clock;
179 
181  std::vector<TRGTime> _history;
182  };
183 
184 //-----------------------------------------------------------------------------
185 
186  inline
187  const std::string&
188  TRGSignal::name(void) const
189  {
190  return _name;
191  }
192 
193  inline
194  const std::string&
195  TRGSignal::name(const std::string& newName)
196  {
197  return _name = newName;
198  }
199 
200  inline
201  TRGSignal
203  {
204  TRGSignal t(* this);
205  TRGSignal left(l);
206  return t & left;
207  }
208 
209  inline
210  TRGSignal&
212  {
213  TRGSignal left(l);
214 
215 #if TRG_DEBUG
217 #endif
218 
219  return (* this) &= left;
220  }
221 
222  inline
223  TRGSignal
225  {
226  TRGSignal t(* this);
227  TRGSignal left(l);
228 
229 #if TRG_DEBUG
231 #endif
232 
233  return t | left;
234  }
235 
236  inline
237  TRGSignal&
239  {
240  TRGSignal left(l);
241 
242 #if TRG_DEBUG
244 #endif
245 
246  return (* this) |= left;
247  }
248 
249  inline
250  unsigned
252  {
253  return _history.size() / 2;
254  }
255 
256  inline
257  unsigned
258  TRGSignal::nEdges(void) const
259  {
260  return _history.size();
261  }
262 
263  inline
264  void
266  {
267 
268 #if TRG_DEBUG
270 #endif
271 
272  _history.clear();
273  }
274 
275  inline
276  bool
277  TRGSignal::active(void) const
278  {
279  if (_history.size())
280  return true;
281  return false;
282  }
283 
284  inline
285  bool
286  TRGSignal::state(int a) const
287  {
288  if (_history.size()) {
289  bool last = false;
290  for (unsigned i = 0; i < _history.size(); i++) {
291  if (_history[i].time() <= a)
292  last = _history[i].edge();
293  else if (_history[i].time() > a)
294  break;
295  }
296  return last;
297  }
298  return false;
299 
300  }
301 
302  inline
303  bool
304  TRGSignal::riseEdge(int a) const
305  {
306  if (_history.size()) {
307  bool last = false;
308  for (unsigned i = 0; i < _history.size(); i++) {
309  if (_history[i].time() < a) {
310  continue;
311  } else if (_history[i].time() == a) {
312  last = _history[i].edge();
313  } else if (_history[i].time() > a) {
314  break;
315  }
316  }
317  return last;
318  } else
319  return false;
320  }
321 
322  inline
323  const TRGTime*
324  TRGSignal::operator[](unsigned i) const
325  {
326  return & _history[i];
327  }
328 
329  inline
330  const TRGClock&
331  TRGSignal::clock(void) const
332  {
333  return * _clock;
334  }
335 
336  inline
337  bool
339  {
340  return (! operator==(a));
341  }
342 
343  inline
344  bool
345  TRGSignal::active(int c0, int c1) const
346  {
347  for (unsigned i = 0; i < _history.size(); i++) {
348  if (! _history[i].edge()) {
349  const int t0 = _history[i - 1].time();
350  const int t1 = _history[i].time();
351 
352  if ((c1 > t0) && (c0 < t1))
353  return true;
354  if ((c1 < t0) && (c1 < t1))
355  return false;
356  }
357  }
358  return false;
359  }
360 
362 } // namespace Belle2
363 
364 #endif /* TRGSignal_FLAG_ */
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
TRGSignal & operator=(const TRGSignal &)=default
Default assignment operator.
const TRGClock * _clock
Clock.
Definition: Signal.h:178
std::vector< TRGTime > _history
Timing history.
Definition: Signal.h:181
std::string _name
Name.
Definition: Signal.h:175
const TRGSignal & set(double t0, double t1)
makes a pulse with leading edge at t0 and with trailing edge at t1.
A class to represent a signal timing in the trigger system.
Definition: Time.h:25
TRGSignal & operator|=(const TRGSignal &)
returns OR result.
Definition: Signal.cc:273
unsigned nEdges(void) const
returns # of edges.
Definition: Signal.h:258
static std::vector< TRGTime > orOperation(const std::vector< TRGTime > &)
Or operation.
Definition: Signal.cc:292
TRGSignal & operator&=(const TRGSignal &)
returns AND result.
Definition: Signal.cc:210
const TRGTime * operator[](unsigned i) const
returns timing of i'th edge.
Definition: Signal.h:324
TRGSignal operator&(const TRGSignal &) const
returns AND result.
Definition: Signal.cc:165
const TRGClock & clock(void) const
returns clock.
Definition: Signal.h:331
bool operator!=(const TRGSignal &) const
returns true if two are the same.
Definition: Signal.h:338
TRGSignal & widen(unsigned width)
returns widen signals. Signals wider than "width" will be untouched.
Definition: Signal.cc:321
bool riseEdge(int clockPosition) const
returns true if signal is active and rising edge in give clock position.
Definition: Signal.h:304
const std::string & name(void) const
returns name.
Definition: Signal.h:188
std::vector< int > stateChanges(void) const
returns a list of clock position of state change.
Definition: Signal.cc:349
const TRGSignal & unset(int t0, int t1)
clear(or unset) with leading edge at clock t0 and with trailing edge at clock t1.
Definition: Signal.cc:426
unsigned width(unsigned i=0) const
returns width of i'th signal (i=0,1,2,...).
Definition: Signal.cc:388
unsigned nSignals(void) const
returns # of signals.
Definition: Signal.h:251
TRGSignal(const TRGClock &=Belle2_GDL::GDLSystemClock)
Constructor.
Definition: Signal.cc:23
bool state(int clockPosition) const
returns true if signal is active in given clock position.
Definition: Signal.h:286
virtual ~TRGSignal()
Destructor.
Definition: Signal.cc:131
bool active(void) const
returns true if there is a signal.
Definition: Signal.h:277
bool operator==(const TRGSignal &) const
returns true if two are the same.
Definition: Signal.cc:531
void sort(void)
Sort operation.
Definition: Signal.cc:442
TRGSignal operator|(const TRGSignal &) const
returns OR result.
Definition: Signal.cc:257
void clear(void)
clears contents.
Definition: Signal.h:265
bool consistencyCheck(void) const
Self-consitency check. True is return if something wrong.
Definition: Signal.cc:452
const TRGSignal & invert(void)
makes signal inverted.
Definition: Signal.cc:499
void dump(const std::string &message="", const std::string &pre="") const
dumps contents.
Definition: Signal.cc:139
Abstract base class for different kinds of events.