tic  130
tinamath.hxx
Go to the documentation of this file.
1 #ifndef _TINA_TINAMATH_HXX
2 #define _TINA_TINAMATH_HXX
3 
4 #include <assert.h>
5 #include <iostream>
6 #include <iomanip>
7 //#include <sys/types.h>
8 #include <string.h>
9 #include <limits>
10 #include <map>
11 #include <deque>
12 
13 
14 namespace INdepTina100 {
15 
24  class Event {
25  friend class EventStream;
26 
27  unsigned _cEpoch; /* epoch counter */
28  unsigned _cCoarse; /* coarse counter of event */
29  unsigned _cFract; /* fract of event */
30  public:
31  unsigned cCoarse() const { return _cCoarse; }
32 
33  friend bool operator==(const Event &evL, const Event &evR) {
34  return (evL._cCoarse == evR._cCoarse) && (evL._cFract == evR._cFract) ;
35  }
36 
37  /*
38  *
39  * REMIND
40  * IEEE-754 double precision is limited to 15 decimal digits
41  * Precision error analysis required.
42  *
43  */
44  double td(double rdFreq, unsigned cFracts, unsigned ccEpochs) {
45  return (1.0/rdFreq)*((double)_cEpoch*(double)ccEpochs + (double)_cCoarse + ((double(_cFract)/(double(cFracts)))));
46  }
47  Event( unsigned cEpoch=0, unsigned cCoarse=0, unsigned cFract=0 ):_cEpoch(cEpoch), _cCoarse(cCoarse), _cFract(cFract) { }
48  ~Event( ) { }
49  };
50 
54  class EventStream {
55  /* quasi const */
56  unsigned _ccEpochs; /* coarse counts per epoch */
57  unsigned _ccFracts; /* fract counter fractions */
58  unsigned _ccScale; /* prescaler */
59  double _crdFreq; /* coarse counter frequency */
60 
61  unsigned _cEpochCur; /* current epoch */
62  Event _evPrev; /* previous event */
63  Event _evCur; /* current event */
64 
65 
66 
67  public:
68  void reset() {
69  _cEpochCur=0;
70  _evCur=_evPrev=Event();
71  }
72 
73  unsigned cEpochCur() const { return _cEpochCur; }
74 
75  double rdFreq();
77  void epochTick () {
78  _cEpochCur++;
79  assert(_cEpochCur); /* no overflow */
80  }
81 
82  unsigned cDiff() const { return _evCur._cCoarse - _evPrev._cCoarse; }
83 
84  void sampleTick(const Event &ev) {
85  _evPrev=_evCur;
86  _evCur=ev;
87  }
88 
89  EventStream(unsigned cEpochs, unsigned cFracts, unsigned ccScale, double rdFreq):_ccEpochs(cEpochs), _ccFracts(cFracts), _ccScale(ccScale), _crdFreq(rdFreq) {
90  reset();
91  }
92  ~EventStream() { }
93  };
94 
95  class Statistics {
96  typedef std::deque<double> Movav;
97  typedef std::deque<double>::const_iterator IcMovav;
98  typedef std::map<double, unsigned> Histogram;
99  typedef std::map<double, unsigned>::iterator ItHist;
100 
101  Movav _ma; /* moving average */
102  Histogram _hi;
103  double _rdMin;
104  double _rdMax;
105  public:
106  void sample(double rd) {
107  if (rd < _rdMin) _rdMin=rd;
108  if (rd > _rdMax) _rdMax=rd;
109  ItHist it=_hi.lower_bound(rd);
110 
111  //std::cout << std::setprecision(9);
112  if (it!=_hi.end()) {
113  it->second++;
114  //std::cout << rd << " " << it->first << " " << it->second;
115  }
116  else {
117  //std::cout << rd << " ?";
118  }
119  //std::cout << std::endl;
120  if (_ma.size() == 50) _ma.pop_front();
121  _ma.push_back(rd);
122  }
123 
124  double rdMovav() {
125  double rd=0;
126  for (IcMovav ic=_ma.begin(); ic !=_ma.end(); ++ic) {
127  rd += *ic;
128  }
129  return rd/((double)_ma.size());
130  }
131 
132  std::ostream &print(std::ostream & os) {
133  for (ItHist it=_hi.begin(); it !=_hi.end(); ++it) {
134  os << std::setprecision(9);
135  os << it->first << " " << it->second << '\n';
136  }
137  return os;
138  }
139  Statistics(double rdMin, double rdWidth, unsigned c) {
140  double rd;
141  unsigned i;
142  for (i=0, rd=rdMin; i<c; i++, rd+=rdWidth) {
143  _hi[rd]=0;
144  }
145  _rdMin=(std::numeric_limits<double>::max)();
146  _rdMax=(-1.0)*(std::numeric_limits<double>::max)();
147  }
148  ~Statistics() { }
149  };
150 
151 
152 
153  inline double EventStream::rdFreq() {
154  if (_evPrev == _evCur) return 0.0;
155 
156  double tdCur, tdPrev, tdDiff, tdPeriod;
157 
158  tdCur=_evCur.td(_crdFreq, _ccFracts, _ccEpochs);
159  tdPrev=_evPrev.td(_crdFreq, _ccFracts, _ccEpochs);
160  tdDiff=tdCur-tdPrev;
161  tdPeriod=tdDiff/(double)_ccScale;
162  return 1.0/tdPeriod;
163  }
164 
165  inline double rdPpm(double rdFreq, double rdRef) {
166  return (1.00-(rdRef/rdFreq)) * 1E06;
167  }
168 }
169 
170 
171 #endif
stream of Events from the timing hardware
Definition: tinamath.hxx:54
An Event as captured by the timing hardware.
Definition: tinamath.hxx:24
double rdFreq()
current frequency, 0 = not available
Definition: tinamath.hxx:153
Definition: tinamath.hxx:95
namespace for the ines Timing Analyzer (Tina) interface
Definition: protocols.cxx:7