Jpp
 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"
17 
18 #include "dbclient/KM3NeTDBClient.h"
19 
20 
21 /**
22  * \author mdejong
23  */
24 namespace JDATABASE {}
25 namespace JPP { using namespace JDATABASE; }
26 
27 namespace JDATABASE {
28 
30  using JLANG::JException;
31 
32 
33  /**
34  * Type definition of period.
35  */
37 
38 
39  /**
40  * Write period to output stream.
41  *
42  * \param out output stream
43  * \param object period
44  * \return output stream
45  */
46  inline std::ostream& operator<<(std::ostream& out, const JPeriod_t& object)
47  {
48  return out << "(" << object.getLowerLimit() << "," << object.getUpperLimit() << ")";
49  }
50 
51 
52  /**
53  * Auxiliary class for product integration data.
54  *
55  * This class is primarily used to
56  * - convert the string data from JDATABASE::JProductIntegration to corresponding data structures;
57  * - implement efficient (in)equality operations;
58  */
60  /**
61  * Default constructor.
62  */
64  status(false)
65  {}
66 
67 
68  /**
69  * Constructor.
70  *
71  * \param input product integration data
72  */
74  status(false)
75  {
76  using namespace std;
77 
78  this->operation = input.OPERATIONID;
79  this->position = input.POSITION;
80  this->container = input.CONTAINER_UPI;
81  this->content = input.CONTENT_UPI;
82 
83  istringstream(input.STARTD) >> this->start_date;
84  istringstream(input.ENDD) >> this->end_date;
85  }
86 
87 
88  /**
89  * Has status.
90  */
91  bool has() const
92  {
93  return status;
94  }
95 
96 
97  /**
98  * Set status.
99  */
100  void set()
101  {
102  status = true;
103  }
104 
105 
106  /**
107  * Unset status.
108  */
109  void unset()
110  {
111  status = false;
112  }
113 
114 
115  /**
116  * Check whether there is an overlap between products.
117  *
118  * The overlap is evaluated based on container UPI and position.
119  *
120  * \param product product
121  * \return true if this product overlaps with given product; else false
122  */
123  bool overlap(const JProductIntegration_t& product) const
124  {
125  return (this->container == product.container &&
126  this->position == product.position);
127  }
128 
129 
130  /**
131  * Write product integration to output stream.
132  *
133  * \param out output stream
134  * \param object product integration
135  * \return output stream
136  */
137  friend inline std::ostream& operator<<(std::ostream& out, const JProductIntegration_t& object)
138  {
139  using namespace std;
140 
141  out << object.operation << ' '
142  << object.container.getUPI() << ' '
143  << object.content .getUPI() << ' '
144  << object.position << ' '
145  << object.start_date << ' '
146  << object.end_date;
147 
148  return out;
149  }
150 
151 
152  std::string operation;
153  int position;
158  bool status;
159  };
160 
161 
162  /**
163  * Detector integration.
164  *
165  * This class is used
166  * - to read product integration data from the data base;
167  * - to organise detector specific integration data in memory;
168  * - to efficiently %trace a product;
169  * - to efficiently %find product(s);
170  *
171  * Note that the method configure should be used to set up the integration data for a specific detector.
172  */
174  public std::vector<JProductIntegration_t>
175  {
176  static const char* const getName() { return JProductIntegration::getName(); } //!< Table name
177 
179  typedef std::pair<map_type::const_iterator,
180  map_type::const_iterator> range_type;
181  typedef map_type::const_iterator range_const_iterator;
182  typedef map_type::const_iterator range_iterator;
183 
184 
185  /**
186  * Default constructor.
187  */
189  {}
190 
191 
192  /**
193  * Configure detector integration for given detector identifier.
194  *
195  * The components of the given detector are selected based on the following rules:
196  * -# the component should have a start date before that of the given detector;
197  * -# if multiple components appear at the same place in the same container,
198  * the one with the latest start date is taken.
199  *
200  * \param detid detector identifier
201  */
202  void configure(const std::string& detid)
203  {
204  using namespace std;
205 
206  up .clear();
207  down.clear();
208 
209  for (size_t index = 0; index != this->size(); ++index) {
210 
211  JProductIntegration_t& product = (*this)[index];
212 
213  product.unset();
214 
215  up .insert(make_pair(product.content .getUPI(), index));
216  down.insert(make_pair(product.container.getUPI(), index));
217  }
218 
219  for (iterator product = this->begin(); product != this->end(); ++product) {
220  if (product->operation == detid) {
221  configure(*product, JPeriod_t(product->start_date, product->end_date));
222  }
223  }
224 
225  up .clear();
226  down.clear();
227 
228  for (size_t index = 0; index != this->size(); ++index) {
229 
230  const JProductIntegration_t& product = (*this)[index];
231 
232  if (product.has()) {
233  up .insert(make_pair(product.content .getUPI(), index));
234  down.insert(make_pair(product.container.getUPI(), index));
235  }
236  }
237  }
238 
239 
240  /**
241  * Trace product up to given integration level.
242  *
243  * \param upi %UPI
244  * \param pbs %PBS
245  * \return product
246  */
247  const JProductIntegration_t& trace(const JUPI_t& upi,
248  const JPBS_t& pbs = PBS::DETECTOR) const
249  {
250  using namespace std;
251 
252  if (upi.getPBS() >= pbs) {
253 
254  const range_type range = find(upi);
255 
256  switch (distance(range.first, range.second)) {
257 
258  case 1:
259  {
260  const JProductIntegration_t& product = (*this)[range.first->second];
261 
262  if (product.container.getPBS() == pbs)
263  return product;
264  else
265  return trace(product.container.getUPI(), pbs);
266  }
267 
268  case 0:
269  THROW(JDatabaseException, "Invalid UPI " << upi);
270 
271  default:
272  THROW(JDatabaseException, "Ambiguous integration of UPI " << upi);
273  }
274 
275  } else {
276 
277  THROW(JDatabaseException, "Invalid PBS " << upi.getPBS() << " < " << pbs);
278  }
279  }
280 
281 
282  /**
283  * Print product trace.
284  *
285  * \param out output stream
286  * \param upi %UPI
287  */
288  void print(std::ostream& out, const JUPI_t& upi) const
289  {
290  using namespace std;
291 
293 
294  range = find(upi);
295 
296  for (map_type::const_iterator i = range.first; i != range.second; ++i) {
297 
298  out << upi << " -> ";
299 
300  print(out, (*this)[i->second]);
301  }
302 
303  range = down.equal_range(upi);
304 
305  for (range_iterator i = range.first; i != range.second; ++i) {
306  out << "<- " << (*this)[i->second] << endl;
307  }
308  }
309 
310 
311  /**
312  * Print product trace.
313  *
314  * \param out output stream
315  * \param product product
316  */
317  void print(std::ostream& out, const JProductIntegration_t& product) const
318  {
319  using namespace std;
320 
321  out << product.container.getUPI() << ' '
322  //<< JPeriod_t(product.start_date, product.end_date) << ' '
323  << "-> ";
324 
325  const range_type range = find(product.container.getUPI());
326 
327  for (map_type::const_iterator i = range.first; i != range.second; ++i) {
328  print(out, (*this)[i->second]);
329  }
330 
331  if (distance(range.first, range.second) != 1) {
332  out << product.operation << std::endl;
333  }
334  }
335 
336 
337  /**
338  * Read detector integration from result set.
339  *
340  * \param rs result set
341  * \param detector detector
342  * \return true if read; else false
343  */
344  friend inline bool operator>>(ResultSet& rs, JDetectorIntegration_t& detector)
345  {
346  using namespace std;
347 
348  for (JProductIntegration parameters; rs >> parameters; ) {
349 
350  const JProductIntegration_t product(parameters);
351 
352  detector.up .insert(make_pair(product.content .getUPI(), detector.size()));
353  detector.down.insert(make_pair(product.container.getUPI(), detector.size()));
354 
355  detector.push_back(product);
356  }
357 
358  rs.Close();
359 
360  return true;
361  }
362 
363 
364  /**
365  * Write detector integration to output stream.
366  *
367  * \param out output stream
368  * \param object detector integration
369  * \return output stream
370  */
371  friend inline std::ostream& operator<<(std::ostream& out, const JDetectorIntegration_t& object)
372  {
373  for (const_iterator i = object.begin(); i != object.end(); ++i) {
374  out << *i << std::endl;
375  }
376 
377  return out;
378  }
379 
380 
381  /**
382  * Find range of products with given %UPI.\n
383  * The returned range corresponds to the usual begin and end iterators,
384  * each pointing to an STL pair consisting of a %UPI and index.
385  *
386  * \param upi %UPI
387  * \return index
388  */
389  range_type find(const JUPI_t& upi) const
390  {
391  return up.equal_range(upi);
392  }
393 
394 
395  /**
396  * Find range of products with given %PBS.\n
397  * The returned range corresponds to the usual begin and end iterators,
398  * each pointing to an STL pair consisting of a %UPI and index.
399  *
400  * \param pbs %PBS
401  * \return range
402  */
403  range_type find(const JPBS_t& pbs) const
404  {
405  range_const_iterator p = up.lower_bound(JUPI_t(pbs));
406  range_const_iterator q = p;
407 
408  while (q != up.end() && (*this)[q->second].content.getPBS() <= pbs) {
409  ++q;
410  }
411 
412  return range_type(p,q);
413  }
414 
415 
416  /**
417  * Get components of product with given %UPI.\n
418  * The returned range corresponds to the usual begin and end iterators,
419  * each pointing to an STL pair consisting of a %UPI and index.
420  *
421  * \param upi %UPI
422  * \return index
423  */
424  range_type get(const JUPI_t& upi) const
425  {
426  return down.equal_range(upi);
427  }
428 
429 
430  protected:
431  /**
432  * Configure detector integration for given detector identifier.\n
433  * This method sets the status all related products.
434  *
435  * \param product product
436  * \param period validity period
437  */
438  void configure(JProductIntegration_t& product, const JPeriod_t& period)
439  {
440  using namespace std;
441 
442  product.set();
443 
444  vector<int> buffer;
445 
446  range_type range = down.equal_range(product.content);
447 
448  for (range_iterator i = range.first; i != range.second; ++i) {
449 
450  if ((*this)[i->second].start_date <= period.getLowerLimit()) {
451 
452  int ns = 0;
453 
454  for (vector<int>::iterator p = buffer.begin(); p != buffer.end(); ++p) {
455 
456  if ((*this)[*p].overlap((*this)[i->second])) {
457 
458  ++ns;
459 
460  if ((*this)[i->second].start_date > (*this)[*p].start_date) {
461  *p = i->second;
462  }
463  }
464  }
465 
466  if (ns == 0) {
467  buffer.push_back(i->second);
468  }
469  }
470  }
471 
472  for (vector<int>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
473  configure((*this)[*i], period);
474  }
475  }
476 
477  map_type up; //!< up link %UPI to integration data
478  map_type down; //!< down link %UPI to integration data
479  };
480 }
481 
482 #endif
set_variable DETECTOR
General exception.
Definition: JException.hh:23
Exceptions.
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:202
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:648
friend std::ostream & operator<<(std::ostream &out, const JDetectorIntegration_t &object)
Write detector integration to output stream.
bool overlap(const JProductIntegration_t &product) const
Check whether there is an overlap between products.
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:670
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
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::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.
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 mv $WORKDIR/fit.root $MODULE_ROOT typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
Definition: module-Z:fit.sh:84
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.
then $JPP_DIR software JDB JAsciiDB q upi pbs
Definition: JTuneHV.sh:149
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Product breakdown structure (PBS).
Definition: JPBS_t.hh:25
do set_variable DETECTOR_TXT $WORKDIR detector
static const char *const getName()
Table name.
friend std::ostream & operator<<(std::ostream &out, const JProductIntegration_t &object)
Write product integration to output stream.
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.