#ifndef __JCALIBRATE_JTDC_t__ #define __JCALIBRATE_JTDC_t__ #include #include #include #include #include "km3net-dataformat/online/JDAQ.hh" #include "JSystem/JStat.hh" #include "JLang/JStringStream.hh" #include "JLang/JException.hh" #include "Jeep/JComment.hh" /** * \author mdejong */ namespace JCALIBRATE {} namespace JPP { using namespace JCALIBRATE; } /** * Auxiliary classes and methods for PMT calibration. */ namespace JCALIBRATE { using JLANG::JValueOutOfRange; using KM3NETDAQ::NUMBER_OF_PMTS; using JEEP::JComment; /** * Auxiliary class for TDC constraints. */ struct JTDC_t : public std::multimap // module identifier -> TDC { typedef std::multimap multimap_type; /** * Type definition for range of TDC constraints of a given module identfier. */ typedef std::pair range_type; /** * Wild card for module identifier and TDC. */ enum { WILDCARD = -1 }; /** * Insert constraint. * * Note that if TDC equals JTDC_t::WILDCARD, * all possible TDCs in the given module are inserted. * * \param value module identifier and TDC */ void insert(const value_type& value) { if (value.second == WILDCARD) { for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) { multimap_type::insert(value_type(value.first, pmt)); } } else { multimap_type::insert(value); } } /** * Insert constraint. * * Note that if TDC equals JTDC_t::WILDCARD, * all possible TDCs in the given module are inserted. * * \param id module identifier * \param tdc TDC */ void insert(const int id, const int tdc) { this->insert(value_type(id, tdc)); } /** * Get range of constraints for given module. * * Note that if no data are available for given module identifier, * the data corresponding to JTDC_t::WILDCARD are returned. * * \param id module identifier * \return range of constraints */ range_type equal_range(const int id) { range_type range = multimap_type::equal_range(id); if (range.first == range.second) { range = multimap_type::equal_range(WILDCARD); } return range; } /** * Check if TDC is constraint. * * \param id module identifier * \param tdc TDC channel * \return true if given TDC is constraint; else false */ bool has(const int id, const int tdc) { const range_type range = equal_range(id); for (JTDC_t::const_iterator i = range.first; i != range.second; ++i) { if (tdc == i->second) { return true; } } return false; } /** * Reverse constraints. */ void reverse() { JTDC_t buffer; for (JTDC_t::const_iterator p = this->begin(); p != this->end(); ) { JTDC_t::const_iterator q = p; for ( ; q != this->end() && q->first == p->first; ++q) {} for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) { JTDC_t::const_iterator i = p; for ( ; i != q && i->second != pmt; ++i) {} if (i == q) { buffer.insert(value_type(p->first, pmt)); } } p = q; } this->swap(buffer); } /** * Check validity of TDC constrants. * * \param option option (if true, throw exception if not valid) * \return true if valid; else false */ bool is_valid(const bool option = false) const { for (const_iterator i = this->begin(); i != this->end(); ++i) { if (i->first < WILDCARD) { if (option) { THROW(JValueOutOfRange, "Invalid module identifier: " << i->first << " < " << WILDCARD); } return false; } if (i->second < 0 || i->second >= NUMBER_OF_PMTS) { if (option) { THROW(JValueOutOfRange, "Invalid TDC: " << i->second << " {0, .., " << NUMBER_OF_PMTS - 1 << "}"); } return false; } } return true; } /** * Read TDC constraints from input. * * \param in input stream * \param tdc TDC constraints * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JTDC_t& tdc) { using namespace std; using namespace JPP; JStringStream is(in); if (getFileStatus(is.str().c_str())) { is.load(); } is >> tdc.comment; for (int id, pmt; is >> id >> pmt; ) { tdc.insert(JTDC_t::value_type(id, pmt)); } return in; } /** * Write TDC constraints to output. * * \param out output stream * \param tdc TDC constraints * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JTDC_t& tdc) { using namespace std; out << tdc.comment; for (JTDC_t::const_iterator i = tdc.begin(); i != tdc.end(); ++i) { out << setw(10) << i->first << ' ' << setw(2) << i->second << endl; } return out; } JComment comment; }; } #endif