#ifndef __JTOOLS__JMULTIPDF__ #define __JTOOLS__JMULTIPDF__ #include "JTools/JTransformableMultiFunction.hh" #include "JTools/JTransformableMultiHistogram.hh" #include "JTools/JHistogramMap.hh" #include "JTools/JToolsToolkit.hh" #include "JIO/JObjectBinaryIO.hh" /** * \author mdejong */ namespace JTOOLS {} namespace JPP { using namespace JTOOLS; } namespace JTOOLS { using JIO::JObjectBinaryIO; /** * General purpose class for multi-dimensional probability density function (PDF). * * This class is a simple wrapper around the JTransformableMultiFunction class * to properly convert a multi-dimensional histogram to a multi-dimensional PDF. * In this, the histogram contents in each bin are divided by the summed weight * of the multi-dimensional histogram and the width (c.q. area) of the multi-dimensional bin. */ template > class JMultiPDF : public JTransformableMultiFunction, public JObjectBinaryIO< JMultiPDF > { public: typedef JTransformableMultiFunction transformablemultifunction_type; enum { NUMBER_OF_DIMENSIONS = transformablemultifunction_type::NUMBER_OF_DIMENSIONS }; typedef JFunction_t function_type; typedef typename transformablemultifunction_type::value_type value_type; typedef typename transformablemultifunction_type::argument_type argument_type; typedef typename transformablemultifunction_type::supervisor_type supervisor_type; typedef typename transformablemultifunction_type::abscissa_type abscissa_type; typedef typename transformablemultifunction_type::ordinate_type ordinate_type; typedef typename transformablemultifunction_type::result_type result_type; typedef typename transformablemultifunction_type::const_iterator const_iterator; typedef typename transformablemultifunction_type::const_reverse_iterator const_reverse_iterator; typedef typename transformablemultifunction_type::iterator iterator; typedef typename transformablemultifunction_type::reverse_iterator reverse_iterator; typedef typename transformablemultifunction_type::super_iterator super_iterator; typedef typename transformablemultifunction_type::super_const_iterator super_const_iterator; typedef JMultiMapTransformer::value, argument_type> transformer_type; typedef typename transformer_type::array_type array_type; using transformablemultifunction_type::insert; /** * Default constructor. */ JMultiPDF() {} /** * Constructor. * * \param input multi-dimensional input */ template JMultiPDF(const T& input) { insert(input); this->compile(); } /** * Insert multi-dimensional input. * * \param input multi-dimensional histogram */ template void insert(const JMultiHistogram& input) { this->insert(JMultiKey<0, argument_type>(), input, input.getIntegral()); } /** * Insert multi-dimensional input. * * \param input multi-dimensional histogram */ template void insert(const JTransformableMultiHistogram& input) { this->transformer.reset(input.transformer->clone()); this->insert(JMultiKey<0, argument_type>(), input, input.getIntegral()); } protected: /** * Recursive method to insert multi-dimensional histogram at multi-dimensional key. * * \param key multi-dimensional key * \param input multi-dimensional histogram * \param weight weight */ template class __JMap_t, class __JDistance_t> void insert(const JMultiKey& key, const JHistogramMap<__JAbscissa_t, __JContents_t, __JMap_t, __JDistance_t>& input, const double weight) { if (input.size() > 1) { for (typename JHistogramMap<__JAbscissa_t, __JContents_t, __JMap_t, __JDistance_t>::const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) { const argument_type x = 0.5 * (i->getX() + j->getX()); const double w = weight * input.getDistance(i->getX(), j->getX()); insert(JMultiKey(key, x), i->getY(), w); } } } /** * Termination method to insert multi-dimensional histogram at multi-dimensional key. * * Convert one-dimensional histogram to PDF, normalise PDF with given weight and insert result at given multi-dimensional key. * * \param key multi-dimensional key * \param input histogram * \param weight weight */ template class __JContainer_t, class __JDistance_t> void insert(const JMultiKey::value, argument_type>& key, const JHistogram1D<__JElement_t, __JContainer_t, __JDistance_t>& input, const double weight) { JFunction_t buffer; makePDF(input, buffer); buffer.div(weight); transformablemultifunction_type::insert(key, buffer); } }; /** * Conversion of multi-dimensional histogram to multi-dimensional PDF. * * \param input multi-dimensional histogram * \param output multi-dimensional PDF */ template inline void makePDF(const JMultiHistogram& input, JMultiPDF& output) { output.insert(input); } } #endif