Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:13:41

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