#ifndef __JSUPPORT__JSUMMARYROUTER__ #define __JSUPPORT__JSUMMARYROUTER__ #include "km3net-dataformat/online/JDAQ.hh" #include "km3net-dataformat/online/JDAQSummaryslice.hh" #include "km3net-dataformat/online/JDAQModuleIdentifier.hh" #include "km3net-dataformat/online/JDAQPMTIdentifier.hh" #include "JTools/JRouter.hh" #include "JLang/JException.hh" /** * \author mdejong */ namespace JSUPPORT {} namespace JPP { using namespace JSUPPORT; } namespace JSUPPORT { using KM3NETDAQ::JDAQSummaryslice; using KM3NETDAQ::JDAQSummaryFrame; using KM3NETDAQ::JDAQPMTIdentifier; using JLANG::JValueOutOfRange; /** * Router for fast addressing of summary data in KM3NETDAQ::JDAQSummaryslice data structure * as a function of the optical module identifier and PMT address. * * The member method update() should be used to update the internal router for a given event. * The member method getRate(const JDAQPMTIdentifier&) const can subsequently be used to obtain * the measured singles rate for a given PMT. * * Optionally, a default rate can be set. * In that case, a default KM3NETDAQ::JDAQSummaryFrame is setup which is used * when no summary data are available or if the rate is below the specified default rate.\n * Note that the default rate is subject to the limited accuracy of the summary data. */ class JSummaryRouter { public: /** * Default constructor. */ JSummaryRouter() : router (-1), summary(NULL), rate_Hz(0.0) {} /** * Constructor with default rate. * * \param rate_Hz singles rate [Hz] */ JSummaryRouter(const double rate_Hz) : router (-1), summary(NULL), rate_Hz(rate_Hz) { setDefault(rate_Hz); } /** * Get default summary frame. * * \return summary frame */ const JDAQSummaryFrame& getSummaryFrame() const { return frame; } /** * Get default rate. * * \return rate [Hz] */ double getRate() const { return this->rate_Hz; } /** * Has default. * * \return true if default has been set; else false */ bool hasDefault() const { return (frame.getModuleID() == MODULE_IDENTIFIER); } /** * Set default. * * \param rate_Hz rate [Hz] */ void setDefault(const double rate_Hz) { using namespace KM3NETDAQ; this->rate_Hz = rate_Hz; frame.setModuleIdentifier(MODULE_IDENTIFIER); // allow default frame.setDAQFrameStatus(JDAQFrameStatus::getInstance()); // valid status for (int i = 0; i != NUMBER_OF_PMTS; ++i) { frame.setRate(i, rate_Hz); } } /** * Unset default. */ void unsetDefault() { using namespace KM3NETDAQ; frame.setModuleIdentifier(JDAQModuleIdentifier()); // disallow default frame.setDAQFrameStatus(JDAQFrameStatus()); // invalid status for (int i = 0; i != NUMBER_OF_PMTS; ++i) { frame[i] = JDAQRate(); } } /** * Update router. * * \param ps pointer to new summary slice */ void update(const JDAQSummaryslice* ps) { // reset internal router if (summary != NULL) { for (JDAQSummaryslice::const_iterator i = summary->begin(); i != summary->end(); ++i) { router.put(i->getModuleID(), router.getDefaultAddress()); } } summary = ps; if (summary != NULL) { // set internal router for (JDAQSummaryslice::const_iterator i = summary->begin(); i != summary->end(); ++i) { router.put(i->getModuleID(), distance(summary->begin(), i)); } } } /** * Check validity of summary data. * * \return true if summary data available; else false */ bool is_valid() const { return summary != NULL; } /** * Get summary slice. * * pointer to summary slice (may be NULL) */ const JDAQSummaryslice* getSummaryslice() const { return summary; } /** * Get address of module. * * \param module module * \return address */ const int getAddress(const JDAQModuleIdentifier& module) const { return router.get(module.getModuleID()); } /** * Has summary frame. * * \param module module * \return true if module present; else false */ bool hasSummaryFrame(const JDAQModuleIdentifier& module) const { return router.has(module.getModuleID()) || this->hasDefault(); } /** * Get summary frame. * * \param module module * \return summary frame */ const JDAQSummaryFrame& getSummaryFrame(const JDAQModuleIdentifier& module) const { if (router.has(module.getModuleID())) return (*summary)[getAddress(module)]; else if (this->hasDefault()) return frame; else THROW(JValueOutOfRange, "Invalid module identifier " << module); } /** * Get rate. * * \param id PMT identifier * \return rate [Hz] */ double getRate(const JDAQPMTIdentifier& id) const { double R = 0.0; if (this->hasSummaryFrame(id.getModuleIdentifier())) { R = this->getSummaryFrame(id.getModuleIdentifier()).getRate(id.getPMTAddress()); } if (R < this->getRate()) { R = this->getRate(); } return R; } static const int MODULE_IDENTIFIER = 1; //!< module identifier to allow default private: JTOOLS::JRouter router; const JDAQSummaryslice* summary; JDAQSummaryFrame frame; double rate_Hz; }; } #endif