#include #include #include #include #include #include #include #include #include "JDB/JDB.hh" #include "JDB/JSelector.hh" #include "JDB/JSelectorSupportkit.hh" #include "JDB/JCLBMap.hh" #include "JDB/JDetectorIntegration_t.hh" #include "JDB/JPBSSequence.hh" #include "JDB/JProductRouter.hh" #include "JDB/JDBToolkit.hh" #include "JDetector/JDetector.hh" #include "JDetector/JDetectorToolkit.hh" #include "JDetector/JDetectorSupportkit.hh" #include "JDetector/JDetectorCalibration.hh" #include "JLang/JEquation_t.hh" #include "JLang/JComparator.hh" #include "JLang/JPredicate.hh" #include "JLang/JVectorize.hh" #include "JSupport/JMeta.hh" #include "JSon/JSupport.hh" #include "Jeep/JPrint.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { using namespace JPP; /** * Get delay time for acoustic sensor with given %UPI. * * \param upi %UPI * \return delay time [ns] */ inline double getDelayTime(const JUPI_t& upi) { using namespace std; using namespace JPP; if (upi.getPBS() == PBS::HYDROPHONE) return HYDROPHONE_DELAYTIME_US * 1.0e3; else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V1") != string::npos) return PIEZO_V1_DELAYTIME_US * 1.0e3; else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V2") != string::npos) return PIEZO_V2_DELAYTIME_US * 1.0e3; else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V3") != string::npos) return PIEZO_V3_DELAYTIME_US * 1.0e3; else THROW(JValueOutOfRange, "Invalid UPI " << upi); } static const char* const NOPIEZO = "NOPIEZO"; // variant for absent piezo sensor } /** * \file * * Auxiliary program to download or update detector from database. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; JServer server; string usr; string pwd; string cookie; string id; vector calset; string detectorFile; string outputFile; int run; string variant; JCounter wizzard; double precision; int debug; try { JParser<> zap("Auxiliary program to download or update detector from database."); zap['s'] = make_field(server) = getServernames(); zap['u'] = make_field(usr) = ""; zap['!'] = make_field(pwd) = ""; zap['C'] = make_field(cookie) = ""; zap['D'] = make_field(id, "detector identifier") = ""; zap['r'] = make_field(run, "run number") = -1; zap['@'] = make_field(calset, "calibration sets \" = [; = ]\"" << endl << "possible keys: " << JEEPZ() << make_array(getCalibrationType.begin(), getCalibrationType.end(), &JCalibrationType::nick_name) << endl << "possible values: or \"?\"") = JPARSER::initialised(); zap['a'] = make_field(detectorFile) = ""; zap['o'] = make_field(outputFile) = ""; zap['V'] = make_field(variant, "detector version") = getDetectorVersions(), ""; zap['W'] = make_field(wizzard, "-W add base modules; -WW set time offsets of acoustic sensors"); zap['p'] = make_field(precision) = 1.0e-5; zap['d'] = make_field(debug) = 2; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } map calibration; for (vector::const_iterator i = calset.begin(); i != calset.end(); ++i) { if (getCalibrationType.hasNickname(i->getKey())) FATAL("Invalid calibration set \"" << i->getKey() << "\"" << endl); else calibration[i->getKey()] = i->getValue(); } if (calibration.size() != 0u && calibration.size() != 3u && calibration.size() != 6u) { FATAL("Inconsistent specificaton of calibration sets, either {}, {t,p,r}cal or {t,p,r,a,c,s}cal."); } const bool file = (detectorFile != ""); const bool tpr = (calibration.count(TCAL) != 0 && calibration.count(PCAL) != 0 && calibration.count(RCAL) != 0 && calibration.count(ACAL) == 0 && calibration.count(CCAL) == 0 && calibration.count(SCAL) == 0); const bool tpracs = (calibration.count(TCAL) != 0 && calibration.count(PCAL) != 0 && calibration.count(RCAL) != 0 && calibration.count(ACAL) != 0 && calibration.count(CCAL) != 0 && calibration.count(SCAL) != 0); const bool daq = (run != -1); if (file && id != "") { FATAL("Inconsistent specificaton of detector file and detector identifier."); } if (file && tpr) { FATAL("Inconsistent specificaton of detector file and {t,p,r}cal calibration sets."); } if (file && tpracs) { FATAL("Inconsistent specificaton of detector file and {t,p,r,a,c,s}cal calibration sets."); } if (file && daq) { FATAL("Inconsistent specificaton of detector file and run number."); } if (tpr && id == "") { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and no detector identifier."); } if (tpr && variant != "") { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and variant."); } if (tpr && daq) { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and run number."); } if (tpracs && id == "") { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and no detector identifier."); } if (tpracs && variant == "") { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and no variant."); } if (tpracs && daq) { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and run number."); } if (daq && id == "") { FATAL("Inconsistent specificaton of run number and no detector identifier."); } if (wizzard && variant != "" && getDetectorVersion(variant) < JDetectorVersion::V4) { FATAL("Inconsistent detector variant and option -W."); } try { JDB::reset(usr, pwd, cookie); } catch(const exception& error) { FATAL(error.what() << endl); } JDetector detector; try { if (file) load(detectorFile, detector); else if (tpr) *(JDB::get()->DetX)(id.c_str(), calibration[TCAL] != "?" ? calibration[TCAL].c_str() : NULL, calibration[PCAL] != "?" ? calibration[PCAL].c_str() : NULL, calibration[RCAL] != "?" ? calibration[RCAL].c_str() : NULL) >> detector; else if (tpracs) *(JDB::get()->DetX)(id.c_str(), calibration[TCAL] != "?" ? calibration[TCAL].c_str() : NULL, calibration[PCAL] != "?" ? calibration[PCAL].c_str() : NULL, calibration[RCAL] != "?" ? calibration[RCAL].c_str() : NULL, calibration[ACAL] != "?" ? calibration[ACAL].c_str() : NULL, calibration[CCAL] != "?" ? calibration[CCAL].c_str() : NULL, calibration[SCAL] != "?" ? calibration[SCAL].c_str() : NULL, getDetectorVersion(variant)) >> detector; else if (daq && variant == "") *(JDB::get()->DetX)(id.c_str(), run) >> detector; else if (daq && variant != "") *(JDB::get()->DetX)(id.c_str(), run, getDetectorVersion(variant)) >> detector; else FATAL("Invalid options."); } catch(const exception& error) { FATAL(error.what() << endl); } detector.comment.add(JMeta(argc,argv)); if (detector.setToLatestVersion()) { NOTICE("Set detector version to " << detector.getVersion() << endl); } if (wizzard) { vector clbmap; try { JSelector selector = getSelector(getDetector(detector.getID())); selector.add(&JCLBMap::FLOORID, 0); ResultSet& rs = getResultSet(getTable(), selector); rs >> clbmap; rs.Close(); } catch(const exception& error) { FATAL(error.what() << endl); } map > zmap; for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) { zmap[module->getString()][module->getFloor()] = module->getPosition(); } for (vector::const_iterator i = clbmap.begin(); i != clbmap.end(); ++i) { if (i->FLOORID == 0) { JDetector::iterator p = find_if(detector.begin(), detector.end(), make_predicate(&JModule::getID, i->DOMID, JComparison::eq())); if (p == detector.end()) { if (zmap[i->DUID].count(1) != 0) { JModule module(i->DOMID, JLocation(i->DUID, i->FLOORID)); module.setPosition(JPosition3D(zmap[i->DUID][1].getX(), zmap[i->DUID][1].getY(), isARCADetector(detector) ? ARCA_TBARZ_M : isORCADetector(detector) ? ORCA_TBARZ_M : 0.0)); NOTICE("Adding module " << module << endl); detector.push_back(module); } else { ERROR("Missing floor in string " << FILL(4,'0') << i->DUID << FILL() << endl); } } else if (zmap[i->DUID].count(1) != 0) { if (fabs(p->getX() - zmap[i->DUID][1].getX()) > precision || fabs(p->getY() - zmap[i->DUID][1].getY()) > precision) { NOTICE("Offset module " << getLabel(p->getLocation()) << " (x,y) position " << SCIENTIFIC(12,3) << (p->getX() - zmap[i->DUID][1].getX()) << ' ' << SCIENTIFIC(12,3) << (p->getY() - zmap[i->DUID][1].getY()) << endl); p->setPosition(JPosition3D(zmap[i->DUID][1].getX(), zmap[i->DUID][1].getY(), p->getZ())); } } else { FATAL("Missing floor in string " << setw(4) << i->DUID << endl); } } } } if (wizzard > 1) { try { JDetectorIntegration_t detector_t; ResultSet& rs = getResultSet(getTable()); if (! (rs >> detector_t)) { THROW(JDatabaseException, "Error reading " << getTable()); } detector_t.configure(getDetector(detector.getID())); for (const JPBS_t& pbs : { PBS::ACOUSTIC_SENSOR, PBS::HYDROPHONE }) { const JProductRouter router(detector_t, getPBSSequences(pbs)); JDetectorIntegration_t::range_type range = detector_t.find(pbs); for (JDetectorIntegration_t::range_const_iterator i = range.first; i != range.second; ++i) { const JLocation_t location = router.getLocation(i->first); JDetector::iterator p = find_if(detector.begin(), detector.end(), make_predicate(&JModule::getLocation, JLocation(location.string, (pbs != PBS::HYDROPHONE ? location.floor : 0)), JComparison::eq())); if (p != detector.end()) { try { const double t0 = getAverage(make_array(p->begin(), p->end(), &JPMT::getT0), 0.0) - getDelayTime(i->first); if (t0 != p->getT0()) { NOTICE("Set module time calibration " << getLabel(p->getLocation()) << ' ' << setw(10) << p->getID() << ' ' << setw(28) << left << i->first << right << ' ' << FIXED(12,3) << t0 << endl); p->setCalibration(t0); } } catch(const exception& error) { if (i->first.getVariant() != NOPIEZO) { ERROR(location << ' ' << error.what() << endl); } } } else { ERROR("No module for UPI " << i->first << " at " << location << endl); } } } } catch(const exception& error) { FATAL(error.what() << endl); } } if (outputFile != "") store(outputFile, detector); else cout << detector; }