#ifndef __JACOUSTICS__JACOUSTICSTOOLKIT__ #define __JACOUSTICS__JACOUSTICSTOOLKIT__ #include #include #include "km3net-dataformat/online/JDAQChronometer.hh" #include "JLang/JException.hh" #include "JLang/JPredicate.hh" #include "JGeometry3D/JVector3D.hh" /** * \file * * Acoustics toolkit. * \author mdejong */ namespace JACOUSTICS {} namespace JPP { using namespace JACOUSTICS; } namespace JACOUSTICS { using JLANG::JValueOutOfRange; using JLANG::JPredicate; using JGEOMETRY3D::JVector3D; /** * Auxiliary data structure to unify weights of acoustics data * according to the number of pings per emitter. * The default option is to unify the weights. */ struct JWeight : public std::map { /** * Constructor. * * \param __begin begin of data * \param __end end of data */ template JWeight(T __begin, T __end) : min(std::numeric_limits::max()) { for (T i = __begin; i != __end; ++i) { (*this)[i->getID()] += 1; } for (const_iterator i = this->begin(); i != this->end(); ++i) { if (i->second < min) { min = i->second; } } } /** * Get option to unify weights. * * \return option */ static bool getUnify() { return get_unify(); } /** * Set option to unify weights. * * \param unify option */ static void setUnify(const bool unify) { get_unify() = unify; } /** * Get weight. * * \param id identifier * \return weight */ double operator()(const int id) const { if (getUnify()) { const_iterator p = this->find(id); if (p != this->end()) return (double) min / (double) p->second; else THROW(JValueOutOfRange, "Invalid identifier " << id); } else { return 1.0; } } private: /** * Get option to unify weights. * * \return option */ static bool& get_unify() { static bool unify = true; return unify; } size_t min; }; /** * Get position from element in data which corresponds to given predicate. * * \param __begin begin of data * \param __end end of data * \param predicate predicate * \return position */ template inline JVector3D getPosition(T __begin, T __end, const JPredicate& predicate) { T p = std::find_if(__begin, __end, predicate); if (p != __end) return p->getPosition(); else THROW(JValueOutOfRange, "No element in container which corresponds to given predicate."); } /** * Get position from element in data which corresponds to given predicate. * * \param __begin begin of data * \param __end end of data * \param predicate predicate * \param position default position * \return position */ template inline JVector3D getPosition(T __begin, T __end, const JPredicate& predicate, const JVector3D& position) { try { return getPosition(__begin, __end, predicate); } catch(const std::exception&) { return position; } } /** * Get UNIX time of given DAQ object. * * \param chronometer chronometer * \return UNIX time [s] */ inline double getUNIXTime(const KM3NETDAQ::JDAQChronometer& chronometer) { return chronometer.getTimesliceStart().getTimeNanoSecond() * 1.0e-9; } } #endif