CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/MagneticField/Interpolation/src/Grid1D.h

Go to the documentation of this file.
00001 #ifndef Grid1D_H
00002 #define Grid1D_H
00003 #include <cmath>
00004 #include <algorithm>
00005 #include "FWCore/Utilities/interface/Visibility.h"
00006 
00007 class dso_internal Grid1D {
00008 public:
00009 
00010   typedef float   Scalar;
00011   //  typedef double Scalar;
00012 
00013   Grid1D() {}
00014 
00015   Grid1D( Scalar lower, Scalar upper, int nodes) : 
00016     lower_(lower), upper_(upper), edges_(nodes-2) {
00017     stepinv_ =  (nodes-1)/(upper - lower);
00018   }
00019 
00020 
00021   Scalar step() const {return 1./stepinv_;}
00022   Scalar lower() const {return lower_;}
00023   Scalar upper() const {return upper_;}
00024   int nodes() const {return  edges_+2;}
00025   int cells() const {return  edges_+1;}
00026 
00027   Scalar node( int i) const { return i*step() + lower();}
00028 
00029   bool inRange(int i) const {
00030     return i>=0 && i<=edges_;
00031   }
00032 
00033   // return index and fractional part...
00034   int index(Scalar a, Scalar & f) const {
00035     Scalar b;
00036     f = modff((a-lower())*stepinv_, &b);
00037     return b;
00038   }
00039 
00040   // move index and fraction in range..
00041   void normalize(int & ind,  Scalar & f) const {
00042     if (ind<0) {
00043       f -= ind;
00044       ind = 0;
00045     }
00046     else if (ind>edges_) {
00047       f += ind-edges_;
00048       ind = edges_;
00049     }
00050   }
00051 
00052 
00053   Scalar closestNode( Scalar a) const {
00054     Scalar b = (a-lower())/step();
00055     Scalar c = floor(b);
00056     Scalar tmp = (b-c < 0.5) ? std::max(c,0.f) : std::min(c+1.f,static_cast<Scalar>(nodes()-1));
00057     return tmp*step()+lower();
00058   }
00059 
00061   int index( Scalar a) const {
00062     int ind = static_cast<int>((a-lower())/step());
00063     // FIXME: this causes an exception to be thrown later. Should be tested
00064     // more carefully before release
00065     //  if (ind < -1 || ind > cells()) {
00066     //     std::cout << "**** ind = " << ind << " cells: " << cells() << std::endl;
00067     //    return -1;
00068     //  }
00069     return std::max(0, std::min( cells()-1, ind));
00070   }
00071 
00072  
00073 private:
00074 
00075   Scalar stepinv_;
00076   Scalar lower_;
00077   Scalar upper_;
00078   int    edges_; // number of lower edges = nodes-2...
00079 
00080 };
00081 
00082 #endif