Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:10:41

0001 #ifndef NPSTAT_LINEARMAPPER1D_HH_
0002 #define NPSTAT_LINEARMAPPER1D_HH_
0003 
0004 /*!
0005 // \file LinearMapper1d.h
0006 //
0007 // \brief Linear transformation functor
0008 //
0009 // Author: I. Volobouev
0010 //
0011 // October 2009
0012 */
0013 
0014 #include "JetMETCorrections/InterpolationTables/interface/NpstatException.h"
0015 
0016 namespace npstat {
0017   /** Functor which performs linear mapping in 1-d */
0018   class LinearMapper1d {
0019   public:
0020     /** Default constructor builds an identity transformation */
0021     inline LinearMapper1d() : a_(1.0), b_(0.0) {}
0022 
0023     /**
0024         // Transform definition from two points. The point at x0
0025         // is mapped into y0, the point at x1 is mapped into y1.
0026         // The linear transformation is thus fully defined.
0027         */
0028     inline LinearMapper1d(const double x0, const double y0, const double x1, const double y1) {
0029       const double dx = x1 - x0;
0030       if (!dx)
0031         throw npstat::NpstatInvalidArgument(
0032             "In npstat::LinearMapper1d constructor: "
0033             "invalid arguments (x0 == x1)");
0034       a_ = (y1 - y0) / dx;
0035       b_ = ((y0 + y1) - a_ * (x0 + x1)) / 2.0;
0036     }
0037 
0038     /** Explicitly provide the transform coefficients as in y = ca*x + cb */
0039     inline LinearMapper1d(const double ca, const double cb) : a_(ca), b_(cb) {}
0040 
0041     /** Perform the transformation */
0042     inline double operator()(const double& x) const { return a_ * x + b_; }
0043 
0044     /** Get the linear coefficient of the transform */
0045     inline double a() const { return a_; }
0046 
0047     /** Get the transform constant */
0048     inline double b() const { return b_; }
0049 
0050     /** Create the inverse transform */
0051     inline LinearMapper1d inverse() const {
0052       if (!a_)
0053         throw npstat::NpstatInvalidArgument(
0054             "In npstat::LinearMapper1d::inverse: "
0055             "mapping is not invertible");
0056       return LinearMapper1d(1.0 / a_, -b_ / a_);
0057     }
0058 
0059     /** Sequence of two transforms: the one on the right is applied first */
0060     inline LinearMapper1d operator*(const LinearMapper1d& r) const { return LinearMapper1d(a_ * r.a_, a_ * r.b_ + b_); }
0061 
0062   private:
0063     double a_;
0064     double b_;
0065   };
0066 }  // namespace npstat
0067 
0068 #endif  // NPSTAT_LINEARMAPPER1D_HH_