#ifndef __JDB_JAHRSCALIBRATION_T__ #define __JDB_JAHRSCALIBRATION_T__ #include #include #include #include #include #include "JLang/JException.hh" #include "JLang/JObjectStreamIO.hh" #include "JDB/JDB.hh" #include "JDB/JDBToolkit.hh" #include "JDB/JPBS_t.hh" #include "JDB/JAHRSCalibration.hh" #include "JDB/JAHRSCalibrationToolkit.hh" #include "JDB/JDetectorIntegration_t.hh" /** * \author mdejong */ namespace JDATABASE {} namespace JPP { using namespace JDATABASE; } namespace JDATABASE { using JLANG::JValueOutOfRange; using JLANG::JObjectStreamIO; /** * Auxiliary class to map module identifier to AHRS calibration. */ struct JAHRSCalibration_t : public std::map, public JObjectStreamIO { /** * Default constructor. */ JAHRSCalibration_t() {} /** * Constructor. * * \param id detector identifier * \param option option */ JAHRSCalibration_t(const int id, const int option) { using namespace std; using namespace JPP; JDetectorIntegration_t detector; ResultSet& rs = getResultSet(getTable()); rs >> detector; detector.configure(getDetector(id)); this->configure(detector, option); } /** * Constructor. * * \param detector detector integration * \param option option */ JAHRSCalibration_t(const JDetectorIntegration_t& detector, const int option) { this->configure(detector, option); } /** * Constructor. * * \param file_name input file */ JAHRSCalibration_t(const char* file_name) { load(file_name); } /** * Configure. * * The latest valid AHRS calibration data to the identifier of the parent module.\n * In this, the time order and validity of the AHRS calibration data are defined by AHRSComparator and JAHRSValidity, respectively.\n * The option corresponds to the maximal difference between latest version and latest valid version. * * \param detector detector integration * \param option option */ void configure(const JDetectorIntegration_t& detector, const int option) { using namespace std; using namespace JPP; const JAHRSCalibrationValidity is_valid; JDBToolkit::initialise(getUPI, PBS::AHRS); JDBToolkit::initialise(getCLBID); this->clear(); vector calibration; ResultSet& rs = getResultSet(getTable()); rs >> calibration; rs.Close(); sort(calibration.begin(), calibration.end(), [](const JAHRSCalibration& first, const JAHRSCalibration& second) { return !JAHRSCalibrationComparator()(first, second); }); for (vector::const_iterator p = calibration.begin(); p != calibration.end(); ) { vector::const_iterator q = p; for (++q; q != calibration.end() && q->SERIALNUMBER == p->SERIALNUMBER; ++q) {} const JAHRSCalibration& calibration = *p; const JUPI_t upi = getUPI(PBS::AHRS, calibration.SERIALNUMBER); const JDetectorIntegration_t::range_type r1 = detector.find(upi); if (distance(r1.first, r1.second) == 1) { const int id = getCLBID(detector[r1.first->second].container.getUPI()); for (vector::const_iterator i = p; i != q; ++i) { if (is_valid(*i) && distance(p,i) <= option) { this->insert(make_pair(id, calibration)); break; } } } p = q; } } /** * Check availability of AHRS calibration for given module identifier. * * \param id module identifier * \return true if AHRS calibration available; else false */ bool has(int id) const { return (this->find(id) != this->end()); } /** * Get AHRS calibration for given module identifier. * * \param id module identifier * \return AHRS calibration */ const JAHRSCalibration& get(int id) const { const_iterator i = this->find(id); if (i != this->end()) return i->second; else THROW(JValueOutOfRange, "Invalid module identifier " << id); } /** * Read AHRS calibration from input stream. * * \param in input stream * \param calibration AHRS calibration * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JAHRSCalibration_t& calibration) { int id; JAHRSCalibration buffer; while (in >> id >> buffer) { calibration[id] = buffer; } return in; } /** * Write AHRS calibration to output stream. * * \param out output stream * \param calibration AHRS calibration * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JAHRSCalibration_t& calibration) { using namespace std; for (JAHRSCalibration_t::const_iterator i = calibration.begin(); i != calibration.end(); ++i) { out << i->first << ' ' << i->second << endl; } return out; } }; } #endif