#ifndef __JDB_JPMTRUNSETUPPARAMS__ #define __JDB_JPMTRUNSETUPPARAMS__ #include #include #include #include #include "JDB/JDB.hh" #include "JDB/JSelector.hh" #include "JDB/JSelectorSupportkit.hh" #include "JDB/JDBToolkit.hh" #include "JDB/JRuns.hh" #include "JDB/JAllParams.hh" #include "JDB/JRunsetupParams.hh" #include "JDB/JUPI_t.hh" #include "Jeep/JPrint.hh" /** * \author mdejong */ namespace JDATABASE {} namespace JPP { using namespace JDATABASE; } namespace JDATABASE { const char* const OPTICS_t = "OPTICS"; //!< sub-system of PMT const char* const PMT_THRESHOLD_t = "PMT_THRESHOLD"; //!< parameter name of PMT threshold const char* const PMT_HV_t = "PMT_HIGHVOLT"; //!< parameter name of PMT high voltage /** * Auxiliary data structure for fallback order of PMT data. */ template struct JPMTValue : std::pair { /** * Default constructor. */ JPMTValue() : std::pair(-1, JValue_t()) {} }; /** * Auxiliary class for PMT run setup parameters. * * This class provides for an implementation of the fallback method based on the %UPI of a PMT and the value of JRunsetupParams::ORDER. */ template class JPMTRunsetupParams : public std::map > { public: /** * Type definition of PMT parameter value. */ struct result_type { bool is_default; //!< true if fallback; else false JValue_t value; //!< value }; /** * Default constructor. */ JPMTRunsetupParams() {} /** * Configure PMT run setup parameters for given detector and run. * * \param id detector identifier * \param run run number * \param parameter parameter name */ void configure(const int id, const int run, const std::string& parameter) { using namespace std; using namespace JPP; this->clear(); string rs_oid; JAllParams upars; { ResultSet& rs = getResultSet(getTable(), getSelector(id, run)); JRuns parameters; if (rs >> parameters) { rs_oid = parameters.RUNSETUPID; } rs.Close(); } { ResultSet& rs = getResultSet(getTable(), getSelector(OPTICS_t)); for (JAllParams parameters; rs >> parameters; ) { if (parameters.NAME == parameter) { upars = parameters; } } rs.Close(); } { ResultSet& rs = getResultSet(getTable(), getSelector(getDetector(id), rs_oid)); for (JRunsetupParams parameters; rs >> parameters; ) { if (parameters.PAR_OID == upars.OID && parameters.ISINPUT == 'Y') { if (parameters.VALUE != "") { JPMTValue& value = (*this)[parameters.UPIFILTER]; if (parameters.ORDER > value.first) { value.first = parameters.ORDER; istringstream(parameters.VALUE) >> value.second; } } } } rs.Close(); } } /** * Get PMT parameter value for given %UPI of PMT. * * \param upi %UPI * \return PMT parameter value */ result_type operator()(const JUPI_t& upi) const { typename JPMTRunsetupParams::const_iterator p = this->find(upi.toString()), p0 = p, p1 = p; // fallbacks const JUPI_t buffer[] = { JUPI_t(upi.getPBS(), upi.getVariant(), JUPI_t::DEFAULT_VERSION, 0), JUPI_t(upi.getPBS(), upi.getVariant(), JUPI_t::DEFAULT_VERSION, JUPI_t::DEFAULT_NUMBER), JUPI_t(upi.getPBS(), "", JUPI_t::DEFAULT_VERSION, 0), JUPI_t(upi.getPBS(), "", JUPI_t::DEFAULT_VERSION, JUPI_t::DEFAULT_NUMBER) }; for (const JUPI_t& i : buffer) { if ((p = this->find(i.toString())) != this->end()) { if (p1 == this->end() || p->first > p1->first) { p1 = p; } } } if (p1 != this->end()) { return { p0 != p1, p1->second.second }; } THROW(JDatabaseException, "Invalid UPI " << upi); } /** * Write PMT run setup parameters to output stream. * * \param out output stream * \param object PMT run setup parameters * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JPMTRunsetupParams& object) { using namespace std; for (typename JPMTRunsetupParams::const_iterator i = object.begin(); i != object.end(); ++i) { out << left << setw(32) << i->first << ' ' << right << i->second.second << endl; } return out; } }; } #endif