Belle II Software  release-08-01-10
TOPSampleTimes.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 <top/dbobjects/TOPSampleTimes.h>
10 
11 #include <framework/logging/Logger.h>
12 
13 #include <iostream>
14 
15 using namespace std;
16 
17 namespace Belle2 {
23  void TOPSampleTimes::setTimeAxis(double syncTimeBase)
24  {
25  double DTime = 2 * syncTimeBase;
26  double timeBin = DTime / static_cast<double>(c_TimeAxisSize);
27  for (unsigned i = 0; i < c_TimeAxisSize; i++) m_timeAxis[i] = timeBin * i;
28  m_timeAxis[c_TimeAxisSize] = DTime;
29  m_calibrated = c_Default;
30  }
31 
32 
33  void TOPSampleTimes::setTimeAxis(const std::vector<double>& sampleTimes,
34  double syncTimeBase)
35  {
36  if (sampleTimes.size() < c_TimeAxisSize) {
37  B2FATAL("TOPSampleTimes::setTimeAxis: vector too short");
38  return;
39  }
40 
41  for (unsigned i = 0; i < c_TimeAxisSize; i++) m_timeAxis[i] = sampleTimes[i];
42  double DTime = 2 * syncTimeBase;
43  m_timeAxis[c_TimeAxisSize] = DTime;
44  m_calibrated = c_Calibrated;
45  }
46 
47 
48  std::vector<double> TOPSampleTimes::getTimeAxis() const
49  {
50  std::vector<double> timeAxis;
51  for (unsigned i = 0; i < c_TimeAxisSize + 1; i++) {
52  timeAxis.push_back(m_timeAxis[i]);
53  }
54  return timeAxis;
55  }
56 
57 
58  double TOPSampleTimes::getFullTime(int window, double sample) const
59  {
60 
61  int sampleNum = int(sample);
62  if (sample < 0) sampleNum--;
63  double frac = sample - sampleNum;
64 
65  sampleNum += window * c_WindowSize; // counted from window 0
66  int n = sampleNum / c_TimeAxisSize;
67  int k = sampleNum % c_TimeAxisSize;
68  if (k < 0) {
69  n--;
70  k += c_TimeAxisSize;
71  }
72 
73  double time = n * getTimeRange() + m_timeAxis[k]; // from sample 0 window 0
74  time += (m_timeAxis[k + 1] - m_timeAxis[k]) * frac; // add fraction
75 
76  return time;
77  }
78 
79 
80  double TOPSampleTimes::getSample(int window, double time) const
81  {
82  time += window * getTimeRange() / 4.0;
83  int n = int(time / getTimeRange());
84  if (time < 0) n--;
85 
86  double t = time - getTimeRange() * n;
87  int i1 = 0;
88  int i2 = c_TimeAxisSize;
89  while (i2 - i1 > 1) {
90  int i = (i1 + i2) / 2;
91  if (t > m_timeAxis[i]) {
92  i1 = i;
93  } else {
94  i2 = i;
95  }
96  }
97 
98  return (n * c_TimeAxisSize - window * c_WindowSize + i1 +
99  (t - m_timeAxis[i1]) / (m_timeAxis[i2] - m_timeAxis[i1]));
100 
101  }
102 
103 
104  double TOPSampleTimes::getTimeBin(int window, int sampleNumber) const
105  {
106  int i = (window * c_WindowSize + sampleNumber) % c_TimeAxisSize;
107  if (i < 0) i += c_TimeAxisSize;
108  return m_timeAxis[i + 1] - m_timeAxis[i];
109  }
110 
112 } // end Belle2 namespace
113 
Abstract base class for different kinds of events.