Jpp  pmt_effective_area_update
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 accessible for anyone with a %KM3NeT account at
79  * the <a href="https://drive.google.com/open?id=0B6l8SNtndcwaUTZPOWZOXzd6R3M">google drive</a>.
80  */
81  class JDetector :
82  public JDetectorID,
83  public JDetectorVersion,
84  public JDetectorHeader,
85  public std::vector<JModule>,
86  public JMultiEquals<JDetector, JTYPELIST<JDetectorID, JVersion>::typelist>,
87  public JSerialisable
88  {
89  public:
90  /**
91  * Default constructor.
92  */
94  JDetectorID(),
96  JDetectorHeader (),
97  std::vector<JModule>(),
99  {}
100 
101 
102  /**
103  * Constructor.
104  *
105  * \param id detector identifier
106  * \param version version
107  * \param header header
108  */
110  const JVersion& version,
111  const JDetectorHeader& header) :
112  JDetectorID(id),
113  JDetectorVersion(version),
114  JDetectorHeader (header),
115  std::vector<JModule>(),
116  JSerialisable()
117  {
118  setVersion();
119  }
120 
121 
122  /**
123  * Set version.
124  *
125  * \param version version
126  */
128  {
129  static_cast<JDetectorVersion&>(*this).setVersion(version.getVersion());
130 
131  setVersion();
132  }
133 
134 
135  /**
136  * Set version.
137  *
138  * Note that the version is only set to a higher version or when forced to set it.
139  *
140  * \param version version
141  * \param force force setting of version
142  * \return true if set; else false
143  */
144  bool setVersion(const JDetectorVersion::JVersion_t& version, const bool force = false)
145  {
146  if (this->getVersion() == JDetectorVersion() || version > getDetectorVersion(this->getVersion()) || force) {
147 
148  setVersion(putDetectorVersion(version));
149 
150  return true;
151  }
152 
153  return false;
154  }
155 
156 
157  /**
158  * Set to latest version.
159  *
160  * \return true if set; else false
161  */
163  {
164  return setVersion(getLatestDetectorVersion<JDetectorVersion::JVersion_t>());
165  }
166 
167 
168  /**
169  * Move detector elements.
170  *
171  * \param pos offset position
172  * \return this JDetector
173  */
175  {
176  for (iterator i = begin(); i != end(); ++i) {
177  i->add(pos);
178  }
179 
180  return *this;
181  }
182 
183 
184  /**
185  * Move detector elements.
186  *
187  * \param pos offset position
188  * \return this JDetector
189  */
191  {
192  for (iterator i = begin(); i != end(); ++i) {
193  i->sub(pos);
194  }
195 
196  return *this;
197  }
198 
199 
200  /**
201  * Get module parameters.
202  *
203  * \param address module address
204  * \return module parameters
205  */
206  const JModule& getModule(const JModuleAddress& address) const
207  {
208  return at(address.first);
209  }
210 
211 
212  /**
213  * Get module parameters.
214  *
215  * \param address module address
216  * \return module parameters
217  */
219  {
220  return at(address.first);
221  }
222 
223 
224  /**
225  * Get module parameters.
226  *
227  * \param location module location
228  * \return module parameters
229  */
230  const JModule& getModule(const JLocation& location) const
231  {
232  for (JDetector::const_iterator module = this->begin(); module != this->end(); ++module) {
233  if (*module == location) {
234  return *module;
235  }
236  }
237 
238  THROW(JIndexOutOfRange, "Invalid location " << location);
239  }
240 
241 
242  /**
243  * Get module parameters.
244  *
245  * \param location module location
246  * \return module parameters
247  */
248  JModule& getModule(const JLocation& location)
249  {
250  for (JDetector::iterator module = this->begin(); module != this->end(); ++module) {
251  if (*module == location) {
252  return *module;
253  }
254  }
255 
256  THROW(JIndexOutOfRange, "Invalid location " << location);
257  }
258 
259 
260  /**
261  * Get PMT parameters.
262  *
263  * \param address JPMTAddress
264  * \return JPMT
265  */
266  const JPMT& getPMT(const JPMTAddress& address) const
267  {
268  return at(address.first).at(address.second);
269  }
270 
271 
272  /**
273  * Get PMT parameters.
274  *
275  * \param address JPMTAddress
276  * \return JPMT
277  */
278  JPMT& getPMT(const JPMTAddress& address)
279  {
280  return at(address.first).at(address.second);
281  }
282 
283 
284  /**
285  * Read detector from input.
286  *
287  * \param in input stream
288  * \param detector detector
289  * \return input stream
290  */
291  friend inline std::istream& operator>>(std::istream& in, JDetector& detector)
292  {
293  using namespace std;
294 
295  detector.clear();
296 
297  string buffer;
298 
299  if (in
300  >> detector.comment
301  >> static_cast<JDetectorID&>(detector)
302  >> buffer) {
303 
304  int number_of_modules;
305  int version = -1;
306 
307  try {
308  version = getDetectorVersion(buffer);
309  }
310  catch(std::exception&) {}
311 
312  switch (version) {
313 
314  default:
315  in >> static_cast<JDetectorHeader&>(detector);
316 
317  case V1:
318  in >> number_of_modules;
319  detector.setVersion(buffer);
320  break;
321 
322  case -1:
323  detector.setVersion(putDetectorVersion(V1));
324  istringstream(buffer) >> number_of_modules;
325  break;
326  }
327 
328  for (JModule module; number_of_modules != 0 && in >> module; --number_of_modules) {
329  detector.push_back(module);
330  }
331  }
332 
333  return in;
334  }
335 
336 
337  /**
338  * Write detector to output.
339  *
340  * \param out output stream
341  * \param detector detector
342  * \return output stream
343  */
344  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
345  {
346  using namespace std;
347  using namespace JPP;
348 
349  detector.setVersion();
350 
351  setFormat<JPosition3D> (JFormat_t(9, 3, std::ios::fixed | std::ios::showpos));
352  setFormat<JDirection3D> (JFormat_t(9, 6, std::ios::fixed | std::ios::showpos));
353  setFormat<JQuaternion3D>(JFormat_t(9, 6, std::ios::fixed | std::ios::showpos));
354  setFormat<JCalibration> (JFormat_t(9, 3, std::ios::fixed | std::ios::showpos));
355 
356  if (getDetectorVersion(detector.getVersion()) >= V3) {
357  out << detector.comment;
358  }
359 
360  out << static_cast<const JDetectorID&>(detector);
361 
362  switch (getDetectorVersion(detector.getVersion())) {
363 
364  default:
365  out << " ";
366  out << static_cast<const JDetectorVersion&>(detector) << endl;
367  out << static_cast<const JDetectorHeader&> (detector) << endl;
368  break;
369 
370  case V1:
371  out << " ";
372  break;
373  }
374 
375  out << detector.size() << endl;
376 
377  for (const_iterator i = detector.begin(); i != detector.end(); ++i) {
378  out << *i << endl;
379  }
380 
381  return out;
382  }
383 
384 
385  /**
386  * Read from input.
387  *
388  * \param in reader
389  * \return reader
390  */
391  virtual JReader& read(JReader& in) override
392  {
393  using namespace std;
394  using namespace JPP;
395 
396  this->clear();
397 
399 
400  char prefix[N];
401 
402  for (string buffer; in.read(prefix, N) == N && count(prefix, prefix + N, JComment::START_COMMENT) == N; ) {
403 
404  in >> buffer;
405 
406  this->comment.add(buffer);
407  }
408 
409  if (this->comment.empty()) {
410  THROW(JIOException, "Missing comments");
411  }
412 
413  in.read(&prefix[N], sizeof(JDetectorID) - N);
414 
415  JByteArrayReader is(prefix, N);
416 
417  int number_of_modules;
418 
419  is >> static_cast<JDetectorID&> (*this);
420  in >> static_cast<JDetectorVersion&>(*this);
421  in >> static_cast<JDetectorHeader&> (*this);
422  in >> number_of_modules;
423 
424  this->setVersion();
425 
426  for (JModule module; number_of_modules != 0; --number_of_modules) {
427 
428  in >> module;
429 
430  this->push_back(module);
431  }
432 
433  return in;
434  }
435 
436 
437  /**
438  * Write to output.
439  *
440  * \param out writer
441  * \return writer
442  */
443  virtual JWriter& write(JWriter& out) const override
444  {
445  using namespace std;
446  using namespace JPP;
447 
448  if (getDetectorVersion(this->getVersion()) < V3 || this->comment.empty()) {
449  THROW(JIOException, "Version " << getDetectorVersion(this->getVersion()) << " < " << V3 << " or missing comments.");
450  }
451 
452  this->setVersion();
453 
454  if (getDetectorVersion(this->getVersion()) >= V3) {
455 
456  for (JComment::const_iterator i = this->comment.begin(); i != this->comment.end(); ++i) {
457 
458  for (size_t i = 0; i != LENGTH_START_OF_COMMENT; ++i) {
460  }
461 
462  out << *i;
463  }
464  }
465 
466  const int number_of_modules = this->size();
467 
468  out << static_cast<const JDetectorID&> (*this);
469  out << static_cast<const JDetectorVersion&>(*this);
470  out << static_cast<const JDetectorHeader&> (*this);
471  out << number_of_modules;
472 
473  for (const_iterator module = this->begin(); module != this->end(); ++module) {
474  out << *module;
475  }
476 
477  return out;
478  }
479 
480 
482 
483 
484  /**
485  * Enumeration for different lengths of start of comment in binary I/O.
486  */
490  };
491 
492 
493  /**
494  * Get option for short start of comment in binary I/O.
495  *
496  * \return option
497  */
499  {
500  return get_start_of_comment();
501  }
502 
503 
504  /**
505  * Set option for short start of comment in binary I/O.
506  *
507  * \param option option
508  */
509  static void setStartOfComment(const start_of_comment_type option)
510  {
511  get_start_of_comment() = option;
512  }
513 
514  protected:
515  /**
516  * Set version.
517  *
518  * This method should be called to set up the handling of detector version specific module and PMT I/O.
519  */
520  void setVersion() const
521  {
523  JPMT ::setVersion(this->getVersion());
524  }
525 
526 
527  /**
528  * Get option for short start of comment in binary I/O.
529  *
530  * \return option
531  */
533  {
534  static start_of_comment_type start_of_comment = NEW_START_OF_COMMENT;
535 
536  return start_of_comment;
537  }
538 
539 
540  /**
541  * Length of start of comment in binary I/O.
542  */
543  static const size_t LENGTH_START_OF_COMMENT = sizeof(JDetectorID);
544  };
545 }
546 
547 #endif
start_of_comment_type
Enumeration for different lengths of start of comment in binary I/O.
Definition: JDetector.hh:487
static start_of_comment_type getStartOfComment()
Get option for short start of comment in binary I/O.
Definition: JDetector.hh:498
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:121
Data structure for a composite optical module.
Definition: JModule.hh:66
JUTMPosition & sub(const JUTMPosition &pos)
Subtract UTM position.
JDetectorVersion()
Default constructor.
JVersion_t
Enumeration of version types.
Detector data structure.
Definition: JDetector.hh:81
friend std::ostream & operator<<(std::ostream &out, const JDetector &detector)
Write detector to output.
Definition: JDetector.hh:344
#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:162
int second
index of PMT in module data structure.
Definition: JPMTAddress.hh:100
static void setStartOfComment(const start_of_comment_type option)
Set option for short start of comment in binary I/O.
Definition: JDetector.hh:509
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:291
const std::string & getVersion() const
Get version.
const JPMT & getPMT(const JPMTAddress &address) const
Get PMT parameters.
Definition: JDetector.hh:266
static const size_t LENGTH_START_OF_COMMENT
Length of start of comment in binary I/O.
Definition: JDetector.hh:543
void setVersion(const JVersion &version)
Set version.
Definition: JDetector.hh:127
is
Definition: JDAQCHSM.chsm:167
JModule & getModule(const JLocation &location)
Get module parameters.
Definition: JDetector.hh:248
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:93
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:391
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:443
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:42
Data structure for detector version.
bool setVersion(const JDetectorVersion::JVersion_t &version, const bool force=false)
Set version.
Definition: JDetector.hh:144
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:206
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:113
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:109
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:532
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:278
void setVersion() const
Set version.
Definition: JDetector.hh:520
JDetector & operator+=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:174
JUTMPosition & add(const JUTMPosition &pos)
Add UTM position.
const JModule & getModule(const JLocation &location) const
Get module parameters.
Definition: JDetector.hh:230
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:40
JDetector & operator-=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:190
JLANG::JObjectID JDetectorID
Type definition of detector identifier.
Definition: JDetectorID.hh:19
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:36
Data structure for format specifications.
Definition: JManip.hh:522
JModule & getModule(const JModuleAddress &address)
Get module parameters.
Definition: JDetector.hh:218
STD extensions for binary I/O.
Data structure for optical module.
Exception for I/O.
Definition: JException.hh:324