CMS 3D CMS Logo

Grid1D.h

Go to the documentation of this file.
00001 #ifndef Grid1D_H
00002 #define Grid1D_H
00003 
00004 template <class T>
00005 class Grid1D {
00006 public:
00007 
00008   typedef T Scalar;
00009 
00010   Grid1D() {}
00011 
00012   Grid1D( Scalar lower, Scalar upper, int nodes) : 
00013     lower_(lower), upper_(upper), nodes_(nodes) {
00014     step_ = (upper - lower) / (nodes-1);
00015   }
00016 
00017 
00018   Scalar step() const {return step_;}
00019   Scalar lower() const {return lower_;}
00020   Scalar upper() const {return upper_;}
00021   int nodes() const {return nodes_;}
00022   int cells() const {return nodes()-1;}
00023 
00024   Scalar node( int i) const { return i*step() + lower();}
00025 
00026   Scalar closestNode( Scalar a) const {
00027     Scalar b = (a-lower())/step();
00028     Scalar c = floor(b);
00029     Scalar tmp = (b-c < 0.5) ? std::max(c,0.) : std::min(c+1.,static_cast<Scalar>(nodes()-1));
00030     return tmp*step()+lower();
00031   }
00032 
00034   int index( Scalar a) const {
00035     int ind = static_cast<int>((a-lower())/step());
00036     // FIXME: this causes an exception to be thrown later. Should be tested
00037     // more carefully before release
00038     //  if (ind < -1 || ind > cells()) {
00039     //     std::cout << "**** ind = " << ind << " cells: " << cells() << std::endl;
00040     //    return -1;
00041     //  }
00042     return std::max(0, std::min( cells()-1, ind));
00043   }
00044 
00045 private:
00046 
00047   Scalar step_;
00048   Scalar lower_;
00049   Scalar upper_;
00050   int    nodes_;
00051 
00052 };
00053 
00054 #endif

Generated on Tue Jun 9 17:40:34 2009 for CMSSW by  doxygen 1.5.4