Belle II Software  release-08-01-10
Clock.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 <limits>
10 #include <iostream>
11 #include <cmath>
12 #include "trg/trg/Clock.h"
13 #include "trg/trg/Time.h"
14 #include "trg/trg/Signal.h"
15 #include "trg/trg/Utilities.h"
16 
17 using namespace std;
18 
19 namespace Belle2_GDL {
20 
21  //...This should be moved to GDL module...
22  const Belle2::TRGClock GDLSystemClock("GDL system clock", 0, 125.000);
23 }
24 
25 namespace Belle2 {
31  TRGClock::TRGClock(const string& name,
32  double offset,
33  double frequency)
34  : _name(name),
35  _source(0),
36  _multi(1),
37  _div(1),
38  _offset(offset),
39  _frequency(frequency),
40  _cycle(1000 / frequency),
41 // _min(int(TRGTime::min() / frequency)),
42 // _max(int(TRGTime::max() / frequency)),
43 // _min(numeric_limits<int>::min() + 100),
44 // _max(numeric_limits<int>::max() - 100),
45  _min(numeric_limits<int>::min() / 16),
46  _max(numeric_limits<int>::max() / 16),
47  _clockCounter(0)
48  {
49 
50  if (this != & Belle2_GDL::GDLSystemClock) {
51  if (Belle2_GDL::GDLSystemClock.minTiming() > minTiming())
52  _min = int((Belle2_GDL::GDLSystemClock.minTiming() - _offset)
53  / _cycle);
54  if (Belle2_GDL::GDLSystemClock.maxTiming() < maxTiming())
55  _max = int((Belle2_GDL::GDLSystemClock.maxTiming() - _offset)
56  / _cycle);
57  }
58  }
59 
60  TRGClock::TRGClock(const string& name,
61  const TRGClock& source,
62  unsigned multiplicationFactor,
63  unsigned divisionFactor)
64  : _name(name),
65  _source(& source),
66  _multi(multiplicationFactor),
67  _div(divisionFactor),
68  _offset(source._offset),
69  _frequency(source._frequency *
70  double(multiplicationFactor) /
71  double(divisionFactor)),
72  _cycle(source._cycle /
73  double(multiplicationFactor) *
74  double(divisionFactor)),
75  _min(source._min * int(multiplicationFactor) / int(divisionFactor)),
76  _max(source._max * int(multiplicationFactor) / int(divisionFactor)),
77  _clockCounter(0)
78  {
79  }
80 
82  {
83  if (_clockCounter)
84  delete _clockCounter;
85  }
86 
87  void
88  TRGClock::dump(const string&,
89  const string& pre) const
90  {
91  cout << pre << _name;
92  if (_source) {
93  cout << ":sync'd to " << _source->name() << endl;
94  cout << pre << " multiplication factor=" << _multi << endl;
95  cout << pre << " division factor =" << _div << endl;
96  } else {
97  cout << endl;
98  }
99 
100  cout << pre << " offset :" << _offset << endl
101  << pre << " freq(MHz):" << _frequency << endl
102  << pre << " cycle(ns):" << _cycle << endl
103  << pre << " min pos :" << _min << endl
104  << pre << " max pos :" << _max << endl
105  << pre << " min(ns) :" << minTiming() << endl
106  << pre << " max(ns) :" << maxTiming() << endl;
107 
108  cout << pre << " numeric limit of int (min,max="
109  << numeric_limits<int>::min() << ","
110  << numeric_limits<int>::max() << ")" << endl;
111  }
112 
113  int
114  TRGClock::position(double t) const
115  {
116 #ifdef TRG_DEBUG
117  if ((t < minTiming()) || (t > maxTiming()))
118  cout << "TRGClock::unit(" << _name
119  << ") !!! out of time window : min=" << minTiming()
120  << ",max=" << maxTiming() << ",given value=" << t << endl;
121 // cout << "t,offset,unit=" << t << "," << _offset << "," << int((t
122 // - _offset) / _cycle) << endl;
123 #endif
124  return floor((t - _offset) / _cycle);
125  }
126 
127  double
129  {
130  return double(t) * _cycle + _offset;
131  }
132 
133  double
134  TRGClock::overShoot(double t) const
135  {
136  return (t - _offset) - _cycle * double(position(t));
137  }
138 
139  TRGTime
140  TRGClock::minTRGTime(bool edge) const
141  {
142  TRGTime a(minTiming(), edge, * this, _name + "_min");
143  return a;
144  }
145 
146  TRGTime
147  TRGClock::maxTRGTime(bool edge) const
148  {
149  TRGTime a(maxTiming(), edge, * this, _name + "_max");
150  return a;
151  }
152 
153  double
154  TRGClock::phase(double a) const
155  {
156 // return overShoot(a) / _cycle * 360;
157  const double pos = (a - _offset) / _cycle;
158  const double pos0 = double(int(pos));
159  const double dif = pos - pos0;
160  // std::cout << "a,offset,pos,pos0,dif=" << a << "," << _offset << ","
161  // << pos << "," << pos0 << "," << dif << std::endl;
162  return dif * 360;
163  }
164 
165  const TRGSignalVector&
167  {
168 
169  if (_clockCounter)
170  return * _clockCounter;
171 
172  _clockCounter = new TRGSignalVector(name() + " counter", * this);
173 
174  //...5 bit clock counter...
175  unsigned cicle = 2;
176  for (int i = 0; i < 5; i++) {
177  TRGSignal s("tmp", * this);
178 
179  //...From 0 to 1280*5...
180 // const int maxN = 1280 * 5;
181  const int maxN = 400;
182  for (int j = 0; j < maxN; j++) {
183  if (((j - cicle / 2) % cicle) == 0)
184  s.set(j, j + cicle / 2);
185  }
186 
187  //...Append this bit...
188  s.name("ClockCounterBit" + TRGUtilities::itostring(i));
189  (* _clockCounter) += s;
190 
191  //...Double cicle...
192  cicle *= 2;
193  }
194 
195  return * _clockCounter;
196  }
197 
199 } // namespace Belle2
A class to represent a digitized signal. Unit is nano second.
Definition: Clock.h:38
const double _offset
Clock offset in nano second.
Definition: Clock.h:128
const unsigned _div
Division factor.
Definition: Clock.h:125
const std::string _name
Name.
Definition: Clock.h:116
const double _cycle
Clock cycle in nano second.
Definition: Clock.h:134
int _max
Clock max. count.
Definition: Clock.h:140
const double _frequency
Frequency in MHz.
Definition: Clock.h:131
int _min
Clock min. count.
Definition: Clock.h:137
const unsigned _multi
Multiplication factor.
Definition: Clock.h:122
const TRGClock * _source
Clock source.
Definition: Clock.h:119
TRGSignalVector * _clockCounter
Clock counter.
Definition: Clock.h:143
A class to represent a bundle of digitized signals.
Definition: SignalVector.h:26
A class to represent a digitized signal. Unit is nano second.
Definition: Signal.h:23
A class to represent a signal timing in the trigger system.
Definition: Time.h:25
double absoluteTime(int clockPosition) const
returns absolute time of clock position
Definition: Clock.cc:128
double phase(double timing) const
returns phase of given timing in degree (0 to 360).
Definition: Clock.cc:154
const std::string & name(void) const
returns name.
Definition: Clock.h:157
static std::string itostring(int i)
converts int to string. (Use boost::lexical_cast)
Definition: Utilities.cc:50
double maxTiming(void) const
returns max. timing.
Definition: Clock.h:172
double overShoot(double timing) const
returns over shoot.
Definition: Clock.cc:134
TRGClock(const std::string &name, double offset, double frequency)
Constructor. "offset" is in unit of ns. "frequency" is in unit of MHz.
Definition: Clock.cc:31
const TRGSignalVector & clockCounter(void) const
returns the clock counter.
Definition: Clock.cc:166
int position(double timing) const
returns clock position.
Definition: Clock.cc:114
TRGTime minTRGTime(bool edge) const
returns min. TRGtime with clock.
Definition: Clock.cc:140
virtual ~TRGClock()
Destructor.
Definition: Clock.cc:81
TRGTime maxTRGTime(bool edge) const
returns max. TRGtime with clock.
Definition: Clock.cc:147
double minTiming(void) const
returns min. timing.
Definition: Clock.h:164
void dump(const std::string &message="", const std::string &pre="") const
dumps contents.
Definition: Clock.cc:88
Abstract base class for different kinds of events.