Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:33

0001 #ifndef NPSTAT_NUHISTOAXIS_HH_
0002 #define NPSTAT_NUHISTOAXIS_HH_
0003 
0004 /*!
0005 // \file NUHistoAxis.h
0006 //
0007 // \brief Histogram axis with non-uniform bin spacing
0008 //
0009 // Author: I. Volobouev
0010 //
0011 // December 2011
0012 */
0013 
0014 #include <vector>
0015 #include <utility>
0016 
0017 #include "Alignment/Geners/interface/ClassId.hh"
0018 #include "JetMETCorrections/InterpolationTables/interface/Interval.h"
0019 
0020 namespace npstat {
0021   template <typename Numeric, class Axis>
0022   class HistoND;
0023   class DualHistoAxis;
0024 
0025   /**
0026     // This class can be used to create histograms with non-uniform binning
0027     */
0028   class NUHistoAxis {
0029   public:
0030     /**
0031         // The number of bin edges provided must be at least 2. Edge
0032         // coordinates will be sorted internally in the increasing order.
0033         // The number of bins will be less by 1 than the number of edges.
0034         */
0035     NUHistoAxis(const std::vector<double>& binEdges, const char* label = nullptr);
0036 
0037     //@{
0038     /** Examine axis properties */
0039     inline double min() const { return min_; }
0040     inline double max() const { return max_; }
0041     inline Interval<double> interval() const { return Interval<double>(min_, max_); }
0042     inline double length() const { return max_ - min_; }
0043     inline unsigned nBins() const { return nBins_; }
0044     inline double binWidth(const int binNum) const { return binEdges_.at(binNum + 1) - binEdges_.at(binNum); }
0045     inline const std::string& label() const { return label_; }
0046     inline bool isUniform() const { return uniform_; }
0047     //@}
0048 
0049     /** Return the coordinate of the given bin left edge */
0050     inline double leftBinEdge(const int binNum) const { return binEdges_.at(binNum); }
0051 
0052     /** Return the coordinate of the given bin right edge */
0053     inline double rightBinEdge(const int binNum) const { return binEdges_.at(binNum + 1); }
0054 
0055     /** Return the coordinate of the given bin center */
0056     inline double binCenter(const int binNum) const { return 0.5 * (binEdges_.at(binNum) + binEdges_.at(binNum + 1)); }
0057 
0058     /** Return the coordinate interval occupied by the given bin */
0059     inline Interval<double> binInterval(const int binNum) const {
0060       return Interval<double>(binEdges_.at(binNum), binEdges_.at(binNum + 1));
0061     }
0062 
0063     /** Change the axis label */
0064     inline void setLabel(const char* newlabel) { label_ = newlabel ? newlabel : ""; }
0065 
0066     /**
0067         // This method returns -1 for values below the lower limit and
0068         // "nBins()" for values equal to or above the upper limit
0069         */
0070     int binNumber(double x) const;
0071 
0072     /**
0073         // Floating point bin number given the coordinate. Useful for
0074         // interpolation methods and such.
0075         */
0076     double fltBinNumber(double x, bool mapLeftEdgeTo0 = true) const;
0077 
0078     /**
0079         // This method returns the closest valid bin number
0080         // (above 0 and below nBins() )
0081         */
0082     unsigned closestValidBin(double x) const;
0083 
0084     bool operator==(const NUHistoAxis&) const;
0085     bool operator!=(const NUHistoAxis&) const;
0086 
0087     /** Comparison of axis coordinates within given tolerance */
0088     bool isClose(const NUHistoAxis&, double tol) const;
0089 
0090     /** Return uniformly rebinned axis */
0091     NUHistoAxis rebin(unsigned newBins) const;
0092 
0093     //@{
0094     /** Method related to "geners" I/O */
0095     inline gs::ClassId classId() const { return gs::ClassId(*this); }
0096     bool write(std::ostream& of) const;
0097     //@}
0098 
0099     static inline const char* classname() { return "npstat::NUHistoAxis"; }
0100     static inline unsigned version() { return 1; }
0101     static NUHistoAxis* read(const gs::ClassId& id, std::istream& in);
0102 
0103   private:
0104     NUHistoAxis(unsigned nBins, double min, double max, const char* label = nullptr);
0105 
0106     double min_;
0107     double max_;
0108     std::vector<double> binEdges_;
0109     std::string label_;
0110     unsigned nBins_;
0111     bool uniform_;
0112 
0113     template <typename Numeric, class Axis>
0114     friend class HistoND;
0115     friend class DualHistoAxis;
0116 
0117     inline unsigned overflowIndex(const double x, unsigned* binNum) const {
0118       if (x < min_)
0119         return 0U;
0120       else if (x >= max_)
0121         return 2U;
0122       else {
0123         *binNum = binNumber(x);
0124         return 1U;
0125       }
0126     }
0127 
0128     inline NUHistoAxis() : min_(0.0), max_(0.0), nBins_(0), uniform_(false) {}
0129   };
0130 }  // namespace npstat
0131 
0132 #endif  // NPSTAT_NUHISTOAXIS_HH_