Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDetector.hh
Go to the documentation of this file.
1 #ifndef __JDETECTOR__JDETECTOR__
2 #define __JDETECTOR__JDETECTOR__
3 
4 #include <istream>
5 #include <ostream>
6 #include <sstream>
7 #include <string>
8 #include <vector>
9 
10 #include "JLang/JObjectID.hh"
11 #include "JLang/JMultiEquals.hh"
12 #include "JLang/JException.hh"
13 #include "JLang/JManip.hh"
14 #include "Jeep/JComment.hh"
17 #include "JDetector/JPMT.hh"
18 #include "JDetector/JModule.hh"
19 #include "JDetector/JPMTAddress.hh"
21 #include "JGeometry3D/JVector3D.hh"
22 #include "JIO/JSerialisable.hh"
23 #include "JIO/JByteArrayIO.hh"
24 
25 
26 /**
27  * \file
28  *
29  * Data structure for detector geometry and calibration.
30  * \author mdejong
31  */
32 namespace JDETECTOR {}
33 namespace JPP { using namespace JDETECTOR; }
34 
35 namespace JDETECTOR {
36 
37  using JLANG::JObjectID;
38  using JLANG::JMultiEquals;
39  using JLANG::JTYPELIST;
41  using JEEP::JComment;
42  using JIO::JSerialisable;
43  using JIO::JReader;
44  using JIO::JWriter;
46 
47 
48  /**
49  * Detector data structure.
50  *
51  * A detector consists of a header and a list of modules, each of which consists of a list of PMTs.
52  * Each PMT comprises position, orientation and time calibration data.
53  * Note that the index of the PMT in the module data structure corresponds to the readout channel (TDC).
54  *
55  * The following formats and file name extension are supported:
56  *
57  * <table border="1" width="300">
58  * <tr><th> extension </th><th> format </th><th> function </th></tr>
59  * <tr><td> ".detx" </td><td> ASCII </td><td> I/O </td></tr>
60  * <tr><td> ".gz" </td><td> gzipped ASCII </td><td> I/O </td></tr>
61  * <tr><td> ".dat" </td><td> binary </td><td> I/O </td></tr>
62  * <tr><td> ".det" </td><td> ASCII </td><td> I </td></tr>
63  * </table>
64  *
65  * The different formats can transparently be read and written using the methods:
66  * <pre>
67  * inline void load(const JString& file_name, JDetector& detector);
68  * inline void store(const JString& file_name, const JDetector& detector);
69  * </pre>
70  * respectively.
71  * These methods are available in the include file JDetectorToolkit.hh.
72  *
73  * The JDETECTOR::JModuleRouter and JDETECTOR::JPMTRouter classes provide for fast look-up
74  * methods of module and PMT calibration data for real data and Monte Carlo data, respectively.\n
75  * Note that the (not) equal operator strictly applies to the detector identifier and version.
76  * For quantitative comparisons, use JCompareDetector.cc.\n
77  * The official detector description document is accessible for anyone with a %KM3NeT account at
78  * the <a href="https://drive.google.com/open?id=0B6l8SNtndcwaUTZPOWZOXzd6R3M">google drive</a>.
79  */
80  class JDetector :
81  public JObjectID,
82  public JDetectorVersion,
83  public JDetectorHeader,
84  public std::vector<JModule>,
85  public JMultiEquals<JDetector, JTYPELIST<JObjectID, JVersion>::typelist>,
86  public JSerialisable
87  {
88  public:
89  /**
90  * Default constructor.
91  */
93  JObjectID(),
95  JDetectorHeader (),
96  std::vector<JModule>(),
98  {}
99 
100 
101  /**
102  * Constructor.
103  *
104  * \param id detector identifier
105  * \param version version
106  * \param header header
107  */
108  JDetector(const JObjectID& id,
109  const JVersion& version,
110  const JDetectorHeader& header) :
111  JObjectID(id),
112  JDetectorVersion(version),
113  JDetectorHeader (header),
114  std::vector<JModule>(),
115  JSerialisable()
116  {
117  setVersion();
118  }
119 
120 
121  /**
122  * Set version.
123  *
124  * \param version version
125  */
127  {
128  static_cast<JDetectorVersion&>(*this).setVersion(version);
129 
130  setVersion();
131  }
132 
133 
134  /**
135  * Set version.
136  *
137  * Note that the version is only set to a higher version or when forced to set it.
138  *
139  * \param version version
140  * \param force force setting of version
141  * \return true if set; else false
142  */
143  bool setVersion(const JDetectorVersion::JVersion_t& version, const bool force = false)
144  {
145  if (this->getVersion() == JDetectorVersion() || version > getDetectorVersion(this->getVersion()) || force) {
146 
147  setVersion(putDetectorVersion(version));
148 
149  return true;
150  }
151 
152  return false;
153  }
154 
155 
156  /**
157  * Set to latest version.
158  *
159  * \return true if set; else false
160  */
162  {
163  return setVersion(getLatestDetectorVersion<JDetectorVersion::JVersion_t>());
164  }
165 
166 
167  /**
168  * Move detector elements.
169  *
170  * \param pos offset position
171  * \return this JDetector
172  */
174  {
175  for (iterator i = begin(); i != end(); ++i) {
176  i->add(pos);
177  }
178 
179  return *this;
180  }
181 
182 
183  /**
184  * Move detector elements.
185  *
186  * \param pos offset position
187  * \return this JDetector
188  */
190  {
191  for (iterator i = begin(); i != end(); ++i) {
192  i->sub(pos);
193  }
194 
195  return *this;
196  }
197 
198 
199  /**
200  * Get module parameters.
201  *
202  * \param address module address
203  * \return module parameters
204  */
205  const JModule& getModule(const JModuleAddress& address) const
206  {
207  return at(address.first);
208  }
209 
210 
211  /**
212  * Get module parameters.
213  *
214  * \param address module address
215  * \return module parameters
216  */
218  {
219  return at(address.first);
220  }
221 
222 
223  /**
224  * Get module parameters.
225  *
226  * \param location module location
227  * \return module parameters
228  */
229  const JModule& getModule(const JLocation& location) const
230  {
231  for (JDetector::const_iterator module = this->begin(); module != this->end(); ++module) {
232  if (*module == location) {
233  return *module;
234  }
235  }
236 
237  THROW(JIndexOutOfRange, "JDetector::getModule()");
238  }
239 
240 
241  /**
242  * Get module parameters.
243  *
244  * \param location module location
245  * \return module parameters
246  */
247  JModule& getModule(const JLocation& location)
248  {
249  for (JDetector::iterator module = this->begin(); module != this->end(); ++module) {
250  if (*module == location) {
251  return *module;
252  }
253  }
254 
255  THROW(JIndexOutOfRange, "JDetector::getModule()");
256  }
257 
258 
259  /**
260  * Get PMT parameters.
261  *
262  * \param address JPMTAddress
263  * \return JPMT
264  */
265  const JPMT& getPMT(const JPMTAddress& address) const
266  {
267  return at(address.first).at(address.second);
268  }
269 
270 
271  /**
272  * Get PMT parameters.
273  *
274  * \param address JPMTAddress
275  * \return JPMT
276  */
277  JPMT& getPMT(const JPMTAddress& address)
278  {
279  return at(address.first).at(address.second);
280  }
281 
282 
283  /**
284  * Read detector from input.
285  *
286  * \param in input stream
287  * \param detector detector
288  * \return input stream
289  */
290  friend inline std::istream& operator>>(std::istream& in, JDetector& detector)
291  {
292  using namespace std;
293 
294  detector.clear();
295 
296  string buffer;
297 
298  if (in
299  >> detector.comment
300  >> static_cast<JObjectID&>(detector)
301  >> buffer) {
302 
303  int n;
304  int version = -1;
305 
306  try {
307  version = getDetectorVersion(buffer);
308  }
309  catch(std::exception&) {}
310 
311  switch (version) {
312 
313  default:
314  in >> static_cast<JDetectorHeader&>(detector);
315 
316  case V1:
317  in >> n;
318  detector.setVersion(buffer);
319  break;
320 
321  case -1:
322  detector.setVersion(putDetectorVersion(V1));
323  istringstream(buffer) >> n;
324  break;
325  }
326 
327  for (JModule module; n != 0 && in >> module; --n) {
328  detector.push_back(module);
329  }
330  }
331 
332  return in;
333  }
334 
335 
336  /**
337  * Write detector to output.
338  *
339  * \param out output stream
340  * \param detector detector
341  * \return output stream
342  */
343  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
344  {
345  using namespace std;
346  using namespace JPP;
347 
348  detector.setVersion();
349 
350  setFormat<JPosition3D> (JFormat_t(9, 3, std::ios::fixed | std::ios::showpos));
351  setFormat<JDirection3D> (JFormat_t(9, 6, std::ios::fixed | std::ios::showpos));
352  setFormat<JQuaternion3D>(JFormat_t(9, 6, std::ios::fixed | std::ios::showpos));
353  setFormat<JCalibration> (JFormat_t(9, 3, std::ios::fixed | std::ios::showpos));
354 
355  out << detector.comment;
356  out << static_cast<const JObjectID&>(detector);
357 
358  switch (getDetectorVersion(detector.getVariant())) {
359 
360  default:
361  out << " ";
362  out << static_cast<const JDetectorVersion&>(detector) << endl;
363  out << static_cast<const JDetectorHeader&> (detector) << endl;
364  break;
365 
366  case V1:
367  out << " ";
368  break;
369  }
370 
371  out << detector.size() << endl;
372 
373  for (const_iterator i = detector.begin(); i != detector.end(); ++i) {
374  out << *i << endl;
375  }
376 
377  return out;
378  }
379 
380 
381  /**
382  * Read from input.
383  *
384  * \param in reader
385  * \return reader
386  */
387  virtual JReader& read(JReader& in) override
388  {
389  using namespace std;
390  using namespace JPP;
391 
392  clear();
393 
394  char c;
395  int n;
396 
397  for (string buffer; in >> c && c == JComment::START_COMMENT; ) {
398 
399  in >> buffer;
400 
401  this->comment.add(buffer);
402  }
403 
404  const int N = sizeof(JObjectID);
405 
406  char buffer[N];
407 
408  buffer[0] = c;
409 
410  in.read(&buffer[1], N - 1);
411 
412  JByteArrayReader is(buffer, N);
413 
414  is >> static_cast<JObjectID&> (*this);
415  in >> static_cast<JDetectorVersion&>(*this);
416  in >> static_cast<JDetectorHeader&> (*this);
417  in >> n;
418 
419  setVersion();
420 
421  for (JModule module; n != 0; --n) {
422 
423  in >> module;
424 
425  push_back(module);
426  }
427 
428  return in;
429  }
430 
431 
432  /**
433  * Write to output.
434  *
435  * \param out writer
436  * \return writer
437  */
438  virtual JWriter& write(JWriter& out) const override
439  {
440  using namespace std;
441  using namespace JPP;
442 
443  setVersion();
444 
445  int n = size();
446  char c = JComment::START_COMMENT;
447 
448  for (JComment::const_iterator i = this->comment.begin(); i != this->comment.end(); ++i) {
449  out << c;
450  out << *i;
451  }
452 
453  out << static_cast<const JObjectID&> (*this);
454  out << static_cast<const JDetectorVersion&>(*this);
455  out << static_cast<const JDetectorHeader&> (*this);
456  out << n;
457 
458  for (const_iterator i = begin(); i != end(); ++i) {
459  out << *i;
460  }
461 
462  return out;
463  }
464 
465 
467 
468  protected:
469  /**
470  * Set version.
471  *
472  * This method should be called to set up the handling of detector version specific module and PMT I/O.
473  */
474  void setVersion() const
475  {
477  JPMT ::setVersion(this->getVersion());
478  }
479  };
480 }
481 
482 #endif
static const char START_COMMENT
start comment
Definition: JComment.hh:48
Exceptions.
Interface for binary output.
static void setVersion(const JVersion &version)
Set detector version.
Definition: JModule.hh:169
Data structure for a composite optical module.
Definition: JModule.hh:57
JUTMPosition & sub(const JUTMPosition &pos)
Subtract UTM position.
JDetectorVersion()
Default constructor.
JVersion_t
Enumeration of version types.
Detector data structure.
Definition: JDetector.hh:80
friend std::ostream & operator<<(std::ostream &out, const JDetector &detector)
Write detector to output.
Definition: JDetector.hh:343
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
bool setToLatestVersion()
Set to latest version.
Definition: JDetector.hh:161
int second
index of PMT in module data structure.
Definition: JPMTAddress.hh:100
virtual int read(char *buffer, const int length)=0
Read byte array.
friend std::istream & operator>>(std::istream &in, JDetector &detector)
Read detector from input.
Definition: JDetector.hh:290
void setVersion(const JVersion &version)
Set version.
Definition: JVersion.hh:78
const JPMT & getPMT(const JPMTAddress &address) const
Get PMT parameters.
Definition: JDetector.hh:265
void setVersion(const JVersion &version)
Set version.
Definition: JDetector.hh:126
is
Definition: JDAQCHSM.chsm:167
JModule & getModule(const JLocation &location)
Get module parameters.
Definition: JDetector.hh:247
JObjectID()
Default constructor.
Definition: JObjectID.hh:30
Data structure for detector header.
Template definition of auxiliary base class for data structures composed of multiple base classes wit...
Definition: JMultiEquals.hh:32
JDetector()
Default constructor.
Definition: JDetector.hh:92
int first
index of module in detector data structure
Forward declaration of binary output.
virtual JReader & read(JReader &in) override
Read from input.
Definition: JDetector.hh:387
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
virtual JWriter & write(JWriter &out) const override
Write to output.
Definition: JDetector.hh:438
Logical location of module.
Definition: JLocation.hh:37
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:351
Data structure for PMT geometry and calibration.
Definition: JPMT.hh:47
Data structure for detector version.
JDetector(const JObjectID &id, const JVersion &version, const JDetectorHeader &header)
Constructor.
Definition: JDetector.hh:108
bool setVersion(const JDetectorVersion::JVersion_t &version, const bool force=false)
Set version.
Definition: JDetector.hh:143
Address of module in detector data structure.
Interface for binary input.
const std::string & getVariant() const
Get variant.
Definition: JVersion.hh:89
static const JGetDetectorVersion getDetectorVersion
Function object to map detector variant to detector version.
I/O manipulators.
const JModule & getModule(const JModuleAddress &address) const
Get module parameters.
Definition: JDetector.hh:205
Auxiliary class for comment.
Definition: JComment.hh:41
static const JPutDetectorVersion putDetectorVersion(getDetectorVersion)
Function object to map detector version to detector variant.
static void setVersion(const JVersion &version)
Set detector version.
Definition: JPMT.hh:118
Address of PMT in detector data structure.
Definition: JPMTAddress.hh:32
Data structure for PMT geometry and calibration.
Auxiliary class for object identification.
Definition: JObjectID.hh:22
JPMT & getPMT(const JPMTAddress &address)
Get PMT parameters.
Definition: JDetector.hh:277
void setVersion() const
Set version.
Definition: JDetector.hh:474
alias put_queue eval echo n
Definition: qlib.csh:19
JDetector & operator+=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:173
JUTMPosition & add(const JUTMPosition &pos)
Add UTM position.
const JModule & getModule(const JLocation &location) const
Get module parameters.
Definition: JDetector.hh:229
JComment & add(const std::string &comment)
Add comment.
Definition: JComment.hh:100
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
do set_variable DETECTOR_TXT $WORKDIR detector
Auxiliary class for version identifier.
Definition: JVersion.hh:30
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:38
JDetector & operator-=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:189
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
Data structure for format specifications.
Definition: JManip.hh:522
version
Definition: JCalibratePMT.sh:7
JModule & getModule(const JModuleAddress &address)
Get module parameters.
Definition: JDetector.hh:217
const JVersion & getVersion() const
Get version.
Definition: JVersion.hh:56
Data structure for a composite optical module.