Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
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"
17#include "JDetector/JPMT.hh"
18#include "JDetector/JModule.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 */
33namespace JDETECTOR {}
34namespace JPP { using namespace JDETECTOR; }
35
36namespace JDETECTOR {
37
39 using JLANG::JTYPELIST;
42 using JEEP::JComment;
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> ".datx" </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(),
105 std::vector<JModule>(),
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),
122 JDetectorHeader (header),
123 std::vector<JModule>(),
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
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 */
174
175
176 /**
177 * Set status of all modules.
178 *
179 * \param bit bit
180 */
181 void setModuleStatus(const int bit)
182 {
183 for (iterator module = this->begin(); module != this->end(); ++module) {
184 module->getStatus().set(bit);
185 }
186 }
187
188
189 /**
190 * Reset status of all modules.
191 *
192 * \param bit bit
193 */
194 void resetModuleStatus(const int bit)
195 {
196 for (iterator module = this->begin(); module != this->end(); ++module) {
197 module->getStatus().reset(bit);
198 }
199 }
200
201
202 /**
203 * Set status of all PMTs.
204 *
205 * \param bit bit
206 */
207 void setPMTStatus(const int bit)
208 {
209 for (iterator module = this->begin(); module != this->end(); ++module) {
210 for (JModule::iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
211 pmt->getStatus().set(bit);
212 }
213 }
214 }
215
216
217 /**
218 * Reset status of all PMTs.
219 *
220 * \param bit bit
221 */
222 void resetPMTStatus(const int bit)
223 {
224 for (iterator module = this->begin(); module != this->end(); ++module) {
225 for (JModule::iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
226 pmt->getStatus().reset(bit);
227 }
228 }
229 }
230
231
232 /**
233 * Move detector elements.
234 *
235 * \param pos offset position
236 * \return this JDetector
237 */
239 {
240 for (iterator i = begin(); i != end(); ++i) {
241 i->add(pos);
242 }
243
244 return *this;
245 }
246
247
248 /**
249 * Move detector elements.
250 *
251 * \param pos offset position
252 * \return this JDetector
253 */
255 {
256 for (iterator i = begin(); i != end(); ++i) {
257 i->sub(pos);
258 }
259
260 return *this;
261 }
262
263
264 /**
265 * Get module parameters.
266 *
267 * \param address module address
268 * \return module parameters
269 */
270 const JModule& getModule(const JModuleAddress& address) const
271 {
272 return at(address.first);
273 }
274
275
276 /**
277 * Get module parameters.
278 *
279 * \param address module address
280 * \return module parameters
281 */
283 {
284 return at(address.first);
285 }
286
287
288 /**
289 * Check availability of module parameters.
290 *
291 * \param location module location
292 * \return true if available; else false
293 */
294 bool hasModule(const JLocation& location) const
295 {
296 for (JDetector::const_iterator module = this->begin(); module != this->end(); ++module) {
297 if (module->getLocation() == location) {
298 return true;
299 }
300 }
301
302 return false;
303 }
304
305
306 /**
307 * Get module parameters.
308 *
309 * \param location module location
310 * \return module parameters
311 */
312 const JModule& getModule(const JLocation& location) const
313 {
314 for (JDetector::const_iterator module = this->begin(); module != this->end(); ++module) {
315 if (module->getLocation() == location) {
316 return *module;
317 }
318 }
319
320 THROW(JIndexOutOfRange, "Invalid location " << location);
321 }
322
323
324 /**
325 * Get module parameters.
326 *
327 * \param location module location
328 * \return module parameters
329 */
330 JModule& getModule(const JLocation& location)
331 {
332 for (JDetector::iterator module = this->begin(); module != this->end(); ++module) {
333 if (module->getLocation() == location) {
334 return *module;
335 }
336 }
337
338 THROW(JIndexOutOfRange, "Invalid location " << location);
339 }
340
341
342 /**
343 * Get PMT parameters.
344 *
345 * \param address JPMTAddress
346 * \return JPMT
347 */
348 const JPMT& getPMT(const JPMTAddress& address) const
349 {
350 return at(address.first).at(address.second);
351 }
352
353
354 /**
355 * Get PMT parameters.
356 *
357 * \param address JPMTAddress
358 * \return JPMT
359 */
360 JPMT& getPMT(const JPMTAddress& address)
361 {
362 return at(address.first).at(address.second);
363 }
364
365
366 /**
367 * Read detector from input.
368 *
369 * \param in input stream
370 * \param detector detector
371 * \return input stream
372 */
373 friend inline std::istream& operator>>(std::istream& in, JDetector& detector)
374 {
375 using namespace std;
376
377 detector.clear();
378
379 string buffer;
380
381 if (in
382 >> detector.comment
383 >> static_cast<JDetectorID&>(detector)
384 >> buffer) {
385
386 int number_of_modules;
387 int version = -1;
388
389 try {
390 version = getDetectorVersion(buffer);
391 }
392 catch(std::exception&) {}
393
394 switch (version) {
395
396 default:
397 in >> static_cast<JDetectorHeader&>(detector);
398
399 case V1:
400 in >> number_of_modules;
401 detector.setVersion(buffer);
402 break;
403
404 case -1:
406 istringstream(buffer) >> number_of_modules;
407 break;
408 }
409
410 for (JModule module; number_of_modules != 0 && in >> module; --number_of_modules) {
411 detector.push_back(module);
412 }
413 }
414
415 return in;
416 }
417
418
419 /**
420 * Write detector to output.
421 *
422 * \param out output stream
423 * \param detector detector
424 * \return output stream
425 */
426 friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
427 {
428 using namespace std;
429 using namespace JPP;
430
431 detector.setVersion();
432
433 setFormat<JPosition3D> (JFormat_t( 9, 3, std::ios::fixed | std::ios::showpos));
434 setFormat<JDirection3D> (JFormat_t( 9, 6, std::ios::fixed | std::ios::showpos));
435 setFormat<JQuaternion3D>(JFormat_t( 9, 6, std::ios::fixed | std::ios::showpos));
436 setFormat<JCalibration> (JFormat_t(10, 3, std::ios::fixed | std::ios::showpos));
437
438 if (getDetectorVersion(detector.getVersion()) >= V3) {
439 out << detector.comment;
440 }
441
442 out << static_cast<const JDetectorID&>(detector);
443
444 switch (getDetectorVersion(detector.getVersion())) {
445
446 default:
447 out << " ";
448 out << static_cast<const JDetectorVersion&>(detector) << endl;
449 out << static_cast<const JDetectorHeader&> (detector) << endl;
450 break;
451
452 case V1:
453 out << " ";
454 break;
455 }
456
457 out << detector.size() << endl;
458
459 for (const_iterator i = detector.begin(); i != detector.end(); ++i) {
460 out << *i << endl;
461 }
462
463 return out;
464 }
465
466
467 /**
468 * Read from input.
469 *
470 * \param in reader
471 * \return reader
472 */
473 virtual JReader& read(JReader& in) override
474 {
475 using namespace std;
476 using namespace JPP;
477
478 this->clear();
479
481
482 char prefix[sizeof(JDetectorID)];
483
484 for (string buffer; in.read(prefix, N) == N && count(prefix, prefix + N, JComment::START_COMMENT) == N; ) {
485
486 in >> buffer;
487
488 this->comment.add(buffer);
489 }
490
491 if (this->comment.empty()) {
492 THROW(JIOException, "Missing comments");
493 }
494
495 in.read(&prefix[N], sizeof(JDetectorID) - N);
496
497 JByteArrayReader is(prefix, N);
498
499 int number_of_modules;
500
501 is >> static_cast<JDetectorID&> (*this);
502 in >> static_cast<JDetectorVersion&>(*this);
503 in >> static_cast<JDetectorHeader&> (*this);
504 in >> number_of_modules;
505
506 this->setVersion();
507 this->resize(number_of_modules);
508
509 for (JDetector::iterator out = this->begin(); number_of_modules != 0; --number_of_modules, ++out) {
510 in >> *out;
511 }
512
513 return in;
514 }
515
516
517 /**
518 * Write to output.
519 *
520 * \param out writer
521 * \return writer
522 */
523 virtual JWriter& write(JWriter& out) const override
524 {
525 using namespace std;
526 using namespace JPP;
527
528 if (getDetectorVersion(this->getVersion()) < V3 || this->comment.empty()) {
529 THROW(JIOException, "Version " << getDetectorVersion(this->getVersion()) << " < " << V3 << " or missing comments.");
530 }
531
532 this->setVersion();
533
534 if (getDetectorVersion(this->getVersion()) >= V3) {
535
536 for (JComment::const_iterator i = this->comment.begin(); i != this->comment.end(); ++i) {
537
538 for (size_t i = 0; i != LENGTH_START_OF_COMMENT; ++i) {
540 }
541
542 out << *i;
543 }
544 }
545
546 const int number_of_modules = this->size();
547
548 out << static_cast<const JDetectorID&> (*this);
549 out << static_cast<const JDetectorVersion&>(*this);
550 out << static_cast<const JDetectorHeader&> (*this);
551 out << number_of_modules;
552
553 for (const_iterator module = this->begin(); module != this->end(); ++module) {
554 out << *module;
555 }
556
557 return out;
558 }
559
560
562
563
564 /**
565 * Enumeration for different lengths of start of comment in binary I/O.
566 */
571
572
573 /**
574 * Get option for short start of comment in binary I/O.
575 *
576 * \return option
577 */
582
583
584 /**
585 * Set option for short start of comment in binary I/O.
586 *
587 * \param option option
588 */
589 static void setStartOfComment(const start_of_comment_type option)
590 {
591 get_start_of_comment() = option;
592 }
593
594 protected:
595 /**
596 * Set version.
597 *
598 * This method should be called to set up the handling of detector version specific module and PMT I/O.
599 */
600 void setVersion() const
601 {
603 JPMT ::setVersion(this->getVersion());
604 }
605
606
607 /**
608 * Get option for short start of comment in binary I/O.
609 *
610 * \return option
611 */
613 {
614 static start_of_comment_type start_of_comment = NEW_START_OF_COMMENT;
615
616 return start_of_comment;
617 }
618
619
620 /**
621 * Length of start of comment in binary I/O.
622 */
623 static const size_t LENGTH_START_OF_COMMENT = sizeof(JDetectorID);
624 };
625}
626
627#endif
Data structure for detector version.
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
I/O manipulators.
void setFormat(const JFormat_t &format)
Set format for given type.
Definition JManip.hh:714
Data structure for optical module.
Data structure for PMT geometry and calibration.
STD extensions for binary I/O.
Data structure for detector header.
Detector data structure.
Definition JDetector.hh:96
bool setVersion(const JDetectorVersion::JVersion_t &version, const bool force=false)
Set version.
Definition JDetector.hh:152
void resetPMTStatus(const int bit)
Reset status of all PMTs.
Definition JDetector.hh:222
static const size_t LENGTH_START_OF_COMMENT
Length of start of comment in binary I/O.
Definition JDetector.hh:623
JDetector & operator+=(const JVector3D &pos)
Move detector elements.
Definition JDetector.hh:238
static start_of_comment_type & get_start_of_comment()
Get option for short start of comment in binary I/O.
Definition JDetector.hh:612
start_of_comment_type
Enumeration for different lengths of start of comment in binary I/O.
Definition JDetector.hh:567
virtual JReader & read(JReader &in) override
Read from input.
Definition JDetector.hh:473
void resetModuleStatus(const int bit)
Reset status of all modules.
Definition JDetector.hh:194
void setPMTStatus(const int bit)
Set status of all PMTs.
Definition JDetector.hh:207
static start_of_comment_type getStartOfComment()
Get option for short start of comment in binary I/O.
Definition JDetector.hh:578
const JModule & getModule(const JLocation &location) const
Get module parameters.
Definition JDetector.hh:312
friend std::istream & operator>>(std::istream &in, JDetector &detector)
Read detector from input.
Definition JDetector.hh:373
static void setStartOfComment(const start_of_comment_type option)
Set option for short start of comment in binary I/O.
Definition JDetector.hh:589
JDetector & operator-=(const JVector3D &pos)
Move detector elements.
Definition JDetector.hh:254
void setVersion(const JVersion &version)
Set version.
Definition JDetector.hh:135
void setModuleStatus(const int bit)
Set status of all modules.
Definition JDetector.hh:181
JDetector(const JDetectorID &id, const JVersion &version, const JDetectorHeader &header)
Constructor.
Definition JDetector.hh:117
JModule & getModule(const JModuleAddress &address)
Get module parameters.
Definition JDetector.hh:282
bool hasModule(const JLocation &location) const
Check availability of module parameters.
Definition JDetector.hh:294
friend std::ostream & operator<<(std::ostream &out, const JDetector &detector)
Write detector to output.
Definition JDetector.hh:426
void setVersion() const
Set version.
Definition JDetector.hh:600
JPMT & getPMT(const JPMTAddress &address)
Get PMT parameters.
Definition JDetector.hh:360
const JModule & getModule(const JModuleAddress &address) const
Get module parameters.
Definition JDetector.hh:270
JModule & getModule(const JLocation &location)
Get module parameters.
Definition JDetector.hh:330
const JPMT & getPMT(const JPMTAddress &address) const
Get PMT parameters.
Definition JDetector.hh:348
virtual JWriter & write(JWriter &out) const override
Write to output.
Definition JDetector.hh:523
JDetector()
Default constructor.
Definition JDetector.hh:101
bool setToLatestVersion()
Set to latest version.
Definition JDetector.hh:170
Logical location of module.
Definition JLocation.hh:40
Address of module in detector data structure.
int first
index of module in detector data structure
Data structure for a composite optical module.
Definition JModule.hh:75
static void setVersion(const JVersion &version)
Set detector version.
Definition JModule.hh:130
Address of PMT in detector data structure.
int second
index of PMT in module data structure.
Data structure for PMT geometry, calibration and status.
Definition JPMT.hh:49
Data structure for vector in three dimensions.
Definition JVector3D.hh:36
Byte array binary input.
Interface for binary input.
Forward declaration of binary output.
Interface for binary output.
virtual int read(char *buffer, const int length)=0
Read byte array.
Exception for I/O.
Exception for accessing an index in a collection that is outside of its range.
Auxiliary class for object identification.
Definition JObjectID.hh:25
file Auxiliary data structures and methods for detector calibration.
Definition JAnchor.hh:12
static const JGetDetectorVersion getDetectorVersion
Function object to map detector version to numerical value.
T getLatestDetectorVersion()
Get latest detector version.
static const JPutDetectorVersion putDetectorVersion(getDetectorVersion)
Function object to map numerical value to detector version.
JLANG::JObjectID JDetectorID
Type definition of detector identifier.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Detector file.
Definition JHead.hh:227
JDetectorVersion()
Default constructor.
JVersion_t
Enumeration of version types.
@ V3
Version with PMT status field and comments.
Auxiliary class for version identifier.
void setVersion(const std::string &version)
Set version.
const std::string & getVersion() const
Get version.
Auxiliary class for comment.
Definition JComment.hh:43
static constexpr char START_COMMENT
start comment
Definition JComment.hh:48
JComment & add(const std::string &comment)
Add comment.
Definition JComment.hh:100
Data structure for format specifications.
Definition JManip.hh:524
Template definition of auxiliary base class for data structures composed of multiple base classes wit...
Auxiliary class for recursive type list generation.
Definition JTypeList.hh:351