Jpp  16.0.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDetectorIntegration_t.hh
Go to the documentation of this file.
1 #ifndef __JDB__JDETECTORINTEGRATION_T__
2 #define __JDB__JDETECTORINTEGRATION_T__
3 
4 #include <istream>
5 #include <ostream>
6 #include <sstream>
7 #include <vector>
8 #include <map>
9 
10 #include "JTools/JRange.hh"
11 #include "JDB/JDB.hh"
13 #include "JDB/JPBS_t.hh"
14 #include "JDB/JUPI_t.hh"
15 #include "JDB/JDate_t.hh"
16 #include "JLang/JException.hh"
18 #include "JLang/JLangToolkit.hh"
19 
20 #include "dbclient/KM3NeTDBClient.h"
21 
22 
23 /**
24  * \author mdejong
25  */
26 namespace JDATABASE {}
27 namespace JPP { using namespace JDATABASE; }
28 
29 namespace JDATABASE {
30 
32  using JLANG::JException;
33 
34 
35  /**
36  * Type definition of period.
37  */
39 
40 
41  /**
42  * Write period to output stream.
43  *
44  * \param out output stream
45  * \param object period
46  * \return output stream
47  */
48  inline std::ostream& operator<<(std::ostream& out, const JPeriod_t& object)
49  {
50  return out << "(" << object.getLowerLimit() << "," << object.getUpperLimit() << ")";
51  }
52 
53 
54  /**
55  * Auxiliary class for product integration data.
56  *
57  * This class is primarily used to
58  * - convert the string data from JDATABASE::JProductIntegration to corresponding data structures;
59  * - implement efficient (in)equality operations;
60  */
62  /**
63  * Default constructor.
64  */
66  status(false)
67  {}
68 
69 
70  /**
71  * Constructor.
72  *
73  * \param input product integration data
74  */
76  status(false)
77  {
78  using namespace std;
79 
80  this->operation = input.OPERATIONID;
81  this->position = input.POSITION;
82  this->container = input.CONTAINER_UPI;
83  this->content = input.CONTENT_UPI;
84 
85  istringstream(input.STARTD) >> this->start_date;
86  istringstream(input.ENDD) >> this->end_date;
87  }
88 
89 
90  /**
91  * Check product status.
92  *
93  * \return true if set; else false
94  */
95  bool is_set() const
96  {
97  return status;
98  }
99 
100 
101  /**
102  * Set product status.
103  */
104  void set()
105  {
106  status = true;
107  }
108 
109 
110  /**
111  * Unset product status.
112  */
113  void unset()
114  {
115  status = false;
116  }
117 
118 
119  /**
120  * Write product integration to output stream.
121  *
122  * \param out output stream
123  * \param object product integration
124  * \return output stream
125  */
126  friend inline std::ostream& operator<<(std::ostream& out, const JProductIntegration_t& object)
127  {
128  using namespace std;
129 
130  out << object.operation << ' '
131  << object.container.getUPI() << ' '
132  << object.content .getUPI() << ' '
133  << object.position << ' '
134  << object.start_date << ' '
135  << object.end_date;
136 
137  return out;
138  }
139 
140 
141  std::string operation;
142  int position;
147  bool status;
148  };
149 
150 
151  /**
152  * Detector integration.
153  *
154  * This class is used
155  * - to read product integration data from the data base;
156  * - to organise detector specific integration data in memory;
157  * - to efficiently %trace a product;
158  * - to efficiently %find product(s);
159  *
160  * Note that the method configure should be used to set up the integration data for a specific detector.
161  */
163  public std::vector<JProductIntegration_t>
164  {
165  static const char* const getName() { return JProductIntegration::getName(); } //!< Table name
166 
168  typedef std::pair<map_type::const_iterator,
169  map_type::const_iterator> range_type;
170  typedef map_type::const_iterator range_const_iterator;
171  typedef map_type::const_iterator range_iterator;
172 
173 
174  /**
175  * Default constructor.
176  */
178  {}
179 
180 
181  /**
182  * Configure detector integration for given detector identifier.
183  *
184  * The components of the given detector are selected based on the following rules:
185  * -# the component should have a start date before that of the given detector;
186  * -# if multiple components appear at the same place in the same container,
187  * the one with the latest start date is taken.
188  *
189  * \param detid detector identifier
190  */
191  void configure(const std::string& detid)
192  {
193  using namespace std;
194 
195  up .clear();
196  down.clear();
197 
198  for (size_t index = 0; index != this->size(); ++index) {
199 
200  JProductIntegration_t& product = (*this)[index];
201 
202  product.unset();
203 
204  up .insert(make_pair(product.content .getUPI(), index));
205  down.insert(make_pair(product.container.getUPI(), index));
206  }
207 
208  for (iterator product = this->begin(); product != this->end(); ++product) {
209  if (product->operation == detid) {
210  configure(*product, JPeriod_t(product->start_date, product->end_date));
211  }
212  }
213 
214  up .clear();
215  down.clear();
216 
217  for (size_t index = 0; index != this->size(); ++index) {
218 
219  const JProductIntegration_t& product = (*this)[index];
220 
221  if (product.is_set()) {
222  up .insert(make_pair(product.content .getUPI(), index));
223  down.insert(make_pair(product.container.getUPI(), index));
224  }
225  }
226  }
227 
228 
229  /**
230  * Trace product up to given integration level.
231  *
232  * \param upi %UPI
233  * \param pbs %PBS
234  * \return product
235  */
236  const JProductIntegration_t& trace(const JUPI_t& upi,
237  const JPBS_t& pbs = PBS::DETECTOR) const
238  {
239  using namespace std;
240 
241  if (upi.getPBS() >= pbs) {
242 
243  const range_type range = find(upi);
244 
245  switch (distance(range.first, range.second)) {
246 
247  case 1:
248  {
249  const JProductIntegration_t& product = (*this)[range.first->second];
250 
251  if (product.container.getPBS() == pbs)
252  return product;
253  else
254  return trace(product.container.getUPI(), pbs);
255  }
256 
257  case 0:
258  THROW(JDatabaseException, "Invalid UPI " << upi);
259 
260  default:
261  THROW(JDatabaseException, "Ambiguous integration of UPI " << upi);
262  }
263 
264  } else {
265 
266  THROW(JDatabaseException, "Invalid PBS " << upi.getPBS() << " < " << pbs);
267  }
268  }
269 
270 
271  /**
272  * Print product trace.
273  *
274  * \param out output stream
275  * \param upi %UPI
276  */
277  void print(std::ostream& out, const JUPI_t& upi) const
278  {
279  using namespace std;
280 
282 
283  range = find(upi);
284 
285  for (map_type::const_iterator i = range.first; i != range.second; ++i) {
286 
287  out << upi << " -> ";
288 
289  print(out, (*this)[i->second]);
290  }
291 
292  range = down.equal_range(upi);
293 
294  for (range_iterator i = range.first; i != range.second; ++i) {
295  out << "<- " << (*this)[i->second] << endl;
296  }
297  }
298 
299 
300  /**
301  * Print product trace.
302  *
303  * \param out output stream
304  * \param product product
305  */
306  void print(std::ostream& out, const JProductIntegration_t& product) const
307  {
308  using namespace std;
309 
310  out << product.container.getUPI() << ' '
311  //<< JPeriod_t(product.start_date, product.end_date) << ' '
312  << "-> ";
313 
314  const range_type range = find(product.container.getUPI());
315 
316  for (map_type::const_iterator i = range.first; i != range.second; ++i) {
317  print(out, (*this)[i->second]);
318  }
319 
320  if (distance(range.first, range.second) != 1) {
321  out << product.operation << std::endl;
322  }
323  }
324 
325 
326  /**
327  * Read detector integration from result set.
328  *
329  * \param rs result set
330  * \param detector detector
331  * \return true if read; else false
332  */
333  friend inline bool operator>>(ResultSet& rs, JDetectorIntegration_t& detector)
334  {
335  using namespace std;
336 
337  for (JProductIntegration parameters; rs >> parameters; ) {
338 
339  const JProductIntegration_t product(parameters);
340 
341  detector.up .insert(make_pair(product.content .getUPI(), detector.size()));
342  detector.down.insert(make_pair(product.container.getUPI(), detector.size()));
343 
344  detector.push_back(product);
345  }
346 
347  rs.Close();
348 
349  return true;
350  }
351 
352 
353  /**
354  * Read detector integration from input stream.
355  *
356  * The input should be conform output of <tt>JPrintDB -q "integration" -c ";" -W1</tt>.
357  *
358  * \param in input stream
359  * \param detector detector integration
360  * \return input stream
361  */
362  friend inline std::istream& operator>>(std::istream& in, JDetectorIntegration_t& detector)
363  {
364  using namespace std;
365  using namespace JPP;
366 
367  in.ignore(numeric_limits<streamsize>::max(), '\n');
368 
369  for (string buffer; getline(in, buffer); ) {
370 
371  istringstream is(buffer);
372 
373  const locale loc(is.getloc(), new JWhiteSpacesFacet(is.getloc(), ";"));
374 
375  is.imbue(loc);
376 
378 
379  is >> buffer; istringstream(trim(buffer)) >> parameters.OPERATIONID;
380  is >> buffer; istringstream(trim(buffer)) >> parameters.CITY;
381  is >> buffer; istringstream(trim(buffer)) >> parameters.LOCATIONID;
382  is >> buffer; istringstream(trim(buffer)) >> parameters.LOGIN;
383  is >> buffer; istringstream(trim(buffer)) >> parameters.USERID;
384  is >> buffer; istringstream(trim(buffer)) >> parameters.STARTD;
385  is >> buffer; istringstream(trim(buffer)) >> parameters.ENDD;
386  is >> buffer; istringstream(trim(buffer)) >> parameters.CONTAINER_UPI;
387  is >> buffer; istringstream(trim(buffer)) >> parameters.POSITION;
388  is >> buffer; istringstream(trim(buffer)) >> parameters.CONTENT_UPI;
389 
390  const JProductIntegration_t product(parameters);
391 
392  detector.up .insert(make_pair(product.content .getUPI(), detector.size()));
393  detector.down.insert(make_pair(product.container.getUPI(), detector.size()));
394 
395  detector.push_back(product);
396  }
397 
398  return in;
399  }
400 
401 
402  /**
403  * Write detector integration to output stream.
404  *
405  * \param out output stream
406  * \param object detector integration
407  * \return output stream
408  */
409  friend inline std::ostream& operator<<(std::ostream& out, const JDetectorIntegration_t& object)
410  {
411  for (const_iterator i = object.begin(); i != object.end(); ++i) {
412  out << *i << std::endl;
413  }
414 
415  return out;
416  }
417 
418 
419  /**
420  * Find range of products with given %UPI.\n
421  * The returned range corresponds to the usual begin and end iterators,
422  * each pointing to an STL pair consisting of a %UPI and index.
423  *
424  * \param upi %UPI
425  * \return index
426  */
427  range_type find(const JUPI_t& upi) const
428  {
429  return up.equal_range(upi);
430  }
431 
432 
433  /**
434  * Find range of products with given %PBS.\n
435  * The returned range corresponds to the usual begin and end iterators,
436  * each pointing to an STL pair consisting of a %UPI and index.
437  *
438  * \param pbs %PBS
439  * \return range
440  */
441  range_type find(const JPBS_t& pbs) const
442  {
443  range_const_iterator p = up.lower_bound(JUPI_t(pbs));
444  range_const_iterator q = p;
445 
446  while (q != up.end() && (*this)[q->second].content.getPBS() <= pbs) {
447  ++q;
448  }
449 
450  return range_type(p,q);
451  }
452 
453 
454  /**
455  * Get components of product with given %UPI.\n
456  * The returned range corresponds to the usual begin and end iterators,
457  * each pointing to an STL pair consisting of a %UPI and index.
458  *
459  * \param upi %UPI
460  * \return index
461  */
462  range_type get(const JUPI_t& upi) const
463  {
464  return down.equal_range(upi);
465  }
466 
467 
468  protected:
469  /**
470  * Configure detector integration for given detector identifier.\n
471  * This method recursively sets the status all related products.
472  *
473  * \param product product
474  * \param period validity period
475  */
476  void configure(JProductIntegration_t& product, const JPeriod_t& period)
477  {
478  using namespace std;
479 
480  product.set();
481 
482  map<int, int> zmap; // position -> index
483 
484  const range_type range = down.equal_range(product.content);
485 
486  for (range_iterator i = range.first; i != range.second; ++i) {
487 
488  const JProductIntegration_t& object = (*this)[i->second];
489 
490  if (object.start_date <= period.getLowerLimit()) {
491  if (zmap.count(object.position) == 0 || object.start_date > (*this)[zmap[object.position]].start_date) {
492  zmap[object.position] = i->second;
493  }
494  }
495  }
496 
497  for (map<int, int>::iterator i = zmap.begin(); i != zmap.end(); ++i) {
498  configure((*this)[i->second], period);
499  }
500  }
501 
502  map_type up; //!< up link %UPI to integration data
503  map_type down; //!< down link %UPI to integration data
504  };
505 }
506 
507 #endif
General exception.
Definition: JException.hh:23
Exceptions.
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:202
void unset()
Unset product status.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
JProductIntegration_t(const JProductIntegration &input)
Constructor.
Database exception.
Definition: JException.hh:666
friend std::ostream & operator<<(std::ostream &out, const JDetectorIntegration_t &object)
Write detector integration to output stream.
map_type up
up link UPI to integration data
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
void configure(JProductIntegration_t &product, const JPeriod_t &period)
Configure detector integration for given detector identifier.
void print(std::ostream &out, const JUPI_t &upi) const
Print product trace.
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
is
Definition: JDAQCHSM.chsm:167
range_type find(const JPBS_t &pbs) const
Find range of products with given PBS.
JProductIntegration_t()
Default constructor.
const JPBS_t & getPBS() const
Get PBS.
Definition: JPBS_t.hh:99
const JUPI_t & getUPI() const
Get UPI.
Definition: JUPI_t.hh:97
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
std::pair< map_type::const_iterator, map_type::const_iterator > range_type
map_type::const_iterator range_const_iterator
static const char *const getName()
Table name.
const JProductIntegration_t & trace(const JUPI_t &upi, const JPBS_t &pbs=PBS::DETECTOR) const
Trace product up to given integration level.
JTOOLS::JRange< JDate_t > JPeriod_t
Type definition of period.
friend bool operator>>(ResultSet &rs, JDetectorIntegration_t &detector)
Read detector integration from result set.
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
range_type find(const JUPI_t &upi) const
Find range of products with given UPI.
std::multimap< JUPI_t, int > map_type
print
Definition: JConvertDusj.sh:44
Range of values.
Definition: JRange.hh:38
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
friend std::istream & operator>>(std::istream &in, JDetectorIntegration_t &detector)
Read detector integration from input stream.
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
map_type down
down link UPI to integration data
Auxiliary class to define a range between two values.
bool is_set() const
Check product status.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Product breakdown structure (PBS).
Definition: JPBS_t.hh:25
set_variable DETECTOR
do set_variable DETECTOR_TXT $WORKDIR detector
static const char *const getName()
Table name.
char * loc(char *orig)
friend std::ostream & operator<<(std::ostream &out, const JProductIntegration_t &object)
Write product integration to output stream.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:42
void configure(const std::string &detid)
Configure detector integration for given detector identifier.
void print(std::ostream &out, const JProductIntegration_t &product) const
Print product trace.
Auxiliary class for product integration data.