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  class JDetector :
78  public JObjectID,
79  public JDetectorVersion,
80  public JDetectorHeader,
81  public std::vector<JModule>,
82  public JMultiEquals<JDetector, JTYPELIST<JObjectID, JVersion>::typelist>,
83  public JSerialisable
84  {
85  public:
86  /**
87  * Default constructor.
88  */
90  JObjectID(),
92  JDetectorHeader (),
93  std::vector<JModule>(),
95  {}
96 
97 
98  /**
99  * Constructor.
100  *
101  * \param id detector identifier
102  * \param version version
103  * \param header header
104  */
105  JDetector(const JObjectID& id,
106  const JVersion& version,
107  const JDetectorHeader& header) :
108  JObjectID(id),
109  JDetectorVersion(version),
110  JDetectorHeader (header),
111  std::vector<JModule>(),
112  JSerialisable()
113  {
115  }
116 
117 
118  /**
119  * Set version.
120  *
121  * \param version version
122  */
123  void setVersion(const JVersion& version)
124  {
125  static_cast<JDetectorVersion&>(*this).setVersion(version);
126 
128  }
129 
130 
131  /**
132  * Set version.
133  *
134  * \param version version
135  */
137  {
138  setVersion(putDetectorVersion(version));
139  }
140 
141 
142  /**
143  * Move detector elements.
144  *
145  * \param pos offset position
146  * \return this JDetector
147  */
149  {
150  for (iterator i = begin(); i != end(); ++i) {
151  i->add(pos);
152  }
153 
154  return *this;
155  }
156 
157 
158  /**
159  * Move detector elements.
160  *
161  * \param pos offset position
162  * \return this JDetector
163  */
165  {
166  for (iterator i = begin(); i != end(); ++i) {
167  i->sub(pos);
168  }
169 
170  return *this;
171  }
172 
173 
174  /**
175  * Get module parameters.
176  *
177  * \param address module address
178  * \return module parameters
179  */
180  const JModule& getModule(const JModuleAddress& address) const
181  {
182  return at(address.first);
183  }
184 
185 
186  /**
187  * Get module parameters.
188  *
189  * \param address module address
190  * \return module parameters
191  */
193  {
194  return at(address.first);
195  }
196 
197 
198  /**
199  * Get module parameters.
200  *
201  * \param location module location
202  * \return module parameters
203  */
204  const JModule& getModule(const JModuleLocation& location) const
205  {
206  for (JDetector::const_iterator module = this->begin(); module != this->end(); ++module) {
207  if (*module == location) {
208  return *module;
209  }
210  }
211 
212  THROW(JIndexOutOfRange, "JDetector::getModule()");
213  }
214 
215 
216  /**
217  * Get module parameters.
218  *
219  * \param location module location
220  * \return module parameters
221  */
223  {
224  for (JDetector::iterator module = this->begin(); module != this->end(); ++module) {
225  if (*module == location) {
226  return *module;
227  }
228  }
229 
230  THROW(JIndexOutOfRange, "JDetector::getModule()");
231  }
232 
233 
234  /**
235  * Get PMT parameters.
236  *
237  * \param address JPMTAddress
238  * \return JPMT
239  */
240  const JPMT& getPMT(const JPMTAddress& address) const
241  {
242  return at(address.first).at(address.second);
243  }
244 
245 
246  /**
247  * Get PMT parameters.
248  *
249  * \param address JPMTAddress
250  * \return JPMT
251  */
252  JPMT& getPMT(const JPMTAddress& address)
253  {
254  return at(address.first).at(address.second);
255  }
256 
257 
258  /**
259  * Read detector from input.
260  *
261  * \param in input stream
262  * \param detector detector
263  * \return input stream
264  */
265  friend inline std::istream& operator>>(std::istream& in, JDetector& detector)
266  {
267  using namespace std;
268 
269  detector.clear();
270 
271  string buffer;
272 
273  if (in
274  >> detector.comment
275  >> static_cast<JObjectID&>(detector)
276  >> buffer) {
277 
278  int n;
279  int version = -1;
280 
281  try {
282  version = getDetectorVersion(buffer);
283  }
284  catch(std::exception&) {}
285 
286  switch (version) {
287 
288  case V3:
289  case V2:
290  in >> static_cast<JDetectorHeader&>(detector);
291 
292  case V1:
293  in >> n;
294  detector.setVersion(buffer);
295  break;
296 
297  default:
298  detector.setVersion(putDetectorVersion(V1));
299  istringstream(buffer) >> n;
300  break;
301  }
302 
303  for (JModule module; n != 0 && in >> module; --n) {
304  detector.push_back(module);
305  }
306  }
307 
308  return in;
309  }
310 
311 
312  /**
313  * Write detector to output.
314  *
315  * \param out output stream
316  * \param detector detector
317  * \return output stream
318  */
319  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
320  {
321  using namespace std;
322 
323  const ios::fmtflags format = out.flags();
324  const streamsize precision = out.precision();
325 
326  out.setf(ios::fixed);
327  out.precision(1);
328 
329  out << detector.comment;
330  out << static_cast<const JObjectID&>(detector);
331 
332  switch (getDetectorVersion(detector.getVariant())) {
333 
334  case V3:
335  case V2:
336  out << " ";
337  out << static_cast<const JDetectorVersion&>(detector) << endl;
338  out << static_cast<const JDetectorHeader&> (detector) << endl;
339  break;
340 
341  default:
342  out << " ";
343  break;
344  }
345 
346  out << detector.size() << endl;
347 
348  out.precision(3);
349 
350  for (const_iterator i = detector.begin(); i != detector.end(); ++i) {
351  out << *i << endl;
352  }
353 
354  out.flags(format);
355  out.precision(precision);
356 
357  return out;
358  }
359 
360 
361  /**
362  * Read from input.
363  *
364  * \param in reader
365  * \return reader
366  */
367  virtual JReader& read(JReader& in)
368  {
369  clear();
370 
371  int n;
372 
373  in >> static_cast<JObjectID&> (*this);
374  in >> static_cast<JDetectorVersion&>(*this);
375  in >> static_cast<JDetectorHeader&> (*this);
376  in >> n;
377 
379 
380  for (JModule module; n != 0; --n) {
381 
382  in >> module;
383 
384  push_back(module);
385  }
386 
387  return in;
388  }
389 
390 
391  /**
392  * Write to output.
393  *
394  * \param out writer
395  * \return writer
396  */
397  virtual JWriter& write(JWriter& out) const
398  {
399  int n = size();
400 
401  out << static_cast<const JObjectID&> (*this);
402  out << static_cast<const JDetectorVersion&>(*this);
403  out << static_cast<const JDetectorHeader&> (*this);
404  out << n;
405 
406  for (const_iterator i = begin(); i != end(); ++i) {
407  out << *i;
408  }
409 
410  return out;
411  }
412 
413 
414 
416  };
417 }
418 
419 #endif
Exceptions.
Interface for binary output.
Data structure for a composite optical module.
Definition: JModule.hh:47
JVersion_t
Enumeration of version types.
void setVersion(const JDetectorVersion::JVersion_t &version)
Set version.
Definition: JDetector.hh:136
Detector data structure.
Definition: JDetector.hh:77
friend std::ostream & operator<<(std::ostream &out, const JDetector &detector)
Write detector to output.
Definition: JDetector.hh:319
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
virtual JWriter & write(JWriter &out) const
Write to output.
Definition: JDetector.hh:397
int second
index of PMT in module data structure.
Definition: JPMTAddress.hh:100
friend std::istream & operator>>(std::istream &in, JDetector &detector)
Read detector from input.
Definition: JDetector.hh:265
virtual JReader & read(JReader &in)
Read from input.
Definition: JDetector.hh:367
const JPMT & getPMT(const JPMTAddress &address) const
Get PMT parameters.
Definition: JDetector.hh:240
void setVersion(const JVersion &version)
Set version.
Definition: JDetector.hh:123
Data structure for detector header.
void setVersion(const JVersion &version)
Set version.
Definition: JVersion.hh:79
Template definition of auxiliary base class for composite data structures composed of base classes wi...
Definition: JMultiEquals.hh:31
JDetector()
Default constructor.
Definition: JDetector.hh:89
int first
index of module in detector data structure
Forward declaration of binary output.
range_type & sub(argument_type x)
Subtract offset.
Definition: JRange.hh:399
Version with UTC time and UTM position data.
const std::string & getVariant() const
Get variant.
Definition: JVersion.hh:90
Version with PMT status field and comments.
Data structure for vector in three dimensions.
Definition: JVector3D.hh:32
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:377
range_type & add(argument_type x)
Add offset.
Definition: JRange.hh:385
JModule & getModule(const JModuleLocation &location)
Get module parameters.
Definition: JDetector.hh:222
Data structure for PMT geometry and calibration.
Definition: JPMT.hh:52
Data structure for detector version.
JDetector(const JObjectID &id, const JVersion &version, const JDetectorHeader &header)
Constructor.
Definition: JDetector.hh:105
Address of module in detector data structure.
Interface for binary input.
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:180
Auxiliary class for comment.
Definition: JComment.hh:34
Auxiliary class for version identifier.
Definition: JVersion.hh:30
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:132
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:252
const JModule & getModule(const JModuleLocation &location) const
Get module parameters.
Definition: JDetector.hh:204
JDetector & operator+=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:148
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
JDetector & operator-=(const JVector3D &pos)
Move detector elements.
Definition: JDetector.hh:164
JModule & getModule(const JModuleAddress &address)
Get module parameters.
Definition: JDetector.hh:192
Logical location of module.
Data structure for a composite optical module.
const JVersion & getVersion() const
Get version.
Definition: JVersion.hh:57