Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:36

0001 /// \file ROOT/RAxisConfig.hxx
0002 /// \ingroup HistV7
0003 /// \author Axel Naumann <axel@cern.ch>
0004 /// \date 2020-02-05
0005 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
0006 /// is welcome!
0007 
0008 /*************************************************************************
0009  * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers.               *
0010  * All rights reserved.                                                  *
0011  *                                                                       *
0012  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0013  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0014  *************************************************************************/
0015 
0016 #ifndef ROOT7_RAxisConfig
0017 #define ROOT7_RAxisConfig
0018 
0019 #include <string>
0020 #include <utility>
0021 #include <vector>
0022 
0023 #include <string_view>
0024 
0025 namespace ROOT {
0026 namespace Experimental {
0027 
0028 /**
0029 \class RAxisConfig
0030 Objects used to configure the different axis types. It can store the
0031 properties of all ROOT-provided axis types, together with the type of the axis.
0032 
0033 TODO: that's what a variant will be invented for!
0034 */
0035 class RAxisConfig {
0036 public:
0037    enum EKind {
0038       kEquidistant, ///< represents a RAxisEquidistant
0039       kGrow,        ///< represents a RAxisGrow
0040       kIrregular,   ///< represents a RAxisIrregular
0041       kLabels,      ///< represents a RAxisLabels
0042       kNumKinds
0043    };
0044 
0045 private:
0046    std::string fTitle;
0047    int fNBinsNoOver;
0048    EKind fKind;                      ///< The kind of axis represented by this configuration
0049    std::vector<double> fBinBorders;  ///< Bin borders of the RAxisIrregular
0050    std::vector<std::string> fLabels; ///< Bin labels for a RAxisLabels
0051 
0052    /// Represents a `RAxisEquidistant` or `RAxisGrow` with `nbins` (excluding over- and
0053    /// underflow bins) from `from` to `to`, with an axis title.
0054    explicit RAxisConfig(std::string_view title, int nbins, double from, double to, EKind kind)
0055       : fTitle(title), fNBinsNoOver(nbins), fKind(kind), fBinBorders(2)
0056    {
0057       if (from > to)
0058          std::swap(to, from);
0059 
0060       fBinBorders[0] = from;
0061       fBinBorders[1] = to;
0062    }
0063 
0064 public:
0065    /// Tag type signalling that an axis should be able to grow; used for calling
0066    /// the appropriate constructor.
0067    struct Grow_t {
0068    };
0069    /// Tag signalling that an axis should be able to grow; used for calling the
0070    /// appropriate constructor like so:
0071    ///     RAxisConfig ac(RAxisConfig::Grow, 10, 0., 1.);
0072    constexpr static const Grow_t Grow{};
0073 
0074    /// Represents a `RAxisEquidistant` with `nbins` from `from` to `to`, and
0075    /// axis title.
0076    RAxisConfig(std::string_view title, int nbins, double from, double to)
0077       : RAxisConfig(title, nbins, from, to, kEquidistant)
0078    {}
0079 
0080    /// Represents a `RAxisEquidistant` with `nbins` from `from` to `to`.
0081    RAxisConfig(int nbins, double from, double to): RAxisConfig("", nbins, from, to, kEquidistant) {}
0082 
0083    /// Represents a `RAxisGrow` with `nbins` from `from` to `to`, and axis title.
0084    RAxisConfig(std::string_view title, Grow_t, int nbins, double from, double to)
0085       : RAxisConfig(title, nbins, from, to, kGrow)
0086    {}
0087 
0088    /// Represents a `RAxisGrow` with `nbins` from `from` to `to`.
0089    RAxisConfig(Grow_t, int nbins, double from, double to): RAxisConfig("", nbins, from, to, kGrow) {}
0090 
0091    /// Represents a `RAxisIrregular` with `binborders` and title.
0092    RAxisConfig(std::string_view title, const std::vector<double> &binborders)
0093       : fTitle(title), fNBinsNoOver(binborders.size() - 1), fKind(kIrregular), fBinBorders(binborders)
0094    {}
0095 
0096    /// Represents a `RAxisIrregular` with `binborders`.
0097    RAxisConfig(const std::vector<double> &binborders): RAxisConfig("", binborders) {}
0098 
0099    /// Represents a `RAxisIrregular` with `binborders` and title.
0100    RAxisConfig(std::string_view title, std::vector<double> &&binborders) noexcept
0101       : fTitle(title), fNBinsNoOver(binborders.size() - 1), fKind(kIrregular),
0102         fBinBorders(std::move(binborders))
0103    {}
0104 
0105    /// Represents a `RAxisIrregular` with `binborders`.
0106    RAxisConfig(std::vector<double> &&binborders) noexcept: RAxisConfig("", std::move(binborders)) {}
0107 
0108    /// Represents a `RAxisLabels` with `labels` and title.
0109    RAxisConfig(std::string_view title, const std::vector<std::string_view> &labels)
0110       : fTitle(title), fNBinsNoOver(labels.size()), fKind(kLabels), fLabels(labels.begin(), labels.end())
0111    {}
0112 
0113    /// Represents a `RAxisLabels` with `labels`.
0114    RAxisConfig(const std::vector<std::string_view> &labels): RAxisConfig("", labels) {}
0115 
0116    /// Represents a `RAxisLabels` with `labels` and title.
0117    RAxisConfig(std::string_view title, std::vector<std::string> &&labels)
0118       : fTitle(title), fNBinsNoOver(labels.size()), fKind(kLabels), fLabels(std::move(labels))
0119    {}
0120 
0121    /// Represents a `RAxisLabels` with `labels`.
0122    RAxisConfig(std::vector<std::string> &&labels): RAxisConfig("", std::move(labels)) {}
0123 
0124    /// Get the axis's title
0125    const std::string &GetTitle() const { return fTitle; }
0126 
0127    /// Get the axis kind represented by this `RAxisConfig`.
0128    EKind GetKind() const noexcept { return fKind; }
0129 
0130    /// Get the number of bins, excluding under- and overflow.
0131    int GetNBinsNoOver() const noexcept { return fNBinsNoOver; }
0132 
0133    /// Get the bin borders; non-empty if the GetKind() == kIrregular.
0134    const std::vector<double> &GetBinBorders() const noexcept { return fBinBorders; }
0135 
0136    /// Get the bin labels; non-empty if the GetKind() == kLabels.
0137    const std::vector<std::string> &GetBinLabels() const noexcept { return fLabels; }
0138 };
0139 
0140 namespace Internal {
0141 
0142 /// Converts a RAxisConfig of whatever kind to the corresponding RAxisBase-derived
0143 /// object.
0144 template <RAxisConfig::EKind>
0145 struct AxisConfigToType; // Only specializations are defined.
0146 
0147 } // namespace Internal
0148 } // namespace Experimental
0149 } // namespace ROOT
0150 
0151 #endif // ROOT7_RAxisConfig header guard