Jpp  16.0.0
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/JMultiEquals.hh"
11 #include "JLang/JException.hh"
12 #include "JLang/JManip.hh"
13 #include "Jeep/JComment.hh"
14 #include "JDetector/JDetectorID.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 #include "JIO/JSTDIO.hh"
25 
26 
27 /**
28  * \file
29  *
30  * Data structure for detector geometry and calibration.
31  * \author mdejong
32  */
33 namespace JDETECTOR {}
34 namespace JPP { using namespace JDETECTOR; }
35 
36 namespace JDETECTOR {
37 
38  using JLANG::JMultiEquals;
39  using JLANG::JTYPELIST;
41  using JLANG::JIOException;
42  using JEEP::JComment;
43  using JIO::JSerialisable;
44  using JIO::JReader;
45  using JIO::JWriter;
47 
48 
49  /**
50  * Detector data structure.
51  *
52  * A detector consists of a header and a list of modules, each of which consists of a list of PMTs.
53  * Each PMT comprises position, orientation and time calibration data.
54  * Note that the index of the PMT in the module data structure corresponds to the readout channel (TDC).
55  *
56  * The following formats and file name extension are supported:
57  *
58  * <table border="1" width="300">
59  * <tr><th> extension </th><th> format </th><th> function </th></tr>
60  * <tr><td> ".detx" </td><td> ASCII </td><td> I/O </td></tr>
61  * <tr><td> ".gz" </td><td> gzipped ASCII </td><td> I/O </td></tr>
62  * <tr><td> ".dat" </td><td> binary </td><td> I/O </td></tr>
63  * <tr><td> ".det" </td><td> ASCII </td><td> I </td></tr>
64  * </table>
65  *
66  * The different formats can transparently be read and written using the methods:
67  * <pre>
68  * inline void load(const JString& file_name, JDetector& detector);
69  * inline void store(const JString& file_name, const JDetector& detector);
70  * </pre>
71  * respectively.
72  * These methods are available in the include file JDetectorToolkit.hh.
73  *
74  * The JDETECTOR::JModuleRouter and JDETECTOR::JPMTRouter classes provide for fast look-up
75  * methods of module and PMT calibration data for real data and Monte Carlo data, respectively.\n
76  * Note that the (not) equal operator strictly applies to the detector identifier and version.
77  * For quantitative comparisons, use JCompareDetector.cc.\n
78  * The official detector description document is available on the google drive for anyone with a %KM3NeT account.\n
79  * It can directly be found at this <a href="https://drive.google.com/open?id=0B6l8SNtndcwaUTZPOWZOXzd6R3M">link</a>.
80  *
81  * For a given detector,
82  * the module identifier (JModule::getID) and
83  * the PMT identifier (JPMT::getID)
84  * are unique.\n
85  * The serial number in the %UPI of a PMT is equal to its identifier in the detector description.\n
86  * For more information on the %PBS (product breakdown structure) and %UPI (universal product identifier),
87  * see this <a href="https://drive.google.com/file/d/0B-oDhZjfBP59QUVrdFFoaktUZTA/view?resourcekey=0-m7KPlni8jv8WTc07ER4m6g">link</a>.
88  */
89  class JDetector :
90  public JDetectorID,
91  public JDetectorVersion,
92  public JDetectorHeader,
93  public std::vector<JModule>,
94  public JMultiEquals<JDetector, JTYPELIST<JDetectorID, JVersion>::typelist>,
95  public JSerialisable
96  {
97  public:
98  /**
99  * Default constructor.
100  */
102  JDetectorID(),
104  JDetectorHeader (),
105  std::vector<JModule>(),
106  JSerialisable()
107  {}
108 
109 
110  /**
111  * Constructor.
112  *
113  * \param id detector identifier
114  * \param version version
115  * \param header header
116  */
118  const JVersion& version,
119  const JDetectorHeader& header) :
120  JDetectorID(id),
121  JDetectorVersion(version),
122  JDetectorHeader (header),
123  std::vector<JModule>(),
124  JSerialisable()
125  {
126  setVersion();
127  }
128 
129 
130  /**
131  * Set version.
132  *
133  * \param version version
134  */
136  {
137  static_cast<JDetectorVersion&>(*this).setVersion(version.getVersion());
138 
139  setVersion();
140  }
141 
142 
143  /**
144  * Set version.
145  *
146  * Note that the version is only set to a higher version or when forced to set it.
147  *
148  * \param version version
149  * \param force force setting of version
150  * \return true if set; else false
151  */
152  bool setVersion(const JDetectorVersion::JVersion_t& version, const bool force = false)
153  {
154  if (this->getVersion() == JDetectorVersion() || version > getDetectorVersion(this->getVersion()) || force) {
155 
156  setVersion(putDetectorVersion(version));
157 
158  return true;
159  }
160 
161  return false;
162  }
163 
164 
165  /**
166  * Set to latest version.
167  *
168  * \return true if set; else false
169  */
171  {
172  return setVersion(getLatestDetectorVersion<JDetectorVersion::JVersion_t>());
173  }
174 
175 
176  /**
177  * Move detector elements.
178  *
179  * \param pos offset position
180  * \return this JDetector
181  */
183  {
184  for (iterator i = begin(); i != end(); ++i) {
185  i->add(pos);
186  }
187 
188  return *this;
189  }
190 
191 
192  /**
193  * Move detector elements.
194  *
195  * \param pos offset position
196  * \return this JDetector
197  */
199  {
200  for (iterator i = begin(); i != end(); ++i) {
201  i->sub(pos);
202  }
203 
204  return *this;
205  }
206 
207 
208  /**
209  * Get module parameters.
210  *
211  * \param address module address
212  * \return module parameters
213  */
214  const JModule& getModule(const JModuleAddress& address) const
215  {
216  return at(address.first);
217  }
218 
219 
220  /**
221  * Get module parameters.
222  *
223  * \param address module address
224  * \return module parameters
225  */
227  {
228  return at(address.first);
229  }
230 
231 
232  /**
233  * Get module parameters.
234  *
235  * \param location module location
236  * \return module parameters
237  */
238  const JModule& getModule(const JLocation& location) const
239  {
240  for (JDetector::const_iterator module = this->begin(); module != this->end(); ++module) {
241  if (*module == location) {
242  return *module;
243  }
244  }
245 
246  THROW(JIndexOutOfRange, "Invalid location " << location);
247  }
248 
249 
250  /**
251  * Get module parameters.
252  *
253  * \param location module location
254  * \return module parameters
255  */
256  JModule& getModule(const JLocation& location)
257  {
258  for (JDetector::iterator module = this->begin(); module != this->end(); ++module) {
259  if (*module == location) {
260  return *module;
261  }
262  }
263 
264  THROW(JIndexOutOfRange, "Invalid location " << location);
265  }
266 
267 
268  /**
269  * Get PMT parameters.
270  *
271  * \param address JPMTAddress
272  * \return JPMT
273  */
274  const JPMT& getPMT(const JPMTAddress& address) const
275  {
276  return at(address.first).at(address.second);
277  }
278 
279 
280  /**
281  * Get PMT parameters.
282  *
283  * \param address JPMTAddress
284  * \return JPMT
285  */
286  JPMT& getPMT(const JPMTAddress& address)
287  {
288  return at(address.first).at(address.second);
289  }
290 
291 
292  /**
293  * Read detector from input.
294  *
295  * \param in input stream
296  * \param detector detector
297  * \return input stream
298  */
299  friend inline std::istream& operator>>(std::istream& in, JDetector& detector)
300  {
301  using namespace std;
302 
303  detector.clear();
304 
305  string buffer;
306 
307  if (in
308  >> detector.comment
309  >> static_cast<JDetectorID&>(detector)
310  >> buffer) {
311 
312  int number_of_modules;
313  int version = -1;
314 
315  try {
316  version = getDetectorVersion(buffer);
317  }
318  catch(std::exception&) {}
319 
320  switch (version) {
321 
322  default:
323  in >> static_cast<JDetectorHeader&>(detector);
324 
325  case V1:
326  in >> number_of_modules;
327  detector.setVersion(buffer);
328  break;
329 
330  case -1:
331  detector.setVersion(putDetectorVersion(V1));
332  istringstream(buffer) >> number_of_modules;
333  break;
334  }
335 
336  for (JModule module; number_of_modules != 0 && in >> module; --number_of_modules) {
337  detector.push_back(module);
338  }
339  }
340 
341  return in;
342  }
343 
344 
345  /**
346  * Write detector to output.
347  *
348  * \param out output stream
349  * \param detector detector
350  * \return output stream
351  */
352  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
353  {
354  using namespace std;
355  using namespace JPP;
356 
357  detector.setVersion();
358 
359  setFormat<JPosition3D> (JFormat_t(9, 3, std::ios::fixed | std::ios::showpos));
360  setFormat<JDirection3D> (JFormat_t(9, 6, std::ios::fixed | std::ios::showpos));
361  setFormat<JQuaternion3D>(JFormat_t(9, 6, std::ios::fixed | std::ios::showpos));
362  setFormat<JCalibration> (JFormat_t(9, 3, std::ios::fixed | std::ios::showpos));
363 
364  if (getDetectorVersion(detector.getVersion()) >= V3) {
365  out << detector.comment;
366  }
367 
368  out << static_cast<const JDetectorID&>(detector);
369 
370  switch (getDetectorVersion(detector.getVersion())) {
371 
372  default:
373  out << " ";
374  out << static_cast<const JDetectorVersion&>(detector) << endl;
375  out << static_cast<const JDetectorHeader&> (detector) << endl;
376  break;
377 
378  case V1:
379  out << " ";
380  break;
381  }
382 
383  out << detector.size() << endl;
384 
385  for (const_iterator i = detector.begin(); i != detector.end(); ++i) {
386  out << *i << endl;
387  }
388 
389  return out;
390  }
391 
392 
393  /**
394  * Read from input.
395  *
396  * \param in reader
397  * \return reader
398  */
399  virtual JReader& read(JReader& in) override
400  {
401  using namespace std;
402  using namespace JPP;
403 
404  this->clear();
405 
407 
408  char prefix[N];
409 
410  for (string buffer; in.read(prefix, N) == N && count(prefix, prefix + N, JComment::START_COMMENT) == N; ) {
411 
412  in >> buffer;
413 
414  this->comment.add(buffer);
415  }
416 
417  if (this->comment.empty()) {
418  THROW(JIOException, "Missing comments");
419  }
420 
421  in.read(&prefix[N], sizeof(JDetectorID) - N);
422 
423  JByteArrayReader is(prefix, N);
424 
425  int number_of_modules;
426 
427  is >> static_cast<JDetectorID&> (*this);
428  in >> static_cast<JDetectorVersion&>(*this);
429  in >> static_cast<JDetectorHeader&> (*this);
430  in >> number_of_modules;
431 
432  this->setVersion();
433 
434  for (JModule module; number_of_modules != 0; --number_of_modules) {
435 
436  in >> module;
437 
438  this->push_back(module);
439  }
440 
441  return in;
442  }
443 
444 
445  /**
446  * Write to output.
447  *
448  * \param out writer
449  * \return writer
450  */
451  virtual JWriter& write(JWriter& out) const override
452  {
453  using namespace std;
454  using namespace JPP;
455 
456  if (getDetectorVersion(this->getVersion()) < V3 || this->comment.empty()) {
457  THROW(JIOException, "Version " << getDetectorVersion(this->getVersion()) << " < " << V3 << " or missing comments.");
458  }
459 
460  this->setVersion();
461 
462  if (getDetectorVersion(this->getVersion()) >= V3) {
463 
464  for (JComment::const_iterator i = this->comment.begin(); i != this->comment.end(); ++i) {
465 
466  for (size_t i = 0; i != LENGTH_START_OF_COMMENT; ++i) {
468  }
469 
470  out << *i;
471  }
472  }
473 
474  const int number_of_modules = this->size();
475 
476  out << static_cast<const JDetectorID&> (*this);
477  out << static_cast<const JDetectorVersion&>(*this);
478  out << static_cast<const JDetectorHeader&> (*this);
479  out << number_of_modules;
480 
481  for (const_iterator module = this->begin(); module != this->end(); ++module) {
482  out << *module;
483  }
484 
485  return out;
486  }
487 
488 
490 
491 
492  /**
493  * Enumeration for different lengths of start of comment in binary I/O.
494  */
498  };
499 
500 
501  /**
502  * Get option for short start of comment in binary I/O.
503  *
504  * \return option
505  */
507  {
508  return get_start_of_comment();
509  }
510 
511 
512  /**
513  * Set option for short start of comment in binary I/O.
514  *
515  * \param option option
516  */
517  static void setStartOfComment(const start_of_comment_type option)
518  {
519  get_start_of_comment() = option;
520  }
521 
522  protected:
523  /**
524  * Set version.
525  *
526  * This method should be called to set up the handling of detector version specific module and PMT I/O.
527  */
528  void setVersion() const
529  {
531  JPMT ::setVersion(this->getVersion());
532  }
533 
534 
535  /**
536  * Get option for short start of comment in binary I/O.
537  *
538  * \return option
539  */
541  {
542  static start_of_comment_type start_of_comment = NEW_START_OF_COMMENT;
543 
544  return start_of_comment;
545  }
546 
547 
548  /**
549  * Length of start of comment in binary I/O.
550  */
551  static const size_t LENGTH_START_OF_COMMENT = sizeof(JDetectorID);
552  };
553 }
554 
555 #endif
start_of_comment_type
Enumeration for different lengths of start of comment in binary I/O.
Definition: JDetector.hh:495
static start_of_comment_type getStartOfComment()
Get option for short start of comment in binary I/O.
Definition: JDetector.hh:506
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:131
Data structure for a composite optical module.
Definition: JModule.hh:68
JUTMPosition & sub(const JUTMPosition &pos)
Subtract UTM position.
JDetectorVersion()
Default constructor.
JVersion_t
Enumeration of version types.
Detector data structure.
Definition: JDetector.hh:89
friend std::ostream & operator<<(std::ostream &out, const JDetector &detector)
Write detector to output.
Definition: JDetector.hh:352
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
bool setToLatestVersion()
Set to latest version.
Definition: JDetector.hh:170
int second
index of PMT in module data structure.
Definition: JPMTAddress.hh:100
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
static void setStartOfComment(const start_of_comment_type option)
Set option for short start of comment in binary I/O.
Definition: JDetector.hh:517
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:299
const std::string & getVersion() const
Get version.
const JPMT & getPMT(const JPMTAddress &address) const
Get PMT parameters.
Definition: JDetector.hh:274
static const size_t LENGTH_START_OF_COMMENT
Length of start of comment in binary I/O.
Definition: JDetector.hh:551
void setVersion(const JVersion &version)
Set version.
Definition: JDetector.hh:135
is
Definition: JDAQCHSM.chsm:167
JModule & getModule(const JLocation &location)
Get module parameters.
Definition: JDetector.hh:256
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:101
int first
index of module in detector data structure
void setVersion(const std::string &version)
Set version.
Forward declaration of binary output.
virtual JReader & read(JReader &in) override
Read from input.
Definition: JDetector.hh:399
Version with PMT status field and comments.
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
virtual JWriter & write(JWriter &out) const override
Write to output.
Definition: JDetector.hh:451
Logical location of module.
Definition: JLocation.hh:37
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:351
Data structure for PMT geometry, calibration and status.
Definition: JPMT.hh:43
Data structure for detector version.
bool setVersion(const JDetectorVersion::JVersion_t &version, const bool force=false)
Set version.
Definition: JDetector.hh:152
Address of module in detector data structure.
Interface for binary input.
static const JGetDetectorVersion getDetectorVersion
Function object to map detector version to numerical value.
I/O manipulators.
const JModule & getModule(const JModuleAddress &address) const
Get module parameters.
Definition: JDetector.hh:214
Auxiliary class for comment.
Definition: JComment.hh:41
static const JPutDetectorVersion putDetectorVersion(getDetectorVersion)
Function object to map numerical value to detector version.
static void setVersion(const JVersion &version)
Set detector version.
Definition: JPMT.hh:114
Address of PMT in detector data structure.
Definition: JPMTAddress.hh:32
JDetector(const JDetectorID &id, const JVersion &version, const JDetectorHeader &header)
Constructor.
Definition: JDetector.hh:117
std::vector< int > count
Definition: JAlgorithm.hh:180
static start_of_comment_type & get_start_of_comment()
Get option for short start of comment in binary I/O.
Definition: JDetector.hh:540
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:286
void setVersion() const
Set version.
Definition: JDetector.hh:528
JDetector & operator+=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:182
JUTMPosition & add(const JUTMPosition &pos)
Add UTM position.
const JModule & getModule(const JLocation &location) const
Get module parameters.
Definition: JDetector.hh:238
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.
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
JDetector & operator-=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:198
JLANG::JObjectID JDetectorID
Type definition of detector identifier.
Definition: JDetectorID.hh:19
Data structure for format specifications.
Definition: JManip.hh:522
JModule & getModule(const JModuleAddress &address)
Get module parameters.
Definition: JDetector.hh:226
STD extensions for binary I/O.
Data structure for optical module.
Exception for I/O.
Definition: JException.hh:324