Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef NPSTAT_GRIDAXIS_HH_
0002 #define NPSTAT_GRIDAXIS_HH_
0003 
0004 /*!
0005 // \file GridAxis.h
0006 //
0007 // \brief Non-uniformly spaced coordinate sets for use in constructing
0008 //        rectangular grids
0009 //
0010 // Author: I. Volobouev
0011 //
0012 // July 2010
0013 */
0014 
0015 #include <vector>
0016 #include <utility>
0017 #include <string>
0018 #include <iostream>
0019 
0020 #include "Alignment/Geners/interface/ClassId.hh"
0021 
0022 namespace npstat {
0023   /**
0024     // Information needed to define an axis of a rectangular grid.
0025     // The distance between grid points can change from point to point.
0026     //
0027     // The UniformAxis class will be more efficient in representing
0028     // equidistant grids.
0029     */
0030   class GridAxis {
0031   public:
0032     //@{
0033     /**
0034         // The number of grid coordinates provided must be at least 2.
0035         // Coordinates will be sorted internally in the increasing order.
0036         */
0037     explicit GridAxis(const std::vector<double>& coords, bool useLogSpace = false);
0038     GridAxis(const std::vector<double>& coords, const char* label, bool useLogSpace = false);
0039     //@}
0040 
0041     //@{
0042     /** Basic accessor returning a parameter provided in the constructor */
0043     inline const std::vector<double>& coords() const { return coords_; }
0044     inline const std::string& label() const { return label_; }
0045     inline bool usesLogSpace() const { return useLogSpace_; }
0046     //@}
0047 
0048     /**
0049         // This method returns the grid interval number and
0050         // the weight of the point at the left side of the interval.
0051         // The weight will be set to 1 if the given coordinate coincides
0052         // with the grid point and will decay to 0 linearly as the
0053         // coordinate moves towards the next point on the right.
0054         //
0055         // The coordinates below the leftmost grid point are mapped
0056         // into the 0th interval with weight 1. The coordinates above
0057         // the rightmost grid point are mapped into the last interval
0058         // with weight 0 for the left point (it is expected that weight 1
0059         // will then be assigned to the right point).
0060         */
0061     std::pair<unsigned, double> getInterval(double coordinate) const;
0062 
0063     /**
0064         // This method returns the grid interval number and
0065         // the weight of the point at the left side of the interval.
0066         // The weight will be set to 1 if the given coordinate coincides
0067         // with the grid point and will decay to 0 linearly as the
0068         // coordinate moves towards the next point on the right.
0069         // The weight for the point on the right should be set to
0070         // one minus the weight on the left.
0071         //
0072         // The coordinates outside of grid boundaries will result in
0073         // weights which are less than zero or more than one. They
0074         // will be calculated by linear extrapolation from the closest
0075         // interval in the grid (i.e., leftmost or rightmost).
0076         */
0077     std::pair<unsigned, double> linearInterval(double coordinate) const;
0078 
0079     //@{
0080     /** Convenience accessor */
0081     inline unsigned nCoords() const { return npt_; }
0082     inline double coordinate(const unsigned i) const { return coords_.at(i); }
0083     inline double min() const { return coords_.front(); }
0084     inline double max() const { return coords_.back(); }
0085     inline double length() const { return coords_.back() - coords_.front(); }
0086     inline bool isUniform() const { return false; }
0087     inline unsigned nIntervals() const { return coords_.size() - 1; }
0088     inline double intervalWidth(const unsigned i = 0) const { return coords_.at(i + 1) - coords_.at(i); }
0089     //@}
0090 
0091     /** Compare two grids for equality */
0092     bool operator==(const GridAxis& r) const;
0093 
0094     /** Logical negation of operator== */
0095     inline bool operator!=(const GridAxis& r) const { return !(*this == r); }
0096 
0097     /**
0098         // Check for closeness of coordinates with another axis
0099         // within the given relative tolerance
0100         */
0101     bool isClose(const GridAxis& r, double tol) const;
0102 
0103     /** Modify the axis label */
0104     inline void setLabel(const char* newlabel) { label_ = newlabel ? newlabel : ""; }
0105 
0106     //@{
0107     /** Method related to "geners" I/O */
0108     inline gs::ClassId classId() const { return gs::ClassId(*this); }
0109     bool write(std::ostream& of) const;
0110     //@}
0111 
0112     static inline const char* classname() { return "npstat::GridAxis"; }
0113     static inline unsigned version() { return 2; }
0114     static GridAxis* read(const gs::ClassId& id, std::istream& in);
0115 
0116   private:
0117     void initialize();
0118 
0119     std::vector<double> coords_;
0120     std::vector<double> logs_;
0121     std::string label_;
0122     unsigned npt_;
0123     bool useLogSpace_;
0124 
0125     inline GridAxis() : npt_(0), useLogSpace_(false) {}
0126   };
0127 }  // namespace npstat
0128 
0129 #endif  // NPSTAT_GRIDAXIS_HH_