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