Jpp
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 "Jeep/JComment.hh"
16 #include "JDetector/JPMT.hh"
17 #include "JDetector/JModule.hh"
18 #include "JDetector/JPMTAddress.hh"
20 #include "JGeometry3D/JVector3D.hh"
21 #include "JIO/JSerialisable.hh"
22 
23 
24 /**
25  * \file
26  *
27  * Data structure for detector geometry and calibration.
28  * \author mdejong
29  */
30 namespace JDETECTOR {}
31 namespace JPP { using namespace JDETECTOR; }
32 
33 namespace JDETECTOR {
34 
35  using JLANG::JObjectID;
36  using JLANG::JMultiEquals;
37  using JLANG::JTYPELIST;
39  using JEEP::JComment;
40  using JIO::JSerialisable;
41  using JIO::JReader;
42  using JIO::JWriter;
44 
45 
46  /**
47  * Detector data structure.
48  *
49  * A detector consists of a header and a list of modules, each of which consists of a list of PMTs.
50  * Each PMT comprises position, orientation and time calibration data.
51  * Note that the index of the PMT in the module data structure corresponds to the readout channel (TDC).
52  *
53  * The following formats and file name extension are supported:
54  *
55  * <table border="1" width="300">
56  * <tr><th> extension </th><th> format </th><th> function </th></tr>
57  * <tr><td> ".detx" </td><td> ASCII </td><td> I/O </td></tr>
58  * <tr><td> ".gz" </td><td> gzipped ASCII </td><td> I/O </td></tr>
59  * <tr><td> ".dat" </td><td> binary </td><td> I/O </td></tr>
60  * <tr><td> ".det" </td><td> ASCII </td><td> I </td></tr>
61  * </table>
62  *
63  * The different formats can transparently be read and written using the methods:
64  * <pre>
65  * inline void load(const JString& file_name, JDetector& detector);
66  * inline void store(const JString& file_name, const JDetector& detector);
67  * </pre>
68  * respectively.
69  * These methods are available in the include file JDetectorToolkit.hh.
70  *
71  * The JDETECTOR::JModuleRouter and JDETECTOR::JPMTRouter classes provide for fast look-up
72  * methods of module and PMT calibration data for real data and Monte Carlo data, respectively.
73  *
74  * Note that the (not) equal operator strictly applies to the detector identifier and version.
75  * For quantitative comparisons, use JCompareDetector.cc.
76  *
77  * The official detector description document is available for anyone with a KM3NeT account at the
78  * <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  {
118  }
119 
120 
121  /**
122  * Set version.
123  *
124  * \param version version
125  */
126  void setVersion(const JVersion& version)
127  {
128  static_cast<JDetectorVersion&>(*this).setVersion(version);
129 
131  }
132 
133 
134  /**
135  * Set version.
136  *
137  * \param version version
138  */
140  {
141  setVersion(putDetectorVersion(version));
142  }
143 
144 
145  /**
146  * Move detector elements.
147  *
148  * \param pos offset position
149  * \return this JDetector
150  */
152  {
153  for (iterator i = begin(); i != end(); ++i) {
154  i->add(pos);
155  }
156 
157  return *this;
158  }
159 
160 
161  /**
162  * Move detector elements.
163  *
164  * \param pos offset position
165  * \return this JDetector
166  */
168  {
169  for (iterator i = begin(); i != end(); ++i) {
170  i->sub(pos);
171  }
172 
173  return *this;
174  }
175 
176 
177  /**
178  * Get module parameters.
179  *
180  * \param address module address
181  * \return module parameters
182  */
183  const JModule& getModule(const JModuleAddress& address) const
184  {
185  return at(address.first);
186  }
187 
188 
189  /**
190  * Get module parameters.
191  *
192  * \param address module address
193  * \return module parameters
194  */
196  {
197  return at(address.first);
198  }
199 
200 
201  /**
202  * Get module parameters.
203  *
204  * \param location module location
205  * \return module parameters
206  */
207  const JModule& getModule(const JModuleLocation& location) const
208  {
209  for (JDetector::const_iterator module = this->begin(); module != this->end(); ++module) {
210  if (*module == location) {
211  return *module;
212  }
213  }
214 
215  THROW(JIndexOutOfRange, "JDetector::getModule()");
216  }
217 
218 
219  /**
220  * Get module parameters.
221  *
222  * \param location module location
223  * \return module parameters
224  */
226  {
227  for (JDetector::iterator module = this->begin(); module != this->end(); ++module) {
228  if (*module == location) {
229  return *module;
230  }
231  }
232 
233  THROW(JIndexOutOfRange, "JDetector::getModule()");
234  }
235 
236 
237  /**
238  * Get PMT parameters.
239  *
240  * \param address JPMTAddress
241  * \return JPMT
242  */
243  const JPMT& getPMT(const JPMTAddress& address) const
244  {
245  return at(address.first).at(address.second);
246  }
247 
248 
249  /**
250  * Get PMT parameters.
251  *
252  * \param address JPMTAddress
253  * \return JPMT
254  */
255  JPMT& getPMT(const JPMTAddress& address)
256  {
257  return at(address.first).at(address.second);
258  }
259 
260 
261  /**
262  * Read detector from input.
263  *
264  * \param in input stream
265  * \param detector detector
266  * \return input stream
267  */
268  friend inline std::istream& operator>>(std::istream& in, JDetector& detector)
269  {
270  using namespace std;
271 
272  detector.clear();
273 
274  string buffer;
275 
276  if (in
277  >> detector.comment
278  >> static_cast<JObjectID&>(detector)
279  >> buffer) {
280 
281  int n;
282  int version = -1;
283 
284  try {
285  version = getDetectorVersion(buffer);
286  }
287  catch(std::exception&) {}
288 
289  switch (version) {
290 
291  case V3:
292  case V2:
293  in >> static_cast<JDetectorHeader&>(detector);
294 
295  case V1:
296  in >> n;
297  detector.setVersion(buffer);
298  break;
299 
300  default:
301  detector.setVersion(putDetectorVersion(V1));
302  istringstream(buffer) >> n;
303  break;
304  }
305 
306  for (JModule module; n != 0 && in >> module; --n) {
307  detector.push_back(module);
308  }
309  }
310 
311  return in;
312  }
313 
314 
315  /**
316  * Write detector to output.
317  *
318  * \param out output stream
319  * \param detector detector
320  * \return output stream
321  */
322  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
323  {
324  using namespace std;
325 
326  const ios::fmtflags format = out.flags();
327  const streamsize precision = out.precision();
328 
329  out.setf(ios::fixed);
330  out.precision(1);
331 
332  out << detector.comment;
333  out << static_cast<const JObjectID&>(detector);
334 
335  switch (getDetectorVersion(detector.getVariant())) {
336 
337  case V3:
338  case V2:
339  out << " ";
340  out << static_cast<const JDetectorVersion&>(detector) << endl;
341  out << static_cast<const JDetectorHeader&> (detector) << endl;
342  break;
343 
344  default:
345  out << " ";
346  break;
347  }
348 
349  out << detector.size() << endl;
350 
351  out.precision(3);
352 
353  for (const_iterator i = detector.begin(); i != detector.end(); ++i) {
354  out << *i << endl;
355  }
356 
357  out.flags(format);
358  out.precision(precision);
359 
360  return out;
361  }
362 
363 
364  /**
365  * Read from input.
366  *
367  * \param in reader
368  * \return reader
369  */
370  virtual JReader& read(JReader& in)
371  {
372  clear();
373 
374  int n;
375 
376  in >> static_cast<JObjectID&> (*this);
377  in >> static_cast<JDetectorVersion&>(*this);
378  in >> static_cast<JDetectorHeader&> (*this);
379  in >> n;
380 
382 
383  for (JModule module; n != 0; --n) {
384 
385  in >> module;
386 
387  push_back(module);
388  }
389 
390  return in;
391  }
392 
393 
394  /**
395  * Write to output.
396  *
397  * \param out writer
398  * \return writer
399  */
400  virtual JWriter& write(JWriter& out) const
401  {
402  int n = size();
403 
404  out << static_cast<const JObjectID&> (*this);
405  out << static_cast<const JDetectorVersion&>(*this);
406  out << static_cast<const JDetectorHeader&> (*this);
407  out << n;
408 
409  for (const_iterator i = begin(); i != end(); ++i) {
410  out << *i;
411  }
412 
413  return out;
414  }
415 
416 
417 
419  };
420 }
421 
422 #endif
JException.hh
JModule.hh
JDETECTOR::JDetectorVersion::V2
Version with UTC time and UTM position data.
Definition: JDetectorVersion.hh:44
JDETECTOR::JDetector::operator-=
JDetector & operator-=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:167
JDETECTOR::JDetector::comment
JComment comment
Definition: JDetector.hh:418
JUTM::JUTMPosition::sub
JUTMPosition & sub(const JUTMPosition &pos)
Subtract UTM position.
Definition: JUTMPosition.hh:169
JIO::JReader
Interface for binary input.
Definition: JSerialisable.hh:62
JLANG::JIndexOutOfRange
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
JDETECTOR::JDetector::setVersion
void setVersion(const JDetectorVersion::JVersion_t &version)
Set version.
Definition: JDetector.hh:139
JDETECTOR::JModuleAddress::first
int first
index of module in detector data structure
Definition: JModuleAddress.hh:90
JVector3D.hh
JDETECTOR::JDetectorHeader
Data structure for detector header.
Definition: JDetectorHeader.hh:35
JDETECTOR::JDetector::getModule
JModule & getModule(const JModuleAddress &address)
Get module parameters.
Definition: JDetector.hh:195
JDETECTOR::JDetector::operator<<
friend std::ostream & operator<<(std::ostream &out, const JDetector &detector)
Write detector to output.
Definition: JDetector.hh:322
JEEP::JVersion::getVersion
const JVersion & getVersion() const
Get version.
Definition: JVersion.hh:57
JDETECTOR::putDetectorVersion
static const JPutDetectorVersion putDetectorVersion(getDetectorVersion)
Function object to map detector version to detector variant.
JDetectorHeader.hh
JIO::JSerialisable
Forward declaration of binary output.
Definition: JSerialisable.hh:31
JDETECTOR::JDetectorVersion::V3
Version with PMT status field and comments.
Definition: JDetectorVersion.hh:45
JDETECTOR::JDetector::setVersion
void setVersion(const JVersion &version)
Set version.
Definition: JDetector.hh:126
JDETECTOR::JDetector::operator+=
JDetector & operator+=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:151
JDETECTOR::getDetectorVersion
static const JGetDetectorVersion getDetectorVersion
Function object to map detector variant to detector version.
Definition: JDetectorVersion.hh:166
JTOOLS::n
const int n
Definition: JPolint.hh:628
std::vector
Definition: JSTDTypes.hh:12
JModuleAddress.hh
JObjectID.hh
JLANG::JTYPELIST
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:377
JDETECTOR::JDetector::operator>>
friend std::istream & operator>>(std::istream &in, JDetector &detector)
Read detector from input.
Definition: JDetector.hh:268
JDETECTOR::JDetector::read
virtual JReader & read(JReader &in)
Read from input.
Definition: JDetector.hh:370
JUTM::JUTMPosition::add
JUTMPosition & add(const JUTMPosition &pos)
Add UTM position.
Definition: JUTMPosition.hh:153
JDETECTOR::JDetector::getModule
const JModule & getModule(const JModuleAddress &address) const
Get module parameters.
Definition: JDetector.hh:183
JDETECTOR::JDetector::write
virtual JWriter & write(JWriter &out) const
Write to output.
Definition: JDetector.hh:400
JDetectorVersion.hh
JDETECTOR::JDetectorVersion::JVersion_t
JVersion_t
Enumeration of version types.
Definition: JDetectorVersion.hh:42
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JDETECTOR::JModuleLocation
Logical location of module.
Definition: JModuleLocation.hh:36
JGEOMETRY3D::JVector3D
Data structure for vector in three dimensions.
Definition: JVector3D.hh:33
JSerialisable.hh
JDETECTOR::JDetectorVersion::V1
First version.
Definition: JDetectorVersion.hh:43
JIO::JWriter
Interface for binary output.
Definition: JSerialisable.hh:131
JMultiEquals.hh
JDETECTOR::JModuleAddress
Address of module in detector data structure.
Definition: JModuleAddress.hh:30
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JDETECTOR::JPMT::setVersion
static void setVersion(const JVersion &version)
Set detector version.
Definition: JPMT.hh:134
JDETECTOR::JModule
Data structure for a composite optical module.
Definition: JModule.hh:49
JDETECTOR::JPMT
Data structure for PMT geometry and calibration.
Definition: JPMT.hh:53
JLANG::JObjectID
Auxiliary class for object identification.
Definition: JObjectID.hh:27
JEEP::JComment
Auxiliary class for comment.
Definition: JComment.hh:34
JDETECTOR::JDetector::JDetector
JDetector()
Default constructor.
Definition: JDetector.hh:92
JDETECTOR::JDetector::getModule
JModule & getModule(const JModuleLocation &location)
Get module parameters.
Definition: JDetector.hh:225
JDETECTOR::JPMTAddress
Address of PMT in detector data structure.
Definition: JPMTAddress.hh:32
JLANG::JMultiEquals
Template definition of auxiliary base class for composite data structures composed of base classes wi...
Definition: JMultiEquals.hh:31
JDETECTOR::JDetector
Detector data structure.
Definition: JDetector.hh:80
JEEP::JVersion
Auxiliary class for version identifier.
Definition: JVersion.hh:30
std
Definition: jaanetDictionary.h:36
JDETECTOR::JDetectorVersion
Detector version.
Definition: JDetectorVersion.hh:35
JDETECTOR::JDetector::JDetector
JDetector(const JObjectID &id, const JVersion &version, const JDetectorHeader &header)
Constructor.
Definition: JDetector.hh:108
JEEP::JVersion::getVariant
const std::string & getVariant() const
Get variant.
Definition: JVersion.hh:90
JDETECTOR::JDetector::getPMT
JPMT & getPMT(const JPMTAddress &address)
Get PMT parameters.
Definition: JDetector.hh:255
JPMTAddress.hh
JPMT.hh
JDETECTOR::JDetector::getModule
const JModule & getModule(const JModuleLocation &location) const
Get module parameters.
Definition: JDetector.hh:207
JDETECTOR::JDetector::getPMT
const JPMT & getPMT(const JPMTAddress &address) const
Get PMT parameters.
Definition: JDetector.hh:243
JDETECTOR
Auxiliary classes and methods for detector calibration.
Definition: JAnchor.hh:12
JDETECTOR::JPMTAddress::second
int second
index of PMT in module data structure.
Definition: JPMTAddress.hh:100
JComment.hh