Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef NPSTAT_DUALAXIS_HH_
0002 #define NPSTAT_DUALAXIS_HH_
0003 
0004 /*!
0005 // \file DualAxis.h
0006 //
0007 // \brief Represent both equidistant and non-uniform coordinate sets
0008 //        for rectangular grids
0009 //
0010 // Author: I. Volobouev
0011 //
0012 // July 2012
0013 */
0014 
0015 #include "JetMETCorrections/InterpolationTables/interface/GridAxis.h"
0016 #include "JetMETCorrections/InterpolationTables/interface/UniformAxis.h"
0017 
0018 namespace npstat {
0019   /**
0020     // Rectangular grid axis which can be either uniform or non-uniform.
0021     // Will work a little bit slower than either GridAxis or UniformAxis,
0022     // but can be used in place of either one of them.
0023     */
0024   class DualAxis {
0025   public:
0026     // Constructors
0027     inline DualAxis(const GridAxis& g) : a_(g), u_(2, 0.0, 1.0), uniform_(false) {}
0028 
0029     inline DualAxis(const UniformAxis& u) : a_(dummy_vec()), u_(u), uniform_(true) {}
0030 
0031     inline DualAxis(unsigned nCoords, double min, double max, const char* label = nullptr)
0032         : a_(dummy_vec()), u_(nCoords, min, max, label), uniform_(true) {}
0033 
0034     inline explicit DualAxis(const std::vector<double>& coords, const bool useLogSpace = false)
0035         : a_(coords, useLogSpace), u_(2, 0.0, 1.0), uniform_(false) {}
0036 
0037     inline DualAxis(const std::vector<double>& coords, const char* label, const bool useLogSpace = false)
0038         : a_(coords, label, useLogSpace), u_(2, 0.0, 1.0), uniform_(false) {}
0039 
0040     // Inspectors
0041     inline bool isUniform() const { return uniform_; }
0042 
0043     inline unsigned nCoords() const { return uniform_ ? u_.nCoords() : a_.nCoords(); }
0044 
0045     inline double min() const { return uniform_ ? u_.min() : a_.min(); }
0046 
0047     inline double max() const { return uniform_ ? u_.max() : a_.max(); }
0048 
0049     inline const std::string& label() const { return uniform_ ? u_.label() : a_.label(); }
0050 
0051     inline bool usesLogSpace() const { return uniform_ ? u_.usesLogSpace() : a_.usesLogSpace(); }
0052 
0053     inline std::pair<unsigned, double> getInterval(const double x) const {
0054       return uniform_ ? u_.getInterval(x) : a_.getInterval(x);
0055     }
0056 
0057     inline std::pair<unsigned, double> linearInterval(const double x) const {
0058       return uniform_ ? u_.linearInterval(x) : a_.linearInterval(x);
0059     }
0060 
0061     inline double coordinate(const unsigned i) const { return uniform_ ? u_.coordinate(i) : a_.coordinate(i); }
0062 
0063     inline double length() const { return uniform_ ? u_.length() : a_.length(); }
0064 
0065     inline unsigned nIntervals() const { return uniform_ ? u_.nIntervals() : a_.nIntervals(); }
0066 
0067     inline double intervalWidth(const unsigned i = 0) const {
0068       return uniform_ ? u_.intervalWidth(i) : a_.intervalWidth(i);
0069     }
0070 
0071     inline std::vector<double> coords() const { return uniform_ ? u_.coords() : a_.coords(); }
0072 
0073     inline bool operator==(const DualAxis& r) const { return uniform_ == r.uniform_ && a_ == r.a_ && u_ == r.u_; }
0074 
0075     inline bool operator!=(const DualAxis& r) const { return !(*this == r); }
0076 
0077     //@{
0078     /**
0079         // Return a pointer to the underlying axis. This will be
0080         // a null pointer if the axis does not correspond to the
0081         // constructed type.
0082         */
0083     inline const GridAxis* getGridAxis() const { return uniform_ ? static_cast<const GridAxis*>(nullptr) : &a_; }
0084 
0085     inline const UniformAxis* getUniformAxis() const {
0086       return uniform_ ? &u_ : static_cast<const UniformAxis*>(nullptr);
0087     }
0088     //@}
0089 
0090     /** Modify the axis label */
0091     inline void setLabel(const char* newlabel) { uniform_ ? u_.setLabel(newlabel) : a_.setLabel(newlabel); }
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::DualAxis"; }
0100     static inline unsigned version() { return 1; }
0101     static DualAxis* read(const gs::ClassId& id, std::istream& in);
0102 
0103   private:
0104     GridAxis a_;
0105     UniformAxis u_;
0106     bool uniform_;
0107 
0108     inline static std::vector<double> dummy_vec() {
0109       std::vector<double> vec(2, 0.0);
0110       vec[1] = 1.0;
0111       return vec;
0112     }
0113 
0114     inline DualAxis() : a_(dummy_vec()), u_(2, 0.0, 1.0), uniform_(true) {}
0115   };
0116 }  // namespace npstat
0117 
0118 #endif  // NPSTAT_DUALAXIS_HH_