Jpp  17.1.0
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->empty()) continue;
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  if(module->empty()) continue;
226  out << " <volume name=\"OMVolume" << module->getID() << "\">\n";
227  out << " <materialref ref=\"Water\"/>\n";
228  out << " <solidref ref=\"OMSphere\"/>\n";
229 
230  for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
231  out << " <physvol>\n";
232  out << " <volumeref ref=\"CathodVolume\"/>\n";
233  out << " <positionref ref=\"CathodPosition" << pmt->getID() << "_" << module->getID() << "\"/>\n";
234  out << " <rotationref ref=\"CathodRotation" << pmt->getID() << "_" << module->getID() << "\"/>\n";
235  out << " </physvol>\n";
236  }
237 
238  out << " </volume>\n";
239  }
240 
241  out << "<!-- StoreyVolume(s) construction -->\n";
242 
243  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
244  if(module->empty()) continue;
245  out << " <volume name=\"StoreyVolume" << module->getID() << "\">\n";
246  out << " <materialref ref=\"Water\"/>\n";
247  out << " <solidref ref=\"StoreyBox\"/>\n";
248  out << " <physvol>\n";
249  out << " <volumeref ref=\"OMVolume" << module->getID() << "\"/>\n";
250  out << " <positionref ref=\"OMShift\"/>\n";
251  out << " <rotationref ref=\"identity\"/>\n";
252  out << " </physvol>\n";
253  out << " </volume>\n";
254  }
255 
256  out << "<!-- Crust Volume construction-->\n";
257  out << "<volume name=\"CrustVolume\">\n";
258  out << " <materialref ref=\"Crust\"/>\n";
259  out << " <solidref ref=\"CrustBox\"/>\n";
260  out << "</volume>\n";
261 
262  out << "<!-- World Volume construction-->\n";
263  out << "<volume name=\"WorldVolume\">\n";
264  out << " <materialref ref=\"Water\"/>\n";
265  out << " <solidref ref=\"WorldTube\"/>\n";
266 
267  out << " <physvol>\n";
268  out << " <volumeref ref=\"CrustVolume\"/>\n";
269  out << " <positionref ref=\"CrustPosition\"/>\n";
270  out << " <rotationref ref=\"identity\"/>\n";
271  out << " </physvol>\n";
272 
273  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
274  if(module->empty()) continue;
275  out << " <physvol>\n";
276  out << " <volumeref ref=\"StoreyVolume" << module->getID() << "\"/>\n";
277  out << " <positionref ref=\"PosString" << module->getString() << "_Dom" << module->getID() << "\"/>\n";
278  out << " <rotationref ref=\"identity\"/>\n";
279  out << " </physvol>\n";
280  }
281 
282  out << "</volume>\n";
283 
284  out << "</structure>\n";
285  out << "<setup name=\"Default\" version=\"1.0\">\n";
286  out << "<world ref=\"WorldVolume\"/>\n";
287  out << "</setup>\n";
288  out << "</gdml>\n";
289  }
290 
291 
292  /**
293  * Get maximal time between modules in detector following causality.
294  *
295  * \param detector detector
296  * \return maximal time [ns]
297  */
298  inline double getMaximalTime(const JDetector& detector)
299  {
300  using namespace JPP;
301 
303  }
304 
305 
306  /**
307  * Get maximal time between modules in detector following causality.
308  * The road width corresponds to the maximal distance traveled by the light.
309  *
310  * \param detector detector
311  * \param roadWidth_m road width [m]
312  * \return maximal time [ns]
313  */
314  inline double getMaximalTime(const JDetector& detector, const double roadWidth_m)
315  {
316  using namespace JPP;
317 
318  const double Dmax_m = getMaximalDistance(detector);
319 
320  return (sqrt((Dmax_m + roadWidth_m*getSinThetaC()) *
321  (Dmax_m - roadWidth_m*getSinThetaC())) +
322  roadWidth_m * getSinThetaC() * getTanThetaC()) * getInverseSpeedOfLight();
323  }
324 
325 
326  /**
327  * Get de-calibrated time range.
328  *
329  * The de-calibrated time range is corrected for minimal and maximal time offset of PMTs in given module.
330  *
331  * \param timeRange time range [ns]
332  * \param module module
333  * \return time range [ns]
334  */
335  inline JTimeRange getTimeRange(const JTimeRange& timeRange, const JModule& module)
336  {
337  if (!module.empty()) {
338 
340 
341  for (JModule::const_iterator pmt = module.begin(); pmt != module.end(); ++pmt) {
342 
343  const JCalibration& calibration = pmt->getCalibration();
344 
345  time_range.include(putTime(timeRange.getLowerLimit(), calibration));
346  time_range.include(putTime(timeRange.getUpperLimit(), calibration));
347  }
348 
349  return time_range;
350 
351  } else {
352 
353  return timeRange;
354  }
355  }
356 
357 
358  /**
359  * Get number of PMTs.
360  *
361  * \param module module
362  * \return number of PMTs
363  */
364  inline int getNumberOfPMTs(const JModule& module)
365  {
366  return module.size();
367  }
368 
369 
370  /**
371  * Get number of PMTs.
372  *
373  * \param detector detector
374  * \return number of PMTs
375  */
376  inline int getNumberOfPMTs(const JDetector& detector)
377  {
378  int number_of_pmts = 0;
379 
380  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
381  number_of_pmts += getNumberOfPMTs(*module);
382  }
383 
384  return number_of_pmts;
385  }
386 
387 
388  /**
389  * Get list of strings identifiers.
390  *
391  * \param detector detector
392  * \return list of string identifiers
393  */
395  {
396  std::set<int> buffer;
397 
398  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
399  buffer.insert(module->getString());
400  }
401 
402  return buffer;
403  }
404 
405 
406  /**
407  * Get number of floors.
408  *
409  * \param detector detector
410  * \return number of floors
411  */
413  {
414  std::set<int> buffer;
415 
416  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
417  buffer.insert(module->getFloor());
418  }
419 
420  return buffer.size();
421  }
422 
423 
424  /**
425  * Type definition for range of floors.
426  */
428 
429 
430  /**
431  * Get range of floors.
432  *
433  * \param detector detector
434  * \return range of floors
435  */
436  inline floor_range getRangeOfFloors(const JDetector& detector)
437  {
438  floor_range buffer = floor_range::DEFAULT_RANGE;
439 
440  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
441  buffer.include(module->getFloor());
442  }
443 
444  return buffer;
445  }
446 
447 
448  /**
449  * Get number of modules.
450  *
451  * \param detector detector
452  * \return number of modules
453  */
455  {
456  std::set<int> buffer;
457 
458  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
459  buffer.insert(module->getID());
460  }
461 
462  return buffer.size();
463  }
464 
465 
466  /**
467  * Load detector from input file.
468  *
469  * Supported file formats:
470  * - ASCII file, extension JDETECTOR::GENDET_DETECTOR_FILE_FORMAT, gendet format;
471  * - binary file, extension JDETECTOR::BINARY_DETECTOR_FILE_FORMAT, Jpp internal format;
472  * - ASCII file, extension JDETECTOR::KM3NET_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
473  * - gzipped file, extension JDETECTOR::ZIPPED_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
474  *
475  * \param file_name file name
476  * \param detector detector
477  */
478  inline void load(const std::string& file_name, JDetector& detector)
479  {
480  using namespace std;
481  using namespace JPP;
482 
484 
485  JMonteCarloDetector buffer(true);
486 
487  ifstream in(file_name.c_str());
488 
489  if (!in) {
490  THROW(JFileOpenException, "File not opened: " << file_name);
491  }
492 
493  in >> buffer;
494 
495  in.close();
496 
497  detector.swap(buffer);
498 
499  } else if (getFilenameExtension(file_name) == BINARY_DETECTOR_FILE_FORMAT[0] ||
501 
502  JFileStreamReader in(file_name.c_str());
503 
504  if (!in) {
505  THROW(JFileOpenException, "File not opened: " << file_name);
506  }
507 
508  try {
509 
510  detector.read(in);
511 
512  } catch(const exception& error) {
513 
514  // recovery procedure for old format of comments
515 
517 
518  in.clear();
519  in.rewind();
520 
521  detector.read(in);
522 
524  }
525 
526  in.close();
527 
528  } else if (getFilenameExtension(file_name) == KM3NET_DETECTOR_FILE_FORMAT) {
529 
530  ifstream in(file_name.c_str());
531 
532  if (!in) {
533  THROW(JFileOpenException, "File not opened: " << file_name);
534  }
535 
536  in >> detector;
537 
538  in.close();
539 
540  } else if (getFilenameExtension(file_name) == ZIPPED_DETECTOR_FILE_FORMAT) {
541 
542  igzstream in(file_name.c_str());
543 
544  if (!in) {
545  THROW(JFileOpenException, "File not opened: " << file_name);
546  }
547 
548  in >> detector;
549 
550  in.close();
551 
552  } else {
553 
554  THROW(JProtocolException, "Protocol not defined: " << file_name);
555  }
556  }
557 
558 
559  /**
560  * Store detector to output file.
561  *
562  * Supported file formats:
563  * - binary file, extension JDETECTOR::BINARY_DETECTOR_FILE_FORMAT, Jpp internal format;
564  * - ASCII file, extension JDETECTOR::KM3NET_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
565  * - gzipped file, extension JDETECTOR::ZIPPED_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
566  * - gdml file, extension JDETECTOR::GDML_DETECTOR_FILE_FORMAT, KM3Sim input format;
567  *
568  * \param file_name file name
569  * \param detector detector
570  */
571  inline void store(const std::string& file_name, const JDetector& detector)
572  {
573  using namespace std;
574  using namespace JPP;
575 
576  if (getFilenameExtension(file_name) == BINARY_DETECTOR_FILE_FORMAT[1]) {
577 
578  JFileStreamWriter out(file_name.c_str());
579 
580  detector.write(out);
581 
582  out.close();
583 
584  } else if (getFilenameExtension(file_name) == KM3NET_DETECTOR_FILE_FORMAT) {
585 
586  std::ofstream out(file_name.c_str());
587 
588  out << detector;
589 
590  out.close();
591 
592  } else if (getFilenameExtension(file_name) == ZIPPED_DETECTOR_FILE_FORMAT) {
593 
594  ogzstream out(file_name.c_str());
595 
596  out << detector;
597 
598  out.close();
599 
600  } else if (getFilenameExtension(file_name) == GDML_DETECTOR_FILE_FORMAT) {
601 
602  std::ofstream out(file_name.c_str());
603 
604  write_gdml(out, detector);
605 
606  out.close();
607 
608  } else {
609 
610  THROW(JProtocolException, "Protocol not defined: " << file_name);
611  }
612  }
613 
614 
615  /**
616  * Get module according module address map.
617  *
618  * \param memo module address map
619  * \param id module identifier
620  * \param location module location
621  * \return module
622  */
623  inline const JModule& getModule(const JModuleAddressMap& memo,
624  const int id = -1,
625  const JLocation& location = JLocation())
626  {
627  static JModule module;
628 
629 
630  module.setID(id);
631 
632  module.setLocation(location);
633 
634  module.resize(memo.size());
635 
636  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))); }
637 
638  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))); }
639  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))); }
640  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))); }
641  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))); }
642  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))); }
643  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))); }
644 
645  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))); }
646  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))); }
647  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))); }
648  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))); }
649  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))); }
650  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))); }
651 
652  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))); }
653  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))); }
654  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))); }
655  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))); }
656  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))); }
657  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))); }
658 
659  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))); }
660  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))); }
661  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))); }
662  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))); }
663  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))); }
664  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))); }
665 
666  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))); }
667  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))); }
668  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))); }
669  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))); }
670  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))); }
671  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))); }
672 
673  module.compile();
674 
675  return module;
676  }
677 
678 
679  /**
680  * Get module according given detector type.
681  *
682  * \param type detector type
683  * \param id module identifier
684  * \param location module location
685  * \return module
686  */
687  template<class JDetector_t>
688  inline const JModule& getModule(const JType<JDetector_t> type,
689  const int id,
690  const JLocation& location = JLocation())
691  {
692  return getModule(getModuleAddressMap<JDetector_t>(id), id, location);
693  }
694 
695 
696  /**
697  * Get module according Antares detector type.
698  *
699  * \param type Antares detector type
700  * \param id module identifier
701  * \param location module location
702  * \return module
703  */
704  inline const JModule& getModule(const JType<JAntares_t> type,
705  const int id,
706  const JLocation& location = JLocation())
707  {
708  static JModule module;
709 
710  module.setID(id);
711 
712  module.setLocation(location);
713 
714  if (module.empty()) {
715 
716  module.resize(3);
717 
718  const double R = 0.5; // [m]
719 
720  const double st = sin(0.75*PI);
721  const double ct = cos(0.75*PI);
722 
723  for (int i = 0; i != 3; ++i) {
724 
725  const double phi = (2.0*PI*i) / 3.0;
726  const double cp = cos(phi);
727  const double sp = sin(phi);
728 
729  module[i] = JPMT(i, JAxis3D(JVector3D(R*st*cp, R*st*sp, R*ct), JVersor3D(st*cp, st*sp, ct)));
730  }
731  }
732 
733  return module;
734  }
735 
736 
737  /**
738  * Get module according detector template.
739  *
740  * \param id module identifier
741  * \param location module location
742  * \return module
743  */
744  template<class JDetector_t>
745  inline const JModule& getModule(const int id,
746  const JLocation& location = JLocation())
747  {
748  return getModule(JType<JDetector_t>(), id, location);
749  }
750 
751 
752  /**
753  * Auxiliary class to get rotation matrix between two optical modules.
754  */
755  struct JRotation :
756  public JRotation3D
757  {
758 
759  static const size_t NUMBER_OF_DIMENSIONS = 3; //!< Number of dimensions
760 
761 
762  /**
763  * Get rotation matrix to go from first to second module.
764  *
765  * \param first first module
766  * \param second second module
767  * \return rotation matrix
768  */
769  const JRotation3D& operator()(const JModule& first, const JModule& second)
770  {
771  this->setIdentity();
772 
773  if (first.size() == second.size()) {
774 
775  const size_t N = first.size();
776 
777  if (N >= NUMBER_OF_DIMENSIONS) {
778 
779  in .resize(N);
780  out.resize(N);
781 
782  for (size_t i = 0; i != N; ++i) {
783  in [i] = first .getPMT(i).getDirection();
784  out[i] = second.getPMT(i).getDirection();
785  }
786 
787  for (size_t i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
788  if (!orthonormalise(i)) {
789  THROW(JException, "Failure to orthonormalise direction " << i);
790  }
791  }
792 
793  this->a00 = out[0].getX() * in[0].getX() + out[1].getX() * in[1].getX() + out[2].getX() * in[2].getX();
794  this->a01 = out[0].getX() * in[0].getY() + out[1].getX() * in[1].getY() + out[2].getX() * in[2].getY();
795  this->a02 = out[0].getX() * in[0].getZ() + out[1].getX() * in[1].getZ() + out[2].getX() * in[2].getZ();
796 
797  this->a10 = out[0].getY() * in[0].getX() + out[1].getY() * in[1].getX() + out[2].getY() * in[2].getX();
798  this->a11 = out[0].getY() * in[0].getY() + out[1].getY() * in[1].getY() + out[2].getY() * in[2].getY();
799  this->a12 = out[0].getY() * in[0].getZ() + out[1].getY() * in[1].getZ() + out[2].getY() * in[2].getZ();
800 
801  this->a20 = out[0].getZ() * in[0].getX() + out[1].getZ() * in[1].getX() + out[2].getZ() * in[2].getX();
802  this->a21 = out[0].getZ() * in[0].getY() + out[1].getZ() * in[1].getY() + out[2].getZ() * in[2].getY();
803  this->a22 = out[0].getZ() * in[0].getZ() + out[1].getZ() * in[1].getZ() + out[2].getZ() * in[2].getZ();
804 
805  } else {
806 
807  THROW(JException, "Module " << first.getID() << " size " << N << " < " << NUMBER_OF_DIMENSIONS);
808  }
809 
810  } else {
811 
812  THROW(JException, "Module " << first.getID() << " size " << first.size() << " != " << second.size());
813  }
814 
815  return *this;
816  }
817 
818  private:
819  /**
820  * Put normalised primary direction at specified index and orthoganilise following directions.\n
821  * This procedure follows Gram-Schmidt process.
822  *
823  * \param index index
824  * \param precision precision
825  * \return true if primary direction exists; else false
826  */
827  bool orthonormalise(const size_t index, const double precision = std::numeric_limits<double>::epsilon())
828  {
829  using namespace std;
830 
831  size_t pos = index;
832 
833  for (size_t i = index + 1; i != in.size(); ++i) {
834  if (in[i].getLengthSquared() > in[pos].getLengthSquared()) {
835  pos = i;
836  }
837  }
838 
839  const double u = in[pos].getLength();
840 
841  if (u > precision) {
842 
843  in [pos] /= u;
844  out[pos] /= u;
845 
846  if (pos != index) {
847  swap(in [pos], in [index]);
848  swap(out[pos], out[index]);
849  }
850 
851  for (size_t i = index + 1; i != in.size(); ++i) {
852 
853  const double dot = in[index].getDot(in[i]);
854 
855  in [i] -= dot * in [index];
856  out[i] -= dot * out[index];
857  }
858 
859  return true;
860 
861  } else {
862 
863  return false;
864  }
865  }
866 
867 
870  };
871 
872 
873  /**
874  * Function object to get rotation matrix to go from first to second module.
875  */
877 
878 
879  /**
880  * Get position to go from first to second module.
881  *
882  * \param first first module
883  * \param second second module
884  * \return position
885  */
886  inline JPosition3D getPosition(const JModule& first, const JModule& second)
887  {
888  return second.getPosition() - first.getPosition();
889  }
890 
891 
892  /**
893  * Get calibration to go from first to second calibration.
894  *
895  * \param first first calibration
896  * \param second second calibration
897  * \return calibration
898  */
900  {
901  return JCalibration(second.getT0() - first.getT0());
902  }
903 }
904 
905 #endif
Exception for opening of file.
Definition: JException.hh:342
static const JRange< double, std::less< double > > DEFAULT_RANGE
Default range.
Definition: JRange.hh:555
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.
Data structure for a composite optical module.
Definition: JModule.hh:68
range_type & include(argument_type x)
Include given value to range.
Definition: JRange.hh:397
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:696
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:573
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:455
Detector file.
Definition: JHead.hh:226
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:507
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:282
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:648
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:109
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:682
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:755
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
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
const double epsilon
Definition: JQuadrature.cc:21
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.