#ifndef __JDETECTOR__JDETECTORCALIBRATION__ #define __JDETECTOR__JDETECTORCALIBRATION__ #include #include #include "JSon/JSon.hh" #include "JSon/JSupport.hh" #include "JDB/JPBS_t.hh" #include "JDB/JUPI_t.hh" #include "JLang/JObjectID.hh" #include "JLang/JException.hh" #include "Jeep/JPrint.hh" #include "Jeep/JStatus.hh" #include "JDetector/JCalibration.hh" #include "JGeometry3D/JPosition3D.hh" #include "JGeometry3D/JQuaternion3D.hh" /** * file * Auxiliary data structures and methods for detector calibration. * * \author mdejong, bjung, acreusot */ namespace JDETECTOR {} namespace JPP { using namespace JDETECTOR; } /** * Auxiliary classes and methods for detector calibration. */ namespace JDETECTOR { using namespace JSON; using JLANG::JObjectID; using JLANG::JValueOutOfRange; using JLANG::JNoValue; using JDATABASE::JUPI_t; using JDATABASE::JPBS_t; using JEEP::JStatus; using JGEOMETRY3D::JPosition3D; using JGEOMETRY3D::JQuaternion3D; /** * Cast single value to array of strings, conform the DB-format. * * \param value value * \return std::vector containing value casted to string */ template inline std::vector get_string_array(T value) { using namespace std; return vector{ to_string(value) }; } /** * Retrieve value from json array of strings. * * \param string_value_array array of string-casted values * \return double-casted value */ inline double retrieve_value(std::vector string_value_array) { return std::stod(string_value_array[0]); } /** * Auxiliary data structure for module address map. */ struct JModuleMap_t : public JObjectID, public std::map { typedef std::map map_type; /** * Default constructor. */ JModuleMap_t() {} /** * Constructor. * * \param id module identifier */ JModuleMap_t(const JObjectID& id) : JObjectID(id) {} /** * Convert module address map to JSon. * * \param js json * \param object module address map */ friend inline void to_json(json& js, const JModuleMap_t& object) { js = json{ { Id_t, object.getID() }, { DOMMap_t, static_cast(object) } }; } /** * Convert JSon to module address map. * * \param js json * \param object module address map */ friend inline void from_json(const json& js, JModuleMap_t& object) { object.setID(js.at(Id_t).get()); static_cast(object) = js.at(DOMMap_t).get(); } }; /** * Auxiliary data structure for PMT time calibration. */ struct JPMTCalibration_t : public JObjectID, public JCalibration { /** * Default constructor. */ JPMTCalibration_t() {} /** * Constructor. * * \param id PMT identifier * \param calibration PMT time calibration */ JPMTCalibration_t(const JObjectID& id, const JCalibration& calibration) : JObjectID (id), JCalibration(calibration) {} /** * Convert PMT time calibration to JSon. * * \param js json * \param object PMT time calibration */ friend inline void to_json(json& js, const JPMTCalibration_t& object) { js = json{ { Serial_t, object.getID() }, { T0_t, object.getT0() } }; } /** * Convert JSon to PMT time calibration. * * \param js json * \param object PMT time calibration */ friend inline void from_json(const json& js, JPMTCalibration_t& object) { object.setID(js.at(Serial_t).get()); object.setT0(js.at(T0_t).get()); } }; /** * Auxiliary data structure for PMT status. */ struct JPMTStatus_t : public JObjectID, public JStatus { /** * Default constructor. */ JPMTStatus_t() {} /** * Constructor. * * \param id PMT identifier * \param status PMT status */ JPMTStatus_t(const JObjectID& id, const JStatus& status) : JObjectID(id), JStatus (status) {} /** * Convert PMT status to JSon. * * \param js json * \param object PMT status */ friend inline void to_json(json& js, const JPMTStatus_t& object) { js = json{ { Serial_t, object.getID() }, { STATUS_t, object.getStatus() } }; } /** * Convert JSon to PMT status. * * \param js json * \param object PMT status */ friend inline void from_json(const json& js, JPMTStatus_t& object) { object.setID(js.at(Serial_t).get()); object.setStatus(js.at(STATUS_t).get()); } }; /** * Data structure for PMT high-voltage calibration. */ struct JHVCalibration_t : public JUPI_t { /** * Default constructor. */ JHVCalibration_t() : JUPI_t (), result (Fail_t), supplyVoltage(0.0), runNumbers (std::vector(0)), PMTgain (0.0) {} /** * Constructor. * * \param upi %UPI * \param result result * \param hv HV * \param runNumberList run numbers * \param gain gain */ JHVCalibration_t(const JUPI_t& upi, const std::string& result, const double hv, const std::vector runNumberList = std::vector(0), const double gain = 1.0) : JUPI_t (upi), result (result), supplyVoltage(hv), runNumbers (runNumberList), PMTgain (gain) {} /** * Get HV-tuning database test type. * * \return HV-tuning database test type */ static int getVersion() { return get_version(); } /** * Set HV-tuning database test type. * * \param version HV-tuning database test type */ static void setVersion(const int version) { get_version() = version; } /** * Convert PMT high-voltage calibration to JSon. * * \param js json2 * \param object PMT high-voltage calibration */ friend inline void to_json(json& js, const JHVCalibration_t& object) { using namespace std; ostringstream os; os << object.getUPI(); js[UPI_t] = os.str(); js[Test_t + Result_t] = object.result; js[Test_t + Parameters_t][0] = json{ { Name_t, PMTSupplyVoltage_t }, { Unit_t, Volt_t }, { Values_t, get_string_array(object.supplyVoltage) } }; if (JHVCalibration_t::getVersion() < 2) { js[Test_t + Parameters_t][2] = json{ { Name_t, PMTGain_t }, { Unit_t, Unitless_t }, { Values_t, get_string_array(object.PMTgain) } }; } if (JHVCalibration_t::getVersion() < 3) { vector runNumberList; for (vector::const_iterator i = object.runNumbers.cbegin(); i != object.runNumbers.cend(); ++i) { runNumberList.push_back(to_string(*i)); } js[Test_t + Parameters_t][1] = json{ { Name_t, RunNumber_t }, { Unit_t, Unitless_t }, { Values_t, runNumberList } }; } } /** * Convert JSon to PMT high-voltage calibration. * * \param js json * \param object PMT high-voltage calibration */ friend inline void from_json(const json& js, JHVCalibration_t& object) { using namespace std; stringstream is(js.at(UPI_t).get()); is >> static_cast(object); json parameters = js.at(Test_t + Parameters_t); if (JHVCalibration_t::getVersion() > 0 && parameters.size() > 0) { object.result = js.at(Test_t + Result_t).get(); object.supplyVoltage = retrieve_value(parameters[0].at(Values_t).get>()); if (JHVCalibration_t::getVersion() < 3 && parameters.size() > 1) { vector runNumberList = parameters[1].at(Values_t).get>(); for (vector::const_iterator i = runNumberList.begin(); i != runNumberList.end(); ++i) { object.runNumbers.push_back(stoi(*i)); } } if (JHVCalibration_t::getVersion() < 2 && parameters.size() > 2) { object.PMTgain = retrieve_value(parameters[2].at(Values_t).get>()); } } else { THROW(JValueOutOfRange, "JHVCalibration_t::from_json(): No " << MAKE_STRING(Test_t + Parameters_t) << " found." << endl); } } std::string result; double supplyVoltage; std::vector runNumbers; double PMTgain; private: /** * Get reference to HV-tuning database test type. * * \return HV-tuning database test type */ static int& get_version() { static int version = 3; return version; } }; /** * Data structure for PMT threshold calibration. */ struct JPMTThresholdCalibration_t : public JUPI_t { /** * Default constructor. */ JPMTThresholdCalibration_t() {} /** * Constructor. * * \param upi %UPI * \param result result * \param threshold threshold * \param runNumbers run numbers */ JPMTThresholdCalibration_t(const JUPI_t& upi, const std::string& result, const double threshold, const std::vector& runNumbers = std::vector(0)) : JUPI_t (upi), result (result), threshold (threshold), runNumberList(runNumbers) {} /** * Convert PMT threshold calibration to JSon. * * \param js json * \param object PMT threshold calibration */ friend inline void to_json(json& js, const JPMTThresholdCalibration_t& object) { std::ostringstream os; os << object.getUPI(); js[UPI_t] = os.str(); js[Test_t + Result_t] = object.result; js[Test_t + Parameters_t][0] = json{ { Name_t, PMTThreshold_t }, { Unit_t, Unitless_t }, { Values_t, get_string_array(object.threshold) } }; js[Test_t + Parameters_t][2] = json{ { Name_t, RunNumber_t }, { Unit_t, Unitless_t }, { Values_t, object.runNumberList } }; } /** * Convert JSon to PMT threshold calibration. * * \param js json * \param object PMT threshold calibration */ friend inline void from_json(const json& js, JPMTThresholdCalibration_t& object) { using namespace std; stringstream is(js.at(UPI_t).get()); is >> static_cast(object); object.result = js.at(Test_t + Result_t).get(); object.threshold = retrieve_value(js.at(Test_t + Parameters_t)[0].at(Values_t).get>()); object.runNumberList = js.at(Test_t + Parameters_t)[2].at(Values_t).get>(); } std::string result; double threshold; std::vector runNumberList; }; /** * Auxiliary data structure for module position. */ struct JModulePosition_t : public JObjectID, public JPosition3D { /** * Default constructor. */ JModulePosition_t() {} /** * Constructor. * * \param id module identifier * \param position module position */ JModulePosition_t(const JObjectID& id, const JPosition3D& position) : JObjectID (id), JPosition3D(position) {} /** * Convert module position to JSon. * * \param js json * \param object module position */ friend inline void to_json(json& js, const JModulePosition_t& object) { js = json{ { Id_t, object.getID() }, { PX_t, object.getX() }, { PY_t, object.getY() }, { PZ_t, object.getZ() } }; } /** * Convert JSon to module position. * * \param js json * \param object module position */ friend inline void from_json(const json& js, JModulePosition_t& object) { if (js.contains(DOMId_t)) object.setID(js.at(DOMId_t).get()); else if (js.contains(Id_t)) object.setID(js.at(Id_t) .get()); else THROW(JNoValue, "Missing module identifier."); object.setPosition(JPosition3D(js.at(PX_t).get(), js.at(PY_t).get(), js.at(PZ_t).get())); } }; /** * Auxiliary data structure for module rotation. */ struct JModuleRotation_t : public JObjectID, public JQuaternion3D { /** * Default constructor. */ JModuleRotation_t() {} /** * Constructor. * * \param id module identifier * \param rotation module rotation */ JModuleRotation_t(const JObjectID& id, const JQuaternion3D& rotation) : JObjectID (id), JQuaternion3D(rotation) {} /** * Convert module rotation to JSon. * * \param js json * \param object module rotation */ friend inline void to_json(json& js, const JModuleRotation_t& object) { js = json{ { Id_t, object.getID() }, { Q0_t, object.getA() }, { QX_t, object.getB() }, { QY_t, object.getC() }, { QZ_t, object.getD() } }; } /** * Convert JSon to module rotation. * * \param js json * \param object module rotation */ friend inline void from_json(const json& js, JModuleRotation_t& object) { if (js.contains(DOMId_t)) object.setID(js.at(DOMId_t).get()); else if (js.contains(Id_t)) object.setID(js.at(Id_t) .get()); else THROW(JNoValue, "Missing module identifier."); object.setQuaternion(JQuaternion3D(js.at(Q0_t).get(), js.at(QX_t).get(), js.at(QY_t).get(), js.at(QZ_t).get())); } }; /** * Auxiliary data structure for module status. */ struct JModuleStatus_t : public JObjectID, public JStatus { /** * Default constructor. */ JModuleStatus_t() {} /** * Constructor. * * \param id module identifier * \param status module status */ JModuleStatus_t(const JObjectID& id, const JStatus& status) : JObjectID(id), JStatus (status) {} /** * Convert module status to JSon. * * \param js json * \param object module status */ friend inline void to_json(json& js, const JModuleStatus_t& object) { js = json{ { Id_t, object.getID() }, { STATUS_t, object.getStatus() } }; } /** * Convert JSon to module status. * * \param js json * \param object module status */ friend inline void from_json(const json& js, JModuleStatus_t& object) { if (js.contains(DOMId_t)) object.setID(js.at(DOMId_t).get()); else if (js.contains(Id_t)) object.setID(js.at(Id_t) .get()); else THROW(JNoValue, "Missing module identifier."); object.setStatus(js.at(STATUS_t).get()); } }; /** * Auxiliary data structure for module time calibration. */ struct JModuleCalibration_t : public JObjectID, public JCalibration { /** * Default constructor. */ JModuleCalibration_t() {} /** * Constructor. * * \param id module identifier * \param calibration module time calibration */ JModuleCalibration_t(const JObjectID& id, const JCalibration& calibration) : JObjectID (id), JCalibration(calibration) {} /** * Convert module time calibration to JSon. * * \param js json * \param object module time calibration */ friend inline void to_json(json& js, const JModuleCalibration_t& object) { js = json{ { Id_t, object.getID() }, { T0_t, object.getT0() } }; } /** * Convert JSon to module time calibration. * * \param js json * \param object module time calibration */ friend inline void from_json(const json& js, JModuleCalibration_t& object) { if (js.contains(DOMId_t)) object.setID(js.at(DOMId_t).get()); else if (js.contains(Id_t)) object.setID(js.at(Id_t) .get()); else THROW(JNoValue, "Missing module identifier."); object.setT0(js.at(T0_t).get()); } }; /** * Auxiliary data structure for compass rotation. */ struct JCompassRotation_t : public JObjectID, public JQuaternion3D { /** * Default constructor. */ JCompassRotation_t() {} /** * Constructor. * * \param id module identifier * \param rotation compass rotation */ JCompassRotation_t(const JObjectID& id, const JQuaternion3D& rotation) : JObjectID (id), JQuaternion3D(rotation) {} /** * Convert compass rotation to JSon. * * \param js json * \param object compass rotation */ friend inline void to_json(json& js, const JCompassRotation_t& object) { js = json{ { Id_t, object.getID() }, { Q0_t, object.getA() }, { QX_t, object.getB() }, { QY_t, object.getC() }, { QZ_t, object.getD() } }; } /** * Convert JSon to compass rotation. * * \param js json * \param object compass rotation */ friend inline void from_json(const json& js, JCompassRotation_t& object) { if (js.contains(DOMId_t)) object.setID(js.at(DOMId_t).get()); else if (js.contains(Id_t)) object.setID(js.at(Id_t) .get()); else THROW(JNoValue, "Missing module identifier."); object.setQuaternion(JQuaternion3D(js.at(Q0_t).get(), js.at(QX_t).get(), js.at(QY_t).get(), js.at(QZ_t).get())); } }; typedef std::vector JHVCalibration; //!< PMT high voltage calibration typedef std::vector JPMTThresholdCalibration; //!< PMT threshold calibration typedef std::vector JModuleMap; //!< module map typedef std::vector JPMTCalibration; //!< PMT time calibration typedef std::vector JPMTStatus; //!< PMT status typedef std::vector JModulePosition; //!< Module position typedef std::vector JModuleRotation; //!< Module rotation typedef std::vector JModuleStatus; //!< Module status typedef std::vector JModuleCalibration; //!< Module time calibration typedef std::vector JCompassRotation; //!< Compass rotation } #endif