Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JProductRouter.hh
Go to the documentation of this file.
1 #ifndef __JDB__JPRODUCTROUTER__
2 #define __JDB__JPRODUCTROUTER__
3 
4 #include <ostream>
5 #include <map>
6 
8 #include "JDB/JPBS_t.hh"
9 #include "JDB/JUPI_t.hh"
10 #include "JDB/JPBSSequence.hh"
11 #include "JLang/JComparable.hh"
12 #include "Jeep/JPrint.hh"
13 
14 
15 /**
16  * \author mdejong
17  */
18 namespace JDATABASE {}
19 namespace JPP { using namespace JDATABASE; }
20 
21 namespace JDATABASE {
22 
23  using JLANG::JComparable;
24 
25 
26  /**
27  * Auxiliary data structure for location of product in detector.
28  */
29  struct JLocation_t :
30  public JComparable<JLocation_t>
31  {
32  /**
33  * Default constructor.\n
34  * The default corresponds to an invalid location.
35  */
37  string (-1),
38  floor (-1),
39  position(-1)
40  {}
41 
42 
43  /**
44  * Constructor.
45  *
46  * \param string string
47  * \param floor floor
48  * \param position position
49  */
50  JLocation_t(const int string,
51  const int floor,
52  const int position) :
53  string (string),
54  floor (floor),
55  position(position)
56  {}
57 
58 
59  /**
60  * Check validity of location.
61  *
62  * \return true if valid; else false
63  */
64  bool is_valid() const
65  {
66  return *this != JLocation_t();
67  }
68 
69 
70  /**
71  * Less-than method.
72  *
73  * \param location location
74  * \return true if this location less than given location; else false
75  */
76  bool less(const JLocation_t& location) const
77  {
78  if (this->string == location.string) {
79 
80  if (this->floor == location.floor)
81  return this->position < location.position;
82  else
83  return this->floor < location.floor;
84 
85  } else {
86 
87  return this->string < location.string;
88  }
89  }
90 
91 
92  /**
93  * Write location to output stream.
94  *
95  * \param out output stream
96  * \param object location
97  * \return output stream
98  */
99  friend inline std::ostream& operator<<(std::ostream& out, const JLocation_t& object)
100  {
101  using namespace std;
102  using namespace JPP;
103 
104  return out << right
105  << FILL(4,'0') << object.string << '.'
106  << FILL(2,'0') << object.floor << '.'
107  << FILL(2,'0') << object.position << FILL() << left;
108  }
109 
110 
111  int string; //!< position in detector
112  int floor; //!< position in string
113  int position; //!< position in floor
114  };
115 
116 
117  /**
118  * Auxiliary class to map %UPI to location in detector.
119  */
120  struct JProductRouter :
121  public std::map<JUPI_t, JLocation_t>
122  {
123  /**
124  * Constructor.
125  *
126  * \param detector detector integration
127  * \param pbs %PBS sequences
128  */
130  {
131  for (JDetectorIntegration_t::const_iterator product = detector.begin(); product != detector.end(); ++product) {
132  if (product->has()) {
133  for (JPBSSequences::const_iterator i = pbs.begin(); i != pbs.end(); ++i) {
134  if (insert(detector, *product, *i)) {
135  break;
136  }
137  }
138  }
139  }
140  }
141 
142 
143  /**
144  * Get location of product with given %UPI.
145  *
146  * \param upi %UPI
147  * \return location
148  */
149  JLocation_t getLocation(const JUPI_t& upi) const
150  {
151  const_iterator p = this->find(upi);
152 
153  if (p != this->end())
154  return p->second;
155  else
156  return JLocation_t();
157  }
158 
159  private:
160  /**
161  * Insert product.
162  *
163  * \param detector detector integration
164  * \param product product
165  * \param pbs %PBS sequence
166  * \return true if inserted; else false
167  */
169  const JProductIntegration_t& product,
170  const JPBSSequence& pbs)
171  {
172  using namespace std;
173 
174  if (!pbs.empty()) {
175 
176  JPBSSequence::const_iterator i = pbs.begin();
177 
178  if (product.content.getPBS() == i->getPBS()) {
179 
180  JLocation_t location;
181 
182  for (JUPI_t upi = product.content.getUPI(); ++i != pbs.end(); ) {
183 
184  const JDetectorIntegration_t::range_type range = detector.find(upi);
185 
186  if (distance(range.first, range.second) == 1) {
187 
188  const JProductIntegration_t& ps = detector[range.first->second];
189 
190  if (is_optical_module(ps.container.getPBS()) ||
191  is_base_module (ps.container.getPBS())) { location.position = ps.position; }
192  else if (is_string (ps.container.getPBS())) { location.floor = ps.position; }
193  else if (is_detector (ps.container.getPBS())) { location.string = ps.position; }
194 
195  upi = ps.container.getUPI();
196 
197  } else {
198 
199  break;
200  }
201  }
202 
203  if (i == pbs.end()) {
204 
205  std::map<JUPI_t, JLocation_t>::insert(make_pair(product.content.getUPI(), location));
206 
207  return true;
208  }
209  }
210  }
211 
212  return false;
213  }
214  };
215 }
216 
217 #endif
JLocation_t(const int string, const int floor, const int position)
Constructor.
bool less(const JLocation_t &location) const
Less-than method.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
const JPBS_t & getPBS() const
Get PBS.
Definition: JPBS_t.hh:99
const JUPI_t & getUPI() const
Get UPI.
Definition: JUPI_t.hh:97
int floor
position in string
bool is_valid() const
Check validity of location.
I/O formatting auxiliaries.
Auxiliary data structure for a sequence of PBS values.
Definition: JPBSSequence.hh:30
Auxiliary data structure for location of product in detector.
JLocation_t getLocation(const JUPI_t &upi) const
Get location of product with given UPI.
JLocation_t()
Default constructor.
Auxiliary data structure for set of PBS sequences.
Definition: JPBSSequence.hh:83
range_type find(const JUPI_t &upi) const
Find range of products with given UPI.
Auxiliary class to map UPI to location in detector.
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:24
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
friend std::ostream & operator<<(std::ostream &out, const JLocation_t &object)
Write location to output stream.
int position
position in floor
bool insert(const JDetectorIntegration_t &detector, const JProductIntegration_t &product, const JPBSSequence &pbs)
Insert product.
bool is_string(const JPBS_t &pbs)
Test if given PBS corresponds to a string.
Definition: JPBS_t.hh:265
JProductRouter(const JDetectorIntegration_t &detector, const JPBSSequences &pbs)
Constructor.
bool is_base_module(const JPBS_t &pbs)
Test if given PBS corresponds to a base module.
Definition: JPBS_t.hh:289
do set_variable DETECTOR_TXT $WORKDIR detector
int string
position in detector
bool is_detector(const JPBS_t &pbs)
Test if given PBS corresponds to a detector.
Definition: JPBS_t.hh:253
bool is_optical_module(const JPBS_t &pbs)
Test if given PBS corresponds to a optical module.
Definition: JPBS_t.hh:277
Auxiliary class for product integration data.