#ifndef __JHISTOGRAM__ #define __JHISTOGRAM__ #include "JTools/JGridBounds.hh" namespace JTOOLS { /** * Template definition of histogram interface. */ template class JHistogram { public: typedef JArgument_t argument_type; typedef JResult_t result_type; /** * Virtual destructor. */ virtual ~JHistogram() {} /** * Histogram filling. * * \param pX pointer to argument value * \param y content */ virtual void Fill(const argument_type* pX, const result_type& y) = 0; }; /** * Template definition of histogram. * This class implements the JHistogram::Fill() method. */ template class JCollection_t> class JHistogram1D : public JCollection_t, public JHistogram { public: typedef typename JElement_t::key_type key_type; typedef typename JElement_t::mapped_type mapped_type; typedef JElement_t value_type; typedef JCollection_t JContainer_t; typedef typename JContainer_t::const_iterator const_iterator; typedef typename JContainer_t::const_reverse_iterator const_reverse_iterator; typedef typename JContainer_t::iterator iterator; typedef typename JContainer_t::reverse_iterator reverse_iterator; typedef JHistogram JHistogram1D_t; typedef typename JHistogram1D_t::argument_type argument_type; typedef typename JHistogram1D_t::result_type result_type; using JContainer_t::insert; /** * Default constructor. */ JHistogram1D() : JContainer_t (), JHistogram1D_t(), lowerLimitValue(0.0), upperLimitValue(0.0) {} /** * Constructor. * * \param bounds grid bounds */ JHistogram1D(const JAbstractGridBounds& bounds) : JContainer_t (), JHistogram1D_t(), lowerLimitValue(0.0), upperLimitValue(0.0) { configure(bounds); } /** * Constructor. * * \param __begin begin of x-values * \param __end end of x-values */ template JHistogram1D(T __begin, T __end) : JContainer_t(), lowerLimitValue(0.0), upperLimitValue(0.0) { configure(__begin, __end); } /** * Configure histogram. * * \param bounds grid bounds */ void configure(const JAbstractGridBounds& bounds) { for (unsigned int i = 0; i != bounds.getNumberOfKeys(); ++i) { const argument_type x = bounds.getKey(i); this->insert(value_type(x, 0.0)); } } /** * Configure histogram. * * \param __begin begin of x-values * \param __end end of x-values */ template void configure(T __begin, T __end) { for (T i = __begin; i != __end; ++i) { const argument_type x = *i; this->insert(value_type(x, 0.0)); } } /** * Insert bin. * * \param x x-value */ void insert(const argument_type& x) { this->insert(value_type(x, 0.0)); } /** * Fill histogram. * * \param x key * \param y value */ virtual void Fill(const argument_type& x, const result_type& y) { iterator i = this->lower_bound(x); if (i == this->begin()) lowerLimitValue += y; else if (i == this->end()) upperLimitValue += y; else (--i)->getValue() += y; } /** * Fill histogram. * * \param pX pointer to key list * \param y value */ virtual void Fill(const argument_type* pX, const result_type& y) { Fill(*pX, y); } /** * Integral of histogram contents. * * \param plus include outside data (true), or not (false) * \return contents */ result_type getIntegral(bool plus = true) { result_type w = 0.0; for (const_iterator i = this->begin(); i != this->end(); ++i) w += i->getY(); if (plus) { w += this->lowerLimitValue; w += this->upperLimitValue; } return w; } result_type lowerLimitValue; result_type upperLimitValue; }; } #endif