Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-13 10:32:14

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_Histo_h
0007 #define YODA_Histo_h
0008 
0009 #include "YODA/BinnedDbn.h"
0010 
0011 namespace YODA {
0012 
0013   /// @brief Specialisation of the BinnedDbn for a 1D histogram
0014   template <typename AxisT>
0015   class BinnedDbn<1, AxisT>
0016       : public DbnStorage<1, AxisT>,
0017         public XAxisMixin<BinnedDbn<1, AxisT>, AxisT>,
0018         public XStatsMixin<BinnedDbn<1, AxisT>> {
0019   public:
0020 
0021     using HistoT = BinnedDbn<1, AxisT>;
0022     using BaseT = DbnStorage<1, AxisT>;
0023     using FillType = typename BaseT::FillType;
0024     using BinType = typename BaseT::BinT;
0025     using Ptr = std::shared_ptr<HistoT>;
0026 
0027     /// @brief Inherit constructors.
0028     using BaseT::BaseT;
0029 
0030     BinnedDbn() = default;
0031     BinnedDbn(const HistoT&) = default;
0032     BinnedDbn(HistoT&&) =default;
0033     BinnedDbn& operator =(const HistoT&) = default;
0034     BinnedDbn& operator =(HistoT&&) = default;
0035     using AnalysisObject::operator =;
0036 
0037     /// @brief Constructor with auto-setup of evenly spaced axes.
0038     ///
0039     /// The constructor argument uses double rather than EdgeT to
0040     /// allow for auto-conversion of int to double.
0041     ///
0042     /// @note This constructor is only supported when all axes are continuous.
0043     template <typename EdgeT = double, typename = enable_if_all_CAxisT<EdgeT, AxisT>>
0044     BinnedDbn(size_t nbins, double lower, double upper,
0045               const std::string& path = "", const std::string& title = "")
0046         : BaseT({nbins}, {{lower, upper}}, path, title) {}
0047 
0048     /// @brief Copy constructor (needed for clone functions).
0049     ///
0050     /// @note Compiler won't generate this constructor automatically.
0051     BinnedDbn(const BaseT& other) : BaseT(other) {}
0052     //
0053     BinnedDbn(const HistoT& other, const std::string& path) : BaseT(other, path) {}
0054 
0055     /// @brief Move constructor
0056     BinnedDbn(BaseT&& other) : BaseT(std::move(other)) {}
0057     //
0058     BinnedDbn(HistoT&& other, const std::string& path) : BaseT(std::move(other), path) {}
0059 
0060     /// @brief Make a copy on the stack
0061     HistoT clone() const noexcept {
0062       return HistoT(*this);
0063     }
0064 
0065     /// @brief Make a copy on the heap
0066     HistoT* newclone() const noexcept {
0067       return new HistoT(*this);
0068     }
0069 
0070     /// @brief Fill function with an explicit coordinate.
0071     virtual int fill(const AxisT val, const double weight = 1.0, const double fraction = 1.0) {
0072       return BaseT::fill({val}, weight, fraction);
0073     }
0074 
0075     /// @brief Fill function with FillType.
0076     virtual int fill(FillType&& coords, const double weight = 1.0, const double fraction = 1.0) {
0077       return BaseT::fill(std::move(coords), weight, fraction);
0078     }
0079 
0080     /// @brief Find bin index for given coordinates
0081     size_t indexAt(const AxisT xCoord) const noexcept {
0082       return BaseT::binAt( {xCoord} ).index();
0083     }
0084 
0085     /// @brief Mask/Unmask bin at given set of coordinates
0086     void maskBinAt(const AxisT xCoord, const bool status = true) noexcept {
0087       return BaseT::maskBin({xCoord}, status);
0088     }
0089 
0090   };
0091 
0092 
0093 
0094 
0095 
0096   /// @brief Specialisation of the BinnedDbn for a 2D histogram
0097   template <typename AxisT1, typename AxisT2>
0098   class BinnedDbn<2, AxisT1, AxisT2>
0099       : public DbnStorage<2, AxisT1, AxisT2>,
0100         public XAxisMixin<BinnedDbn<2, AxisT1, AxisT2>, AxisT1>,
0101         public XStatsMixin<BinnedDbn<2, AxisT1, AxisT2>>,
0102         public YAxisMixin<BinnedDbn<2, AxisT1, AxisT2>, AxisT2>,
0103         public YStatsMixin<BinnedDbn<2, AxisT1, AxisT2>> {
0104   public:
0105 
0106     using HistoT = BinnedDbn<2, AxisT1, AxisT2>;
0107     using BaseT = DbnStorage<2, AxisT1, AxisT2>;
0108     using FillType = typename BaseT::FillType;
0109     using BinType = typename BaseT::BinT;
0110     using Ptr = std::shared_ptr<HistoT>;
0111 
0112     /// @brief Inherit constructors.
0113     using BaseT::BaseT;
0114 
0115     BinnedDbn() = default;
0116     BinnedDbn(const HistoT&) = default;
0117     BinnedDbn(HistoT&&) = default;
0118     BinnedDbn& operator =(const HistoT&) = default;
0119     BinnedDbn& operator =(HistoT&&) = default;
0120     using AnalysisObject::operator =;
0121 
0122     /// @brief Constructor with auto-setup of evenly spaced axes.
0123     ///
0124     /// The constructor argument uses double rather than EdgeT to
0125     /// allow for auto-conversion of int to double.
0126     ///
0127     /// @note This constructor is only supported when all axes are continuous.
0128     template <typename EdgeT = double, typename = enable_if_all_CAxisT<EdgeT, AxisT1, AxisT2>>
0129     BinnedDbn(size_t nbinsX, double lowerX, double upperX,
0130               size_t nbinsY, double lowerY, double upperY,
0131               const std::string& path = "", const std::string& title = "")
0132         : BaseT({nbinsX, nbinsY}, {{lowerX, upperX}, {lowerY, upperY}}, path, title) {}
0133 
0134     /// @brief Copy constructor (needed for clone functions).
0135     ///
0136     /// @note Compiler won't generate this constructor automatically.
0137     BinnedDbn(const BaseT& other) : BaseT(other) {}
0138     //
0139     BinnedDbn(const HistoT& other, const std::string& path) : BaseT(other, path) {}
0140 
0141     /// @brief Move constructor
0142     BinnedDbn(BaseT&& other) : BaseT(std::move(other)) {}
0143     //
0144     BinnedDbn(HistoT&& other, const std::string& path) : BaseT(std::move(other), path) {}
0145 
0146     /// @brief Make a copy on the stack
0147     HistoT clone() const noexcept {
0148       return HistoT(*this);
0149     }
0150 
0151     /// @brief Make a copy on the heap
0152     HistoT* newclone() const noexcept {
0153       return new HistoT(*this);
0154     }
0155 
0156     /// @brief Fill function with two explicit coordinates.
0157     virtual int fill(const AxisT1 valX, const AxisT2 valY, const double weight = 1.0, const double fraction = 1.0) {
0158       return BaseT::fill({valX, valY}, weight, fraction);
0159     }
0160 
0161     /// @brief Fill function with FillType.
0162     virtual int fill(FillType&& coords, const double weight = 1.0, const double fraction = 1.0) {
0163       return BaseT::fill(std::move(coords), weight, fraction);
0164     }
0165 
0166     /// @brief Bin access using global index
0167     BinType& bin(const size_t index) noexcept {
0168       return BaseT::bin(index);
0169     }
0170 
0171     /// @brief Bin access using global index (const version)
0172     const BinType& bin(const size_t index) const noexcept {
0173       return BaseT::bin(index);
0174     }
0175 
0176     /// @brief Bin access using local indices
0177     BinType& bin(const size_t localX, const size_t localY) noexcept {
0178       return BaseT::bin( {localX, localY} );
0179     }
0180 
0181     /// @brief Bin access using local indices (const version)
0182     const BinType& bin(const size_t localX, const size_t localY) const noexcept {
0183       return BaseT::bin( {localX, localY} );
0184     }
0185 
0186     /// @brief Bin access using coordinates
0187     BinType& binAt(const AxisT1 xCoord, const AxisT2 yCoord) noexcept {
0188       return BaseT::binAt( {xCoord, yCoord} );
0189     }
0190 
0191     /// @brief Bin access using coordinates (const version)
0192     const BinType& binAt(const AxisT1 xCoord, const AxisT2 yCoord) const noexcept {
0193       return BaseT::binAt( {xCoord, yCoord} );
0194     }
0195 
0196     /// @brief Find bin index for given coordinates
0197     size_t indexAt(const AxisT1 xCoord, const AxisT2 yCoord) const noexcept {
0198       return BaseT::binAt( {xCoord, yCoord} ).index();
0199     }
0200 
0201     /// @brief Mask/Unmask bin at given set of coordinates
0202     void maskBinAt(const AxisT1 xCoord, const AxisT2 yCoord, const bool status = true) noexcept {
0203       return BaseT::maskBin({xCoord, yCoord}, status);
0204     }
0205 
0206   };
0207 
0208 
0209 
0210 
0211   /// @brief Specialisation of the BinnedDbn for a 3D histogram
0212   template <typename AxisT1, typename AxisT2, typename AxisT3>
0213   class BinnedDbn<3, AxisT1, AxisT2, AxisT3>
0214       : public DbnStorage<3, AxisT1, AxisT2, AxisT3>,
0215         public XAxisMixin<BinnedDbn<3, AxisT1, AxisT2, AxisT3>, AxisT1>,
0216         public XStatsMixin<BinnedDbn<3, AxisT1, AxisT2, AxisT3>>,
0217         public YAxisMixin<BinnedDbn<3, AxisT1, AxisT2, AxisT3>, AxisT2>,
0218         public YStatsMixin<BinnedDbn<3, AxisT1, AxisT2, AxisT3>>,
0219         public ZAxisMixin<BinnedDbn<3, AxisT1, AxisT2, AxisT3>, AxisT3>,
0220         public ZStatsMixin<BinnedDbn<3, AxisT1, AxisT2, AxisT3>> {
0221   public:
0222 
0223     using HistoT = BinnedDbn<3, AxisT1, AxisT2, AxisT3>;
0224     using BaseT = DbnStorage<3, AxisT1, AxisT2, AxisT3>;
0225     using FillType = typename BaseT::FillType;
0226     using BinType = typename BaseT::BinT;
0227     using Ptr = std::shared_ptr<HistoT>;
0228 
0229     /// @brief Inherit constructors.
0230     using BaseT::BaseT;
0231 
0232     BinnedDbn() = default;
0233     BinnedDbn(const HistoT&) = default;
0234     BinnedDbn(HistoT&&) = default;
0235     BinnedDbn& operator =(const HistoT&) = default;
0236     BinnedDbn& operator =(HistoT&&) = default;
0237     using AnalysisObject::operator =;
0238 
0239     /// @brief Constructor with auto-setup of evenly spaced axes.
0240     ///
0241     /// The constructor argument uses double rather than EdgeT to
0242     /// allow for auto-conversion of int to double.
0243     ///
0244     /// @note This constructor is only supported when all axes are continuous.
0245     template <typename EdgeT = double, typename = enable_if_all_CAxisT<EdgeT, AxisT1, AxisT2, AxisT3>>
0246     BinnedDbn(size_t nbinsX, double lowerX, double upperX,
0247               size_t nbinsY, double lowerY, double upperY,
0248               size_t nbinsZ, double lowerZ, double upperZ,
0249               const std::string& path = "", const std::string& title = "")
0250          : BaseT({nbinsX, nbinsY, nbinsZ},
0251                  {{lowerX, upperX}, {lowerY, upperY}, {lowerZ, upperZ}},
0252                  path, title) {}
0253 
0254     /// @brief Copy constructor (needed for clone functions).
0255     ///
0256     /// @note Compiler won't generate this constructor automatically.
0257     BinnedDbn(const BaseT& other) : BaseT(other) {}
0258     //
0259     BinnedDbn(const HistoT& other, const std::string& path) : BaseT(other, path) {}
0260 
0261     /// @brief Move constructor
0262     BinnedDbn(BaseT&& other) : BaseT(std::move(other)) {}
0263     //
0264     BinnedDbn(HistoT&& other, const std::string& path) : BaseT(std::move(other), path) {}
0265 
0266     /// @brief Make a copy on the stack
0267     HistoT clone() const noexcept {
0268       return HistoT(*this);
0269     }
0270 
0271     /// @brief Make a copy on the heap
0272     HistoT* newclone() const noexcept {
0273       return new HistoT(*this);
0274     }
0275 
0276     /// @brief Fill function with three explicit coordinates.
0277     virtual int fill(const AxisT1 valX, const AxisT2 valY, const AxisT3 valZ, const double weight = 1.0, const double fraction = 1.0) {
0278       return BaseT::fill({valX, valY, valZ}, weight, fraction);
0279     }
0280 
0281     /// @brief Fill function with FillType.
0282     virtual int fill(FillType&& coords, const double weight = 1.0, const double fraction = 1.0) {
0283       return BaseT::fill(std::move(coords), weight, fraction);
0284     }
0285 
0286     /// @brief Bin access using global index
0287     BinType& bin(const size_t index) noexcept {
0288       return BaseT::bin(index);
0289     }
0290 
0291     /// @brief Bin access using global index (const version)
0292     const BinType& bin(const size_t index) const noexcept {
0293       return BaseT::bin(index);
0294     }
0295 
0296     /// @brief Bin access using local indices
0297     BinType& bin(const size_t localX, const size_t localY, const size_t localZ) noexcept {
0298       return BaseT::bin( {localX, localY, localZ} );
0299     }
0300 
0301     /// @brief Bin access using local indices (const version)
0302     const BinType& bin(const size_t localX, const size_t localY, const size_t localZ) const noexcept {
0303       return BaseT::bin( {localX, localY, localZ} );
0304     }
0305 
0306     /// @brief Bin access using coordinates
0307     BinType& binAt(const AxisT1 xCoord, const AxisT2 yCoord, const AxisT3 zCoord) noexcept {
0308       return BaseT::binAt( {xCoord, yCoord, zCoord} );
0309     }
0310 
0311     /// @brief Bin access using coordinates (const version)
0312     const BinType& binAt(const AxisT1 xCoord, const AxisT2 yCoord, const AxisT3 zCoord) const noexcept {
0313       return BaseT::binAt( {xCoord, yCoord, zCoord} );
0314     }
0315 
0316     /// @brief Find bin index for given coordinates
0317     size_t indexAt(const AxisT1 xCoord, const AxisT2 yCoord, const AxisT3 zCoord) const noexcept {
0318       return BaseT::binAt( {xCoord, yCoord, zCoord} ).index();
0319     }
0320 
0321     /// @brief Mask/Unmask bin at given set of coordinates
0322     void maskBinAt(const AxisT1 xCoord, const AxisT2 yCoord, const AxisT3 zCoord, const bool status = true) noexcept {
0323       return BaseT::maskBin({xCoord, yCoord, zCoord}, status);
0324     }
0325 
0326   };
0327 
0328 
0329   /// Define dimension-specific short-hands (Cython sugar)
0330   template<typename A1>
0331   using BinnedHisto1D = BinnedHisto<A1>;
0332 
0333   template <typename A1, typename A2>
0334   using BinnedHisto2D = BinnedHisto<A1, A2>;
0335 
0336   template <typename A1, typename A2, typename A3>
0337   using BinnedHisto3D = BinnedHisto<A1, A2, A3>;
0338 
0339   /// Anonymous namespace to limit visibility
0340   namespace {
0341     template <class T>
0342     struct HistoMaker;
0343 
0344     template<size_t... Is>
0345     struct HistoMaker<std::index_sequence<Is...>> {
0346       using type = BinnedHisto< std::decay_t<decltype((void)Is, std::declval<double>())>... >;
0347     };
0348   }
0349 
0350   template<size_t N>
0351   using HistoND = typename HistoMaker<std::make_index_sequence<N>>::type;
0352 
0353   /// User-friendly familiar names (continuous axes only)
0354   using Histo1D = BinnedHisto<double>;
0355   using Histo2D = BinnedHisto<double,double>;
0356   using Histo3D = BinnedHisto<double,double,double>;
0357 
0358 }
0359 
0360 #endif