Belle II Software development
Signal.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 <algorithm>
10#include <iostream>
11#include <cctype>
12#include "trg/trg/Clock.h"
13#include "trg/trg/Signal.h"
14
15namespace Belle2 {
20
22 m_name("?"),
23 m_clock(& c),
24 m_history()
25 {
26 }
27
28 TRGSignal::TRGSignal(const TRGTime& t0, const TRGTime& t1) :
29 m_name("?"),
30 m_clock(& t0.clock()),
31 m_history()
32 {
33#if TRG_DEBUG
34 if ((& t0.clock()) != (& t1.clock()))
35 std::cout << "TRGSignal !!! signal is made with two different clocks"
36 << std::endl
37 << " t0.clock=" << t0.clock().name() << std::endl
38 << " t1.clock=" << t1.clock().name() << std::endl;
39#endif
40
41 m_history.push_back(t0);
42 m_history.push_back(t1);
43
44#if TRG_DEBUG
46#endif
47 }
48
49 TRGSignal::TRGSignal(const TRGClock& c, int t0, int t1) :
50 m_name("?"),
51 m_clock(& c),
52 m_history()
53 {
54 m_history.reserve(2);
55 m_history.emplace_back(TRGTime(t0, true, c));
56 m_history.emplace_back(TRGTime(t1, false, c));
57
58#if TRG_DEBUG
60#endif
61 }
62
63 TRGSignal::TRGSignal(const TRGClock& c, double t0, double t1) :
64 m_name("?"),
65 m_clock(& c),
66 m_history()
67 {
68 m_history.reserve(2);
69 m_history.emplace_back(TRGTime(t0, true, c));
70 m_history.emplace_back(TRGTime(t1, false, c));
71
72#if TRG_DEBUG
74#endif
75 }
76
77 TRGSignal::TRGSignal(const std::string& name, const TRGClock& c) :
78 m_name(name),
79 m_clock(& c),
80 m_history()
81 {
82#if TRG_DEBUG
84#endif
85 }
86
88// m_history(t.m_history),
89 m_name(t.m_name),
91 {
92 const unsigned n = t.m_history.size();
93 m_history.reserve(n);
94 m_history.insert(m_history.end(), t.m_history.begin(), t.m_history.end());
95
96#if TRG_DEBUG
98#endif
99 }
100
102 m_name(t.name()),
103 m_clock(& t.clock())
104 {
105
106 m_history.reserve(m_history.size() + 2);
107 //...Check edge...
108 if (t.edge()) {
109
110 //...Store itself...
111 m_history.push_back(t);
112
113 //...Set falling edge...
114 m_history.push_back(t.clock().maxTRGTime(false));
115 } else {
116
117 //...Set rising edge...
118 m_history.push_back(t.clock().minTRGTime(true));
119
120 //...Store itself...
121 m_history.push_back(t);
122 }
123#if TRG_DEBUG
125#endif
126 }
127
129 {
130#if TRG_DEBUG
132#endif
133 }
134
135 void
136 TRGSignal::dump(const std::string& msg,
137 const std::string& pre) const
138 {
139
140 std::string tmp;
141 tmp.resize(m_name.size());
142 transform(m_name.cbegin(), m_name.cend(), tmp.begin(), ::toupper);
143
144 const bool ctr = tmp.find("CLOCKCOUNTER") != std::string::npos;
145
146 std::cout << pre << m_name << ":#signal=" << m_history.size();
147
148 if (msg.find("clock") != std::string::npos ||
149 msg.find("detail") != std::string::npos) {
150 std::cout << ":clock=" << m_clock->name();
151 }
152
153 std::cout << std::endl;
154
155 if (m_history.size() && (! ctr)) {
156 for (unsigned i = 0; i < m_history.size(); i++)
157 m_history[i].dump(msg, pre + " ");
158 }
159 }
160
163 {
164
165 //...Collect state changes...
166 std::vector<int> sc0 = stateChanges();
167 std::vector<int> sc1 = left.stateChanges();
168 sc0.reserve(sc0.size() + sc1.size());
169 sc0.insert(sc0.end(), sc1.begin(), sc1.end());
170
171 //...Sorting...
172 std::sort(sc0.begin(), sc0.end());
173
174 //...Remove multiple same clock...
175 std::vector<int> sc2;
176 sc2.reserve(sc0.size());
177 int last = m_clock->min();
178 for (unsigned i = 0; i < sc0.size(); i++) {
179 const int j = sc0[i];
180 if (j != last) {
181 sc2.push_back(j);
182 last = j;
183 }
184 }
185
186 //...Make a new signal...
187 std::string name = "(" + m_name + ")&(" + left.m_name + ")";
188 TRGSignal t(name, * m_clock);
189 bool active = false;
190 t.m_history.reserve(sc2.size());
191 for (unsigned i = 0; i < sc2.size(); i++) {
192 const int j = sc2[i];
193 if ((! active) and state(j) and left.state(j)) {
194 active = true;
195 t.m_history.push_back(TRGTime(j, true, * m_clock));
196 } else if (active and ((! state(j)) or (! left.state(j)))) {
197 active = false;
198 t.m_history.push_back(TRGTime(j, false, * m_clock));
199 }
200 }
201
202#if TRG_DEBUG
204#endif
205
206 return t;
207 }
208
209 TRGSignal&
211 {
212 TRGSignal t = (* this) & left;
213
214 m_history.clear();
215 m_history = t.m_history;
216
217#if TRG_DEBUG
219#endif
220
221 return * this;
222 }
223
224// std::vector<TRGTime>
225// TRGSignal::andOperation(const std::vector<TRGTime> & history) {
226
227// //...And operation...
228// const unsigned n = history.size();
229// unsigned riseC = 0;
230// bool signal = false;
231// std::vector<TRGTime> tmp;
232// for (unsigned i = 0; i < n; i++) {
233// const bool edge = history[i].edge();
234
235// if (edge)
236// ++riseC;
237// else
238// --riseC;
239
240// // std::cout << "riseC,i,e,t=" << riseC << "," << i << "," << edge
241// // << "," << history[i].time() << std::endl;
242
243// if (riseC == 2) {
244// tmp.push_back(history[i]);
245// signal = true;
246// }
247// else if (signal && (riseC == 1)) {
248// tmp.push_back(history[i]);
249// signal = false;
250// }
251// }
252
253// return tmp;
254// }
255
258 {
259 TRGSignal t(* this);
260 t.m_history.reserve(t.m_history.size() + left.m_history.size());
261 t.m_history.insert(t.m_history.end(),
262 left.m_history.begin(),
263 left.m_history.end());
264 t.m_name = "(" + t.m_name + ")|(" + left.m_name + ")";
265 std::sort(t.m_history.begin(), t.m_history.end(), TRGTime::sortByTime);
266
267 //...And operation...
268 t.m_history = orOperation(t.m_history);
269
270 return t;
271 }
272
273 TRGSignal&
275 {
276 m_history.reserve(m_history.size() + left.m_history.size());
277 m_history.insert(m_history.end(),
278 left.m_history.begin(),
279 left.m_history.end());
280 this->m_name = "(" + this->m_name + ")&(" + left.m_name + ")";
281 std::sort(m_history.begin(), m_history.end(), TRGTime::sortByTime);
282
283 //...And operation...
285
286#if TRG_DEBUG
288#endif
289
290 return * this;
291 }
292
293 std::vector<TRGTime>
294 TRGSignal::orOperation(const std::vector<TRGTime>& history)
295 {
296
297 //...And operation...
298 const unsigned n = history.size();
299 std::vector<TRGTime> tmp;
300 tmp.reserve(n);
301 unsigned signal = 0;
302 for (unsigned i = 0; i < n; i++) {
303 const bool edge = history[i].edge();
304
305 if (edge) {
306 if (signal == 0)
307 tmp.push_back(history[i]);
308 ++signal;
309 } else {
310 if (signal == 1)
311 tmp.push_back(history[i]);
312 --signal;
313 }
314
315// std::cout << "i,time,edge,signal=" << i << "," << history[i].time()
316// << "," << edge << "," << signal << std::endl;
317
318 }
319
320 return tmp;
321 }
322
323 TRGSignal&
325 {
326
327 //...Check rising edges...
328 const unsigned n = m_history.size();
329 for (unsigned i = 0; i < n; i++) {
330 const bool edge = m_history[i].edge();
331
332 if (! edge and i > 0) {
333 const int t0 = m_history[i - 1].time();
334 const int t1 = m_history[i].time();
335 if ((t1 - t0) < int(width))
336 m_history[i].time(t0 + width);
337 }
338 }
339
340 // Merge overlapping history
341 std::sort(m_history.begin(), m_history.end(), TRGTime::sortByTime);
343
344#if TRG_DEBUG
346#endif
347
348 return * this;
349 }
350
351 std::vector<int>
353 {
354 std::vector<int> list;
355 const unsigned n = m_history.size();
356 list.reserve(n);
357 for (unsigned i = 0; i < n; i++)
358 list.push_back(m_history[i].time());
359 return list;
360 }
361
362 const TRGClock&
364 {
365 m_clock = & c;
366
367 const unsigned n = m_history.size();
368 for (unsigned i = 0; i < n; i++)
369 m_history[i].clock(c);
370
371 //...Check pulse width...
372 for (unsigned i = 0; i < n; i++) {
373 const bool edge = m_history[i].edge();
374
375 if (! edge) {
376 const int t0 = m_history[i - 1].time();
377 const int t1 = m_history[i].time();
378 const unsigned w = t1 - t0;
379 if (w == 0)
380 m_history[i].shift(1);
381 }
382 }
383
384#if TRG_DEBUG
386#endif
387
388 return * m_clock;
389 }
390
391 unsigned
392 TRGSignal::width(unsigned a) const
393 {
394 unsigned j = 0;
395 const unsigned n = m_history.size();
396 for (unsigned i = 0; i < n; i++) {
397 const bool edge = m_history[i].edge();
398
399 if (! edge) {
400 const int t0 = m_history[i - 1].time();
401 const int t1 = m_history[i].time();
402 const unsigned w = t1 - t0;
403 if (j == a) {
404 return w;
405 }
406 ++j;
407 }
408 }
409 return 0;
410 }
411
412 const TRGSignal&
413 TRGSignal::set(int t0, int t1, bool state)
414 {
415 if (! state)
416 return unset(t0, t1);
417
418 TRGSignal s(clock(), t0, t1);
419 (* this) |= s;
420
421#if TRG_DEBUG
422 if (consistencyCheck())
423 std::cout << "TRGSignal::set ... t0, t1=" << t0 << "," << t1 << std::endl;
424#endif
425
426 return * this;
427 }
428
429 const TRGSignal&
430 TRGSignal::unset(int t0, int t1)
431 {
432 if (active(t0, t1)) {
433 TRGSignal x(clock(), t0, t1);
434 x.invert();
435 (* this) &= x;
436 }
437
438#if TRG_DEBUG
440#endif
441
442 return * this;
443 }
444
445 void
447 {
448 std::sort(m_history.begin(), m_history.end(), TRGTime::sortByTime);
449
450#if TRG_DEBUG
452#endif
453 }
454
455 bool
457 {
458 const unsigned n = m_history.size();
459 if (n % 2) {
460 std::cout << "TRGSignal::consistencyCheck !!! "
461 << "history has odd number entries : n=" << n << std::endl;
462 dump("detail", "!!! ");
463 }
464
465 if (n < 2)
466 return true;
467
468 unsigned err = 0;
469 unsigned errTiming = 0;
470 for (unsigned i = 0; i < n; i++) {
471 if (i % 2)
472 continue;
473
474 const TRGTime& t0 = m_history[i];
475 const TRGTime& t1 = m_history[i + 1];
476
477 if (t0.edge() != true)
478 err |= (1 << i);
479 if (t1.edge() != false)
480 err |= (1 << (i + 1));
481
482 if ((t0.time() == t1.time()) || (t0.time() > t1.time()))
483 errTiming |= (1 << (i + 1));
484 }
485
486 if (err || errTiming) {
487 if (err) {
488 std::cout << "TRGSignal::consistencyCheck !!! err in edge history"
489 << std::endl;
490 }
491 if (errTiming) {
492 std::cout << "TRGSignal::consistencyCheck !!! err in time history"
493 << std::endl;
494 }
495 dump("detail", "!!! ");
496 return true;
497 }
498
499 return false;
500 }
501
502 const TRGSignal&
504 {
505 if (m_history.size()) {
506 for (unsigned i = 0; i < m_history.size(); i++)
507 m_history[i].reverse();
508 if (m_history[0].time() > m_clock->min()) {
509 TRGTime t0(m_clock->min(), true, * m_clock);
510 m_history.insert(m_history.begin(), t0);
511 } else {
512 m_history.erase(m_history.begin());
513 }
514 if (m_history.back().time() < m_clock->max()) {
515 TRGTime t0(m_clock->max(), false, * m_clock);
516 m_history.push_back(t0);
517 } else {
518 m_history.erase(m_history.end());
519 }
520 } else {
521 TRGTime time0(m_clock->min(), true, * m_clock);
522 TRGTime time1(m_clock->max(), false, * m_clock);
523 m_history.push_back(time0);
524 m_history.push_back(time1);
525 }
526
527#if TRG_DEBUG
529#endif
530
531 return * this;
532 }
533
534 bool
536 {
537 if (m_history.size() != a.m_history.size())
538 return false;
539
540 for (unsigned i = 0; i < m_history.size(); i++) {
541 if (m_history[i] != a.m_history[i])
542 return false;
543 }
544
545 return true;
546 }
547
549} // namespace Belle2
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
const TRGSignal & set(double t0, double t1)
makes a pulse with leading edge at t0 and with trailing edge at t1.
const TRGClock * m_clock
Clock.
Definition Signal.h:178
std::string m_name
Name.
Definition Signal.h:175
std::vector< TRGTime > m_history
Timing history.
Definition Signal.h:181
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:274
TRGSignal & operator&=(const TRGSignal &)
returns AND result.
Definition Signal.cc:210
static std::vector< TRGTime > orOperation(const std::vector< TRGTime > &)
Or operation.
Definition Signal.cc:294
TRGSignal operator&(const TRGSignal &) const
returns AND result.
Definition Signal.cc:162
const TRGClock & clock(void) const
returns clock.
Definition Signal.h:331
TRGSignal & widen(unsigned width)
returns widen signals. Signals wider than "width" will be untouched.
Definition Signal.cc:324
const std::string & name(void) const
returns name.
Definition Clock.h:157
std::vector< int > stateChanges(void) const
returns a list of clock position of state change.
Definition Signal.cc:352
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:430
unsigned width(unsigned i=0) const
returns width of i'th signal (i=0,1,2,...).
Definition Signal.cc:392
TRGSignal(const TRGClock &=Belle2_GDL::GDLSystemClock)
Constructor.
Definition Signal.cc:21
int time(void) const
returns timing in clock position.
Definition Time.h:171
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:128
bool active(void) const
returns true if there is a signal.
Definition Signal.h:277
bool edge(void) const
returns edge information.
Definition Time.h:156
bool operator==(const TRGSignal &) const
returns true if two are the same.
Definition Signal.cc:535
static bool sortByTime(const TRGTime &a, const TRGTime &b)
returns true if a is older than b.
Definition Time.cc:107
void sort(void)
Sort operation.
Definition Signal.cc:446
TRGSignal operator|(const TRGSignal &) const
returns OR result.
Definition Signal.cc:257
bool consistencyCheck(void) const
Self-consitency check. True is return if something wrong.
Definition Signal.cc:456
const TRGSignal & invert(void)
makes signal inverted.
Definition Signal.cc:503
void dump(const std::string &message="", const std::string &pre="") const
dumps contents.
Definition Signal.cc:136
Abstract base class for different kinds of events.