Jpp - the software that should make you happy
 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 
32  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  *
43  * Note that the data in a frame from the same PMT are time ordered
44  * but the data from different PMTs are not necessarily time orderd.\n
45  * The fast routing is based on the assumption that the time difference
46  * between consecutive hits in the same frame is limited.\n
47  * The access speed is then determined by this time difference.
48  *
49  * The time slice router class comprises two internal look-up tables:
50  *
51  * - The first table consists of a one dimensional look-up table which is
52  * is used to map the module identifier to the index of the corresponding
53  * data frame in the time slice.
54  *
55  * - The second table consists of a two dimensional array.
56  * In this, the first index of matches with the index of the corresponding data frame in the time slice.
57  * Each first index is mapped to a one dimensional look-up table.
58  * This one dimensional look-up table maps an uncalibrated time <tt>t</tt>
59  * to a pair of indices <tt>(lpos,rpos)</tt>, where:
60  * -# <tt>lpos</tt> is the index of the first hit in the data frame with time <tt>>= t</tt>;
61  * -# <tt>rpos</tt> is the index of the last hit in the data frame with time <tt>< t</tt>;
62  */
64  public JPointer<const JDAQTimeslice>
65  {
66  /**
67  * Auxiliary structure for indexing hits in a data frame.
68  */
69  struct JPair_t {
70  int lpos;
71  int rpos;
72  };
73 
74  public:
75 
79  typedef JDAQTimeslice::const_iterator const_iterator;
80 
81 
82  /**
83  * Constructor.
84  *
85  * \param numberOfBins number of bins of the hit look-up table
86  */
88  number_of_bins(numberOfBins),
89  router(-1)
90  {}
91 
92 
93  /**
94  * Constructor.
95  *
96  * \param timeslice timeslice
97  * \param numberOfBins number of bins of the hit look-up table
98  */
99  JTimesliceRouter(const JDAQTimeslice& timeslice,
100  const int numberOfBins) :
101  number_of_bins(numberOfBins),
102  router(-1)
103  {
104  configure(timeslice);
105  }
106 
107 
108  /**
109  * Configure.
110  *
111  * \param timeslice timeslice
112  */
113  void configure(const JDAQTimeslice& timeslice)
114  {
115  using namespace std;
116  using namespace JPP;
117  using namespace KM3NETDAQ;
118 
119 
120  this->set(&timeslice);
121 
122 
123  for (const_iterator i = this->get()->begin(); i != this->get()->end(); ++i) {
124  router.put(i->getModuleID(), distance(this->get()->begin(), i));
125  }
126 
127 
128  const JDAQHit::JTDC_t TMIN = numeric_limits<JDAQHit::JTDC_t>::min();
129  const JDAQHit::JTDC_t TMAX = numeric_limits<JDAQHit::JTDC_t>::max();
130 
131  double Tmin = getTimeSinceRTS(this->get()->getFrameIndex()) - 0.05 * getFrameTime(); // [ns]
132  double Tmax = getTimeSinceRTS(this->get()->getFrameIndex()) + 1.05 * getFrameTime(); // [ns]
133 
134  if (Tmin < TMIN) { Tmin = TMIN; }
135  if (Tmax > TMAX) { Tmax = TMAX; }
136 
137 
138  int number_of_elements = 2;
139 
140  for (const_iterator i = this->get()->begin(); i != this->get()->end(); ++i) {
141  if (i->size() > number_of_elements) {
142  number_of_elements = i->size();
143  }
144  }
145 
146  if (number_of_bins > 1 && number_of_bins < number_of_elements) {
147  number_of_elements = number_of_bins;
148  }
149 
150 
151  const JGrid<double> bounds(number_of_elements, Tmin, Tmax);
152 
153 
154  typedef vector<JDAQHit::JTDC_t> JBuffer_t; // temporary buffer to store bin edges with proper data type
155 
156  JBuffer_t buffer(bounds.getSize() + 1);
157 
158  for (int i = 0; i != bounds.getSize(); ++i) {
159  buffer[i] = (JDAQHit::JTDC_t) bounds.getX(i);
160  }
161 
162  buffer[bounds.getSize()] = TMAX;
163 
164 
165  table.resize(this->get()->size()); // same indexing as time slice
166 
167  const JDAQHit JDAQHitMin(0, TMIN, 0); // begin marker
168  const JDAQHit JDAQHitMax(0, TMAX, 0); // end marker
169 
170  typedef vector<JDAQHit> JDAQFrame_t;
171 
172  JDAQFrame_t zbuf; // copy of raw data with additional begin and end marker
173 
174  vector<grid_type>::iterator grid = table.begin();
175 
176  for (const_iterator frame = this->get()->begin(); frame != this->get()->end(); ++frame, ++grid) {
177 
178  grid->configure(bounds);
179 
180  if (!frame->empty()) {
181 
182  if (frame-> begin()->getT() < grid->getXmin()) {
183  THROW(JTriggerException, "Hit time out of range " << frame-> begin()->getT() << " < " << grid->getXmin());
184  }
185 
186  if (frame->rbegin()->getT() > grid->getXmax()) {
187  THROW(JTriggerException, "Hit time out of range " << frame->rbegin()->getT() << " > " << grid->getXmax());
188  }
189 
190  zbuf.resize(frame->size() + 2); // reserve space end markers
191 
192  copy(frame->begin(), frame->end(), zbuf.begin() + 1);
193 
194  *zbuf. begin() = JDAQHitMin;
195  *zbuf.rbegin() = JDAQHitMax;
196 
197  {
198  JDAQFrame_t::const_iterator hit = zbuf.begin(); ++hit; // skip begin marker
199  JBuffer_t ::const_iterator t1 = buffer.begin();
200 
201  for (grid_type::iterator i = grid->begin(); i != grid->end(); ++i, ++t1) {
202 
203  while (hit->getT() < *t1) { ++hit; }
204 
205  i->getY().lpos = distance(static_cast<const JDAQFrame_t&>(zbuf).begin(), hit) - 1;
206  }
207  }
208 
209  {
210  JDAQFrame_t::const_reverse_iterator hit = zbuf.rbegin(); ++hit; // skip end marker
211  JBuffer_t ::const_reverse_iterator t1 = buffer.rbegin();
212 
213  for (grid_type::reverse_iterator i = grid->rbegin(); i != grid->rend(); ++i, ++t1) {
214 
215  while (hit->getT() >= *t1) { ++hit; }
216 
217  i->getY().rpos = distance(static_cast<const JDAQFrame_t&>(zbuf).begin(), hit.base()) - 1;
218  }
219  }
220  }
221  }
222  }
223 
224 
225  /**
226  * Get address of module.
227  *
228  * \param module module
229  * \return address
230  */
231  const int getAddress(const JDAQModuleIdentifier& module) const
232  {
233  return router.get(module.getModuleID());
234  }
235 
236 
237  /**
238  * Check presence of module.
239  *
240  * \param module module
241  * \return true if module present; else false
242  */
243  bool hasSuperFrame(const JDAQModuleIdentifier& module) const
244  {
245  return router.has(module.getModuleID());
246  }
247 
248 
249  /**
250  * Get super frame.
251  *
252  * \param module module
253  * \return super frame
254  */
256  {
257  return (*(this->get()))[this->getAddress(module)];
258  }
259 
260 
261  /**
262  * Get subset of frame given module identifier and range of hit times.
263  *
264  * Note that the hit times should have been corrected for the maximal and minimal
265  * time offsets of the PMTs in the corresponding optical module.
266  *
267  * \param module module
268  * \param timeRange time range [ns]
269  * \return subset with begin and end iterators
270  */
271  JDAQFrameSubset getFrameSubset(const JDAQModuleIdentifier& module, const JTimeRange& timeRange) const
272  {
273  const int address = this->getAddress(module);
274 
275  const grid_type& grid = table [address];
276  const JDAQSuperFrame& frame = (*(this->get()))[address];
277 
278  const int first = grid.getIndex(timeRange.getLowerLimit());
279  const int second = grid.getIndex(timeRange.getUpperLimit());
280 
281  if (first < 0 || second >= grid.getSize()) {
282  THROW(JTriggerException, "Time range [ns] " << timeRange << " bounds " << grid.getXmin() << ' ' << grid.getXmax() << " indices " << first << ' ' << second);
283  }
284 
285  return frame.subset(grid.getY(first).lpos, grid.getY(second).rpos);
286  }
287 
288  private:
292  };
293 }
294 
295 #endif
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.
JTOOLS::JRange< double > JTimeRange
Type definition for time range (unit [ns]).
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
virtual abscissa_type getXmin() const override
Get minimal abscissa value.
Definition: JCollection.hh:220
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
virtual abscissa_type getXmax() const override
Get maximal abscissa value.
Definition: JCollection.hh:231
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
General purpose class for collection of equidistant elements.
virtual abscissa_type getX(int index) const override
Get abscissa value.
Definition: JGrid.hh:81
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:244
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
virtual int getSize() const override
Get number of elements.
Definition: JGrid.hh:69
collection_type::reverse_iterator reverse_iterator
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:139
2D Element.
Definition: JElement.hh:46
KM3NeT DAQ constants, bit handling, etc.
Data frame of one optical module.
virtual void set(JClass_t *p) override
Set pointer.
Definition: JPointer.hh:75
int numberOfBins
number of bins for average CDF integral of optical module
Definition: JSirene.cc:64
JDAQFrameSubset subset(const int i1, const int i2) const
Get subset of data.
Definition: JDAQFrame.hh:178
virtual int getSize() const override
Get number of elements.
Definition: JCollection.hh:197