Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:20

0001 #ifndef NPSTAT_DUALHISTOAXIS_HH_
0002 #define NPSTAT_DUALHISTOAXIS_HH_
0003 
0004 /*!
0005 // \file DualHistoAxis.h
0006 //
0007 // \brief Represent both equidistant and non-uniform histogram axis binning
0008 //
0009 // Author: I. Volobouev
0010 //
0011 // July 2012
0012 */
0013 
0014 #include "JetMETCorrections/InterpolationTables/interface/HistoAxis.h"
0015 #include "JetMETCorrections/InterpolationTables/interface/NUHistoAxis.h"
0016 
0017 namespace npstat {
0018   /**
0019     // Histogram axis which can be either uniform or non-uniform.
0020     // Will work a little bit slower than either HistoAxis or NUHistoAxis,
0021     // but can be used in place of either one of them.
0022     */
0023   class DualHistoAxis {
0024   public:
0025     // Constructors
0026     inline DualHistoAxis(const NUHistoAxis& a) : a_(a), u_(1U, 0.0, 1.0), uniform_(false) {}
0027 
0028     inline DualHistoAxis(const HistoAxis& u) : a_(dummy_vec()), u_(u), uniform_(true) {}
0029 
0030     inline DualHistoAxis(const std::vector<double>& binEdges, const char* label = nullptr)
0031         : a_(binEdges, label), u_(1U, 0.0, 1.0), uniform_(false) {}
0032 
0033     inline DualHistoAxis(unsigned nBins, double min, double max, const char* label = nullptr)
0034         : a_(dummy_vec()), u_(nBins, min, max, label), uniform_(true) {}
0035 
0036     // Inspectors
0037     inline bool isUniform() const { return uniform_; }
0038 
0039     inline double min() const { return uniform_ ? u_.min() : a_.min(); }
0040 
0041     inline double max() const { return uniform_ ? u_.max() : a_.max(); }
0042 
0043     inline Interval<double> interval() const { return uniform_ ? u_.interval() : a_.interval(); }
0044 
0045     inline double length() const { return uniform_ ? u_.length() : a_.length(); }
0046 
0047     inline unsigned nBins() const { return uniform_ ? u_.nBins() : a_.nBins(); }
0048 
0049     inline double binWidth(const int binNum) const { return uniform_ ? u_.binWidth(binNum) : a_.binWidth(binNum); }
0050 
0051     inline const std::string& label() const { return uniform_ ? u_.label() : a_.label(); }
0052 
0053     inline double binCenter(const int binNum) const { return uniform_ ? u_.binCenter(binNum) : a_.binCenter(binNum); }
0054 
0055     inline double leftBinEdge(const int binNum) const {
0056       return uniform_ ? u_.leftBinEdge(binNum) : a_.leftBinEdge(binNum);
0057     }
0058 
0059     inline double rightBinEdge(const int binNum) const {
0060       return uniform_ ? u_.rightBinEdge(binNum) : a_.rightBinEdge(binNum);
0061     }
0062 
0063     inline Interval<double> binInterval(const int binNum) const {
0064       return uniform_ ? u_.binInterval(binNum) : a_.binInterval(binNum);
0065     }
0066 
0067     //@{
0068     /**
0069         // Return a pointer to the underlying axis. This will be
0070         // a null pointer if the axis does not correspond to the
0071         // constructed type.
0072         */
0073     inline const NUHistoAxis* getNUHistoAxis() const {
0074       return uniform_ ? static_cast<const NUHistoAxis*>(nullptr) : &a_;
0075     }
0076 
0077     inline const HistoAxis* getHistoAxis() const { return uniform_ ? &u_ : static_cast<const HistoAxis*>(nullptr); }
0078     //@}
0079 
0080     /** Modify the axis label */
0081     inline void setLabel(const char* newlabel) { uniform_ ? u_.setLabel(newlabel) : a_.setLabel(newlabel); }
0082 
0083     /**
0084         // This method returns arbitrary integer bin number.
0085         // Possible output depends on whether the axis is uniform or not.
0086         */
0087     inline int binNumber(const double x) const { return uniform_ ? u_.binNumber(x) : a_.binNumber(x); }
0088 
0089     /**
0090         // Floating point bin number given the coordinate. Useful for
0091         // interpolation methods and such.
0092         */
0093     inline double fltBinNumber(const double x, const bool mapLeftEdgeTo0 = true) const {
0094       return uniform_ ? u_.fltBinNumber(x, mapLeftEdgeTo0) : a_.fltBinNumber(x, mapLeftEdgeTo0);
0095     }
0096 
0097     /** Return the closest valid bin number (above 0 and below nBins() ) */
0098     inline unsigned closestValidBin(const double x) const {
0099       return uniform_ ? u_.closestValidBin(x) : a_.closestValidBin(x);
0100     }
0101 
0102     inline bool operator==(const DualHistoAxis& r) const { return uniform_ == r.uniform_ && a_ == r.a_ && u_ == r.u_; }
0103 
0104     inline bool operator!=(const DualHistoAxis& r) const { return !(*this == r); }
0105 
0106     /** Comparison within given tolerance */
0107     inline bool isClose(const DualHistoAxis& r, const double tol) const {
0108       return uniform_ == r.uniform_ && a_.isClose(r.a_, tol) && u_.isClose(r.u_, tol);
0109     }
0110 
0111     /** Return uniformly rebinned axis */
0112     inline DualHistoAxis rebin(const unsigned newBins) const {
0113       return DualHistoAxis(newBins, min(), max(), label().c_str());
0114     }
0115 
0116     //@{
0117     // Method related to "geners" I/O
0118     inline gs::ClassId classId() const { return gs::ClassId(*this); }
0119     bool write(std::ostream& of) const;
0120     //@}
0121 
0122     static inline const char* classname() { return "npstat::DualHistoAxis"; }
0123     static inline unsigned version() { return 1; }
0124     static DualHistoAxis* read(const gs::ClassId& id, std::istream& in);
0125 
0126   private:
0127     NUHistoAxis a_;
0128     HistoAxis u_;
0129     bool uniform_;
0130 
0131     template <typename Numeric, class Axis>
0132     friend class HistoND;
0133 
0134     inline unsigned overflowIndex(const double x, unsigned* binNumber) const {
0135       return uniform_ ? u_.overflowIndex(x, binNumber) : a_.overflowIndex(x, binNumber);
0136     }
0137 
0138     inline static std::vector<double> dummy_vec() {
0139       std::vector<double> vec(2, 0.0);
0140       vec[1] = 1.0;
0141       return vec;
0142     }
0143 
0144     inline DualHistoAxis() : a_(dummy_vec()), u_(1U, 0.0, 1.0), uniform_(true) {}
0145   };
0146 }  // namespace npstat
0147 
0148 #endif  // NPSTAT_DUALHISTOAXIS_HH_