Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDAQSummaryFrame.hh
Go to the documentation of this file.
1 #ifndef __JDAQSUMMARYFRAME__
2 #define __JDAQSUMMARYFRAME__
3 
4 #include <limits>
5 #include <cmath>
6 #include <vector>
7 
8 #include "JDAQ/JDAQException.hh"
9 #include "JDAQ/JDAQ.hh"
10 #include "JDAQ/JDAQClock.hh"
12 #include "JDAQ/JDAQFrameStatus.hh"
13 #include "JDAQ/JDAQSuperFrame.hh"
14 
15 
16 /**
17  * \author mdejong
18  */
19 
20 namespace KM3NETDAQ {
21 
22 
23  /**
24  * Forward declaration for friend declaration of JDAQSummaryFrame inside JDAQRate.
25  */
26  class JDAQSummaryFrame;
27 
28 
29  /**
30  * Data storage class for rate measurement of one PMT.
31  *
32  * Note that the rate value is compressed.
33  * The number of bins and corresponding abscissa values can be obtained with
34  * methods JDAQRare::getN and JDAQRate::getData, respectively.
35  */
36  class JDAQRate {
37  public:
38 
39  friend class JDAQSummaryFrame;
40 
41 
42  typedef unsigned char JRate_t; // type of value to store rate
43 
44 
45  /**
46  * Get minimal rate (below this, value is set to zero)
47  *
48  * \return rate [Hz]
49  */
50  static double getMinimalRate()
51  {
52  return 2.0e3;
53  }
54 
55 
56  /**
57  * Get maximal rate (above this, value is set to maximum)
58  *
59  * \return rate [Hz]
60  */
61  static double getMaximalRate()
62  {
63  return 2.0e6;
64  }
65 
66 
67  /**
68  * Get value.
69  *
70  * \param numberOfHits number of hits
71  * \param frameTime_ns frame time [ns]
72  * \return value
73  */
74  static JRate_t getValue(const int numberOfHits, const double frameTime_ns)
75  {
76  return getValue(numberOfHits * 1.0e9 / frameTime_ns);
77  }
78 
79 
80  /**
81  * Get value.
82  *
83  * \param rate_Hz rate [Hz]
84  * \return value
85  */
86  static JRate_t getValue(const double rate_Hz)
87  {
88  if (rate_Hz <= getMinimalRate())
89  return 0;
90  else if (rate_Hz >= getMaximalRate())
91  return std::numeric_limits<JRate_t>::max();
92  else
93  return (JRate_t) (log(rate_Hz/getMinimalRate()) / getFactor() + 0.5);
94  }
95 
96 
97  /**
98  * Get count rate.
99  *
100  * \param value value
101  * \return rate [Hz]
102  */
103  static double getRate(const JRate_t value)
104  {
105  if (value == 0)
106  return 0.0;
107  else
108  return get_rate(value);
109  }
110 
111 
112  /**
113  * Get weight.
114  *
115  * \param value value
116  * \return weight [Hz^-1]
117  */
118  static double getWeight(const JRate_t value)
119  {
120  double W = 1.0;
121 
122  if (value == 0)
123  W = get_rate(0.5) - getMinimalRate();
124  else if (value == std::numeric_limits<JRate_t>::max())
125  W = getMaximalRate() - get_rate(std::numeric_limits<JRate_t>::max() - 0.5);
126  else
127  W = get_rate((double) value + 0.5) - get_rate((double) value - 0.5);
128 
129  return 1.0 / W;
130  }
131 
132 
133  /**
134  * Get number of bins.
135  *
136  * \return number of bins
137  */
138  static int getN()
139  {
140  return (int) std::numeric_limits<JRate_t>::max() + 1;
141  }
142 
143 
144  /**
145  * Get abscissa values.
146  *
147  * \param factor scaling factor
148  * \return abscissa values
149  */
150  static const double* getData(const double factor = 1.0)
151  {
152  static std::vector<double> buffer;
153 
154  buffer.clear();
155 
156  buffer.push_back(getMinimalRate() * factor);
157 
158  for (int i = 1; i != JDAQRate::getN(); ++i) {
159  buffer.push_back(get_rate(i - 0.5) * factor);
160  }
161 
162  buffer.push_back(getMaximalRate() * factor);
163 
164  return buffer.data();
165  }
166 
167 
168  /**
169  * Default constructor.
170  */
172  value(0)
173  {}
174 
175 
176  /**
177  * Get value.
178  *
179  * \return value
180  */
182  {
183  return value;
184  }
185 
186 
187  /**
188  * Set value.
189  *
190  * \param numberOfHits number of hits
191  * \param frameTime_ns frame time [ns]
192  */
193  void setValue(const int numberOfHits, const double frameTime_ns)
194  {
195  value = getValue(numberOfHits, frameTime_ns);
196  }
197 
198 
199  /**
200  * Set value.
201  *
202  * \param rate_Hz rate [Hz]
203  */
204  void setValue(const double rate_Hz)
205  {
206  value = getValue(rate_Hz);
207  }
208 
209 
210  /**
211  * Get count rate.
212  *
213  * \return rate [Hz]
214  */
215  double getRate() const
216  {
217  return getRate(value);
218  }
219 
220 
221  /**
222  * Get weight.
223  *
224  * \return weight [Hz^-1]
225  */
226  double getWeight() const
227  {
228  return getWeight(value);
229  }
230 
231 
232  /**
233  * Scale rate.
234  *
235  * \param factor multiplication factor
236  * \return this rate
237  */
238  JDAQRate& mul(const double factor)
239  {
240  setValue(getRate() * factor);
241 
242  return *this;
243  }
244 
245 
246  /**
247  * Scale rate.
248  *
249  * \param factor multiplication factor
250  * \return this rate
251  */
252  JDAQRate& div(const double factor)
253  {
254  setValue(getRate() / factor);
255 
256  return *this;
257  }
258 
259 
260  protected:
262 
263 
264  private:
265  /**
266  * Get conversion factor.
267  *
268  * \return factor
269  */
270  static const double getFactor()
271  {
272  return std::log(getMaximalRate() / getMinimalRate()) / std::numeric_limits<JRate_t>::max();
273  }
274 
275 
276  /**
277  * Get count rate.
278  *
279  * \param value value
280  * \return rate [Hz]
281  */
282  static double get_rate(const double value)
283  {
284  return getMinimalRate() * std::exp(value * getFactor());
285  }
286  };
287 
288 
289  /**
290  * Equal operator for DAQ rates.
291  *
292  * \param first rate
293  * \param second rate
294  * \result true if first rate equal to second; else false
295  */
296  inline bool operator==(const JDAQRate& first,
297  const JDAQRate& second)
298  {
299  return (first.getValue() == second.getValue());
300  }
301 
302 
303  /**
304  * Not-equal operator for DAQ rates.
305  *
306  * \param first rate
307  * \param second rate
308  * \result true if first rate not equal to second; else false
309  */
310  inline bool operator!=(const JDAQRate& first,
311  const JDAQRate& second)
312  {
313  return (first.getValue() != second.getValue());
314  }
315 
316 
317  /**
318  * Data storage class for rate measurements of all PMTs in one module.
319  */
321  public JDAQModuleIdentifier,
322  public JDAQFrameStatus
323  {
324  public:
325 
327 
328 
329  /**
330  * Default constructor.
331  */
335  {}
336 
337 
338  /**
339  * Constructor.
340  *
341  * \param id module identifier
342  */
346  {}
347 
348 
349  /**
350  * Constructor.
351  *
352  * Note that normally the rate is set to the number of hits per unit frame time
353  * but if either the high-rate veto or FIFO (almost) full bit is on,
354  * the rate is set to the number of hits divided by the time of the last hit.
355  *
356  * \param input super frame
357  */
361  {
362  using namespace std;
363 
364  vector<int> counter(NUMBER_OF_PMTS, 0);
365 
366  int n = input.size();
367 
368  for (JDAQSuperFrame::const_iterator i = input.begin(); n != 0; --n, ++i) {
369  ++counter[i->getPMT()];
370  }
371 
372  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
373  data[i].setValue(counter[i], getFrameTime());
374  }
375 
376  if (input.testHighRateVeto() || input.testFIFOStatus()) {
377 
378  typedef JDAQHit::JTDC_t JTDC_t;
379 
380  vector<JTDC_t> limit(NUMBER_OF_PMTS, numeric_limits<JTDC_t>::min());
381 
382  int n = input.size();
383 
384  for (JDAQSuperFrame::const_iterator i = input.begin(); n != 0; --n, ++i) {
385  if (i->getT() > limit[i->getPMT()]) {
386  limit[i->getPMT()] = i->getT();
387  }
388  }
389 
390  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
391  if (input.testHighRateVeto(i) || input.testFIFOStatus(i)) {
392  if (limit[i] != 0) {
393  data[i].setValue((double) counter[i] * 1.0e9 / (double) limit[i]);
394  }
395  }
396  }
397  }
398  }
399 
400 
401  /**
402  * Get DAQ rate of given PMT.
403  *
404  * \param tdc TDC
405  * \return JDAQRate
406  */
407  const JDAQRate& operator[](const int tdc) const
408  {
409  if (tdc >= 0 && tdc < NUMBER_OF_PMTS)
410  return data[tdc];
411  else
412  throw JDAQException("TDC out of range.");
413  }
414 
415 
416  /**
417  * Get DAQ rate of given PMT.
418  *
419  * \param tdc TDC
420  * \return JDAQRate
421  */
422  JDAQRate& operator[](const int tdc)
423  {
424  if (tdc >= 0 && tdc < NUMBER_OF_PMTS)
425  return data[tdc];
426  else
427  throw JDAQException("TDC out of range.");
428  }
429 
430 
431  /**
432  * Get count rate.
433  *
434  * \param tdc TDC
435  * \param factor scaling factor
436  * \return rate x scaling factor [Hz]
437  */
438  double getRate(const int tdc, const double factor = 1.0) const
439  {
440  return data[tdc].getRate() * factor;
441  }
442 
443 
444  /**
445  * Get weight.
446  *
447  * \param tdc TDC
448  * \param factor scaling factor
449  * \return weight / scaling factor [Hz^-1]
450  */
451  double getWeight(const int tdc, const double factor = 1.0) const
452  {
453  return data[tdc].getWeight() / factor;
454  }
455 
456 
457  /**
458  * Set count rate.
459  *
460  * \param tdc TDC
461  * \param rate_Hz rate [Hz]
462  */
463  void setRate(const int tdc, const double rate_Hz)
464  {
465  return data[tdc].setValue(rate_Hz);
466  }
467 
468 
469  /**
470  * Read DAQ summary frame from input.
471  *
472  * \param in JReader
473  * \param summary JDAQSummaryFrame
474  * \return JReader
475  */
476  friend inline JReader& operator>>(JReader& in, JDAQSummaryFrame& summary)
477  {
478  in >> static_cast<JDAQModuleIdentifier&>(summary);
479  in >> static_cast<JDAQFrameStatus&> (summary);
480 
481  in.read((char*) summary.data, NUMBER_OF_PMTS * sizeof(JRate_t));
482 
483  return in;
484  }
485 
486 
487  /**
488  * Write DAQ summary frame to output.
489  *
490  * \param out JWriter
491  * \param summary DAQSummaryFrame
492  * \return JWriter
493  */
494  friend inline JWriter& operator<<(JWriter& out, const JDAQSummaryFrame& summary)
495  {
496  out << static_cast<const JDAQModuleIdentifier&>(summary);
497  out << static_cast<const JDAQFrameStatus&> (summary);
498 
499  out.write((char*) summary.data, NUMBER_OF_PMTS * sizeof(JRate_t));
500 
501  return out;
502  }
503 
504 
505  /**
506  * Get size of object.
507  *
508  * \return number of bytes
509  */
510  static int sizeOf()
511  {
512  return (JDAQModuleIdentifier::sizeOf() +
514  NUMBER_OF_PMTS * sizeof(JDAQRate));
515  }
516 
517 
518  static int ROOT_IO_VERSION; //!< Streamer version of JDAQSummaryslice as obtained from ROOT file.
519 
520 
522 
523 
524  protected:
525 
527  };
528 
529 
530  /**
531  * Equal operator for DAQ summary frames.
532  *
533  * \param first summary frame
534  * \param second summary frame
535  * \result true if first summary frame equal to second; else false
536  */
537  inline bool operator==(const JDAQSummaryFrame& first,
538  const JDAQSummaryFrame& second)
539  {
540  if (first.getModuleIdentifier() == second.getModuleIdentifier() &&
541  first.getDAQFrameStatus() == second.getDAQFrameStatus()) {
542 
543  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
544  if (first[i] != second[i]) {
545  return false;
546  }
547  }
548 
549  return true;
550 
551  } else {
552 
553  return false;
554  }
555  }
556 
557 
558  /**
559  * Not-equal operator for DAQ summary frames.
560  *
561  * \param first summary frame
562  * \param second summary frame
563  * \result true if first summary frame not equal to second; else false
564  */
565  inline bool operator!=(const JDAQSummaryFrame& first,
566  const JDAQSummaryFrame& second)
567  {
568  return !(first == second);
569  }
570 }
571 
572 #endif
static double get_rate(const double value)
Get count rate.
bool operator==(const JDAQChronometer &first, const JDAQChronometer &second)
Equal operator for DAQ chronometers.
JDAQSummaryFrame()
Default constructor.
JDAQRate & mul(const double factor)
Scale rate.
Interface for binary output.
JDAQSummaryFrame(const JDAQModuleIdentifier &id)
Constructor.
int getModuleID() const
Get module identifier.
friend JReader & operator>>(JReader &in, JDAQSummaryFrame &summary)
Read DAQ summary frame from input.
double getRate(const int tdc, const double factor=1.0) const
Get count rate.
JDAQRate data[NUMBER_OF_PMTS]
static JRate_t getValue(const double rate_Hz)
Get value.
static const JDAQFrameStatus & getInstance()
Get reference to unique instance of this class object.
const JDAQRate & operator[](const int tdc) const
Get DAQ rate of given PMT.
JDAQRate & operator[](const int tdc)
Get DAQ rate of given PMT.
static double getMinimalRate()
Get minimal rate (below this, value is set to zero)
static int sizeOf()
Get size of object.
virtual int read(char *buffer, const int length)=0
Read byte array.
static int getN()
Get number of bins.
static int ROOT_IO_VERSION
Streamer version of JDAQSummaryslice as obtained from ROOT file.
virtual int write(const char *buffer, const int length)=0
Write byte array.
unsigned int JTDC_t
leading edge [ns]
Definition: JDAQHit.hh:41
JDAQSummaryFrame(const JDAQSuperFrame &input)
Constructor.
static double getRate(const JRate_t value)
Get count rate.
void setValue(const double rate_Hz)
Set value.
JDAQRate & div(const double factor)
Scale rate.
static double getWeight(const JRate_t value)
Get weight.
static const double * getData(const double factor=1.0)
Get abscissa values.
ClassDefNV(JDAQSummaryFrame, 2)
Hit data structure.
Definition: JDAQHit.hh:36
const_iterator begin() const
Definition: JDAQFrame.hh:139
JRate_t getValue() const
Get value.
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
Data storage class for rate measurements of all PMTs in one module.
double getWeight() const
Get weight.
Interface for binary input.
double getRate() const
Get count rate.
static int sizeOf()
Get size of object.
static double getMaximalRate()
Get maximal rate (above this, value is set to maximum)
bool testHighRateVeto() const
Test high-rate veto status.
Data storage class for rate measurement of one PMT.
JDAQRate()
Default constructor.
General exception.
friend JWriter & operator<<(JWriter &out, const JDAQSummaryFrame &summary)
Write DAQ summary frame to output.
static int sizeOf()
Get size of object.
void setValue(const int numberOfHits, const double frameTime_ns)
Set value.
static JRate_t getValue(const int numberOfHits, const double frameTime_ns)
Get value.
static const double getFactor()
Get conversion factor.
const JDAQModuleIdentifier & getModuleIdentifier() const
Get Module identifier.
void setRate(const int tdc, const double rate_Hz)
Set count rate.
KM3NeT DAQ constants, bit handling, etc.
bool operator!=(const JDAQChronometer &first, const JDAQChronometer &second)
Not-equal operator for DAQ chronometers.
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
const JDAQFrameStatus & getDAQFrameStatus() const
Get DAQ frame status.
int size() const
Definition: JDAQFrame.hh:157
Data frame of one optical module.
double getWeight(const int tdc, const double factor=1.0) const
Get weight.
bool testFIFOStatus() const
Test FIFO status.