Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTimesliceRouter.hh
Go to the documentation of this file.
1 #ifndef __JTRIGGER__JTIMESLICEROUTER__
2 #define __JTRIGGER__JTIMESLICEROUTER__
3 
4 #include <vector>
5 #include <iterator>
6 #include <utility>
7 #include <limits>
8 #include <algorithm>
9 
10 #include "JTools/JRouter.hh"
11 #include "JTools/JElement.hh"
12 #include "JTools/JGrid.hh"
14 #include "JDAQ/JDAQ.hh"
16 #include "JDAQ/JDAQSuperFrame.hh"
17 #include "JDAQ/JDAQTimeslice.hh"
18 #include "JDetector/JTimeRange.hh"
20 #include "JLang/JPointer.hh"
21 
22 
23 /**
24  * \author mdejong
25  */
26 
27 namespace JTRIGGER {}
28 namespace JPP { using namespace JTRIGGER; }
29 
30 namespace JTRIGGER {
31 
36  using JLANG::JPointer;
37 
38 
39  /**
40  * Router for fast addressing of hits in KM3NETDAQ::JDAQTimeslice data structure
41  * as a function of the optical module identifier and time.
42  * Note that the data in a frame from the same PMT are time ordered
43  * but the data from different PMTs are not necessarily time orderd.
44  * The fast routing is based on the assumption that the time difference
45  * between consecutive hits in the same frame is limited.
46  * The access speed is then determined by this time difference.
47  *
48  * The time slice router class comprises two internal look-up tables:
49  *
50  * - The first table consists of a one dimensional look-up table which is
51  * is used to map the module identifier to the index of the corresponding
52  * data frame in the time slice.
53  *
54  * - The second table consists of a two dimensional array.
55  * In this, the first index of matches with the index of the corresponding data frame in the time slice.
56  * Each first index is mapped to a one dimensional look-up table.
57  * This one dimensional look-up table maps an (uncalibrated) time <tt>t</tt>
58  * to a pair of indices <tt>(lpos,rpos)</tt>, where:
59  * -# <tt>lpos</tt> is the index of the first hit in the data frame with time >= <tt>t</tt>;
60  * -# <tt>rpos</tt> is the index of the last hit in the data frame with time < <tt>t</tt>;
61  */
63  public JLANG::JPointer<const JDAQTimeslice>
64  {
65  /**
66  * Auxiliary structure for indexing hits in a data frame.
67  */
68  struct JPair_t {
69  int lpos;
70  int rpos;
71  };
72 
73  public:
74 
78  typedef JDAQTimeslice::const_iterator const_iterator;
79 
80 
81  /**
82  * Constructor.
83  *
84  * \param numberOfBins number of bins of the hit look-up table
85  */
86  JTimesliceRouter(const int numberOfBins) :
87  number_of_bins(numberOfBins),
88  router(-1)
89  {}
90 
91 
92  /**
93  * Constructor.
94  *
95  * \param timeslice timeslice
96  * \param numberOfBins number of bins of the hit look-up table
97  */
98  JTimesliceRouter(const JDAQTimeslice& timeslice,
99  const int numberOfBins) :
100  number_of_bins(numberOfBins),
101  router(-1)
102  {
103  configure(timeslice);
104  }
105 
106 
107  /**
108  * Configure.
109  *
110  * \param timeslice timeslice
111  */
112  void configure(const JDAQTimeslice& timeslice)
113  {
114  using namespace std;
115  using namespace KM3NETDAQ;
116  using namespace JTOOLS;
117 
118 
119  this->set(&timeslice);
120 
121 
122  for (const_iterator i = this->get()->begin(); i != this->get()->end(); ++i) {
123  router.put(i->getModuleID(), distance(this->get()->begin(), i));
124  }
125 
126 
127  const JDAQHit::JTDC_t TMIN = std::numeric_limits<JDAQHit::JTDC_t>::min();
128  const JDAQHit::JTDC_t TMAX = std::numeric_limits<JDAQHit::JTDC_t>::max();
129 
130  double Tmin = getTimeSinceRTS(this->get()->getFrameIndex()) - 0.05 * getFrameTime(); // [ns]
131  double Tmax = getTimeSinceRTS(this->get()->getFrameIndex()) + 1.05 * getFrameTime(); // [ns]
132 
133  if (Tmin < TMIN) { Tmin = TMIN; }
134  if (Tmax > TMAX) { Tmax = TMAX; }
135 
136 
137  int number_of_elements = 2;
138 
139  for (const_iterator i = this->get()->begin(); i != this->get()->end(); ++i) {
140  if (i->size() > number_of_elements) {
141  number_of_elements = i->size();
142  }
143  }
144 
145  if (number_of_bins > 1 && number_of_bins < number_of_elements) {
146  number_of_elements = number_of_bins;
147  }
148 
149 
150  const JGrid<double> bounds(number_of_elements, Tmin, Tmax);
151 
152 
153  // temporary buffer to store bin edges with proper data type
154 
155  typedef vector<JDAQHit::JTDC_t> JBuffer_t;
156 
157  JBuffer_t buffer(bounds.getSize() + 1);
158 
159  for (int i = 0; i != bounds.getSize(); ++i) {
160  buffer[i] = (JDAQHit::JTDC_t) bounds.getX(i);
161  }
162 
163  buffer[bounds.getSize()] = TMAX;
164 
165 
166  table.resize(this->get()->size()); // same indexing as time slice
167 
168  const JDAQHit JDAQHitMin(0, TMIN, 0);
169  const JDAQHit JDAQHitMax(0, TMAX, 0);
170 
171  typedef std::vector<JDAQHit> JDAQFrame_t;
172 
173  JDAQFrame_t zbuf; // copy of data with begin and end marker
174 
175  vector<grid_type>::iterator grid = table.begin();
176 
177  for (const_iterator frame = this->get()->begin(); frame != this->get()->end(); ++frame, ++grid) {
178 
179  grid->configure(bounds);
180 
181  if (!frame->empty()) {
182 
183  if (frame-> begin()->getT() < grid->getXmin()) {
184  throw JTriggerException("Hit time out of range (lower limit).");
185  }
186 
187  if (frame->rbegin()->getT() > grid->getXmax()) {
188  throw JTriggerException("Hit time out of range (upper limit).");
189  }
190 
191  zbuf.resize(frame->size() + 2); // reserve space end markers
192 
193  copy(frame->begin(), frame->end(), zbuf.begin() + 1);
194 
195  *zbuf. begin() = JDAQHitMin;
196  *zbuf.rbegin() = JDAQHitMax;
197 
198  {
199  JDAQFrame_t::const_iterator hit = zbuf.begin(); ++hit; // skip begin marker
200  JBuffer_t ::const_iterator t1 = buffer.begin();
201 
202  for (grid_type::iterator i = grid->begin(); i != grid->end(); ++i, ++t1) {
203 
204  while (hit->getT() < *t1) { ++hit; }
205 
206  i->getY().lpos = distance(static_cast<const JDAQFrame_t&>(zbuf).begin(),hit) - 1;
207  }
208  }
209 
210  {
211  JDAQFrame_t::const_reverse_iterator hit = zbuf.rbegin(); ++hit; // skip end marker
212  JBuffer_t ::const_reverse_iterator t1 = buffer.rbegin();
213 
214  for (grid_type::reverse_iterator i = grid->rbegin(); i != grid->rend(); ++i, ++t1) {
215 
216  while (hit->getT() >= *t1) { ++hit; }
217 
218  i->getY().rpos = distance(static_cast<const JDAQFrame_t&>(zbuf).begin(),hit.base()) - 1;
219  }
220  }
221  }
222  }
223  }
224 
225 
226  /**
227  * Get address of module.
228  *
229  * \param module module
230  * \return address
231  */
232  const int getAddress(const JDAQModuleIdentifier& module) const
233  {
234  return router.get(module.getModuleID());
235  }
236 
237 
238  /**
239  * Check presence of module.
240  *
241  * \param module module
242  * \return true if module present; else false
243  */
244  bool hasSuperFrame(const JDAQModuleIdentifier& module) const
245  {
246  return router.has(module.getModuleID());
247  }
248 
249 
250  /**
251  * Get super frame.
252  *
253  * \param module module
254  * \return super frame
255  */
257  {
258  return (*(this->get()))[getAddress(module)];
259  }
260 
261 
262  /**
263  * Get subset of frame given module identifier and range of hit times.
264  *
265  * Note that the hit times should have been corrected for the maximal and minimal
266  * time offsets of the PMTs in the corresponding optical module.
267  *
268  * \param module module
269  * \param timeRange time range [ns]
270  * \return subset with begin and end iterators
271  */
272  JDAQFrameSubset getFrameSubset(const JDAQModuleIdentifier& module, const JTimeRange& timeRange) const
273  {
274  const int address = getAddress(module);
275 
276  const grid_type& grid = table [address];
277  const JDAQSuperFrame& frame = (*(this->get()))[address];
278 
279  const int first = grid.getIndex(timeRange.getLowerLimit());
280  const int second = grid.getIndex(timeRange.getUpperLimit());
281 
282  return frame.subset(grid.getY(first).lpos, grid.getY(second).rpos);
283  }
284 
285  private:
289  };
290 }
291 
292 #endif
bool hasSuperFrame(const JDAQModuleIdentifier &module) const
Check presence of module.
const JDAQSuperFrame & getSuperFrame(const JDAQModuleIdentifier &module) const
Get super frame.
2D Element.
Definition: JElement.hh:44
int getModuleID() const
Get module identifier.
std::vector< grid_type > table
JTimesliceRouter(const int numberOfBins)
Constructor.
The elements in a collection are sorted according to their abscissa values and a given distance opera...
void configure(const JDAQTimeslice &timeslice)
Configure.
JTOOLS::JRouter< int > router_type
JDAQTimeslice::const_iterator const_iterator
unsigned int JTDC_t
leading edge [ns]
Definition: JDAQHit.hh:41
JRange< double > JTimeRange
Type definition for time range.
Subset of data frame.
Definition: JDAQFrame.hh:28
JTOOLS::JGridCollection< element_type > grid_type
int getIndex(typename JClass< abscissa_type >::argument_type x) const
Get index of given abscissa value.
Hit data structure.
Definition: JDAQHit.hh:36
int getFrameIndex(const double t_ns)
Get frame index for a given time in ns.
Definition: JDAQClock.hh:251
Auxiliary structure for indexing hits in a data frame.
Router for fast addressing of hits in KM3NETDAQ::JDAQTimeslice data structure as a function of the op...
JDAQFrameSubset getFrameSubset(const JDAQModuleIdentifier &module, const JTimeRange &timeRange) const
Get subset of frame given module identifier and range of hit times.
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
virtual abscissa_type getX(int index) const
Get abscissa value.
Definition: JGrid.hh:81
General purpose class for collection of equidistant elements.
virtual int getSize() const
Get number of elements.
Definition: JGrid.hh:69
Template implementation of class that holds pointer to object(s).
Definition: JPointer.hh:22
Data time slice.
JTimesliceRouter(const JDAQTimeslice &timeslice, const int numberOfBins)
Constructor.
JTOOLS::JElement2D< double, JPair_t > element_type
const ordinate_type & getY(int index) const
Get ordinate value.
Definition: JCollection.hh:224
double getTimeSinceRTS(const int frame_index)
Get time in ns since last RTS for a given frame index.
Definition: JDAQClock.hh:263
const int getAddress(const JDAQModuleIdentifier &module) const
Get address of module.
collection_type::iterator iterator
Simple data structure for an abstract collection of equidistant abscissa values.
Definition: JGrid.hh:32
collection_type::reverse_iterator reverse_iterator
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:40
KM3NeT DAQ constants, bit handling, etc.
Data frame of one optical module.
JDAQFrameSubset subset(const int i1, const int i2) const
Get subset of data.
Definition: JDAQFrame.hh:181
virtual void set(const JDAQTimeslice *p)
Set pointer.
Definition: JPointer.hh:75