Jpp  18.0.0-rc.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JModuleAddressMap.hh
Go to the documentation of this file.
1 #ifndef __JDETECTOR__JMODULEADDRESSMAP__
2 #define __JDETECTOR__JMODULEADDRESSMAP__
3 
4 #include <string>
5 #include <vector>
6 #include <algorithm>
7 
8 #include "JLang/JString.hh"
9 #include "JLang/JException.hh"
12 
13 
14 /**
15  * \author mdejong
16  */
17 
18 namespace JDETECTOR {}
19 namespace JPP { using namespace JDETECTOR; }
20 
21 namespace JDETECTOR {
22 
24 
25  /**
26  * Data structure to translate PMT physical to readout address.
27  */
29  public JPMTReadoutAddress,
30  public JPMTPhysicalAddress
31  {
32  /**
33  * Constructor.
34  *
35  * \param readout readout address
36  * \param address physical address
37  */
39  const JPMTPhysicalAddress& address) :
40  JPMTReadoutAddress (readout),
41  JPMTPhysicalAddress(address)
42  {}
43 
44 
45  /**
46  * Convert PMT address translator to string.
47  *
48  * \return string
49  */
51  {
52  return toString("% % %");
53  }
54 
55 
56  /**
57  * Convert PMT address translator to string.
58  *
59  * The targets <tt>target</tt> in the format string <tt>fmt</tt> are
60  * consecutively replaced by <tt>tdc</tt>, <tt>ring</tt> and <tt>position</tt>.
61  *
62  * \param fmt format
63  * \param target target
64  * \return string
65  */
66  std::string toString(const std::string& fmt, const std::string target = "%") const
67  {
68  JLANG::JString buffer(fmt);
69 
70  buffer.replace(target, tdc, 1);
71  buffer.replace(target, ring, 1);
72  buffer.replace(target, position, 1);
73 
74  return buffer;
75  }
76  };
77 
78 
79  /**
80  * Lookup table for PMT addresses in optical module.
81  */
83  public std::vector<JPMTAddressTranslator>
84  {
85  public:
86  /**
87  * Default constructor.
88  *
89  * The list of valid PMT address translations should be build according to the detector type.
90  * The internal router is used to convert a readout channel (TDC) to a PMT readout address.
91  */
93  std::vector<JPMTAddressTranslator>()
94  {}
95 
96 
97  /**
98  * Test whether index is available.
99  *
100  * \param index index
101  * \return true if index is available; else false
102  */
103  bool has(const int index) const
104  {
105  return (index >= 0 && index < (int) this->size());
106  }
107 
108 
109  /**
110  * Test whether TDC is available.
111  *
112  * \param tdc TDC
113  * \return true if TDC is available; else false
114  */
115  bool hasTDC(const int tdc) const
116  {
117  return (tdc >= 0 && tdc < (int) router.size() && router[tdc] != -1);
118  }
119 
120 
121  /**
122  * Test whether PMT is available.
123  *
124  * \param address PMT address
125  * \return true if PMT is available; else false
126  */
127  bool hasPMT(const JPMTPhysicalAddress& address) const
128  {
129  using namespace std;
130 
131  const_iterator p = lower_bound(this->begin(), this->end(), address, less<JPMTPhysicalAddress>());
132 
133  return (p != this->end() && *p == address);
134  }
135 
136 
137  /**
138  * Configure internal router.
139  */
140  void configure()
141  {
142  using namespace std;
143 
144  sort(this->begin(), this->end(), less<JPMTPhysicalAddress>());
145 
146  for (const_iterator i = this->begin(); i != this->end(); ++i) {
147 
148  if (i->tdc >= (int) router.size()) {
149  router.resize(i->tdc + 1, -1);
150  }
151 
152  router[i->tdc] = std::distance(const_iterator(this->begin()),i);
153  }
154  }
155 
156 
157  /**
158  * Swap readout addresses corresponding to indices.
159  *
160  * \param i1 first index
161  * \param i2 second index
162  */
163  void swap(const int i1, const int i2)
164  {
165  std::swap(router[at(i1).tdc],
166  router[at(i2).tdc]);
167 
168  std::swap(static_cast<JPMTReadoutAddress&>(at(i1)),
169  static_cast<JPMTReadoutAddress&>(at(i2)));
170  }
171 
172 
173  /**
174  * Swap physical addresses corresponding to TDCs.
175  *
176  * \param tdc1 first TDC
177  * \param tdc2 second TDC
178  */
179  void swapTDC(const int tdc1, const int tdc2)
180  {
181  std::swap(static_cast<JPMTReadoutAddress&>(at(router[tdc1])),
182  static_cast<JPMTReadoutAddress&>(at(router[tdc2])));
183 
184  std::swap(router[tdc1],
185  router[tdc2]);
186 
187  }
188 
189 
190  /**
191  * Get PMT logical index for given TDC channel.
192  *
193  * \param tdc TDC
194  * \return PMT logical index
195  */
196  int getIndex(const int tdc) const
197  {
198  return router.at(tdc);
199  }
200 
201 
202  /**
203  * Get PMT address translator.
204  *
205  * \param tdc TDC
206  * \return PMT address translator
207  */
208  const JPMTAddressTranslator& getAddressTranslator(const int tdc) const
209  {
210  return at(getIndex(tdc));
211  }
212 
213 
214  /**
215  * Get PMT physical address.
216  *
217  * \param tdc TDC
218  * \return PMT physical address
219  */
220  const JPMTPhysicalAddress& getPMTPhysicalAddress(const int tdc) const
221  {
222  return getAddressTranslator(tdc);
223  }
224 
225 
226  /**
227  * Get PMT address translator.
228  *
229  * \param address PMT physical address
230  * \return PMT address translator
231  */
233  {
234  using namespace std;
235 
236  const_iterator p = lower_bound(this->begin(), this->end(), address, less<JPMTPhysicalAddress>());
237 
238  if (p != this->end() && *p == address)
239  return *p;
240  else
241  THROW(JIndexOutOfRange, "Invalid PMT address " << address);
242  }
243 
244 
245  /**
246  * Get PMT readout address.
247  *
248  * \param address PMT physical address
249  * \return PMT readout address
250  */
252  {
253  return getAddressTranslator(address);
254  }
255 
256 
257  protected:
259  };
260 }
261 
262 #endif
bool hasTDC(const int tdc) const
Test whether TDC is available.
bool hasPMT(const JPMTPhysicalAddress &address) const
Test whether PMT is available.
Exceptions.
Wrapper class around STL string class.
Definition: JString.hh:27
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
const JPMTAddressTranslator & getAddressTranslator(const JPMTPhysicalAddress &address) const
Get PMT address translator.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
JPMTAddressTranslator(const JPMTReadoutAddress &readout, const JPMTPhysicalAddress &address)
Constructor.
std::string toString(const std::string &fmt, const std::string target="%") const
Convert PMT address translator to string.
void swap(const int i1, const int i2)
Swap readout addresses corresponding to indices.
Lookup table for PMT addresses in optical module.
int getIndex(const int tdc) const
Get PMT logical index for given TDC channel.
std::string toString() const
Convert PMT address translator to string.
void swapTDC(const int tdc1, const int tdc2)
Swap physical addresses corresponding to TDCs.
then awk string
Data structure to translate PMT physical to readout address.
char ring
ring number [&#39;A&#39;,&#39;F&#39;]
JModuleAddressMap()
Default constructor.
const JPMTAddressTranslator & getAddressTranslator(const int tdc) const
Get PMT address translator.
int position
position within ring [1,6]
Data structure for PMT readout address.
JString & replace(const char target, const char replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
Replace characters.
Definition: JString.hh:170
const JPMTPhysicalAddress & getPMTPhysicalAddress(const int tdc) const
Get PMT physical address.
Data structure for PMT physical address.
const JPMTReadoutAddress & getPMTReadoutAddress(const JPMTPhysicalAddress &address) const
Get PMT readout address.
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
void configure()
Configure internal router.
bool has(const int index) const
Test whether index is available.