#include #include #include #include #include #include #include "JDetector/JDetector.hh" #include "JDetector/JDetectorCalibration.hh" #include "JDetector/JDetectorToolkit.hh" #include "JDetector/JDetectorAddressMapToolkit.hh" #include "JDetector/JModuleRouter.hh" #include "JDetector/JPMTRouter.hh" #include "JSupport/JMeta.hh" #include "JSon/JSon.hh" #include "JSystem/JDateAndTime.hh" #include "Jeep/JeepToolkit.hh" #include "Jeep/JPrint.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { using namespace JPP; /** * Join UTC time range. * * \param js calibration input * \param range detector I/O */ void from_json(const json& js, JUTCTimeRange& range) { using namespace std; time_t Tmin = (time_t) range.getLowerLimit(); time_t Tmax = (time_t) range.getUpperLimit(); if (js.contains(ValidFrom_t) && !js[ValidFrom_t] .is_null()) { Tmin = JDateAndTime(js[ValidFrom_t] .get()).getTime(); } if (js.contains(ValidThrough_t) && !js[ValidThrough_t].is_null()) { Tmax = JDateAndTime(js[ValidThrough_t].get()).getTime(); } range.join(JUTCTimeRange(Tmin, Tmax)); } } /** * \file * * Auxiliary program to compose detector from separate calibrations.\n * Note that the validity range as specified in the header of the detector data structure is determined by * the maximal date and time value at JSON::ValidFrom_t and * the minimal date and time value at JSON::ValidThrough_t, respectively. * * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; string detectorFile; vector inputFile; string outputFile; int debug; try { JParser<> zap("Auxiliary program to compose detector from separate calibrations."); zap['a'] = make_field(detectorFile, "detector file"); zap['f'] = make_field(inputFile, "detector calibration files in JSON format "\ "(wild card \'" << FILENAME_WILD_CARD << "\' will be replaced by corresponding calibration set)"); zap['o'] = make_field(outputFile, "detector file") = ""; zap['d'] = make_field(debug) = 1; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } JDetector detector; try { load(detectorFile, detector); } catch(const JException& error) { FATAL(error); } if (!hasDetectorAddressMap(detector.getID())) { FATAL("No detector address map for detector identier " << detector.getID() << endl); } const JDetectorAddressMap& demo = getDetectorAddressMap(detector.getID()); const JModuleRouter moduleRouter(detector); const JPMTRouter pmtRouter (detector); detector.setUTCTimeRange(JUTCTimeRange()); enum { UNDEFINED = 0, DEFINED = 1 }; struct status_type { status_type() : value(UNDEFINED) {} operator int() const { return value; } status_type& operator=(const int value) { this->value = value; return *this; } int value; }; typedef map map_type; map_type tcal; map_type pcal; map_type rcal; map_type acal; map_type ccal; map_type scal[2]; for (const auto& file_name : inputFile) { vector buffer; if (hasWildCard(file_name)) buffer = { setWildCard(file_name, TCAL), setWildCard(file_name, PCAL), setWildCard(file_name, RCAL), setWildCard(file_name, ACAL), setWildCard(file_name, CCAL), setWildCard(file_name, SCAL) }; else buffer = { file_name }; for (const auto& file_name : buffer) { const JSon js(file_name); json::const_iterator data; if (is_valid(js) && (data = js.find(Data_t)) != js.end()) { detector.comment.add(MAKE_STRING("calibration=" << (js.contains(Comment_t) ? js[Comment_t] : "?"))); for (size_t i = 0; i != data->size(); ++i) { from_json(data->at(i), detector.getUTCTimeRange()); if (data->at(i).contains(DetID_t)) { if (data->at(i)[DetID_t].get() != detector.getID()) { FATAL("Detector identifier mismatch " << data->at(i)[DetID_t].get() << " != " << detector.getID() << endl); } } if (data->at(i).contains(PMTT0s_t)) { for (const auto& element : data->at(i)[PMTT0s_t].get()) { if (pmtRouter.hasPMT(element.getID())) { JPMT& pmt = detector.getPMT(pmtRouter.getAddress(element.getID())); pmt.setCalibration(element.getCalibration()); tcal[pmt.getID()] = DEFINED; } } } if (data->at(i).contains(DOMPositions_t)) { for (const auto& element : data->at(i)[DOMPositions_t].get()) { if (moduleRouter.hasModule(element.getID())) { JModule& module = detector.getModule(moduleRouter.getAddress(element.getID())); module.set(element.getPosition()); pcal[module.getID()] = DEFINED; } } } if (data->at(i).contains(BasePositions_t)) { for (const auto& element : data->at(i)[BasePositions_t].get()) { if (moduleRouter.hasModule(element.getID())) { JModule& module = detector.getModule(moduleRouter.getAddress(element.getID())); module.set(element.getPosition()); pcal[module.getID()] = DEFINED; } } } if (data->at(i).contains(DOMRotations_t)) { for (const auto& element : data->at(i)[DOMRotations_t].get()) { if (moduleRouter.hasModule(element.getID())) { JModule& module = detector.getModule(moduleRouter.getAddress(element.getID())); const JModule buffer = getModule(demo.get(module.getID()), module.getID()); if (module.size() != buffer.size()) { FATAL("Module size " << module.size() << " != " << buffer.size() << endl); } const JPosition3D center = module.getCenter(); for (size_t i = 0; i != module.size(); ++i) { module.getPMT(i).setAxis(buffer.getPMT(i).getAxis()); } module.rotate(element.getQuaternion()); module.setPosition(module.getCenter()); module.set(center); rcal[module.getID()] = DEFINED; } } } if (data->at(i).contains(DOMAcousticT0_t)) { for (const auto& element : data->at(i)[DOMAcousticT0_t].get()) { if (moduleRouter.hasModule(element.getID())) { JModule& module = detector.getModule(moduleRouter.getAddress(element.getID())); module.setCalibration(element.getCalibration()); acal[module.getID()] = DEFINED; } } } if (data->at(i).contains(BaseAcousticT0_t)) { for (const auto& element : data->at(i)[BaseAcousticT0_t].get()) { if (moduleRouter.hasModule(element.getID())) { JModule& module = detector.getModule(moduleRouter.getAddress(element.getID())); module.setCalibration(element.getCalibration()); acal[module.getID()] = DEFINED; } } } if (data->at(i).contains(DOMCompassRotations_t)) { for (const auto& element : data->at(i)[DOMCompassRotations_t].get()) { if (moduleRouter.hasModule(element.getID())) { JModule& module = detector.getModule(moduleRouter.getAddress(element.getID())); module.setQuaternion(element.getQuaternion()); ccal[module.getID()] = DEFINED; } } } if (data->at(i).contains(BaseCompassRotations_t)) { for (const auto& element : data->at(i)[BaseCompassRotations_t].get()) { if (moduleRouter.hasModule(element.getID())) { JModule& module = detector.getModule(moduleRouter.getAddress(element.getID())); module.setQuaternion(element.getQuaternion()); ccal[module.getID()] = DEFINED; } } } if (data->at(i).contains(PMTStatusInfo_t)) { for (const auto& element : data->at(i)[PMTStatusInfo_t].get()) { if (pmtRouter.hasPMT(element.getID())) { JPMT& pmt = detector.getPMT(pmtRouter.getAddress(element.getID())); pmt.setStatus(element.getStatus()); scal[0][pmt.getID()] = DEFINED; } } } if (data->at(i).contains(DOMStatusInfo_t)) { for (const auto& element : data->at(i)[DOMStatusInfo_t].get()) { if (moduleRouter.hasModule(element.getID())) { JModule& module = detector.getModule(moduleRouter.getAddress(element.getID())); module.setStatus(element.getStatus()); scal[1][module.getID()] = DEFINED; } } } if (data->at(i).contains(BaseStatusInfo_t)) { for (const auto& element : data->at(i)[BaseStatusInfo_t].get()) { if (moduleRouter.hasModule(element.getID())) { JModule& module = detector.getModule(moduleRouter.getAddress(element.getID())); module.setStatus(element.getStatus()); scal[1][module.getID()] = DEFINED; } } } } } } } for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) { if (pcal [module->getID()] != DEFINED) { ERROR("Module " << setw(10) << module->getID() << ' ' << getLabel(module->getLocation()) << " no position calibration." << endl); } if (rcal [module->getID()] != DEFINED && module->getFloor() != 0) { ERROR("Module " << setw(10) << module->getID() << ' ' << getLabel(module->getLocation()) << " no rotation calibration." << endl); } if (acal [module->getID()] != DEFINED) { ERROR("Module " << setw(10) << module->getID() << ' ' << getLabel(module->getLocation()) << " no acoustics calibration." << endl); } if (ccal [module->getID()] != DEFINED) { ERROR("Module " << setw(10) << module->getID() << ' ' << getLabel(module->getLocation()) << " no compass calibration." << endl); } if (scal[1][module->getID()] != DEFINED) { ERROR("Module " << setw(10) << module->getID() << ' ' << getLabel(module->getLocation()) << " no status calibration." << endl); } for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) { if (tcal [pmt->getID()] != DEFINED) { ERROR("PMT " << setw(8) << pmt->getID() << " no time calibration." << endl); } if (scal[0][pmt->getID()] != DEFINED) { ERROR("PMT " << setw(8) << pmt->getID() << " no status calibration." << endl); } } } detector.comment.add(JMeta(argc,argv)); try { if (outputFile != "") store(outputFile, detector); else cout << detector << endl; } catch(const JException& error) { FATAL(error); } return 0; }