Jpp
 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 "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  */
127  {
128  static_cast<JDetectorVersion&>(*this).setVersion(version);
129 
130  JPMT::setVersion(this->getVersion());
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  * Move detector elements.
158  *
159  * \param pos offset position
160  * \return this JDetector
161  */
163  {
164  for (iterator i = begin(); i != end(); ++i) {
165  i->add(pos);
166  }
167 
168  return *this;
169  }
170 
171 
172  /**
173  * Move detector elements.
174  *
175  * \param pos offset position
176  * \return this JDetector
177  */
179  {
180  for (iterator i = begin(); i != end(); ++i) {
181  i->sub(pos);
182  }
183 
184  return *this;
185  }
186 
187 
188  /**
189  * Get module parameters.
190  *
191  * \param address module address
192  * \return module parameters
193  */
194  const JModule& getModule(const JModuleAddress& address) const
195  {
196  return at(address.first);
197  }
198 
199 
200  /**
201  * Get module parameters.
202  *
203  * \param address module address
204  * \return module parameters
205  */
207  {
208  return at(address.first);
209  }
210 
211 
212  /**
213  * Get module parameters.
214  *
215  * \param location module location
216  * \return module parameters
217  */
218  const JModule& getModule(const JLocation& location) const
219  {
220  for (JDetector::const_iterator module = this->begin(); module != this->end(); ++module) {
221  if (*module == location) {
222  return *module;
223  }
224  }
225 
226  THROW(JIndexOutOfRange, "JDetector::getModule()");
227  }
228 
229 
230  /**
231  * Get module parameters.
232  *
233  * \param location module location
234  * \return module parameters
235  */
236  JModule& getModule(const JLocation& location)
237  {
238  for (JDetector::iterator module = this->begin(); module != this->end(); ++module) {
239  if (*module == location) {
240  return *module;
241  }
242  }
243 
244  THROW(JIndexOutOfRange, "JDetector::getModule()");
245  }
246 
247 
248  /**
249  * Get PMT parameters.
250  *
251  * \param address JPMTAddress
252  * \return JPMT
253  */
254  const JPMT& getPMT(const JPMTAddress& address) const
255  {
256  return at(address.first).at(address.second);
257  }
258 
259 
260  /**
261  * Get PMT parameters.
262  *
263  * \param address JPMTAddress
264  * \return JPMT
265  */
266  JPMT& getPMT(const JPMTAddress& address)
267  {
268  return at(address.first).at(address.second);
269  }
270 
271 
272  /**
273  * Read detector from input.
274  *
275  * \param in input stream
276  * \param detector detector
277  * \return input stream
278  */
279  friend inline std::istream& operator>>(std::istream& in, JDetector& detector)
280  {
281  using namespace std;
282 
283  detector.clear();
284 
285  string buffer;
286 
287  if (in
288  >> detector.comment
289  >> static_cast<JObjectID&>(detector)
290  >> buffer) {
291 
292  int n;
293  int version = -1;
294 
295  try {
296  version = getDetectorVersion(buffer);
297  }
298  catch(std::exception&) {}
299 
300  switch (version) {
301 
302  case V3:
303  case V2:
304  in >> static_cast<JDetectorHeader&>(detector);
305 
306  case V1:
307  in >> n;
308  detector.setVersion(buffer);
309  break;
310 
311  default:
312  detector.setVersion(putDetectorVersion(V1));
313  istringstream(buffer) >> n;
314  break;
315  }
316 
317  for (JModule module; n != 0 && in >> module; --n) {
318  detector.push_back(module);
319  }
320  }
321 
322  return in;
323  }
324 
325 
326  /**
327  * Write detector to output.
328  *
329  * \param out output stream
330  * \param detector detector
331  * \return output stream
332  */
333  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
334  {
335  using namespace std;
336 
337  const ios::fmtflags format = out.flags();
338  const streamsize precision = out.precision();
339 
340  out.setf(ios::fixed);
341  out.precision(1);
342 
343  out << detector.comment;
344  out << static_cast<const JObjectID&>(detector);
345 
346  switch (getDetectorVersion(detector.getVariant())) {
347 
348  case V3:
349  case V2:
350  out << " ";
351  out << static_cast<const JDetectorVersion&>(detector) << endl;
352  out << static_cast<const JDetectorHeader&> (detector) << endl;
353  break;
354 
355  default:
356  out << " ";
357  break;
358  }
359 
360  out << detector.size() << endl;
361 
362  out.precision(3);
363 
364  for (const_iterator i = detector.begin(); i != detector.end(); ++i) {
365  out << *i << endl;
366  }
367 
368  out.flags(format);
369  out.precision(precision);
370 
371  return out;
372  }
373 
374 
375  /**
376  * Read from input.
377  *
378  * \param in reader
379  * \return reader
380  */
381  virtual JReader& read(JReader& in)
382  {
383  clear();
384 
385  int n;
386 
387  in >> static_cast<JObjectID&> (*this);
388  in >> static_cast<JDetectorVersion&>(*this);
389  in >> static_cast<JDetectorHeader&> (*this);
390  in >> n;
391 
393 
394  for (JModule module; n != 0; --n) {
395 
396  in >> module;
397 
398  push_back(module);
399  }
400 
401  return in;
402  }
403 
404 
405  /**
406  * Write to output.
407  *
408  * \param out writer
409  * \return writer
410  */
411  virtual JWriter& write(JWriter& out) const
412  {
413  int n = size();
414 
415  out << static_cast<const JObjectID&> (*this);
416  out << static_cast<const JDetectorVersion&>(*this);
417  out << static_cast<const JDetectorHeader&> (*this);
418  out << n;
419 
420  for (const_iterator i = begin(); i != end(); ++i) {
421  out << *i;
422  }
423 
424  return out;
425  }
426 
427 
428 
430  };
431 }
432 
433 #endif
Exceptions.
Interface for binary output.
Data structure for a composite optical module.
Definition: JModule.hh:50
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:333
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
virtual JWriter & write(JWriter &out) const
Write to output.
Definition: JDetector.hh:411
int second
index of PMT in module data structure.
Definition: JPMTAddress.hh:100
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
friend std::istream & operator>>(std::istream &in, JDetector &detector)
Read detector from input.
Definition: JDetector.hh:279
virtual JReader & read(JReader &in)
Read from input.
Definition: JDetector.hh:381
void setVersion(const JVersion &version)
Set version.
Definition: JVersion.hh:78
const JPMT & getPMT(const JPMTAddress &address) const
Get PMT parameters.
Definition: JDetector.hh:254
void setVersion(const JVersion &version)
Set version.
Definition: JDetector.hh:126
JModule & getModule(const JLocation &location)
Get module parameters.
Definition: JDetector.hh:236
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.
Version with UTC time and UTM position data.
Version with PMT status field and comments.
Data structure for vector in three dimensions.
Definition: JVector3D.hh:33
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.
const JModule & getModule(const JModuleAddress &address) const
Get module parameters.
Definition: JDetector.hh:194
Auxiliary class for comment.
Definition: JComment.hh:29
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:27
JPMT & getPMT(const JPMTAddress &address)
Get PMT parameters.
Definition: JDetector.hh:266
alias put_queue eval echo n
Definition: qlib.csh:19
JDetector & operator+=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:162
JUTMPosition & add(const JUTMPosition &pos)
Add UTM position.
const JModule & getModule(const JLocation &location) const
Get module parameters.
Definition: JDetector.hh:218
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
Auxiliary class for version identifier.
Definition: JVersion.hh:30
JDetector & operator-=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:178
version
Definition: JCalibratePMT.sh:7
JModule & getModule(const JModuleAddress &address)
Get module parameters.
Definition: JDetector.hh:206
const JVersion & getVersion() const
Get version.
Definition: JVersion.hh:56
Data structure for a composite optical module.