Jpp  15.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDetectorToolkit.hh
Go to the documentation of this file.
1 #ifndef __JDETECTOR__JDETECTORTOOLKIT__
2 #define __JDETECTOR__JDETECTORTOOLKIT__
3 
4 #include <limits>
5 #include <istream>
6 #include <ostream>
7 #include <iostream>
8 #include <fstream>
9 #include <cmath>
10 #include <vector>
11 #include <set>
12 #include <algorithm>
13 #include <cstdlib>
14 
15 #include "JDetector/JDetector.hh"
17 #include "JDetector/JTimeRange.hh"
20 #include "JDetector/JLocation.hh"
24 #include "JGeometry3D/JVector3D.hh"
25 #include "JGeometry3D/JVersor3D.hh"
27 #include "JPhysics/JConstants.hh"
28 #include "JMath/JConstants.hh"
29 #include "JMath/JMathToolkit.hh"
30 #include "JTools/JRange.hh"
31 #include "JIO/JFileStreamIO.hh"
32 #include "Jeep/JeepToolkit.hh"
33 #include "JLang/JException.hh"
34 #include "JLang/gzstream.h"
35 #include "JLang/JManip.hh"
36 #include "JLang/Jpp.hh"
37 #include "JLang/JType.hh"
38 
39 
40 /**
41  * \author mdejong
42  */
43 
44 namespace JDETECTOR {}
45 namespace JPP { using namespace JDETECTOR; }
46 
47 /**
48  * Auxiliary classes and methods for detector calibration and simulation.
49  */
50 namespace JDETECTOR {
51 
52  using JLANG::JException;
55  using JLANG::JType;
56 
57 
58  /**
59  * File name extensions.
60  */
61  static const char* const GENDET_DETECTOR_FILE_FORMAT = "det"; //!< file format used by gendet
62  static const char* const BINARY_DETECTOR_FILE_FORMAT[] = { "dat", "datx" }; //!< JIO binary file format
63  static const char* const KM3NET_DETECTOR_FILE_FORMAT = "detx"; //!< %KM3NeT standard ASCII format
64  static const char* const ZIPPED_DETECTOR_FILE_FORMAT = "gz"; //!< zipped %KM3NeT standard ASCII format
65  static const char* const GDML_DETECTOR_FILE_FORMAT = "gdml"; //!< KM3Sim input format
66 
67  static const char* const GDML_SCHEMA = getenv("GDML_SCHEMA_DIR"); //!< directory necessary for correct GDML header output
68  static const char* const CAN_MARGIN_M = getenv("CAN_MARGIN_M"); //!< extension of the detector size to comply with the can definition
69  static const char* const G4GDML_DEFAULT_SCHEMALOCATION = "http://service-spi.web.cern.ch/service-spi/app/releases/GDML/schema/gdml.xsd";
70 
71 
72  /**
73  * Get maximal distance between modules in detector.
74  *
75  * \param detector detector
76  * \return maximal distance [m]
77  */
78  inline double getMaximalDistance(const JDetector& detector)
79  {
80  using namespace JPP;
81 
82  double dmax = 0.0;
83 
84  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
85  for (JDetector::const_iterator j = detector.begin(); j != i; ++j) {
86  if (getDistance(i->getPosition(), j->getPosition()) > dmax) {
87  dmax = getDistance(i->getPosition(), j->getPosition());
88  }
89  }
90  }
91 
92  return dmax;
93  }
94 
95 
96  /**
97  * Get rotation over X axis in Geant4 coordinate system
98  *
99  * \param dir direction
100  * \return X-rotation [deg]
101  */
102  inline double GetXrotationG4(const JVersor3D dir)
103  {
104  using namespace JPP;
105 
106  const double phi = atan2(dir.getDY(), dir.getDZ())*(180.0/PI);
107 
108  if (phi < 0.0){
109  return phi + 360.0;
110  }
111  else{
112  return phi;
113  }
114  }
115 
116 
117  /**
118  * Get rotation over Y axis in Geant4 coordinate system
119  *
120  * \param dir direction
121  * \return Y-rotation [deg]
122  */
123  inline double GetYrotationG4(const JVersor3D dir)
124  {
125  using namespace JPP;
126 
127  return asin(-dir.getDX())*(180.0/PI);
128  }
129 
130 
131  inline void read_gdml(std::istream&, JDetector&)
132  {
133  THROW(JException, "Operation not defined.");
134  }
135 
136 
137  /**
138  * Writes KM3Sim GDML input file from detector
139  *
140  * \param out output stream
141  * \param detector detector
142  */
143  inline void write_gdml(std::ostream& out, const JDetector& detector)
144  {
145  using namespace std;
146  using namespace JPP;
147 
148  const double DEFAULT_CAN_MARGIN_M = 350.0; // default can margin [m]
149  const double DEFAULT_WORLD_MARGIN_M = 50.0; // default world margin [m]
150 
151  const JCylinder3D cylinder(detector.begin(), detector.end());
152 
153  double can_margin;
154 
155  if (CAN_MARGIN_M) {
156  can_margin = atof(CAN_MARGIN_M);
157  } else {
158  cerr << "CAN_MARGIN_M not defined! Setting standard CAN_MARGIN_M " << DEFAULT_CAN_MARGIN_M << " m." << endl;
159  can_margin = DEFAULT_CAN_MARGIN_M; //this is given in meters like all the dimensions in the GDML file (look at the unit field of every position and solid definition)
160  }
161 
162  const double WorldCylinderHeight = 2*(cylinder.getZmax() - cylinder.getZmin() + can_margin + DEFAULT_WORLD_MARGIN_M);
163  const double WorldRadius = cylinder.getLength() + cylinder.getRadius() + can_margin + DEFAULT_WORLD_MARGIN_M;
164 
165  const double Crust_Z_size = WorldCylinderHeight/2;
166  const double Crust_Z_position = -WorldCylinderHeight/4;
167 
168  out << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<gdml xmlns:gdml=\"http://cern.ch/2001/Schemas/GDML\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"";
169  if (!GDML_SCHEMA) {
170  cerr << "GDML_SCHEMA_DIR NOT DEFINED! Setting default path." << endl;
171  out << G4GDML_DEFAULT_SCHEMALOCATION << "\">\n\n\n";
172  } else {
173  out << GDML_SCHEMA << "gdml.xsd\">\n\n\n";
174  }
175  out << "<!-- Jpp version: "<< getGITVersion() << " -->\n";
176  out << "<define>\n";
177  out << "<rotation name=\"identity\"/>\n<position name=\"zero\"/>\n";
178 
179  int PMTs_NO = 0;
180 
181  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
182 
183  if (module->getFloor() != 0) {
184 
185  const JVector3D center = module->getCenter();
186 
187  out << "<position name=\"PosString" << module->getString() << "_Dom" << module->getID() << "\" unit=\"m\" x=\"" << module->getX() << "\" y=\"" << module->getY() << "\" z=\"" << module->getZ() << "\"/>\n";
188 
189  for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
190 
191  const JVector3D vec = static_cast<JVector3D>(*pmt).sub(center);
192  out << "<position name=\"CathodPosition" << pmt->getID() << "_" << module->getID() << "\" unit=\"m\" x=\"" << vec.getX() << "\" y=\"" << vec.getY() << "\" z=\"" << vec.getZ() << "\"/>\n";
193  out << "<rotation name=\"CathodRotation" << pmt->getID() << "_" << module->getID() << "\" unit=\"deg\" x=\"" << GetXrotationG4(*pmt) << "\" y=\"" << GetYrotationG4(*pmt) << "\" z=\"0.000000\"/>\n";
194  out << "<constant name=\"CathodID_" << PMTs_NO << "\" value=\"" << pmt->getID() << "\"/>\n";
195  PMTs_NO++;
196  }
197  }
198  }
199 
200  out << "<position name=\"OMShift\" unit=\"m\" x=\"0\" y=\"0\" z=\"0.0392\"/>\n";
201  out << "\n\n\n";
202  out << "<!-- end of DU position definitions -->\n<position name=\"CrustPosition\" unit=\"m\" x=\"0\" y=\"0\" z=\"" << Crust_Z_position << "\"/>\n";
203 
204  out << "</define>\n";
205  out << "<materials>\n";
206  out << "</materials>\n";
207  out << "<solids>\n";
208  out << " <box name=\"CrustBox\" lunit=\"m\" x=\"2200\" y=\"2200\" z=\"" << Crust_Z_size << "\"/>\n";
209  out << " <box name=\"StoreyBox\" lunit=\"m\" x=\"0.6\" y=\"0.6\" z=\"0.6\"/>\n";
210  out << " <sphere name=\"OMSphere\" lunit=\"cm\" aunit=\"deg\" rmin=\"0.0\" rmax=\"21.6\" startphi=\"0.0\" deltaphi=\"360.0\" starttheta=\"0.0\" deltatheta=\"180.0\"/>\n";
211  out << " <tube name=\"CathodTube\" lunit=\"cm\" aunit=\"deg\" rmin=\"0.0\" rmax=\"4.7462\" z=\"0.5\" startphi=\"0.0\" deltaphi=\"360.0\"/>\n";
212  out << " <tube name=\"WorldTube\" lunit=\"m\" aunit=\"deg\" rmin=\"0.0\" rmax=\"" << WorldRadius << "\" z=\"" << WorldCylinderHeight << "\" startphi=\"0.0\" deltaphi=\"360.0\"/>\n";
213  out << "</solids>\n\n\n";
214 
215  out << "<structure>\n";
216  out << " <volume name=\"CathodVolume\">\n";
217  out << " <materialref ref=\"Cathod\"/>\n";
218  out << " <solidref ref=\"CathodTube\"/>\n";
219  out << " </volume>\n";
220 
221  out << "<!-- OMVolume(s) construction -->\n";
222 
223  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
224 
225  out << " <volume name=\"OMVolume" << module->getID() << "\">\n";
226  out << " <materialref ref=\"Water\"/>\n";
227  out << " <solidref ref=\"OMSphere\"/>\n";
228 
229  for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
230  out << " <physvol>\n";
231  out << " <volumeref ref=\"CathodVolume\"/>\n";
232  out << " <positionref ref=\"CathodPosition" << pmt->getID() << "_" << module->getID() << "\"/>\n";
233  out << " <rotationref ref=\"CathodRotation" << pmt->getID() << "_" << module->getID() << "\"/>\n";
234  out << " </physvol>\n";
235  }
236 
237  out << " </volume>\n";
238  }
239 
240  out << "<!-- StoreyVolume(s) construction -->\n";
241 
242  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
243  out << " <volume name=\"StoreyVolume" << module->getID() << "\">\n";
244  out << " <materialref ref=\"Water\"/>\n";
245  out << " <solidref ref=\"StoreyBox\"/>\n";
246  out << " <physvol>\n";
247  out << " <volumeref ref=\"OMVolume" << module->getID() << "\"/>\n";
248  out << " <positionref ref=\"OMShift\"/>\n";
249  out << " <rotationref ref=\"identity\"/>\n";
250  out << " </physvol>\n";
251  out << " </volume>\n";
252  }
253 
254  out << "<!-- Crust Volume construction-->\n";
255  out << "<volume name=\"CrustVolume\">\n";
256  out << " <materialref ref=\"Crust\"/>\n";
257  out << " <solidref ref=\"CrustBox\"/>\n";
258  out << "</volume>\n";
259 
260  out << "<!-- World Volume construction-->\n";
261  out << "<volume name=\"WorldVolume\">\n";
262  out << " <materialref ref=\"Water\"/>\n";
263  out << " <solidref ref=\"WorldTube\"/>\n";
264 
265  out << " <physvol>\n";
266  out << " <volumeref ref=\"CrustVolume\"/>\n";
267  out << " <positionref ref=\"CrustPosition\"/>\n";
268  out << " <rotationref ref=\"identity\"/>\n";
269  out << " </physvol>\n";
270 
271  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
272  out << " <physvol>\n";
273  out << " <volumeref ref=\"StoreyVolume" << module->getID() << "\"/>\n";
274  out << " <positionref ref=\"PosString" << module->getString() << "_Dom" << module->getID() << "\"/>\n";
275  out << " <rotationref ref=\"identity\"/>\n";
276  out << " </physvol>\n";
277  }
278 
279  out << "</volume>\n";
280 
281  out << "</structure>\n";
282  out << "<setup name=\"Default\" version=\"1.0\">\n";
283  out << "<world ref=\"WorldVolume\"/>\n";
284  out << "</setup>\n";
285  out << "</gdml>\n";
286  }
287 
288 
289  /**
290  * Get maximal time between modules in detector following causality.
291  *
292  * \param detector detector
293  * \return maximal time [ns]
294  */
295  inline double getMaximalTime(const JDetector& detector)
296  {
297  using namespace JPP;
298 
300  }
301 
302 
303  /**
304  * Get maximal time between modules in detector following causality.
305  * The road width corresponds to the maximal distance traveled by the light.
306  *
307  * \param detector detector
308  * \param roadWidth_m road width [m]
309  * \return maximal time [ns]
310  */
311  inline double getMaximalTime(const JDetector& detector, const double roadWidth_m)
312  {
313  using namespace JPP;
314 
315  const double Dmax_m = getMaximalDistance(detector);
316 
317  return (sqrt((Dmax_m + roadWidth_m*getSinThetaC()) *
318  (Dmax_m - roadWidth_m*getSinThetaC())) +
319  roadWidth_m * getSinThetaC() * getTanThetaC()) * getInverseSpeedOfLight();
320  }
321 
322 
323  /**
324  * Get de-calibrated time range.
325  *
326  * The de-calibrated time range is corrected for minimal and maximal time offset of PMTs in given module.
327  *
328  * \param timeRange time range [ns]
329  * \param module module
330  * \return time range [ns]
331  */
332  inline JTimeRange getTimeRange(const JTimeRange& timeRange, const JModule& module)
333  {
334  if (!module.empty()) {
335 
337 
338  for (JModule::const_iterator pmt = module.begin(); pmt != module.end(); ++pmt) {
339 
340  const JCalibration& calibration = pmt->getCalibration();
341 
342  time_range.include(putTime(timeRange.getLowerLimit(), calibration));
343  time_range.include(putTime(timeRange.getUpperLimit(), calibration));
344  }
345 
346  return time_range;
347 
348  } else {
349 
350  return timeRange;
351  }
352  }
353 
354 
355  /**
356  * Get number of PMTs.
357  *
358  * \param module module
359  * \return number of PMTs
360  */
361  inline int getNumberOfPMTs(const JModule& module)
362  {
363  return module.size();
364  }
365 
366 
367  /**
368  * Get number of PMTs.
369  *
370  * \param detector detector
371  * \return number of PMTs
372  */
373  inline int getNumberOfPMTs(const JDetector& detector)
374  {
375  int number_of_pmts = 0;
376 
377  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
378  number_of_pmts += getNumberOfPMTs(*module);
379  }
380 
381  return number_of_pmts;
382  }
383 
384 
385  /**
386  * Get list of strings identifiers.
387  *
388  * \param detector detector
389  * \return list of string identifiers
390  */
392  {
393  std::set<int> buffer;
394 
395  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
396  buffer.insert(module->getString());
397  }
398 
399  return buffer;
400  }
401 
402 
403  /**
404  * Get number of floors.
405  *
406  * \param detector detector
407  * \return number of floors
408  */
410  {
411  std::set<int> buffer;
412 
413  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
414  buffer.insert(module->getFloor());
415  }
416 
417  return buffer.size();
418  }
419 
420 
421  /**
422  * Type definition for range of floors.
423  */
425 
426 
427  /**
428  * Get range of floors.
429  *
430  * \param detector detector
431  * \return range of floors
432  */
433  inline floor_range getRangeOfFloors(const JDetector& detector)
434  {
435  floor_range buffer = floor_range::DEFAULT_RANGE;
436 
437  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
438  buffer.include(module->getFloor());
439  }
440 
441  return buffer;
442  }
443 
444 
445  /**
446  * Get number of modules.
447  *
448  * \param detector detector
449  * \return number of modules
450  */
452  {
453  std::set<int> buffer;
454 
455  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
456  buffer.insert(module->getID());
457  }
458 
459  return buffer.size();
460  }
461 
462 
463  /**
464  * Load detector from input file.
465  *
466  * Supported file formats:
467  * - ASCII file, extension JDETECTOR::GENDET_DETECTOR_FILE_FORMAT, gendet format;
468  * - binary file, extension JDETECTOR::BINARY_DETECTOR_FILE_FORMAT, Jpp internal format;
469  * - ASCII file, extension JDETECTOR::KM3NET_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
470  * - gzipped file, extension JDETECTOR::ZIPPED_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
471  *
472  * \param file_name file name
473  * \param detector detector
474  */
475  inline void load(const std::string& file_name, JDetector& detector)
476  {
477  using namespace std;
478  using namespace JPP;
479 
481 
482  JMonteCarloDetector buffer(true);
483 
484  ifstream in(file_name.c_str());
485 
486  if (!in) {
487  THROW(JFileOpenException, "File not opened: " << file_name);
488  }
489 
490  in >> buffer;
491 
492  in.close();
493 
494  detector.swap(buffer);
495 
496  } else if (getFilenameExtension(file_name) == BINARY_DETECTOR_FILE_FORMAT[0] ||
498 
499  JFileStreamReader in(file_name.c_str());
500 
501  if (!in) {
502  THROW(JFileOpenException, "File not opened: " << file_name);
503  }
504 
505  try {
506 
507  detector.read(in);
508 
509  } catch(const exception& error) {
510 
511  // recovery procedure for old format of comments
512 
514 
515  in.clear();
516  in.rewind();
517 
518  detector.read(in);
519 
521  }
522 
523  in.close();
524 
525  } else if (getFilenameExtension(file_name) == KM3NET_DETECTOR_FILE_FORMAT) {
526 
527  ifstream in(file_name.c_str());
528 
529  if (!in) {
530  THROW(JFileOpenException, "File not opened: " << file_name);
531  }
532 
533  in >> detector;
534 
535  in.close();
536 
537  } else if (getFilenameExtension(file_name) == ZIPPED_DETECTOR_FILE_FORMAT) {
538 
539  igzstream in(file_name.c_str());
540 
541  if (!in) {
542  THROW(JFileOpenException, "File not opened: " << file_name);
543  }
544 
545  in >> detector;
546 
547  in.close();
548 
549  } else {
550 
551  THROW(JProtocolException, "Protocol not defined: " << file_name);
552  }
553  }
554 
555 
556  /**
557  * Store detector to output file.
558  *
559  * Supported file formats:
560  * - binary file, extension JDETECTOR::BINARY_DETECTOR_FILE_FORMAT, Jpp internal format;
561  * - ASCII file, extension JDETECTOR::KM3NET_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
562  * - gzipped file, extension JDETECTOR::ZIPPED_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
563  * - gdml file, extension JDETECTOR::GDML_DETECTOR_FILE_FORMAT, KM3Sim input format;
564  *
565  * \param file_name file name
566  * \param detector detector
567  */
568  inline void store(const std::string& file_name, const JDetector& detector)
569  {
570  using namespace std;
571  using namespace JPP;
572 
573  if (getFilenameExtension(file_name) == BINARY_DETECTOR_FILE_FORMAT[1]) {
574 
575  JFileStreamWriter out(file_name.c_str());
576 
577  detector.write(out);
578 
579  out.close();
580 
581  } else if (getFilenameExtension(file_name) == KM3NET_DETECTOR_FILE_FORMAT) {
582 
583  std::ofstream out(file_name.c_str());
584 
585  out << detector;
586 
587  out.close();
588 
589  } else if (getFilenameExtension(file_name) == ZIPPED_DETECTOR_FILE_FORMAT) {
590 
591  ogzstream out(file_name.c_str());
592 
593  out << detector;
594 
595  out.close();
596 
597  } else if (getFilenameExtension(file_name) == GDML_DETECTOR_FILE_FORMAT) {
598 
599  std::ofstream out(file_name.c_str());
600 
601  write_gdml(out, detector);
602 
603  out.close();
604 
605  } else {
606 
607  THROW(JProtocolException, "Protocol not defined: " << file_name);
608  }
609  }
610 
611 
612  /**
613  * Get module according module address map.
614  *
615  * \param memo module address map
616  * \param id module identifier
617  * \param location module location
618  * \return module
619  */
620  inline const JModule& getModule(const JModuleAddressMap& memo,
621  const int id = -1,
622  const JLocation& location = JLocation())
623  {
624  static JModule module;
625 
626 
627  module.setID(id);
628 
629  module.setLocation(location);
630 
631  module.resize(memo.size());
632 
633  if (memo.has( 0)) { module[memo[ 0].tdc] = JPMT( 1, JAxis3D(JVector3D(+0.000, +0.000, -0.200), JVersor3D(+0.000, +0.000, -1.000))); }
634 
635  if (memo.has( 1)) { module[memo[ 1].tdc] = JPMT( 2, JAxis3D(JVector3D(+0.000, +0.105, -0.170), JVersor3D(+0.000, +0.527, -0.850))); }
636  if (memo.has( 2)) { module[memo[ 2].tdc] = JPMT( 3, JAxis3D(JVector3D(+0.091, +0.053, -0.170), JVersor3D(+0.456, +0.263, -0.850))); }
637  if (memo.has( 3)) { module[memo[ 3].tdc] = JPMT( 4, JAxis3D(JVector3D(+0.091, -0.053, -0.170), JVersor3D(+0.456, -0.263, -0.850))); }
638  if (memo.has( 4)) { module[memo[ 4].tdc] = JPMT( 5, JAxis3D(JVector3D(+0.000, -0.105, -0.170), JVersor3D(+0.000, -0.527, -0.850))); }
639  if (memo.has( 5)) { module[memo[ 5].tdc] = JPMT( 6, JAxis3D(JVector3D(-0.091, -0.053, -0.170), JVersor3D(-0.456, -0.263, -0.850))); }
640  if (memo.has( 6)) { module[memo[ 6].tdc] = JPMT( 7, JAxis3D(JVector3D(-0.091, +0.053, -0.170), JVersor3D(-0.456, +0.263, -0.850))); }
641 
642  if (memo.has( 7)) { module[memo[ 7].tdc] = JPMT( 8, JAxis3D(JVector3D(+0.083, +0.144, -0.111), JVersor3D(+0.416, +0.720, -0.555))); }
643  if (memo.has( 8)) { module[memo[ 8].tdc] = JPMT( 9, JAxis3D(JVector3D(+0.166, +0.000, -0.111), JVersor3D(+0.832, +0.000, -0.555))); }
644  if (memo.has( 9)) { module[memo[ 9].tdc] = JPMT(10, JAxis3D(JVector3D(+0.083, -0.144, -0.111), JVersor3D(+0.416, -0.720, -0.555))); }
645  if (memo.has(10)) { module[memo[10].tdc] = JPMT(11, JAxis3D(JVector3D(-0.083, -0.144, -0.111), JVersor3D(-0.416, -0.720, -0.555))); }
646  if (memo.has(11)) { module[memo[11].tdc] = JPMT(12, JAxis3D(JVector3D(-0.166, +0.000, -0.111), JVersor3D(-0.832, +0.000, -0.555))); }
647  if (memo.has(12)) { module[memo[12].tdc] = JPMT(13, JAxis3D(JVector3D(-0.083, +0.144, -0.111), JVersor3D(-0.416, +0.720, -0.555))); }
648 
649  if (memo.has(13)) { module[memo[13].tdc] = JPMT(14, JAxis3D(JVector3D(+0.000, +0.191, -0.059), JVersor3D(+0.000, +0.955, -0.295))); }
650  if (memo.has(14)) { module[memo[14].tdc] = JPMT(15, JAxis3D(JVector3D(+0.165, +0.096, -0.059), JVersor3D(+0.827, +0.478, -0.295))); }
651  if (memo.has(15)) { module[memo[15].tdc] = JPMT(16, JAxis3D(JVector3D(+0.165, -0.096, -0.059), JVersor3D(+0.827, -0.478, -0.295))); }
652  if (memo.has(16)) { module[memo[16].tdc] = JPMT(17, JAxis3D(JVector3D(+0.000, -0.191, -0.059), JVersor3D(+0.000, -0.955, -0.295))); }
653  if (memo.has(17)) { module[memo[17].tdc] = JPMT(18, JAxis3D(JVector3D(-0.165, -0.096, -0.059), JVersor3D(-0.827, -0.478, -0.295))); }
654  if (memo.has(18)) { module[memo[18].tdc] = JPMT(19, JAxis3D(JVector3D(-0.165, +0.096, -0.059), JVersor3D(-0.827, +0.478, -0.295))); }
655 
656  if (memo.has(19)) { module[memo[19].tdc] = JPMT(20, JAxis3D(JVector3D(+0.096, +0.165, +0.059), JVersor3D(+0.478, +0.827, +0.295))); }
657  if (memo.has(20)) { module[memo[20].tdc] = JPMT(21, JAxis3D(JVector3D(+0.191, +0.000, +0.059), JVersor3D(+0.955, +0.000, +0.295))); }
658  if (memo.has(21)) { module[memo[21].tdc] = JPMT(22, JAxis3D(JVector3D(+0.096, -0.165, +0.059), JVersor3D(+0.478, -0.827, +0.295))); }
659  if (memo.has(22)) { module[memo[22].tdc] = JPMT(23, JAxis3D(JVector3D(-0.096, -0.165, +0.059), JVersor3D(-0.478, -0.827, +0.295))); }
660  if (memo.has(23)) { module[memo[23].tdc] = JPMT(24, JAxis3D(JVector3D(-0.191, +0.000, +0.059), JVersor3D(-0.955, +0.000, +0.295))); }
661  if (memo.has(24)) { module[memo[24].tdc] = JPMT(25, JAxis3D(JVector3D(-0.096, +0.165, +0.059), JVersor3D(-0.478, +0.827, +0.295))); }
662 
663  if (memo.has(25)) { module[memo[25].tdc] = JPMT(26, JAxis3D(JVector3D(+0.000, +0.166, +0.111), JVersor3D(+0.000, +0.832, +0.555))); }
664  if (memo.has(26)) { module[memo[26].tdc] = JPMT(27, JAxis3D(JVector3D(+0.144, +0.083, +0.111), JVersor3D(+0.720, +0.416, +0.555))); }
665  if (memo.has(27)) { module[memo[27].tdc] = JPMT(28, JAxis3D(JVector3D(+0.144, -0.083, +0.111), JVersor3D(+0.720, -0.416, +0.555))); }
666  if (memo.has(28)) { module[memo[28].tdc] = JPMT(29, JAxis3D(JVector3D(+0.000, -0.166, +0.111), JVersor3D(+0.000, -0.832, +0.555))); }
667  if (memo.has(29)) { module[memo[29].tdc] = JPMT(30, JAxis3D(JVector3D(-0.144, -0.083, +0.111), JVersor3D(-0.720, -0.416, +0.555))); }
668  if (memo.has(30)) { module[memo[30].tdc] = JPMT(31, JAxis3D(JVector3D(-0.144, +0.083, +0.111), JVersor3D(-0.720, +0.416, +0.555))); }
669 
670  module.compile();
671 
672  return module;
673  }
674 
675 
676  /**
677  * Get module according given detector type.
678  *
679  * \param type detector type
680  * \param id module identifier
681  * \param location module location
682  * \return module
683  */
684  template<class JDetector_t>
685  inline const JModule& getModule(const JType<JDetector_t> type,
686  const int id,
687  const JLocation& location = JLocation())
688  {
689  return getModule(getModuleAddressMap<JDetector_t>(id), id, location);
690  }
691 
692 
693  /**
694  * Get module according Antares detector type.
695  *
696  * \param type Antares detector type
697  * \param id module identifier
698  * \param location module location
699  * \return module
700  */
701  inline const JModule& getModule(const JType<JAntares_t> type,
702  const int id,
703  const JLocation& location = JLocation())
704  {
705  static JModule module;
706 
707  module.setID(id);
708 
709  module.setLocation(location);
710 
711  if (module.empty()) {
712 
713  module.resize(3);
714 
715  const double R = 0.5; // [m]
716 
717  const double st = sin(0.75*PI);
718  const double ct = cos(0.75*PI);
719 
720  for (int i = 0; i != 3; ++i) {
721 
722  const double phi = (2.0*PI*i) / 3.0;
723  const double cp = cos(phi);
724  const double sp = sin(phi);
725 
726  module[i] = JPMT(i, JAxis3D(JVector3D(R*st*cp, R*st*sp, R*ct), JVersor3D(st*cp, st*sp, ct)));
727  }
728  }
729 
730  return module;
731  }
732 
733 
734  /**
735  * Get module according detector template.
736  *
737  * \param id module identifier
738  * \param location module location
739  * \return module
740  */
741  template<class JDetector_t>
742  inline const JModule& getModule(const int id,
743  const JLocation& location = JLocation())
744  {
745  return getModule(JType<JDetector_t>(), id, location);
746  }
747 
748 
749  /**
750  * Auxiliary class to get rotation matrix between two optical modules.
751  */
752  struct JRotation :
753  public JRotation3D
754  {
755 
756  static const size_t NUMBER_OF_DIMENSIONS = 3; //!< Number of dimensions
757 
758 
759  /**
760  * Get rotation matrix to go from first to second module.
761  *
762  * \param first first module
763  * \param second second module
764  * \return rotation matrix
765  */
766  const JRotation3D& operator()(const JModule& first, const JModule& second)
767  {
768  this->setIdentity();
769 
770  if (first.size() == second.size()) {
771 
772  const size_t N = first.size();
773 
774  if (N >= NUMBER_OF_DIMENSIONS) {
775 
776  in .resize(N);
777  out.resize(N);
778 
779  for (size_t i = 0; i != N; ++i) {
780  in [i] = first .getPMT(i).getDirection();
781  out[i] = second.getPMT(i).getDirection();
782  }
783 
784  for (size_t i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
785  if (!orthonormalise(i)) {
786  THROW(JException, "Failure to orthonormalise direction " << i);
787  }
788  }
789 
790  this->a00 = out[0].getX() * in[0].getX() + out[1].getX() * in[1].getX() + out[2].getX() * in[2].getX();
791  this->a01 = out[0].getX() * in[0].getY() + out[1].getX() * in[1].getY() + out[2].getX() * in[2].getY();
792  this->a02 = out[0].getX() * in[0].getZ() + out[1].getX() * in[1].getZ() + out[2].getX() * in[2].getZ();
793 
794  this->a10 = out[0].getY() * in[0].getX() + out[1].getY() * in[1].getX() + out[2].getY() * in[2].getX();
795  this->a11 = out[0].getY() * in[0].getY() + out[1].getY() * in[1].getY() + out[2].getY() * in[2].getY();
796  this->a12 = out[0].getY() * in[0].getZ() + out[1].getY() * in[1].getZ() + out[2].getY() * in[2].getZ();
797 
798  this->a20 = out[0].getZ() * in[0].getX() + out[1].getZ() * in[1].getX() + out[2].getZ() * in[2].getX();
799  this->a21 = out[0].getZ() * in[0].getY() + out[1].getZ() * in[1].getY() + out[2].getZ() * in[2].getY();
800  this->a22 = out[0].getZ() * in[0].getZ() + out[1].getZ() * in[1].getZ() + out[2].getZ() * in[2].getZ();
801 
802  } else {
803 
804  THROW(JException, "Module " << first.getID() << " size " << N << " < " << NUMBER_OF_DIMENSIONS);
805  }
806 
807  } else {
808 
809  THROW(JException, "Module " << first.getID() << " size " << first.size() << " != " << second.size());
810  }
811 
812  return *this;
813  }
814 
815  private:
816  /**
817  * Put normalised primary direction at specified index and orthoganilise following directions.\n
818  * This procedure follows Gram-Schmidt process.
819  *
820  * \param index index
821  * \param precision precision
822  * \return true if primary direction exists; else false
823  */
824  bool orthonormalise(const size_t index, const double precision = std::numeric_limits<double>::epsilon())
825  {
826  using namespace std;
827 
828  size_t pos = index;
829 
830  for (size_t i = index + 1; i != in.size(); ++i) {
831  if (in[i].getLengthSquared() > in[pos].getLengthSquared()) {
832  pos = i;
833  }
834  }
835 
836  const double u = in[pos].getLength();
837 
838  if (u > precision) {
839 
840  in [pos] /= u;
841  out[pos] /= u;
842 
843  if (pos != index) {
844  swap(in [pos], in [index]);
845  swap(out[pos], out[index]);
846  }
847 
848  for (size_t i = index + 1; i != in.size(); ++i) {
849 
850  const double dot = in[index].getDot(in[i]);
851 
852  in [i] -= dot * in [index];
853  out[i] -= dot * out[index];
854  }
855 
856  return true;
857 
858  } else {
859 
860  return false;
861  }
862  }
863 
864 
867  };
868 
869 
870  /**
871  * Function object to get rotation matrix to go from first to second module.
872  */
874 
875 
876  /**
877  * Get position to go from first to second module.
878  *
879  * \param first first module
880  * \param second second module
881  * \return position
882  */
883  inline JPosition3D getPosition(const JModule& first, const JModule& second)
884  {
885  return second.getPosition() - first.getPosition();
886  }
887 
888 
889  /**
890  * Get calibration to go from first to second calibration.
891  *
892  * \param first first calibration
893  * \param second second calibration
894  * \return calibration
895  */
897  {
898  return JCalibration(second.getT0() - first.getT0());
899  }
900 }
901 
902 #endif
Exception for opening of file.
Definition: JException.hh:342
static const JRange< double, std::less< double > > DEFAULT_RANGE
Default range.
Definition: JRange.hh:568
double GetYrotationG4(const JVersor3D dir)
Get rotation over Y axis in Geant4 coordinate system.
General exception.
Definition: JException.hh:23
Exceptions.
Auxiliary methods for geometrical methods.
range_type include(argument_type x)
Include given value to range.
Definition: JRange.hh:398
Data structure for a composite optical module.
Definition: JModule.hh:68
void setLocation(const JLocation &location)
Set location.
Definition: JLocation.hh:91
const JCalibration & getCalibration() const
Get calibration.
double getIndexOfRefraction()
Get average index of refraction of water corresponding to group velocity.
Detector data structure.
Definition: JDetector.hh:89
const JDirection3D & getDirection() const
Get direction.
JTOOLS::JRange< double > JTimeRange
Type definition for time range (unit [ns]).
Rotation matrix.
Definition: JRotation3D.hh:111
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
std::set< int > getStringIDs(const JDetector &detector)
Get list of strings identifiers.
JTOOLS::JRange< int > floor_range
Type definition for range of floors.
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
int getNumberOfPMTs(const JModule &module)
Get number of PMTs.
static void setStartOfComment(const start_of_comment_type option)
Set option for short start of comment in binary I/O.
Definition: JDetector.hh:517
static JRotation getRotation
Function object to get rotation matrix to go from first to second module.
JCalibration getCalibration(const JCalibration &first, const JCalibration &second)
Get calibration to go from first to second calibration.
Jpp environment information.
static const char *const BINARY_DETECTOR_FILE_FORMAT[]
JIO binary file format.
Data structure for time calibration.
Auxiliary class for a type holder.
Definition: JType.hh:19
void read_gdml(std::istream &, JDetector &)
static const char *const GDML_SCHEMA
directory necessary for correct GDML header output
static const JModuleCounter getNumberOfModules
Function object to count unique modules.
static const char *const CAN_MARGIN_M
extension of the detector size to comply with the can definition
floor_range getRangeOfFloors(const JDetector &detector)
Get range of floors.
Data structure for detector geometry and calibration.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
JTimeRange getTimeRange(const Evt &event)
Get time range (i.e. time between earliest and latest hit) of Monte Carlo event.
Axis object.
Definition: JAxis3D.hh:38
Monte Carlo detector (i.e. so-called .det file).
Lookup table for PMT addresses in optical module.
double putTime(const T &t1, const JCalibration &cal)
Get de-calibrated time.
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
Detector specific mapping between logical positions and readout channels of PMTs in optical modules...
double getMaximalDistance(const JDetector &detector)
Get maximal distance between modules in detector.
Cylinder object.
Definition: JCylinder3D.hh:39
virtual JReader & read(JReader &in) override
Read from input.
Definition: JDetector.hh:399
Detector file.
Definition: JHead.hh:196
std::string getGITVersion(const std::string &tag)
Get GIT version for given GIT tag.
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
virtual JWriter & write(JWriter &out) const override
Write to output.
Definition: JDetector.hh:451
Logical location of module.
Definition: JLocation.hh:37
Mathematical constants.
double getDY() const
Get y direction.
Definition: JVersor3D.hh:106
Auxiliary class to get rotation matrix between two optical modules.
double getDX() const
Get x direction.
Definition: JVersor3D.hh:95
Auxiliary methods for handling file names, type names and environment.
int getID() const
Get identifier.
Definition: JObjectID.hh:50
Data structure for PMT geometry, calibration and status.
Definition: JPMT.hh:43
void compile()
Compile module data.
Definition: JModule.hh:281
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
JPosition3D getPosition(const Vec &pos)
Get position.
int getNumberOfFloors(const JDetector &detector)
Get number of floors.
Physics constants.
static const double PI
Mathematical constants.
double getY() const
Get y position.
Definition: JVector3D.hh:104
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
double getMaximalTime(const double R_Hz)
Get maximal time for given rate.
const JPMT & getPMT(const int index) const
Get PMT.
Definition: JModule.hh:173
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:43
Logical location of module.
Protocol exception.
Definition: JException.hh:630
const JRotation3D & operator()(const JModule &first, const JModule &second)
Get rotation matrix to go from first to second module.
std::vector< JVector3D > in
I/O manipulators.
static const char *const KM3NET_DETECTOR_FILE_FORMAT
KM3NeT standard ASCII format
static const char *const ZIPPED_DETECTOR_FILE_FORMAT
zipped KM3NeT standard ASCII format
static const char *const GENDET_DETECTOR_FILE_FORMAT
File name extensions.
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Auxiliary class to define a range between two values.
std::string getFilenameExtension(const std::string &file_name)
Get file name extension, i.e. part after last JEEP::FILENAME_SEPARATOR if any.
Definition: JeepToolkit.hh:69
const double getInverseSpeedOfLight()
Get inverse speed of light.
void setID(const int id)
Set identifier.
Definition: JObjectID.hh:72
double getX() const
Get x position.
Definition: JVector3D.hh:94
static const char *const G4GDML_DEFAULT_SCHEMALOCATION
then cp
double getTanThetaC()
Get average tangent of Cherenkov angle of water corresponding to group velocity.
int j
Definition: JPolint.hh:666
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
do set_variable DETECTOR_TXT $WORKDIR detector
Data structure for normalised vector in three dimensions.
Definition: JVersor3D.hh:26
double u[N+1]
Definition: JPolint.hh:739
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:41
Binary buffered file output.
double GetXrotationG4(const JVersor3D dir)
Get rotation over X axis in Geant4 coordinate system.
static const char *const GDML_DETECTOR_FILE_FORMAT
KM3Sim input format.
double getZ() const
Get z position.
Definition: JVector3D.hh:115
Binary buffered file input.
bool has(const int index) const
Test whether index is available.
double getDZ() const
Get z direction.
Definition: JVersor3D.hh:117
std::vector< JVector3D > out
bool orthonormalise(const size_t index, const double precision=std::numeric_limits< double >::epsilon())
Put normalised primary direction at specified index and orthoganilise following directions.
const JModule & getModule(const JDetector &detector, const JModuleLocation &location)
find module with a given string and floor number
double getSinThetaC()
Get average sine of Cherenkov angle of water corresponding to group velocity.
void write_gdml(std::ostream &out, const JDetector &detector)
Writes KM3Sim GDML input file from detector.
double getT0() const
Get time offset.