Jpp
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 
37 
38  /**
39  * Router for fast addressing of hits in KM3NETDAQ::JDAQTimeslice data structure
40  * as a function of the optical module identifier and time.
41  *
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.\n
44  * The fast routing is based on the assumption that the time difference
45  * between consecutive hits in the same frame is limited.\n
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 JPP;
116  using namespace KM3NETDAQ;
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 = numeric_limits<JDAQHit::JTDC_t>::min();
128  const JDAQHit::JTDC_t TMAX = 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  typedef vector<JDAQHit::JTDC_t> JBuffer_t; // temporary buffer to store bin edges with proper data type
154 
155  JBuffer_t buffer(bounds.getSize() + 1);
156 
157  for (int i = 0; i != bounds.getSize(); ++i) {
158  buffer[i] = (JDAQHit::JTDC_t) bounds.getX(i);
159  }
160 
161  buffer[bounds.getSize()] = TMAX;
162 
163 
164  table.resize(this->get()->size()); // same indexing as time slice
165 
166  const JDAQHit JDAQHitMin(0, TMIN, 0); // begin marker
167  const JDAQHit JDAQHitMax(0, TMAX, 0); // end marker
168 
169  typedef vector<JDAQHit> JDAQFrame_t;
170 
171  JDAQFrame_t zbuf; // copy of raw data with additional begin and end marker
172 
173  vector<grid_type>::iterator grid = table.begin();
174 
175  for (const_iterator frame = this->get()->begin(); frame != this->get()->end(); ++frame, ++grid) {
176 
177  grid->configure(bounds);
178 
179  if (!frame->empty()) {
180 
181  if (frame-> begin()->getT() < grid->getXmin()) {
182  THROW(JTriggerException, "Hit time out of range " << frame-> begin()->getT() << " < " << grid->getXmin());
183  }
184 
185  if (frame->rbegin()->getT() > grid->getXmax()) {
186  THROW(JTriggerException, "Hit time out of range " << frame->rbegin()->getT() << " > " << grid->getXmax());
187  }
188 
189  zbuf.resize(frame->size() + 2); // reserve space end markers
190 
191  copy(frame->begin(), frame->end(), zbuf.begin() + 1);
192 
193  *zbuf. begin() = JDAQHitMin;
194  *zbuf.rbegin() = JDAQHitMax;
195 
196  {
197  JDAQFrame_t::const_iterator hit = zbuf.begin(); ++hit; // skip begin marker
198  JBuffer_t ::const_iterator t1 = buffer.begin();
199 
200  for (grid_type::iterator i = grid->begin(); i != grid->end(); ++i, ++t1) {
201 
202  while (hit->getT() < *t1) { ++hit; }
203 
204  i->getY().lpos = distance(static_cast<const JDAQFrame_t&>(zbuf).begin(), hit) - 1;
205  }
206  }
207 
208  {
209  JDAQFrame_t::const_reverse_iterator hit = zbuf.rbegin(); ++hit; // skip end marker
210  JBuffer_t ::const_reverse_iterator t1 = buffer.rbegin();
211 
212  for (grid_type::reverse_iterator i = grid->rbegin(); i != grid->rend(); ++i, ++t1) {
213 
214  while (hit->getT() >= *t1) { ++hit; }
215 
216  i->getY().rpos = distance(static_cast<const JDAQFrame_t&>(zbuf).begin(), hit.base()) - 1;
217  }
218  }
219  }
220  }
221  }
222 
223 
224  /**
225  * Get address of module.
226  *
227  * \param module module
228  * \return address
229  */
230  const int getAddress(const JDAQModuleIdentifier& module) const
231  {
232  return router.get(module.getModuleID());
233  }
234 
235 
236  /**
237  * Check presence of module.
238  *
239  * \param module module
240  * \return true if module present; else false
241  */
242  bool hasSuperFrame(const JDAQModuleIdentifier& module) const
243  {
244  return router.has(module.getModuleID());
245  }
246 
247 
248  /**
249  * Get super frame.
250  *
251  * \param module module
252  * \return super frame
253  */
255  {
256  return (*(this->get()))[this->getAddress(module)];
257  }
258 
259 
260  /**
261  * Get subset of frame given module identifier and range of hit times.
262  *
263  * Note that the hit times should have been corrected for the maximal and minimal
264  * time offsets of the PMTs in the corresponding optical module.
265  *
266  * \param module module
267  * \param timeRange time range [ns]
268  * \return subset with begin and end iterators
269  */
270  JDAQFrameSubset getFrameSubset(const JDAQModuleIdentifier& module, const JTimeRange& timeRange) const
271  {
272  const int address = this->getAddress(module);
273 
274  const grid_type& grid = table [address];
275  const JDAQSuperFrame& frame = (*(this->get()))[address];
276 
277  const int first = grid.getIndex(timeRange.getLowerLimit());
278  const int second = grid.getIndex(timeRange.getUpperLimit());
279 
280  if (first < 0 || second >= grid.getSize()) {
281  THROW(JTriggerException, "Time range [ns] " << timeRange << " bounds " << grid.getXmin() << ' ' << grid.getXmax() << " indices " << first << ' ' << second);
282  }
283 
284  return frame.subset(grid.getY(first).lpos, grid.getY(second).rpos);
285  }
286 
287  private:
291  };
292 }
293 
294 #endif
JTRIGGER::JTimesliceRouter::JPair_t
Auxiliary structure for indexing hits in a data frame.
Definition: JTimesliceRouter.hh:68
JLANG::JPointer< const JDAQTimeslice >::set
virtual void set(const JDAQTimeslice *p)
Set pointer.
Definition: JPointer.hh:75
JTOOLS::JGridCollection::iterator
collection_type::iterator iterator
Definition: JGridCollection.hh:43
JTOOLS::JGridCollection
General purpose class for collection of equidistant elements.
Definition: JGridCollection.hh:30
JDAQ.hh
JElement.hh
JTRIGGER::JTimesliceRouter::router
router_type router
Definition: JTimesliceRouter.hh:289
JTRIGGER::JTimesliceRouter::grid_type
JTOOLS::JGridCollection< element_type > grid_type
Definition: JTimesliceRouter.hh:76
JLANG::JPointer< const JDAQTimeslice >::get
virtual const JDAQTimeslice * get() const
Get pointer.
Definition: JPointer.hh:64
JTRIGGER::JTimesliceRouter::getFrameSubset
JDAQFrameSubset getFrameSubset(const JDAQModuleIdentifier &module, const JTimeRange &timeRange) const
Get subset of frame given module identifier and range of hit times.
Definition: JTimesliceRouter.hh:270
JTOOLS::JCollection::getXmin
virtual abscissa_type getXmin() const
Get minimal abscissa value.
Definition: JCollection.hh:200
JTriggerException.hh
JGrid.hh
JTRIGGER::JTimesliceRouter::number_of_bins
int number_of_bins
Definition: JTimesliceRouter.hh:288
std::vector< JDAQHit::JTDC_t >
JTRIGGER::JTimesliceRouter::JTimesliceRouter
JTimesliceRouter(const JDAQTimeslice &timeslice, const int numberOfBins)
Constructor.
Definition: JTimesliceRouter.hh:98
KM3NETDAQ::JDAQModuleIdentifier::getModuleID
int getModuleID() const
Get module identifier.
Definition: JDAQModuleIdentifier.hh:72
KM3NETDAQ::JDAQTimeslice
Data time slice.
Definition: JDAQTimeslice.hh:36
distance
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Definition: PhysicsEvent.hh:434
JDAQTimeslice.hh
JAANET::copy
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:152
JTimeRange.hh
JTOOLS::JGridCollection::reverse_iterator
collection_type::reverse_iterator reverse_iterator
Definition: JGridCollection.hh:44
KM3NETDAQ::getFrameTime
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
JTOOLS::JTimeRange
JRange< double > JTimeRange
Type definition for time range.
Definition: JTools/JTimeRange.hh:19
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JLANG::JPointer
Template implementation of class that holds pointer to object(s).
Definition: JPointer.hh:22
JTOOLS::JCollection::getXmax
virtual abscissa_type getXmax() const
Get maximal abscissa value.
Definition: JCollection.hh:211
JTOOLS::JGridCollection::getIndex
int getIndex(typename JClass< abscissa_type >::argument_type x) const
Get index of given abscissa value.
Definition: JGridCollection.hh:66
JTOOLS::JRouter< int >
JTRIGGER::JTriggerException
General exception.
Definition: JTriggerException.hh:23
JTRIGGER::JTimesliceRouter::JPair_t::lpos
int lpos
Definition: JTimesliceRouter.hh:69
JTRIGGER::JTimesliceRouter::getSuperFrame
const JDAQSuperFrame & getSuperFrame(const JDAQModuleIdentifier &module) const
Get super frame.
Definition: JTimesliceRouter.hh:254
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:669
KM3NETDAQ::JDAQModuleIdentifier
Module identifier.
Definition: JDAQModuleIdentifier.hh:24
JTRIGGER::JTimesliceRouter::JTimesliceRouter
JTimesliceRouter(const int numberOfBins)
Constructor.
Definition: JTimesliceRouter.hh:86
KM3NETDAQ::JDAQFrame::subset
JDAQFrameSubset subset(const int i1, const int i2) const
Get subset of data.
Definition: JDAQFrame.hh:181
JTRIGGER::JTimesliceRouter::table
std::vector< grid_type > table
Definition: JTimesliceRouter.hh:290
KM3NETDAQ::JDAQFrameSubset
Subset of data frame.
Definition: JDAQFrame.hh:28
JTRIGGER::JTimesliceRouter::configure
void configure(const JDAQTimeslice &timeslice)
Configure.
Definition: JTimesliceRouter.hh:112
KM3NETDAQ::getTimeSinceRTS
double getTimeSinceRTS(const int frame_index)
Get time in ns since last RTS for a given frame index.
Definition: JDAQClock.hh:263
KM3NETDAQ::JDAQHit::JTDC_t
unsigned int JTDC_t
leading edge [ns]
Definition: JDAQHit.hh:45
JTRIGGER::JTimesliceRouter::getAddress
const int getAddress(const JDAQModuleIdentifier &module) const
Get address of module.
Definition: JTimesliceRouter.hh:230
std
Definition: jaanetDictionary.h:36
JGridCollection.hh
JDAQSuperFrame.hh
KM3NETDAQ
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
JTRIGGER::JTimesliceRouter::router_type
JTOOLS::JRouter< int > router_type
Definition: JTimesliceRouter.hh:77
JRouter.hh
KM3NETDAQ::JDAQSuperFrame
Data frame of one optical module.
Definition: JDAQSuperFrame.hh:27
JTOOLS::JCollection::getSize
virtual int getSize() const
Get number of elements.
Definition: JCollection.hh:177
JTRIGGER::JTimesliceRouter::JPair_t::rpos
int rpos
Definition: JTimesliceRouter.hh:70
KM3NETDAQ::JDAQHit
Hit data structure.
Definition: JDAQHit.hh:40
JDAQModuleIdentifier.hh
JTRIGGER
Checksum.
Definition: JSupport/JSupport.hh:35
JTOOLS::JElement2D
2D Element.
Definition: JElement.hh:44
JTRIGGER::JTimesliceRouter::hasSuperFrame
bool hasSuperFrame(const JDAQModuleIdentifier &module) const
Check presence of module.
Definition: JTimesliceRouter.hh:242
JPointer.hh
JTRIGGER::JTimesliceRouter::const_iterator
JDAQTimeslice::const_iterator const_iterator
Definition: JTimesliceRouter.hh:78
JTRIGGER::JTimesliceRouter::element_type
JTOOLS::JElement2D< double, JPair_t > element_type
Definition: JTimesliceRouter.hh:75
JTOOLS::JCollection::getY
const ordinate_type & getY(int index) const
Get ordinate value.
Definition: JCollection.hh:224
JTRIGGER::JTimesliceRouter
Router for fast addressing of hits in KM3NETDAQ::JDAQTimeslice data structure as a function of the op...
Definition: JTimesliceRouter.hh:62
KM3NETDAQ::getFrameIndex
int getFrameIndex(const double t_ns)
Get frame index for a given time in ns.
Definition: JDAQClock.hh:251