Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-03 11:12:50

0001 // -*- C++ -*-
0002 //
0003 // This file is part of YODA -- Yet more Objects for Data Analysis
0004 // Copyright (C) 2008-2025 The YODA collaboration (see AUTHORS for details)
0005 //
0006 #ifndef YODA_BinnedEstimate_h
0007 #define YODA_BinnedEstimate_h
0008 
0009 #include "YODA/AnalysisObject.h"
0010 #include "YODA/BinnedStorage.h"
0011 #include "YODA/Estimate.h"
0012 #include "YODA/Scatter.h"
0013 #include "YODA/Transformation.h"
0014 #include "YODA/Utils/BinnedUtils.h"
0015 #include <regex>
0016 
0017 #ifdef HAVE_HDF5
0018 #include "YODA/Utils/H5Utils.h"
0019 #endif
0020 
0021 namespace YODA {
0022 
0023 
0024   /// @note Anonymous namespace to limit visibility to this file
0025   namespace {
0026     /// @brief Nullify bin uncertainty for discrete axes
0027     template<size_t axisN, typename BinT>
0028     std::pair<double, double> nullifyIfDisc(const BinT& /* b */, const double /* val */, std::false_type, const double null = 0.0) {
0029       return { null, null };
0030     }
0031 
0032     /// @brief Work out bin uncertainty with respect to
0033     /// central value for continuous axes
0034     template<size_t axisN, typename BinT>
0035     std::pair<double, double> nullifyIfDisc(const BinT& b, const double val, std::true_type, const double /* null */ = 0.0) {
0036       return { val - b.template min<axisN>(), b.template max<axisN>() - val };
0037     }
0038 
0039     /// @brief Unify bin interval for discrete axes
0040     template<size_t axisN, typename BinT>
0041     double unifyIfDisc(const BinT& /* b */, std::false_type, const double unity = 1.0) {
0042       return unity;
0043     }
0044 
0045     /// @brief Get bin width for continuous axes
0046     template<size_t axisN, typename BinT>
0047     double unifyIfDisc(const BinT& b, std::true_type, const double /* unity */ = 1.0) {
0048       return b.template width<axisN>();
0049     }
0050 
0051     /// @brief Return bin index for non-integral discrete axes
0052     template<size_t axisN, typename BinT>
0053     double coordPicker(const BinT& b, std::false_type, std::false_type) {
0054       return b.index();
0055     }
0056 
0057     /// @brief Return bin edge for integral discrete axes
0058     template<size_t axisN, typename BinT>
0059     double coordPicker(const BinT& b, std::true_type, std::false_type) {
0060       return b.template edge<axisN>();
0061     }
0062     /// @brief Return bin mid point for continuous axes
0063     template<size_t axisN, typename BinT>
0064     double coordPicker(const BinT& b, std::true_type, std::true_type) {
0065       return b.template mid<axisN>();
0066     }
0067   }
0068 
0069 
0070   /// Forward declaration
0071   template <typename... AxisT>
0072   class BinnedEstimate;
0073 
0074   /// @brief EstimateStorage convenience class based on BinnedStorage.
0075   ///
0076   /// @note We use this abstraction layer to implement the bulk once and only once.
0077   /// The user-facing BinnedEstimate type will inherit all its methods from this
0078   /// base class along with a few axis-specifc mixins.
0079   template<typename... AxisT>
0080   class EstimateStorage
0081       : public BinnedStorage<Estimate, AxisT...>,
0082         public AnalysisObject {
0083   public:
0084 
0085     using BaseT = BinnedStorage<Estimate, AxisT...>;
0086     using BinningT = typename BaseT::BinningT;
0087     using BinT = typename BaseT::BinT;
0088     using BinType = BinT;
0089     using AnalysisObject::operator =;
0090 
0091     /// @name Constructors
0092     /// @{
0093 
0094     /// @brief Nullary constructor for unique pointers etc.
0095     EstimateStorage(const std::string& path = "", const std::string& title = "")
0096         : BaseT(), AnalysisObject(mkTypeString<AxisT...>(), path, title) { }
0097 
0098     /// @brief Constructor giving explicit bin edges as rvalue reference.
0099     ///
0100     /// Discrete axes have as many edges as bins.
0101     /// Continuous axes have bins.size()+1 edges, the last one
0102     /// being the upper bound of the last bin.
0103     EstimateStorage(std::vector<AxisT>&&... binsEdges, const std::string& path = "", const std::string& title = "")
0104         : BaseT(Axis<AxisT>(std::move(binsEdges))...), AnalysisObject(mkTypeString<AxisT...>(), path, title) { }
0105 
0106     /// @brief Constructor giving explicit bin edges as lvalue reference.
0107     ///
0108     /// Discrete axes have as many edges as bins.
0109     /// Continuous axes have bins.size()+1 edges, the last one
0110     /// being the upper bound of the last bin.
0111     EstimateStorage(const std::vector<AxisT>&... binsEdges, const std::string& path = "", const std::string& title = "")
0112         : BaseT(Axis<AxisT>(binsEdges)...), AnalysisObject(mkTypeString<AxisT...>(), path, title) { }
0113 
0114     /// @brief Constructor giving explicit bin edges as initializer list.
0115     ///
0116     /// Discrete axes have as many edges as bins.
0117     /// Continuous axes have bins.size()+1 edges, the last one
0118     /// being the upper bound of the last bin.
0119     EstimateStorage(std::initializer_list<AxisT>&&... binsEdges, const std::string& path = "", const std::string& title = "")
0120         : BaseT(Axis<AxisT>(std::move(binsEdges))...), AnalysisObject(mkTypeString<AxisT...>(), path, title) { }
0121 
0122     /// @brief Constructor giving range and number of bins.
0123     template <typename EdgeT = double, typename = enable_if_all_CAxisT<EdgeT, AxisT...>>
0124     EstimateStorage(const std::vector<size_t>& nBins,
0125                     const std::vector<std::pair<EdgeT, EdgeT>>& limitsLowUp,
0126                     const std::string& path = "", const std::string& title = "")
0127         //: BaseT( BinningT(nBins, limitsLowUp, std::make_index_sequence<sizeof...(AxisT)>{}) ),
0128         : BaseT( _mkBinning(nBins, limitsLowUp, std::make_index_sequence<sizeof...(AxisT)>{}) ),
0129           AnalysisObject(mkTypeString<AxisT...>(), path, title) { }
0130 
0131     /// @brief Constructor given a binning type
0132     EstimateStorage(const BinningT& binning, const std::string& path = "", const std::string& title = "")
0133         : BaseT(binning), AnalysisObject(mkTypeString<AxisT...>(), path, title) { }
0134 
0135     /// @brief Constructor given an rvalue BinningT
0136     EstimateStorage(BinningT&& binning, const std::string& path = "", const std::string& title = "")
0137         : BaseT(std::move(binning)), AnalysisObject(mkTypeString<AxisT...>(), path, title) { }
0138 
0139     /// @brief Constructor given a scatter
0140     template <typename EdgeT = double, typename = enable_if_all_CAxisT<EdgeT, AxisT...>>
0141     EstimateStorage(const ScatterND<sizeof...(AxisT)+1>& s, const std::string& path = "", const std::string& title = "")
0142          : BaseT(_mkBinning(s, std::make_index_sequence<sizeof...(AxisT)>{})),
0143            AnalysisObject(mkTypeString<AxisT...>(), path, title) { }
0144 
0145     /// @brief Copy constructor
0146     ///
0147     /// @todo Also allow title setting from the constructor?
0148     EstimateStorage(const EstimateStorage& other, const std::string& path = "") : BaseT(other),
0149         AnalysisObject(mkTypeString<AxisT...>(), path!=""? path : other.path(), other, other.title()) { }
0150 
0151     /// @brief Move constructor
0152     ///
0153     /// @todo Also allow title setting from the constructor?
0154     EstimateStorage(EstimateStorage&& other, const std::string& path = "") : BaseT(std::move(other)),
0155         AnalysisObject(mkTypeString<AxisT...>(), path!=""? path : other.path(), other, other.title()) { }
0156 
0157     /// @brief Make a copy on the stack
0158     EstimateStorage clone() const noexcept {
0159       return EstimateStorage(*this);
0160     }
0161 
0162     /// @brief Make a copy on the heap
0163     EstimateStorage* newclone() const noexcept {
0164       return new EstimateStorage(*this);
0165     }
0166 
0167     /// @}
0168 
0169 
0170     /// @name I/O
0171     /// @{
0172 
0173   private:
0174 
0175     /// @brief Render information about this AO (private implementation)
0176     template<size_t... Is>
0177     void _renderYODA_aux(std::ostream& os, const int width, std::index_sequence<Is...>) const noexcept {
0178 
0179       // print bin edges
0180       BaseT::_binning._renderYODA(os);
0181 
0182       // Assemble union of error sources, as it's not guaranteed
0183       // that every bin has the same error breakdown
0184       const std::vector<std::string> labels = this->sources();
0185       if (labels.size()) {
0186         os << "ErrorLabels: [";
0187         for (size_t i = 0; i < labels.size(); ++i) {
0188           const std::string& src = labels[i];
0189           if (i)  os << ", ";
0190           os << std::quoted(src);
0191         }
0192         os << "]\n";
0193       }
0194 
0195       // column header: content types
0196       os << std::setw(width) << std::left << "# value" << "\t";
0197       const int errwidth = std::max(int(std::to_string(labels.size()).size()+7), width); // "errDn(" + src + ")"
0198       for (size_t i = 0; i < labels.size(); ++i) {
0199         const std::string& src = labels[i];
0200         if (src.empty()) {
0201           os << std::setw(errwidth) << std::left << "totalDn" << "\t"
0202              << std::setw(errwidth) << std::left << "totalUp" << "\t";
0203         }
0204         else {
0205           os << std::setw(errwidth) << std::left << ("errDn(" + std::to_string(i+1) + ")") << "\t"
0206              << std::setw(errwidth) << std::left << ("errUp(" + std::to_string(i+1) + ")") << "\t";
0207         }
0208       }
0209       os << "\n";
0210 
0211       for (const auto& b : BaseT::bins(true, true)) {
0212         os << std::setw(width) << std::left << b.val() << "\t"; // print value
0213         // print systs if available
0214         for (const std::string& src : labels) {
0215           if (!b.hasSource(src)) {
0216             os << std::setw(errwidth) << std::left << "---" << "\t"
0217                << std::setw(errwidth) << std::left << "---" << "\t";
0218             continue;
0219           }
0220           const auto& err = b.err(src);
0221           os << std::setw(errwidth) << std::left << err.first << "\t"
0222              << std::setw(errwidth) << std::left << err.second << "\t";
0223         }
0224         os << "\n";
0225       }
0226     }
0227 
0228   public:
0229 
0230     /// @brief Render information about this AO
0231     void _renderYODA(std::ostream& os, const int width = 13) const noexcept {
0232       _renderYODA_aux(os, width, std::make_index_sequence<sizeof...(AxisT)>{});
0233     }
0234 
0235     /// @brief Render scatter-like information about this AO
0236     void _renderFLAT(std::ostream& os, const int width = 13) const noexcept {
0237       const ScatterND<sizeof...(AxisT)+1> tmp = mkScatter();
0238       tmp._renderYODA(os, width);
0239     }
0240 
0241     #ifdef HAVE_HDF5
0242 
0243     /// @brief Extract error labels of this AO
0244     void _extractLabels(std::vector<std::string>& labels,
0245                         std::vector<size_t>& labelsizes) const noexcept {
0246       size_t nlabels = 0;
0247       for (const auto& b : BaseT::bins(true, true)) {
0248         std::vector<std::string> keys = b.sources();
0249         nlabels += keys.size()+1; //< +1 for length info itself
0250         labels.insert(std::end(labels),
0251                       std::make_move_iterator(std::begin(keys)),
0252                       std::make_move_iterator(std::end(keys)));
0253       }
0254       std::sort(labels.begin(), labels.end());
0255       labels.erase( std::unique(labels.begin(), labels.end()), labels.end() );
0256       labelsizes.emplace_back(std::move(nlabels));
0257     }
0258 
0259     /// @brief Extract axes edges of this AO into map of @a edges
0260     void _extractEdges(std::map<std::string, EdgeHandlerBasePtr>& edges,
0261                        const std::vector<std::string>& labels) const noexcept {
0262 
0263       using lenT = EdgeHandler<size_t>;
0264       using lenPtr = EdgeHandlerPtr<size_t>;
0265       const std::string lengthID("sizeinfo");
0266       lenPtr nedges = std::static_pointer_cast<lenT>(edges.find(lengthID)->second);
0267 
0268       auto extractEdges = [&edges, &binning = BaseT::_binning, &nedges](auto I) {
0269 
0270         using thisEdgeT = typename BinningT::template getEdgeT<I>;
0271         using thisHandlerT = EdgeHandler<thisEdgeT>;
0272         using thisHandlerPtr = EdgeHandlerPtr<thisEdgeT>;
0273 
0274         const std::string edgeID = std::string("edges_") + TypeID<thisEdgeT>::name();
0275         std::vector<thisEdgeT> tmp = binning.template edges<I>();
0276         nedges->extend({ tmp.size() });
0277 
0278         auto itr = edges.find(edgeID);
0279         if (itr != edges.cend()) {
0280           thisHandlerPtr edgeset = std::static_pointer_cast<thisHandlerT>(itr->second);
0281           edgeset->extend(std::move(tmp));
0282         }
0283         else {
0284           edges[edgeID] = std::make_shared<thisHandlerT>(std::move(tmp));
0285         }
0286       };
0287       MetaUtils::staticFor<sizeof...(AxisT)>(extractEdges);
0288 
0289       std::vector<size_t> masks = BaseT::_binning.maskedBins();
0290       nedges->extend({ masks.size() });
0291       nedges->extend(std::move(masks));
0292 
0293       const auto& itr = labels.cbegin();
0294       const auto& itrEnd = labels.cend();
0295       for (const auto& b : BaseT::bins(true, true)) {
0296         vector<string> keys = b.sources();
0297         nedges->extend({ keys.size() });
0298         for (const string& k : keys) {
0299           size_t dist = std::find(itr, itrEnd, k) - itr;
0300           nedges->extend({ dist });
0301         }
0302       }
0303     }
0304 
0305     #endif
0306 
0307     /// @}
0308 
0309 
0310     /// @name Transformations
0311     /// @{
0312 
0313     /// @brief Rescale as if all fill weights had been different by factor @a scalefactor.
0314     void scale(const double scalefactor) noexcept {
0315       setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
0316       for (auto& bin : BaseT::_bins) {
0317         bin.scale(scalefactor);
0318       }
0319     }
0320 
0321 
0322     /// @brief Merge every group of @a n bins, from start to end inclusive
0323     ///
0324     /// If the number of bins is not a multiple of @a n, the last @a m < @a n
0325     /// bins on the RHS will also be merged, as the closest possible approach to
0326     /// factor @n rebinning everywhere.
0327     ///
0328     /// @note Only visible bins are being rebinned. Underflow (index = 0) and
0329     /// overflow (index = numBins() + 1) are not included.
0330     ///
0331     /// @note Only supported for continuous axes
0332     template <size_t axisN, typename = std::enable_if_t<BinningT::template is_CAxis<axisN>::value>>
0333     void rebinBy(unsigned int n, size_t begin=1, size_t end=UINT_MAX) {
0334       if (n < 1)  throw UserError("Rebinning requested in groups of 0!");
0335       if (!begin) throw UserError("Visible bins start with index 1!");
0336       if (end > BaseT::numBinsAt(axisN)+1)  end = BaseT::numBinsAt(axisN) + 1;
0337       for (size_t m = begin; m < end; ++m) {
0338         if (m > BaseT::numBinsAt(axisN)+1) break; // nothing to be done
0339         const size_t myend = (m+n-1 < BaseT::numBinsAt(axisN)+1) ? m+n-1 : BaseT::numBinsAt(axisN);
0340         if (myend > m) {
0341           BaseT::template mergeBins<axisN>({m, myend});
0342           end -= myend-m; //< reduce upper index by the number of removed bins
0343         }
0344       }
0345     }
0346 
0347     /// @brief Overloaded alias for rebinBy
0348     ///
0349     /// @note Only supported for continuous axes
0350     template <size_t axisN, typename = std::enable_if_t<BinningT::template is_CAxis<axisN>::value>>
0351     void rebin(unsigned int n, size_t begin=1, size_t end=UINT_MAX) {
0352       rebinBy<axisN>(n, begin, end);
0353     }
0354 
0355     /// @brief Rebin to the given list of bin edges
0356     ///
0357     /// @note Only supported for continuous axes
0358     template <size_t axisN, typename = std::enable_if_t<BinningT::template is_CAxis<axisN>::value>>
0359     void rebinTo(const std::vector<typename BinningT::template getAxisT<axisN>::EdgeT>& newedges) {
0360       if (newedges.size() < 2)
0361         throw UserError("Requested rebinning to an edge list which defines no bins");
0362       using thisAxisT = typename BinningT::template getAxisT<axisN>;
0363       using thisEdgeT = typename thisAxisT::EdgeT;
0364       // get list of shared edges
0365       thisAxisT& oldAxis = BaseT::_binning.template axis<axisN>();
0366       const thisAxisT newAxis(std::move(newedges));
0367       const std::vector<thisEdgeT> eshared = oldAxis.sharedEdges(newAxis);
0368       if (eshared.size() != newAxis.edges().size())
0369         throw BinningError("Requested rebinning to incompatible edges");
0370       // loop over new lower bin edges (= first bin index of merge range)
0371       for (size_t begin = 0; begin < eshared.size() - 1; ++begin) {
0372         // find index of upper edge along old axis
0373         // (subtracting 1 gives index of last bin to be merged)
0374         size_t end = oldAxis.index(eshared[begin+1]) - 1;
0375         // if the current edge is the last visible edge before the overflow
0376         // merge the remaining bins into the overflow
0377         if (begin == newAxis.numBins(true)-1)  end = oldAxis.numBins(true)-1;
0378         // merge this range
0379         if (end > begin)  BaseT::template mergeBins<axisN>({begin, end});
0380         if (eshared.size() == oldAxis.edges().size())  break; // we're done
0381       }
0382     }
0383 
0384     /// @brief Overloaded alias for rebinTo
0385     ///
0386     /// @note Only supported for continuous axes
0387     template <size_t axisN, typename = std::enable_if_t<BinningT::template is_CAxis<axisN>::value>>
0388     void rebin(const std::vector<typename BinningT::template getAxisT<axisN>::EdgeT>& newedges) {
0389       rebinTo<axisN>(std::move(newedges));
0390     }
0391 
0392     /// @brief Reset the EstimateStorage
0393     ///
0394     /// Keep the binning but set all bin contents and related quantities to zero
0395     void reset() noexcept { BaseT::clearBins(); }
0396 
0397     /// @brief Copy assignment
0398     EstimateStorage& operator = (const EstimateStorage& est) noexcept {
0399       if (this != &est) {
0400         AnalysisObject::operator = (est);
0401         BaseT::operator = (est);
0402       }
0403       return *this;
0404     }
0405 
0406     /// Move assignment
0407     EstimateStorage& operator = (EstimateStorage&& est) noexcept {
0408       if (this != &est) {
0409         AnalysisObject::operator = (est);
0410         BaseT::operator = (std::move(est));
0411       }
0412       return *this;
0413     }
0414 
0415     /// @brief Add two EstimateStorages
0416     ///
0417     /// @note Adding EstimateStorages will unset any ScaledBy
0418     /// attribute from previous calls to scale or normalize.
0419     EstimateStorage& add(const EstimateStorage& est,
0420                          const std::string& pat_uncorr="^stat|^uncor" ) {
0421       if (*this != est)
0422         throw BinningError("Arithmetic operation requires compatible binning!");
0423       if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
0424       for (size_t i = 0; i< BaseT::numBins(true, true); ++i) {
0425         BaseT::bin(i).add(est.bin(i), pat_uncorr);
0426       }
0427       BaseT::maskBins(est.maskedBins(), true);
0428       return *this;
0429     }
0430     //
0431     EstimateStorage& operator += (const EstimateStorage& est) {
0432       return add(est);
0433     }
0434 
0435     /// @brief Add two (rvalue) EstimateStorages
0436     ///
0437     /// @note Adding EstimateStorages will unset any ScaledBy
0438     /// attribute from previous calls to scale or normalize.
0439     EstimateStorage& add(EstimateStorage&& est,
0440                          const std::string& pat_uncorr="^stat|^uncor" ) {
0441       if (*this != est)
0442         throw BinningError("Arithmetic operation requires compatible binning!");
0443       if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
0444       for (size_t i = 0; i< BaseT::numBins(true, true); ++i) {
0445         BaseT::bin(i).add(std::move(est.bin(i)), pat_uncorr);
0446       }
0447       BaseT::maskBins(est.maskedBins(), true);
0448       return *this;
0449     }
0450     //
0451     EstimateStorage& operator += (EstimateStorage&& est) {
0452       return add(std::move(est));
0453     }
0454 
0455 
0456     /// @brief Subtract one EstimateStorages from another one
0457     ///
0458     /// @note Subtracting EstimateStorages will unset any ScaledBy
0459     /// attribute from previous calls to scale or normalize.
0460     EstimateStorage& subtract(const EstimateStorage& est,
0461                               const std::string& pat_uncorr="^stat|^uncor" ) {
0462       if (*this != est)
0463         throw BinningError("Arithmetic operation requires compatible binning!");
0464       if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
0465       for (size_t i = 0; i< BaseT::numBins(true, true); ++i) {
0466         BaseT::bin(i).subtract(est.bin(i), pat_uncorr);
0467       }
0468       BaseT::maskBins(est.maskedBins(), true);
0469       return *this;
0470     }
0471     //
0472     EstimateStorage& operator -= (const EstimateStorage& est) {
0473       return subtract(est);
0474     }
0475 
0476     /// @brief Subtract one (rvalue) EstimateStorages from another one
0477     EstimateStorage& subtract(EstimateStorage&& est,
0478                               const std::string& pat_uncorr="^stat|^uncor" ) {
0479       if (*this != est)
0480         throw BinningError("Arithmetic operation requires compatible binning!");
0481       if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
0482       for (size_t i = 0; i< BaseT::numBins(true, true); ++i) {
0483         BaseT::bin(i) -= std::move(est.bin(i), pat_uncorr);
0484       }
0485       BaseT::maskBins(est.maskedBins(), true);
0486       return *this;
0487     }
0488     //
0489     EstimateStorage& operator -= (EstimateStorage&& est) {
0490       return subtract(std::move(est));
0491     }
0492 
0493     /// @}
0494 
0495 
0496     /// @name Binning info.
0497     /// @{
0498 
0499     /// @brief Total dimension of the object (number of axes + estimate)
0500     size_t dim() const noexcept { return sizeof...(AxisT) + 1; }
0501 
0502     /// @brief Returns the axis configuration
0503     std::string _config() const noexcept { return mkAxisConfig<AxisT...>(); }
0504 
0505     /// @}
0506 
0507 
0508     /// @name Whole EstimateStorage data
0509     /// @{
0510 
0511     /// @brief Get list of central values
0512     std::vector<double> vals(const bool includeOverflows=false,
0513                              const bool includeMaskedBins=false) const {
0514       std::vector<double> rtn;
0515       rtn.reserve(BaseT::numBins(includeOverflows, includeMaskedBins));
0516       for (const auto& b : BaseT::bins(includeOverflows, includeMaskedBins)) {
0517         rtn.push_back(b.val());
0518       }
0519       return rtn;
0520     }
0521 
0522     /// @brief Get list of error sources
0523     std::vector<std::string> sources() const {
0524       // Assemble union of error sources, as it's not guaranteed
0525       // that every bin has the same error breakdown
0526       std::vector<std::string> rtn;
0527 
0528       for (const auto& b : BaseT::bins(true)) {
0529         std::vector<std::string> keys = b.sources();
0530         rtn.insert(std::end(rtn),
0531                    std::make_move_iterator(std::begin(keys)),
0532                    std::make_move_iterator(std::end(keys)));
0533       }
0534       std::sort(rtn.begin(), rtn.end());
0535       rtn.erase( std::unique(rtn.begin(), rtn.end()), rtn.end() );
0536 
0537       return rtn;
0538     }
0539 
0540     /// @brief Calculate the volume underneath the EstimateStorage
0541     ///
0542     /// @note overflow bins have infinite bin width
0543     double areaUnderCurve(const bool includeBinVol=true,
0544                           const bool includeOverflows=false,
0545                           const bool includeMaskedBins=false) const {
0546       double ret = 0.;
0547       for (const auto& b : BaseT::bins(includeOverflows, includeMaskedBins)) {
0548         const double val = fabs(b.val());
0549         const double vol = includeBinVol? b.dVol() : 1.0;
0550         if (std::isfinite(vol))  ret += val*vol; // aggregate bin volume
0551       }
0552       return ret;
0553     }
0554 
0555     /// @brief Convenient alias for areaUnderCurve()
0556     double auc(const bool includeBinVol=true,
0557                const bool includeOverflows=false,
0558                const bool includeMaskedBins=false) const {
0559       return areaUnderCurve(includeBinVol, includeOverflows, includeMaskedBins);
0560     }
0561 
0562 
0563 
0564     /// @brief Construct a covariance matrix from the error breakdown
0565     std::vector<std::vector<double>> covarianceMatrix(const bool ignoreOffDiagonalTerms=false,
0566                                                       const bool includeOverflows=false,
0567                                                       const bool includeMaskedBins=false,
0568                                                       const std::string& pat_uncorr="^stat|^uncor") const {
0569       const size_t nBins = BaseT::numBins(includeOverflows,includeMaskedBins);
0570       std::vector<std::vector<double> > covM(nBins);
0571 
0572       // initialise cov matrix to be the right shape
0573       for (size_t i = 0; i < nBins; ++i) {
0574         covM[i] = std::vector<double>(nBins, 0.0);
0575       }
0576 
0577       const std::vector<std::string> error_sources = sources();
0578 
0579       // nominal-only case, i.e. total uncertainty, labelled as empty string
0580       if (error_sources.size() == 1 && error_sources[0] == "") {
0581         size_t i = 0;
0582         for (const auto& b : BaseT::bins(includeOverflows,includeMaskedBins)) {
0583           covM[i][i] = b.hasSource("")? sqr(0.5*(b.err("").first+b.err("").second)) : 1.0;
0584           ++i;
0585         }
0586         return covM;
0587       }
0588 
0589       // more interesting case where we actually have some uncertainty breakdown!
0590       std::smatch match;
0591       const std::regex re(pat_uncorr, std::regex_constants::icase);
0592       for (const std::string& sname : error_sources) {
0593         if (sname == "")  continue;
0594         std::vector<double> systErrs(nBins, 0.0);
0595         size_t k = 0;
0596         for (const auto& b : BaseT::bins(includeOverflows,includeMaskedBins)) {
0597           if (b.hasSource(sname)) {
0598             const auto& errs = b.err(sname); // dn-up pair
0599             systErrs[k] = 0.5 *( fabs(errs.first)+fabs(errs.second));
0600           }
0601           ++k;
0602         }
0603         const bool skipOffDiag = (ignoreOffDiagonalTerms
0604                                   || std::regex_search(sname, match, re));
0605         for (size_t i = 0; i < nBins; ++i) {
0606           for (size_t j = 0; j < nBins; ++j) {
0607             if (skipOffDiag && i != j)  continue;
0608             covM[i][j] += systErrs[i]*systErrs[j];
0609           }
0610         }
0611       }
0612 
0613       return covM;
0614     }
0615 
0616     /// @brief Alias for covarianceMatrix
0617     std::vector<std::vector<double>> covMat(const bool ignoreOffDiagonalTerms=false,
0618                                             const bool includeOverflows=false,
0619                                             const bool includeMaskedBins=false,
0620                                             const std::string& pat_uncorr="^stat|^uncor") const {
0621       return covarianceMatrix(ignoreOffDiagonalTerms, includeOverflows, includeMaskedBins, pat_uncorr);
0622     }
0623 
0624     /// @}
0625 
0626     /// @name Type reductions
0627     /// @{
0628 
0629     /// @brief Produce a ScatterND from a EstimateStorage
0630     auto mkScatter(const std::string& path="", const std::string& pat_match = "",
0631                                                const bool includeOverflows=false,
0632                                                const bool includeMaskedBins=false) const {
0633 
0634       constexpr size_t N = sizeof...(AxisT);
0635 
0636       ScatterND<N+1> rtn;
0637       for (const std::string& a : annotations()) {
0638         if (a != "Type")  rtn.setAnnotation(a, annotation(a));
0639       }
0640       rtn.setAnnotation("Path", path);
0641 
0642       const bool incOF = all_CAxes<AxisT...>::value && includeOverflows;
0643       for (const auto& b : BaseT::bins(incOF, includeMaskedBins)) {
0644 
0645         // Create the vector of coordinates
0646         Utils::ndarray<double, N+1> vals;
0647         // first fill bin centres, use bin index if axis non-arithmetic
0648         auto indexIfDiscrete = [&vals, &b](auto I) {
0649           using isContinuous = typename BinningT::template is_CAxis<I>;
0650           using isArithmetic = typename BinningT::template is_Arithmetic<I>;
0651           vals[I] = coordPicker<I>(b, std::integral_constant<bool, isArithmetic::value>{},
0652                                       std::integral_constant<bool, isContinuous::value>{});
0653         };
0654         MetaUtils::staticFor<BinningT::Dimension::value>(indexIfDiscrete);
0655         // then fill bin content
0656         vals[N] = b.val();
0657 
0658         // Create the vector of error pairs, use 0 if axis not continuous
0659         Utils::ndarray<std::pair<double,double>, N+1> errs;
0660         auto nullifyDiscrete = [&errs, &vals, &b](auto I) {
0661           using isContinuous = typename BinningT::template is_CAxis<I>;
0662           errs[I] = nullifyIfDisc<I>(b, vals[I], std::integral_constant<bool, isContinuous::value>{});
0663         };
0664         MetaUtils::staticFor<BinningT::Dimension::value>(nullifyDiscrete);
0665         const std::pair<double,double> tot = b.totalErr(pat_match);
0666         errs[N] = {fabs(tot.first), tot.second};
0667 
0668         // Add the PointND
0669         rtn.addPoint( PointND<N+1>(vals, errs) );
0670       }
0671 
0672       // Decorate output scatter with the discrete edges
0673       const BinningT& binning = BaseT::_binning;
0674       auto decorateEdges = [&rtn, &binning](auto I) {
0675         using isContinuous = typename BinningT::template is_CAxis<I>;
0676         if constexpr( !isContinuous::value ) {
0677           const auto& axis  = binning.template axis<I>();
0678           if (axis.numBins()) {
0679             std::stringstream ss;
0680             axis._renderYODA(ss);
0681             rtn.setAnnotation("EdgesA" + std::to_string(I+1), ss.str());
0682           }
0683         }
0684       };
0685       MetaUtils::staticFor<BinningT::Dimension::value>(decorateEdges);
0686 
0687       return rtn;
0688     }
0689 
0690     /// @brief Split into vector of BinnedEstimates along @a axisN
0691     ///
0692     /// The binning dimension of the returned objects are reduced by one unit.
0693     /// @note Requires at least two binning dimensions.
0694     template<size_t axisN, typename = std::enable_if_t< (axisN < sizeof...(AxisT) && sizeof...(AxisT)>=2) >>
0695     auto mkEstimates(const std::string& path="", const bool includeOverflows=false) const {
0696 
0697       // Need to provide a prescription for how to add the two bin contents
0698       auto how2add = [](auto& pivot, const BinType& toCopy) { pivot = toCopy; };
0699       auto rtn = BaseT::template mkBinnedSlices<axisN, BinnedEstimate>(how2add, includeOverflows);
0700       for (const std::string& a : annotations()) {
0701         if (a == "Type")  continue;
0702         for (size_t i = 0; i < rtn.size(); ++i) {
0703           rtn[i].setAnnotation(a, annotation(a));
0704         }
0705       }
0706       for (size_t i = 0; i < rtn.size(); ++i) {
0707         rtn[i].setAnnotation("Path", path);
0708       }
0709 
0710       return rtn;
0711     }
0712 
0713 
0714     /// @brief Method returns clone of the estimate with streamlined error source
0715     AnalysisObject* mkInert(const std::string& path = "",
0716                             const std::string& source = "") const noexcept {
0717       EstimateStorage* rtn = newclone();
0718       rtn->setPath(path);
0719       for (auto& b : rtn->bins(true, true)) {
0720         if (b.numErrs() == 1) {
0721           try {
0722             b.renameSource("", source);
0723           }
0724           catch (YODA::UserError& e) { }
0725         }
0726       }
0727       return rtn;
0728     }
0729 
0730     /// @}
0731 
0732     /// @name MPI (de-)serialisation
0733     /// @{
0734 
0735     size_t lengthContent(bool fixed_length = false) const noexcept {
0736       size_t rtn = 0;
0737       for (const auto& bin : BaseT::bins(true, true)) {
0738         rtn += bin._lengthContent(fixed_length);
0739       }
0740       return rtn;
0741     }
0742 
0743     std::vector<double> serializeContent(bool fixed_length = false) const noexcept {
0744       std::vector<double> rtn;
0745       const size_t nBins = BaseT::numBins(true, true);
0746       rtn.reserve(nBins * 4);
0747       for (size_t i = 0; i < nBins; ++i) {
0748         const auto& b = BaseT::bin(i);
0749         std::vector<double> bdata = b._serializeContent(fixed_length);
0750         rtn.insert(std::end(rtn),
0751                    std::make_move_iterator(std::begin(bdata)),
0752                    std::make_move_iterator(std::end(bdata)));
0753       }
0754       return rtn;
0755     }
0756 
0757 
0758     void deserializeContent(const std::vector<double>& data) {
0759 
0760       const size_t nBins = BaseT::numBins(true, true);
0761       const size_t minLen = 2*nBins;
0762       if (data.size() < minLen)
0763         throw UserError("Length of serialized data should be at least " + std::to_string(minLen)+"!");
0764 
0765       size_t i = 0;
0766       auto itr = data.cbegin();
0767       const auto itrEnd = data.cend();
0768       const bool fixedLen = data.size() == 2*minLen;
0769       while (itr != itrEnd) {
0770         // for estimates, the first element represents the central,
0771         // the subsequent value represents the number of error pairs
0772         const size_t nErrs = fixedLen? 1 : (*(itr + 1) + 0.5); // add 0.5 to avoid rounding issues
0773         auto last = itr + 2*(nErrs+1); // last element + 1
0774         BaseT::bin(i)._deserializeContent(std::vector<double>{itr, last}, fixedLen);
0775         // update for next iteration
0776         itr = last;
0777         ++i;
0778       }
0779     }
0780 
0781     /// @}
0782 
0783   private:
0784 
0785     /// @brief Helper function to create a BinningT from
0786     /// a given set @a nBins within a range @a limitsLowUp
0787     template<size_t... Is>
0788     BinningT _mkBinning(const std::vector<size_t>& nBins,
0789                         const std::vector<std::pair<double, double>>& limitsLowUp,
0790                         std::index_sequence<Is...>) const {
0791       return BinningT({((void)Is, Axis<AxisT>(nBins[Is], limitsLowUp[Is].first, limitsLowUp[Is].second))...});
0792     }
0793 
0794     /// @brief Helper function to create a BinningT from a scatter @a s
0795     template<size_t... Is>
0796     BinningT _mkBinning(const ScatterND<sizeof...(AxisT)+1>& s, std::index_sequence<Is...>) const {
0797       return BinningT({((void)Is, Axis<AxisT>(s.edges(Is)))...});
0798     }
0799 
0800   };
0801 
0802 
0803 
0804   /// @brief User-facing BinnedEstimate class in arbitrary dimensions
0805   template <typename... AxisT>
0806   class BinnedEstimate : public EstimateStorage<AxisT...> {
0807   public:
0808     using EstimateT = BinnedEstimate<AxisT...>;
0809     using BaseT = EstimateStorage<AxisT...>;
0810     using BinType = typename BaseT::BinT;
0811     using Ptr = std::shared_ptr<EstimateT>;
0812 
0813     /// @brief Inherit constructors.
0814     using BaseT::BaseT;
0815 
0816     BinnedEstimate(const EstimateT&) = default;
0817     BinnedEstimate(EstimateT&&) = default;
0818     BinnedEstimate& operator =(const EstimateT&) = default;
0819     BinnedEstimate& operator =(EstimateT&&) = default;
0820     using AnalysisObject::operator =;
0821 
0822     /// @brief Copy constructor (needed for clone functions).
0823     ///
0824     /// @note Compiler won't generate this constructor automatically.
0825     BinnedEstimate(const BaseT& other) : BaseT(other) {}
0826     //
0827     BinnedEstimate(const EstimateT& other, const std::string& path) : BaseT(other, path) {}
0828 
0829     /// @brief Move constructor.
0830     BinnedEstimate(BaseT&& other) : BaseT(std::move(other)) {}
0831     //
0832     BinnedEstimate(EstimateT&& other, const std::string& path) : BaseT(std::move(other), path) {}
0833 
0834   };
0835 
0836 
0837   /// @brief Specialisation of the BinnedEstimate for a 1D histogram
0838   template <typename AxisT>
0839   class BinnedEstimate<AxisT>
0840       : public EstimateStorage<AxisT>,
0841         public XAxisMixin<BinnedEstimate<AxisT>, AxisT> {
0842   public:
0843     using EstimateT = BinnedEstimate<AxisT>;
0844     using BaseT = EstimateStorage<AxisT>;
0845     using BinType = typename BaseT::BinT;
0846     using Ptr = std::shared_ptr<EstimateT>;
0847 
0848     /// @brief Inherit constructors.
0849     using BaseT::BaseT;
0850 
0851     BinnedEstimate(const EstimateT&) = default;
0852     BinnedEstimate(EstimateT&&) = default;
0853     BinnedEstimate& operator =(const EstimateT&) = default;
0854     BinnedEstimate& operator =(EstimateT&&) = default;
0855     using AnalysisObject::operator =;
0856 
0857     /// @brief Copy constructor (needed for clone functions).
0858     ///
0859     /// @note Compiler won't generate this constructor automatically.
0860     BinnedEstimate(const BaseT& other) : BaseT(other) {}
0861     //
0862     BinnedEstimate(const EstimateT& other, const std::string& path) : BaseT(other, path) {}
0863 
0864     /// @brief Move constructor.
0865     BinnedEstimate(BaseT&& other) : BaseT(std::move(other)) {}
0866     //
0867     BinnedEstimate(EstimateT&& other, const std::string& path) : BaseT(std::move(other), path) {}
0868 
0869 
0870     BinnedEstimate(std::vector<AxisT>&& edges, const std::string& path="", const std::string& title="")
0871               : BaseT(std::move(edges), path, title) {}
0872 
0873     BinnedEstimate(const std::vector<AxisT>& edges, const std::string& path="", const std::string& title="")
0874               : BaseT(edges, path, title) {}
0875 
0876     /// @brief Constructor with auto-setup of evenly spaced axes.
0877     ///
0878     /// The constructor argument uses double rather than EdgeT to
0879     /// allow for auto-conversion of int to double.
0880     ///
0881     /// @note This constructor is only supported when all axes are continuous.
0882     template <typename EdgeT = double, typename = enable_if_all_CAxisT<EdgeT, AxisT>>
0883     BinnedEstimate(size_t nbins, double lower, double upper, const std::string& path = "", const std::string& title = "")
0884               : BaseT({nbins}, {{lower, upper}}, path, title) {}
0885 
0886     /// @brief Make a copy on the stack
0887     EstimateT clone() const noexcept {
0888       return EstimateT(*this);
0889     }
0890 
0891     /// @brief Make a copy on the heap
0892     EstimateT* newclone() const noexcept {
0893       return new EstimateT(*this);
0894     }
0895 
0896     /// @brief Find bin index for given coordinates
0897     size_t indexAt(const AxisT xCoord) const noexcept {
0898       return BaseT::binAt( {xCoord} ).index();
0899     }
0900 
0901     /// @brief Mask/Unmask bin at given set of coordinates
0902     void maskBinAt(const AxisT xCoord, const bool status = true) noexcept {
0903       return BaseT::maskBin({xCoord}, status);
0904     }
0905 
0906   };
0907 
0908 
0909 
0910   /// @brief Specialisation of the BinnedEstimate for a 2D BinnedEstimate
0911   template <typename AxisT1, typename AxisT2>
0912   class BinnedEstimate<AxisT1, AxisT2>
0913       : public EstimateStorage<AxisT1, AxisT2>,
0914         public XAxisMixin<BinnedEstimate<AxisT1, AxisT2>, AxisT1>,
0915         public YAxisMixin<BinnedEstimate<AxisT1, AxisT2>, AxisT2> {
0916   public:
0917     using EstimateT = BinnedEstimate<AxisT1, AxisT2>;
0918     using BaseT = EstimateStorage<AxisT1, AxisT2>;
0919     using BinType = typename BaseT::BinT;
0920     using Ptr = std::shared_ptr<EstimateT>;
0921 
0922     /// @brief Inherit constructors.
0923     using BaseT::BaseT;
0924 
0925     BinnedEstimate(const EstimateT&) = default;
0926     BinnedEstimate(EstimateT&&) = default;
0927     BinnedEstimate& operator =(const EstimateT&) = default;
0928     BinnedEstimate& operator =(EstimateT&&) = default;
0929     using AnalysisObject::operator =;
0930 
0931     /// @brief Copy constructor (needed for clone functions).
0932     ///
0933     /// @note Compiler won't generate this constructor automatically.
0934     BinnedEstimate(const BaseT& other) : BaseT(std::move(other)) {}
0935     //
0936     BinnedEstimate(const EstimateT& other, const std::string& path) : BaseT(other, path) {}
0937 
0938     /// @brief Move constructor.
0939     BinnedEstimate(BaseT&& other) : BaseT(std::move(other)) {}
0940     //
0941     BinnedEstimate(EstimateT&& other, const std::string& path) : BaseT(std::move(other), path) {}
0942 
0943     BinnedEstimate(std::vector<AxisT1>&& xEdges, std::vector<AxisT2>&& yEdges,
0944                    const std::string& path="", const std::string& title="")
0945               : BaseT(std::move(xEdges), std::move(yEdges), path, title) {}
0946 
0947     BinnedEstimate(const std::vector<AxisT1>& xEdges, const std::vector<AxisT2>& yEdges,
0948                    const std::string& path="", const std::string& title="")
0949               : BaseT(xEdges, yEdges, path, title) {}
0950 
0951     /// @brief Constructor with auto-setup of evenly spaced axes.
0952     ///
0953     /// The constructor argument uses double rather than EdgeT to
0954     /// allow for auto-conversion of int to double.
0955     ///
0956     /// @note This constructor is only supported when all axes are continuous.
0957     template <typename EdgeT = double, typename = enable_if_all_CAxisT<EdgeT, AxisT1, AxisT2>>
0958     BinnedEstimate(size_t nbinsX, double lowerX, double upperX,
0959                    size_t nbinsY, double lowerY, double upperY,
0960                    const std::string& path = "", const std::string& title = "")
0961               : BaseT({nbinsX, nbinsY}, {{lowerX, upperX}, {lowerY, upperY}}, path, title) {}
0962 
0963     /// @brief Make a copy on the stack
0964     EstimateT clone() const noexcept {
0965       return EstimateT(*this);
0966     }
0967 
0968     /// @brief Make a copy on the heap
0969     EstimateT* newclone() const noexcept {
0970       return new EstimateT(*this);
0971     }
0972 
0973     /// @brief Bin access using global index
0974     BinType& bin(const size_t index) noexcept {
0975       return BaseT::bin(index);
0976     }
0977 
0978     /// @brief Bin access using global index (const version)
0979     const BinType& bin(const size_t index) const noexcept {
0980       return BaseT::bin(index);
0981     }
0982 
0983     /// @brief Bin access using local indices
0984     BinType& bin(const size_t localX, const size_t localY) noexcept {
0985       return BaseT::bin( {localX, localY} );
0986     }
0987 
0988     /// @brief Bin access using local indices (const version)
0989     const BinType& bin(const size_t localX, const size_t localY) const noexcept {
0990       return BaseT::bin( {localX, localY} );
0991     }
0992 
0993     /// @brief Bin access using coordinates
0994     BinType& binAt(const AxisT1 xCoord, const AxisT2 yCoord) noexcept {
0995       return BaseT::binAt( {xCoord, yCoord} );
0996     }
0997 
0998     /// @brief Bin access using coordinates (const version)
0999     const BinType& binAt(const AxisT1 xCoord, const AxisT2 yCoord) const noexcept {
1000       return BaseT::binAt( {xCoord, yCoord} );
1001     }
1002 
1003     /// @brief Find bin index for given coordinates
1004     size_t indexAt(const AxisT1 xCoord, const AxisT2 yCoord) const noexcept {
1005       return BaseT::binAt( {xCoord, yCoord} ).index();
1006     }
1007 
1008     /// @brief Mask/Unmask bin at given set of coordinates
1009     void maskBinAt(const AxisT1 xCoord, const AxisT2 yCoord, const bool status = true) noexcept {
1010       return BaseT::maskBin({xCoord, yCoord}, status);
1011     }
1012 
1013   };
1014 
1015 
1016   /// @brief Specialisation of the BinnedEstimate for a 3D BinnedEstimate
1017   template <typename AxisT1, typename AxisT2, typename AxisT3>
1018   class BinnedEstimate<AxisT1, AxisT2, AxisT3>
1019       : public EstimateStorage<AxisT1, AxisT2, AxisT3>,
1020         public XAxisMixin<BinnedEstimate<AxisT1, AxisT2, AxisT3>, AxisT1>,
1021         public YAxisMixin<BinnedEstimate<AxisT1, AxisT2, AxisT3>, AxisT2>,
1022         public ZAxisMixin<BinnedEstimate<AxisT1, AxisT2, AxisT3>, AxisT3> {
1023   public:
1024     using EstimateT = BinnedEstimate<AxisT1, AxisT2, AxisT3>;
1025     using BaseT = EstimateStorage<AxisT1, AxisT2, AxisT3>;
1026     using BinType = typename BaseT::BinT;
1027     using Ptr = std::shared_ptr<EstimateT>;
1028 
1029     /// @brief Inherit constructors.
1030     using BaseT::BaseT;
1031 
1032     BinnedEstimate(const EstimateT&) = default;
1033     BinnedEstimate(EstimateT&&) = default;
1034     BinnedEstimate& operator =(const EstimateT&) = default;
1035     BinnedEstimate& operator =(EstimateT&&) = default;
1036     using AnalysisObject::operator =;
1037 
1038     /// @brief Copy constructor (needed for clone functions).
1039     ///
1040     /// @note Compiler won't generate this constructor automatically.
1041     BinnedEstimate(const BaseT& other) : BaseT(other) {}
1042     //
1043     BinnedEstimate(const EstimateT& other, const std::string& path) : BaseT(other, path) {}
1044 
1045     /// @brief Move constructor.
1046     BinnedEstimate(BaseT&& other) : BaseT(std::move(other)) {}
1047     //
1048     BinnedEstimate(EstimateT&& other, const std::string& path) : BaseT(std::move(other), path) {}
1049 
1050     /// @brief Constructor with auto-setup of evenly spaced axes.
1051     ///
1052     /// The constructor argument uses double rather than EdgeT to
1053     /// allow for auto-conversion of int to double.
1054     ///
1055     /// @note This constructor is only supported when all axes are continuous.
1056     template <typename EdgeT = double, typename = enable_if_all_CAxisT<EdgeT, AxisT1, AxisT2, AxisT3>>
1057     BinnedEstimate(size_t nbinsX, double lowerX, double upperX,
1058                    size_t nbinsY, double lowerY, double upperY,
1059                    size_t nbinsZ, double lowerZ, double upperZ,
1060                    const std::string& path = "", const std::string& title = "")
1061               : BaseT({nbinsX, nbinsY, nbinsZ}, {{lowerX, upperX}, {lowerY, upperY},
1062                       {lowerZ, upperZ}}, path, title) {}
1063 
1064     /// @brief Make a copy on the stack
1065     EstimateT clone() const noexcept {
1066       return EstimateT(*this);
1067     }
1068 
1069     /// @brief Make a copy on the heap
1070     EstimateT* newclone() const noexcept {
1071       return new EstimateT(*this);
1072     }
1073 
1074     /// @brief Bin access using global index
1075     BinType& bin(const size_t index) noexcept {
1076       return BaseT::bin(index);
1077     }
1078 
1079     /// @brief Bin access using global index (const version)
1080     const BinType& bin(const size_t index) const noexcept {
1081       return BaseT::bin(index);
1082     }
1083 
1084     /// @brief Bin access using local indices
1085     BinType& bin(const size_t localX, const size_t localY, const size_t localZ) noexcept {
1086       return BaseT::bin( {localX, localY, localZ} );
1087     }
1088 
1089     /// @brief Bin access using local indices
1090     const BinType& bin(const size_t localX, const size_t localY, const size_t localZ) const noexcept {
1091       return BaseT::bin( {localX, localY, localZ} );
1092     }
1093 
1094     /// @brief Bin access using coordinates
1095     BinType& binAt(const AxisT1 xCoord, const AxisT2 yCoord, const AxisT3 zCoord) noexcept {
1096       return BaseT::binAt( {xCoord, yCoord, zCoord} );
1097     }
1098 
1099     /// @brief Bin access using coordinates (const version)
1100     const BinType& binAt(const AxisT1 xCoord, const AxisT2 yCoord, const AxisT3 zCoord) const noexcept {
1101       return BaseT::binAt( {xCoord, yCoord, zCoord} );
1102     }
1103 
1104     /// @brief Find bin index for given coordinates
1105     size_t indexAt(const AxisT1 xCoord, const AxisT2 yCoord, const AxisT3 zCoord) const noexcept {
1106       return BaseT::binAt( {xCoord, yCoord, zCoord} ).index();
1107     }
1108 
1109     /// @brief Mask/Unmask bin at given set of coordinates
1110     void maskBinAt(const AxisT1 xCoord, const AxisT2 yCoord, const AxisT3 zCoord, const bool status = true) noexcept {
1111       return BaseT::maskBin({xCoord, yCoord, zCoord}, status);
1112     }
1113 
1114   };
1115 
1116   /// @name Combining BinnedEstimates: global operators
1117   /// @{
1118 
1119   /// @brief Add two BinnedEstimates
1120   template <typename... AxisT>
1121   inline BinnedEstimate<AxisT...>
1122   operator + (BinnedEstimate<AxisT...> first, const BinnedEstimate<AxisT...>& second) {
1123     first += second;
1124     return first;
1125   }
1126 
1127 
1128   /// @brief Add two BinnedEstimates
1129   template <typename... AxisT>
1130   inline BinnedEstimate<AxisT...>
1131   operator + (BinnedEstimate<AxisT...> first, BinnedEstimate<AxisT...>&& second) {
1132     first += std::move(second);
1133     return first;
1134   }
1135 
1136 
1137   /// @brief Subtract two BinnedEstimates
1138   template <typename... AxisT>
1139   inline BinnedEstimate< AxisT...>
1140   operator - (BinnedEstimate<AxisT...> first, const BinnedEstimate<AxisT...>& second) {
1141     first -= second;
1142     return first;
1143   }
1144 
1145   /// @brief Subtract two BinnedEstimates
1146   template <typename... AxisT>
1147   inline BinnedEstimate< AxisT...>
1148   operator - (BinnedEstimate<AxisT...> first, BinnedEstimate<AxisT...>&& second) {
1149     first -= std::move(second);
1150     return first;
1151   }
1152 
1153   /// @brief Divide two BinnedEstimates
1154   template <typename... AxisT>
1155   inline BinnedEstimate<AxisT...>
1156   divide(const BinnedEstimate<AxisT...>& numer, const BinnedEstimate<AxisT...>& denom,
1157          const std::string& pat_uncorr="^stat|^uncor" ) {
1158     if (numer != denom) {
1159       throw BinningError("Arithmetic operation requires compatible binning!");
1160     }
1161 
1162     BinnedEstimate<AxisT...> rtn(numer.binning());
1163     if (numer.path() == denom.path())  rtn.setPath(numer.path());
1164     if (rtn.hasAnnotation("ScaledBy")) rtn.rmAnnotation("ScaledBy");
1165 
1166     for (const auto& b_num : numer.bins(true, true)) {
1167       const size_t idx = b_num.index();
1168       rtn.bin(idx) = divide(b_num, denom.bin(idx), pat_uncorr);
1169     }
1170     rtn.maskBins(denom.maskedBins(), true);
1171 
1172     return rtn;
1173   }
1174 
1175   /// @brief Divide two BinnedEstimates
1176   template <typename... AxisT>
1177   inline BinnedEstimate<AxisT...>
1178   operator / (const BinnedEstimate<AxisT...>& numer, const BinnedEstimate<AxisT...>& denom) {
1179     return divide(numer, denom);
1180   }
1181 
1182   /// @brief Divide two BinnedEstimates
1183   template <typename... AxisT>
1184   inline BinnedEstimate<AxisT...>
1185   operator / (BinnedEstimate<AxisT...>&& numer, const BinnedEstimate<AxisT...>& denom) {
1186     return divide(std::move(numer), denom);
1187   }
1188 
1189   /// @brief Divide two BinnedEstimates
1190   template <typename... AxisT>
1191   inline BinnedEstimate<AxisT...>
1192   operator / (const BinnedEstimate<AxisT...>& numer, BinnedEstimate<AxisT...>&& denom) {
1193     return divide(numer, std::move(denom));
1194   }
1195 
1196   /// @brief Divide two BinnedEstimates
1197   template <typename... AxisT>
1198   inline BinnedEstimate<AxisT...>
1199   operator / (BinnedEstimate<AxisT...>&& numer, BinnedEstimate<AxisT...>&& denom) {
1200     return divide(std::move(numer), std::move(denom));
1201   }
1202 
1203 
1204   /// @brief Calculate a binned efficiency ratio of two BinnedEstimate objects
1205   ///
1206   /// @note An efficiency is not the same thing as a standard division of two
1207   /// BinnedEstimate objects: the errors are treated as correlated via binomial statistics.
1208   template <typename... AxisT>
1209   inline BinnedEstimate<AxisT...>
1210   efficiency(const BinnedEstimate<AxisT...>& accepted, const BinnedEstimate<AxisT...>& total,
1211              const std::string& pat_uncorr="^stat|^uncor" ) {
1212 
1213     if (accepted != total) {
1214       throw BinningError("Arithmetic operation requires compatible binning!");
1215     }
1216 
1217     BinnedEstimate<AxisT...> rtn(accepted.binning());
1218 
1219     for (const auto& b_acc : accepted.bins(true, true)) {
1220       Estimate est;
1221       const size_t idx = b_acc.index();
1222       try {
1223         est = efficiency(b_acc, total.bin(idx), pat_uncorr);
1224       } catch (const UserError& e) { }
1225       rtn.bin(idx) = est;
1226     }
1227     return rtn;
1228   }
1229 
1230 
1231   /// @brief Calculate the asymmetry (a-b)/(a+b) of two BinnedDbn objects
1232   template <typename... AxisT>
1233   inline BinnedEstimate<AxisT...>
1234   asymm(const BinnedEstimate<AxisT...>& a,
1235         const BinnedEstimate<AxisT...>& b,
1236         const std::string& pat_uncorr="^stat|^uncor" ) {
1237     return divde(subtract(a-b, pat_uncorr), add(a+b, pat_uncorr), pat_uncorr);
1238   }
1239 
1240 
1241   /// @brief Multiply two BinnedEstimates
1242   template <typename... AxisT>
1243   inline BinnedEstimate<AxisT...>
1244   multiply(const BinnedEstimate<AxisT...>& lhs, const BinnedEstimate<AxisT...>& rhs,
1245            const std::string& pat_uncorr="^stat|^uncor" ) {
1246     if (lhs != rhs) {
1247       throw BinningError("Arithmetic operation requires compatible binning!");
1248     }
1249 
1250     BinnedEstimate<AxisT...> rtn(lhs.binning());
1251     if (lhs.path() == rhs.path())  rtn.setPath(lhs.path());
1252     if (rtn.hasAnnotation("ScaledBy")) rtn.rmAnnotation("ScaledBy");
1253 
1254     for (const auto& b_lhs : lhs.bins(true, true)) {
1255       const size_t idx = b_lhs.index();
1256       rtn.bin(idx) = multiply(b_lhs, rhs.bin(idx), pat_uncorr);
1257     }
1258     rtn.maskBins(rhs.maskedBins(), true);
1259 
1260     return rtn;
1261   }
1262 
1263   /// @brief Multiply two BinnedEstimates
1264   template <typename... AxisT>
1265   inline BinnedEstimate<AxisT...>
1266   operator * (const BinnedEstimate<AxisT...>& lhs, const BinnedEstimate<AxisT...>& rhs) {
1267     return multiply(lhs, rhs);
1268   }
1269 
1270   /// @brief Multiply two BinnedEstimates
1271   template <typename... AxisT>
1272   inline BinnedEstimate<AxisT...>
1273   operator * (BinnedEstimate<AxisT...>&& lhs, const BinnedEstimate<AxisT...>& rhs) {
1274     return multiply(std::move(lhs), rhs);
1275   }
1276 
1277   /// @brief Multiply two BinnedEstimates
1278   template <typename... AxisT>
1279   inline BinnedEstimate<AxisT...>
1280   operator * (const BinnedEstimate<AxisT...>& lhs, BinnedEstimate<AxisT...>&& rhs) {
1281     return multiply(lhs, std::move(rhs));
1282   }
1283 
1284   /// @brief Multiply two BinnedEstimates
1285   template <typename... AxisT>
1286   inline BinnedEstimate<AxisT...>
1287   operator * (BinnedEstimate<AxisT...>&& lhs, BinnedEstimate<AxisT...>&& rhs) {
1288     return multiply(std::move(lhs), std::move(rhs));
1289   }
1290 
1291 
1292   /// @}
1293 
1294 
1295   /// @name Generalised transformations
1296   /// @{
1297 
1298   template <typename... AxisT>
1299   inline void transform(BinnedEstimate<AxisT...>& est, const Trf<1>& fn) {
1300     for (auto& b : est.bins(true, true)) {
1301       b.transform(fn);
1302     }
1303   }
1304 
1305   template <typename... AxisT, typename FN>
1306   inline void transform(BinnedEstimate<AxisT...>& est, const FN& fn) {
1307     transform(est, Trf<1>(fn));
1308   }
1309 
1310   /// @}
1311 
1312 
1313   /// @name Convenience aliases
1314   /// @{
1315 
1316   /// Define dimension-specific short-hands (Cython sugar)
1317   template<typename A1>
1318   using BinnedEstimate1D = BinnedEstimate<A1>;
1319 
1320   template <typename A1, typename A2>
1321   using BinnedEstimate2D = BinnedEstimate<A1, A2>;
1322 
1323   template <typename A1, typename A2, typename A3>
1324   using BinnedEstimate3D = BinnedEstimate<A1, A2, A3>;
1325 
1326   /// Anonymous namespace to limit visibility
1327   namespace {
1328     template <class T>
1329     struct EstimateMaker;
1330 
1331     template<size_t... Is>
1332     struct EstimateMaker<std::index_sequence<Is...>> {
1333       using type = BinnedEstimate< std::decay_t<decltype((void)Is, std::declval<double>())>... >;
1334     };
1335   }
1336 
1337   template<size_t N>
1338   using EstimateND = typename EstimateMaker<std::make_index_sequence<N>>::type;
1339 
1340 
1341   // User-friendly names (continuous axes only)
1342   using Estimate1D = BinnedEstimate<double>;
1343   using Estimate2D = BinnedEstimate<double,double>;
1344   using Estimate3D = BinnedEstimate<double,double,double>;
1345 
1346   /// @}
1347 
1348 }
1349 
1350 #endif