Jpp  18.5.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"
18 #include "JDetector/JLocation.hh"
21 #include "JGeometry3D/JVector3D.hh"
22 #include "JGeometry3D/JVersor3D.hh"
25 #include "JPhysics/JConstants.hh"
26 #include "JMath/JConstants.hh"
27 #include "JMath/JMathToolkit.hh"
28 #include "JTools/JRange.hh"
29 #include "JIO/JFileStreamIO.hh"
30 #include "Jeep/JeepToolkit.hh"
31 #include "JLang/JException.hh"
32 #include "JLang/gzstream.h"
33 #include "JLang/JManip.hh"
34 #include "JLang/Jpp.hh"
35 
36 
37 /**
38  * \author mdejong
39  */
40 
41 namespace JDETECTOR {}
42 namespace JPP { using namespace JDETECTOR; }
43 
44 /**
45  * Auxiliary classes and methods for detector calibration and simulation.
46  */
47 namespace JDETECTOR {
48 
49  using JLANG::JException;
53 
54 
55  /**
56  * File name extensions.
57  */
58  static const char* const GENDET_DETECTOR_FILE_FORMAT = "det"; //!< file format used by gendet
59  static const char* const BINARY_DETECTOR_FILE_FORMAT[] = { "dat", "datx" }; //!< JIO binary file format
60  static const char* const KM3NET_DETECTOR_FILE_FORMAT = "detx"; //!< %KM3NeT standard ASCII format
61  static const char* const ZIPPED_DETECTOR_FILE_FORMAT = "gz"; //!< zipped %KM3NeT standard ASCII format
62  static const char* const GDML_DETECTOR_FILE_FORMAT = "gdml"; //!< KM3Sim input format
63 
64  static const char* const GDML_SCHEMA = getenv("GDML_SCHEMA_DIR"); //!< directory necessary for correct GDML header output
65  static const char* const CAN_MARGIN_M = getenv("CAN_MARGIN_M"); //!< extension of the detector size to comply with the can definition
66  static const char* const G4GDML_DEFAULT_SCHEMALOCATION = "http://service-spi.web.cern.ch/service-spi/app/releases/GDML/schema/gdml.xsd";
67 
68 
69  /**
70  * Get maximal distance between modules in detector.
71  * The option can be used to include base modules, if any.
72  *
73  * \param detector detector
74  * \param option option
75  * \return maximal distance [m]
76  */
77  inline double getMaximalDistance(const JDetector& detector, const bool option = false)
78  {
79  using namespace JPP;
80 
81  double dmax = 0.0;
82 
83  for (JDetector::const_iterator i1 = detector.begin(); i1 != detector.end(); ++i1) {
84  for (JDetector::const_iterator i2 = detector.begin(); i2 != i1; ++i2) {
85 
86  if (option || (i1->getFloor() != 0 && i2->getFloor() != 0)) {
87 
88  const double ds = getDistance(i1->getPosition(), i2->getPosition());
89 
90  if (ds > dmax) {
91  dmax = ds;
92  }
93  }
94  }
95  }
96 
97  return dmax;
98  }
99 
100 
101  /**
102  * Get maximal time between optical modules in detector following causality.
103  *
104  * \param detector detector
105  * \return maximal time [ns]
106  */
107  inline double getMaximalTime(const JDetector& detector)
108  {
109  using namespace JPP;
110 
112  }
113 
114 
115  /**
116  * Get maximal time between optical modules in detector following causality.
117  * The road width corresponds to the maximal distance traveled by the light.
118  *
119  * \param detector detector
120  * \param roadWidth_m road width [m]
121  * \return maximal time [ns]
122  */
123  inline double getMaximalTime(const JDetector& detector, const double roadWidth_m)
124  {
125  using namespace JPP;
126 
127  const double Dmax_m = getMaximalDistance(detector);
128 
129  return (sqrt((Dmax_m + roadWidth_m*getSinThetaC()) *
130  (Dmax_m - roadWidth_m*getSinThetaC())) +
131  roadWidth_m * getSinThetaC() * getTanThetaC()) * getInverseSpeedOfLight();
132  }
133 
134 
135  /**
136  * Get de-calibrated time range.
137  *
138  * The de-calibrated time range is corrected for minimal and maximal time offset of PMTs in given module.
139  *
140  * \param timeRange time range [ns]
141  * \param module module
142  * \return time range [ns]
143  */
144  inline JTimeRange getTimeRange(const JTimeRange& timeRange, const JModule& module)
145  {
146  if (!module.empty()) {
147 
149 
150  for (JModule::const_iterator pmt = module.begin(); pmt != module.end(); ++pmt) {
151 
152  const JCalibration& calibration = pmt->getCalibration();
153 
154  time_range.include(putTime(timeRange.getLowerLimit(), calibration));
155  time_range.include(putTime(timeRange.getUpperLimit(), calibration));
156  }
157 
158  return time_range;
159 
160  } else {
161 
162  return timeRange;
163  }
164  }
165 
166 
167  /**
168  * Get number of PMTs.
169  *
170  * \param module module
171  * \return number of PMTs
172  */
173  inline int getNumberOfPMTs(const JModule& module)
174  {
175  return module.size();
176  }
177 
178 
179  /**
180  * Get number of PMTs.
181  *
182  * \param detector detector
183  * \return number of PMTs
184  */
185  inline int getNumberOfPMTs(const JDetector& detector)
186  {
187  int number_of_pmts = 0;
188 
189  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
190  number_of_pmts += getNumberOfPMTs(*module);
191  }
192 
193  return number_of_pmts;
194  }
195 
196 
197  /**
198  * Get list of strings identifiers.
199  *
200  * \param detector detector
201  * \return list of string identifiers
202  */
204  {
205  std::set<int> buffer;
206 
207  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
208  buffer.insert(module->getString());
209  }
210 
211  return buffer;
212  }
213 
214 
215  /**
216  * Get number of floors.
217  *
218  * \param detector detector
219  * \return number of floors
220  */
222  {
223  std::set<int> buffer;
224 
225  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
226  buffer.insert(module->getFloor());
227  }
228 
229  return buffer.size();
230  }
231 
232 
233  /**
234  * Type definition for range of floors.
235  */
237 
238 
239  /**
240  * Get range of floors.
241  *
242  * \param detector detector
243  * \return range of floors
244  */
245  inline floor_range getRangeOfFloors(const JDetector& detector)
246  {
247  floor_range buffer = floor_range::DEFAULT_RANGE();
248 
249  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
250  buffer.include(module->getFloor());
251  }
252 
253  return buffer;
254  }
255 
256 
257  /**
258  * Get number of modules.
259  *
260  * \param detector detector
261  * \return number of modules
262  */
264  {
265  std::set<int> buffer;
266 
267  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
268  buffer.insert(module->getID());
269  }
270 
271  return buffer.size();
272  }
273 
274 
275  /**
276  * Get rotation over X axis in Geant4 coordinate system
277  *
278  * \param dir direction
279  * \return X-rotation [deg]
280  */
281  inline double GetXrotationG4(const JVersor3D& dir)
282  {
283  using namespace JPP;
284 
285  const double phi = atan2(dir.getDY(), dir.getDZ())*(180.0/PI);
286 
287  if (phi < 0.0){
288  return phi + 360.0;
289  }
290  else{
291  return phi;
292  }
293  }
294 
295 
296  /**
297  * Get rotation over Y axis in Geant4 coordinate system
298  *
299  * \param dir direction
300  * \return Y-rotation [deg]
301  */
302  inline double GetYrotationG4(const JVersor3D& dir)
303  {
304  using namespace JPP;
305 
306  return asin(-dir.getDX())*(180.0/PI);
307  }
308 
309 
310  inline void read_gdml(std::istream&, JDetector&)
311  {
312  THROW(JException, "Operation not defined.");
313  }
314 
315 
316  /**
317  * Writes KM3Sim GDML input file from detector
318  *
319  * \param out output stream
320  * \param detector detector
321  */
322  inline void write_gdml(std::ostream& out, const JDetector& detector)
323  {
324  using namespace std;
325  using namespace JPP;
326 
327  const double DEFAULT_CAN_MARGIN_M = 350.0; // default can margin [m]
328  const double DEFAULT_WORLD_MARGIN_M = 50.0; // default world margin [m]
329 
330  const JCylinder3D cylinder(detector.begin(), detector.end());
331 
332  double can_margin;
333 
334  if (CAN_MARGIN_M) {
335  can_margin = atof(CAN_MARGIN_M);
336  } else {
337  cerr << "CAN_MARGIN_M not defined! Setting standard CAN_MARGIN_M " << DEFAULT_CAN_MARGIN_M << " m." << endl;
338  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)
339  }
340 
341  const double WorldCylinderHeight = 2*(cylinder.getZmax() - cylinder.getZmin() + can_margin + DEFAULT_WORLD_MARGIN_M);
342  const double WorldRadius = cylinder.getLength() + cylinder.getRadius() + can_margin + DEFAULT_WORLD_MARGIN_M;
343 
344  const double Crust_Z_size = WorldCylinderHeight/2;
345  const double Crust_Z_position = -WorldCylinderHeight/4;
346 
347  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=\"";
348  if (!GDML_SCHEMA) {
349  cerr << "GDML_SCHEMA_DIR NOT DEFINED! Setting default path." << endl;
350  out << G4GDML_DEFAULT_SCHEMALOCATION << "\">\n\n\n";
351  } else {
352  out << GDML_SCHEMA << "gdml.xsd\">\n\n\n";
353  }
354  out << "<!-- Jpp version: "<< getGITVersion() << " -->\n";
355  out << "<define>\n";
356  out << "<rotation name=\"identity\"/>\n<position name=\"zero\"/>\n";
357 
358  int PMTs_NO = 0;
359 
360  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
361 
362  if(module->empty()) continue;
363 
364  const JVector3D center = module->getCenter();
365 
366  out << "<position name=\"PosString" << module->getString() << "_Dom" << module->getID() << "\" unit=\"m\" x=\"" << module->getX() << "\" y=\"" << module->getY() << "\" z=\"" << module->getZ() << "\"/>\n";
367 
368  for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
369 
370  const JVector3D vec = static_cast<JVector3D>(*pmt).sub(center);
371  out << "<position name=\"CathodPosition" << pmt->getID() << "_" << module->getID() << "\" unit=\"m\" x=\"" << vec.getX() << "\" y=\"" << vec.getY() << "\" z=\"" << vec.getZ() << "\"/>\n";
372  out << "<rotation name=\"CathodRotation" << pmt->getID() << "_" << module->getID() << "\" unit=\"deg\" x=\"" << GetXrotationG4(*pmt) << "\" y=\"" << GetYrotationG4(*pmt) << "\" z=\"0.000000\"/>\n";
373  out << "<constant name=\"CathodID_" << PMTs_NO << "\" value=\"" << pmt->getID() << "\"/>\n";
374  PMTs_NO++;
375  }
376 
377  }
378 
379  out << "<position name=\"OMShift\" unit=\"m\" x=\"0\" y=\"0\" z=\"0.0392\"/>\n";
380  out << "\n\n\n";
381  out << "<!-- end of DU position definitions -->\n<position name=\"CrustPosition\" unit=\"m\" x=\"0\" y=\"0\" z=\"" << Crust_Z_position << "\"/>\n";
382 
383  out << "</define>\n";
384  out << "<materials>\n";
385  out << "</materials>\n";
386  out << "<solids>\n";
387  out << " <box name=\"CrustBox\" lunit=\"m\" x=\"2200\" y=\"2200\" z=\"" << Crust_Z_size << "\"/>\n";
388  out << " <box name=\"StoreyBox\" lunit=\"m\" x=\"0.6\" y=\"0.6\" z=\"0.6\"/>\n";
389  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";
390  out << " <tube name=\"CathodTube\" lunit=\"cm\" aunit=\"deg\" rmin=\"0.0\" rmax=\"4.7462\" z=\"0.5\" startphi=\"0.0\" deltaphi=\"360.0\"/>\n";
391  out << " <tube name=\"WorldTube\" lunit=\"m\" aunit=\"deg\" rmin=\"0.0\" rmax=\"" << WorldRadius << "\" z=\"" << WorldCylinderHeight << "\" startphi=\"0.0\" deltaphi=\"360.0\"/>\n";
392  out << "</solids>\n\n\n";
393 
394  out << "<structure>\n";
395  out << " <volume name=\"CathodVolume\">\n";
396  out << " <materialref ref=\"Cathod\"/>\n";
397  out << " <solidref ref=\"CathodTube\"/>\n";
398  out << " </volume>\n";
399 
400  out << "<!-- OMVolume(s) construction -->\n";
401 
402  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
403 
404  if(module->empty()) continue;
405  out << " <volume name=\"OMVolume" << module->getID() << "\">\n";
406  out << " <materialref ref=\"Water\"/>\n";
407  out << " <solidref ref=\"OMSphere\"/>\n";
408 
409  for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
410  out << " <physvol>\n";
411  out << " <volumeref ref=\"CathodVolume\"/>\n";
412  out << " <positionref ref=\"CathodPosition" << pmt->getID() << "_" << module->getID() << "\"/>\n";
413  out << " <rotationref ref=\"CathodRotation" << pmt->getID() << "_" << module->getID() << "\"/>\n";
414  out << " </physvol>\n";
415  }
416 
417  out << " </volume>\n";
418  }
419 
420  out << "<!-- StoreyVolume(s) construction -->\n";
421 
422  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
423  if(module->empty()) continue;
424  out << " <volume name=\"StoreyVolume" << module->getID() << "\">\n";
425  out << " <materialref ref=\"Water\"/>\n";
426  out << " <solidref ref=\"StoreyBox\"/>\n";
427  out << " <physvol>\n";
428  out << " <volumeref ref=\"OMVolume" << module->getID() << "\"/>\n";
429  out << " <positionref ref=\"OMShift\"/>\n";
430  out << " <rotationref ref=\"identity\"/>\n";
431  out << " </physvol>\n";
432  out << " </volume>\n";
433  }
434 
435  out << "<!-- Crust Volume construction-->\n";
436  out << "<volume name=\"CrustVolume\">\n";
437  out << " <materialref ref=\"Crust\"/>\n";
438  out << " <solidref ref=\"CrustBox\"/>\n";
439  out << "</volume>\n";
440 
441  out << "<!-- World Volume construction-->\n";
442  out << "<volume name=\"WorldVolume\">\n";
443  out << " <materialref ref=\"Water\"/>\n";
444  out << " <solidref ref=\"WorldTube\"/>\n";
445 
446  out << " <physvol>\n";
447  out << " <volumeref ref=\"CrustVolume\"/>\n";
448  out << " <positionref ref=\"CrustPosition\"/>\n";
449  out << " <rotationref ref=\"identity\"/>\n";
450  out << " </physvol>\n";
451 
452  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
453  if(module->empty()) continue;
454  out << " <physvol>\n";
455  out << " <volumeref ref=\"StoreyVolume" << module->getID() << "\"/>\n";
456  out << " <positionref ref=\"PosString" << module->getString() << "_Dom" << module->getID() << "\"/>\n";
457  out << " <rotationref ref=\"identity\"/>\n";
458  out << " </physvol>\n";
459  }
460 
461  out << "</volume>\n";
462 
463  out << "</structure>\n";
464  out << "<setup name=\"Default\" version=\"1.0\">\n";
465  out << "<world ref=\"WorldVolume\"/>\n";
466  out << "</setup>\n";
467  out << "</gdml>\n";
468  }
469 
470 
471  /**
472  * Load detector from input file.
473  *
474  * Supported file formats:
475  * - ASCII file, extension JDETECTOR::GENDET_DETECTOR_FILE_FORMAT, gendet format;
476  * - binary file, extension JDETECTOR::BINARY_DETECTOR_FILE_FORMAT, Jpp internal format;
477  * - ASCII file, extension JDETECTOR::KM3NET_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
478  * - gzipped file, extension JDETECTOR::ZIPPED_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
479  *
480  * \param file_name file name
481  * \param detector detector
482  */
483  inline void load(const std::string& file_name, JDetector& detector)
484  {
485  using namespace std;
486  using namespace JPP;
487 
489 
490  JMonteCarloDetector buffer(true);
491 
492  ifstream in(file_name.c_str());
493 
494  if (!in) {
495  THROW(JFileOpenException, "File not opened for reading: " << file_name);
496  }
497 
498  in >> buffer;
499 
500  in.close();
501 
502  detector.swap(buffer);
503 
504  } else if (getFilenameExtension(file_name) == BINARY_DETECTOR_FILE_FORMAT[0] ||
506 
507  JFileStreamReader in(file_name.c_str());
508 
509  if (!in) {
510  THROW(JFileOpenException, "File not opened for reading: " << file_name);
511  }
512 
513  try {
514 
515  detector.read(in);
516 
517  } catch(const exception& error) {
518 
519  // recovery procedure for old format of comments
520 
522 
523  in.clear();
524  in.rewind();
525 
526  detector.read(in);
527 
529  }
530 
531  in.close();
532 
533  } else if (getFilenameExtension(file_name) == KM3NET_DETECTOR_FILE_FORMAT) {
534 
535  ifstream in(file_name.c_str());
536 
537  if (!in) {
538  THROW(JFileOpenException, "File not opened for reading: " << file_name);
539  }
540 
541  in >> detector;
542 
543  in.close();
544 
545  } else if (getFilenameExtension(file_name) == ZIPPED_DETECTOR_FILE_FORMAT) {
546 
547  igzstream in(file_name.c_str());
548 
549  if (!in) {
550  THROW(JFileOpenException, "File not opened for reading: " << file_name);
551  }
552 
553  in >> detector;
554 
555  in.close();
556 
557  } else {
558 
559  THROW(JProtocolException, "Protocol not defined: " << file_name);
560  }
561  }
562 
563 
564  /**
565  * Store detector to output file.
566  *
567  * Supported file formats:
568  * - binary file, extension JDETECTOR::BINARY_DETECTOR_FILE_FORMAT, Jpp internal format;
569  * - ASCII file, extension JDETECTOR::KM3NET_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
570  * - gzipped file, extension JDETECTOR::ZIPPED_DETECTOR_FILE_FORMAT, %KM3NeT standard format;
571  * - gdml file, extension JDETECTOR::GDML_DETECTOR_FILE_FORMAT, KM3Sim input format;
572  *
573  * \param file_name file name
574  * \param detector detector
575  */
576  inline void store(const std::string& file_name, const JDetector& detector)
577  {
578  using namespace std;
579  using namespace JPP;
580 
581  if (getFilenameExtension(file_name) == BINARY_DETECTOR_FILE_FORMAT[1]) {
582 
583  JFileStreamWriter out(file_name.c_str());
584 
585  if (!out) {
586  THROW(JFileOpenException, "File not opened for writing: " << file_name);
587  }
588 
589  detector.write(out);
590 
591  out.close();
592 
593  } else if (getFilenameExtension(file_name) == KM3NET_DETECTOR_FILE_FORMAT) {
594 
595  std::ofstream out(file_name.c_str());
596 
597  if (!out) {
598  THROW(JFileOpenException, "File not opened for writing: " << file_name);
599  }
600 
601  out << detector;
602 
603  out.close();
604 
605  } else if (getFilenameExtension(file_name) == ZIPPED_DETECTOR_FILE_FORMAT) {
606 
607  ogzstream out(file_name.c_str());
608 
609  if (!out) {
610  THROW(JFileOpenException, "File not opened for writing: " << file_name);
611  }
612 
613  out << detector;
614 
615  out.close();
616 
617  } else if (getFilenameExtension(file_name) == GDML_DETECTOR_FILE_FORMAT) {
618 
619  std::ofstream out(file_name.c_str());
620 
621  if (!out) {
622  THROW(JFileOpenException, "File not opened for writing: " << file_name);
623  }
624 
625  write_gdml(out, detector);
626 
627  out.close();
628 
629  } else {
630 
631  THROW(JProtocolException, "Protocol not defined: " << file_name);
632  }
633  }
634 
635 
636  /**
637  * Auxiliary class to get rotation matrix between two optical modules.
638  */
639  struct JRotation :
640  public JRotation3D
641  {
642 
643  static const size_t NUMBER_OF_DIMENSIONS = 3; //!< Number of dimensions
644 
645 
646  /**
647  * Get rotation matrix to go from first to second module.
648  *
649  * \param first first module
650  * \param second second module
651  * \return rotation matrix
652  */
653  const JRotation3D& operator()(const JModule& first, const JModule& second)
654  {
655  this->setIdentity();
656 
657  if (first.size() == second.size()) {
658 
659  const size_t N = first.size();
660 
661  if (N >= NUMBER_OF_DIMENSIONS) {
662 
663  in .resize(N);
664  out.resize(N);
665 
666  for (size_t i = 0; i != N; ++i) {
667  in [i] = first .getPMT(i).getDirection();
668  out[i] = second.getPMT(i).getDirection();
669  }
670 
671  for (size_t i = 0; i != NUMBER_OF_DIMENSIONS; ++i) {
672  if (!orthonormalise(i)) {
673  THROW(JException, "Failure to orthonormalise direction " << i);
674  }
675  }
676 
677  this->a00 = out[0].getX() * in[0].getX() + out[1].getX() * in[1].getX() + out[2].getX() * in[2].getX();
678  this->a01 = out[0].getX() * in[0].getY() + out[1].getX() * in[1].getY() + out[2].getX() * in[2].getY();
679  this->a02 = out[0].getX() * in[0].getZ() + out[1].getX() * in[1].getZ() + out[2].getX() * in[2].getZ();
680 
681  this->a10 = out[0].getY() * in[0].getX() + out[1].getY() * in[1].getX() + out[2].getY() * in[2].getX();
682  this->a11 = out[0].getY() * in[0].getY() + out[1].getY() * in[1].getY() + out[2].getY() * in[2].getY();
683  this->a12 = out[0].getY() * in[0].getZ() + out[1].getY() * in[1].getZ() + out[2].getY() * in[2].getZ();
684 
685  this->a20 = out[0].getZ() * in[0].getX() + out[1].getZ() * in[1].getX() + out[2].getZ() * in[2].getX();
686  this->a21 = out[0].getZ() * in[0].getY() + out[1].getZ() * in[1].getY() + out[2].getZ() * in[2].getY();
687  this->a22 = out[0].getZ() * in[0].getZ() + out[1].getZ() * in[1].getZ() + out[2].getZ() * in[2].getZ();
688 
689  } else {
690 
691  THROW(JException, "Module " << first.getID() << " size " << N << " < " << NUMBER_OF_DIMENSIONS);
692  }
693 
694  } else {
695 
696  THROW(JException, "Module " << first.getID() << " size " << first.size() << " != " << second.size());
697  }
698 
699  return *this;
700  }
701 
702  private:
703  /**
704  * Put normalised primary direction at specified index and orthoganilise following directions.\n
705  * This procedure follows Gram-Schmidt process.
706  *
707  * \param index index
708  * \param precision precision
709  * \return true if primary direction exists; else false
710  */
711  bool orthonormalise(const size_t index, const double precision = std::numeric_limits<double>::epsilon())
712  {
713  using namespace std;
714 
715  size_t pos = index;
716 
717  for (size_t i = index + 1; i != in.size(); ++i) {
718  if (in[i].getLengthSquared() > in[pos].getLengthSquared()) {
719  pos = i;
720  }
721  }
722 
723  const double u = in[pos].getLength();
724 
725  if (u > precision) {
726 
727  in [pos] /= u;
728  out[pos] /= u;
729 
730  if (pos != index) {
731  swap(in [pos], in [index]);
732  swap(out[pos], out[index]);
733  }
734 
735  for (size_t i = index + 1; i != in.size(); ++i) {
736 
737  const double dot = in[index].getDot(in[i]);
738 
739  in [i] -= dot * in [index];
740  out[i] -= dot * out[index];
741  }
742 
743  return true;
744 
745  } else {
746 
747  return false;
748  }
749  }
750 
751 
754  };
755 
756 
757  /**
758  * Function object to get rotation matrix to go from first to second module.
759  */
761 
762 
763  /**
764  * Get position to go from first to second module.
765  *
766  * \param first first module
767  * \param second second module
768  * \return position
769  */
770  inline JPosition3D getPosition(const JModule& first, const JModule& second)
771  {
772  return second.getPosition() - first.getPosition();
773  }
774 
775 
776  /**
777  * Get calibration to go from first to second calibration.
778  *
779  * \param first first calibration
780  * \param second second calibration
781  * \return calibration
782  */
784  {
785  return JCalibration(second.getT0() - first.getT0());
786  }
787 }
788 
789 #endif
Exception for opening of file.
Definition: JException.hh:358
General exception.
Definition: JException.hh:24
Exceptions.
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:202
Auxiliary methods for geometrical methods.
Data structure for a composite optical module.
Definition: JModule.hh:67
range_type & include(argument_type x)
Include given value to range.
Definition: JRange.hh:397
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.
Rotation matrix.
Definition: JRotation3D.hh:111
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
std::set< int > getStringIDs(const JDetector &detector)
Get list of strings identifiers.
JTOOLS::JRange< int > floor_range
Type definition for range of floors.
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:589
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.
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.
double GetYrotationG4(const JVersor3D &dir)
Get rotation over Y axis in Geant4 coordinate system.
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.
Monte Carlo detector (i.e. ".det" file).
double putTime(const T &t1, const JCalibration &cal)
Get de-calibrated time.
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
virtual JReader & read(JReader &in) override
Read from input.
Definition: JDetector.hh:473
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:523
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
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
JPosition3D getPosition(const Vec &pos)
Get position.
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:213
int getNumberOfFloors(const JDetector &detector)
Get number of floors.
then awk string
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:172
Range of values.
Definition: JRange.hh:38
Logical location of module.
Protocol exception.
Definition: JException.hh:664
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.
double GetXrotationG4(const JVersor3D &dir)
Get rotation over X axis in Geant4 coordinate system.
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.
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Auxiliary class to define a range between two values.
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:48
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.
double getX() const
Get x position.
Definition: JVector3D.hh:94
double getMaximalDistance(const JDetector &detector, const bool option=false)
Get maximal distance between modules in detector.
static const char *const G4GDML_DEFAULT_SCHEMALOCATION
double getTanThetaC()
Get average tangent of Cherenkov angle of water corresponding to group velocity.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
static JRange< double, std::less< double > > DEFAULT_RANGE()
Default range.
Definition: JRange.hh:555
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:865
static const char *const GDML_DETECTOR_FILE_FORMAT
KM3Sim input format.
double getZ() const
Get z position.
Definition: JVector3D.hh:115
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.
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.