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"
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
virtual abscissa_type getXmin() const
Get minimal abscissa value.
Definition: JCollection.hh:219
bool hasSuperFrame(const JDAQModuleIdentifier &module) const
Check presence of module.
const JDAQSuperFrame & getSuperFrame(const JDAQModuleIdentifier &module) const
Get super frame.
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
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
virtual int getSize() const
Get number of elements.
Definition: JCollection.hh:196
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JDAQTimeslice::const_iterator const_iterator
unsigned int JTDC_t
leading edge [ns]
Definition: JDAQHit.hh:39
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
JRange< double > JTimeRange
Type definition for time range.
Subset of data frame.
Definition: JDAQFrame.hh:22
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:34
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:243
double getTimeSinceRTS(const int frame_index)
Get time in ns since last RTS for a given frame index.
Definition: JDAQClock.hh:263
virtual abscissa_type getXmax() const
Get maximal abscissa value.
Definition: JCollection.hh:230
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:153
2D Element.
Definition: JElement.hh:44
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:178
virtual void set(const JDAQTimeslice *p)
Set pointer.
Definition: JPointer.hh:75