# Project CMSSW displayed by LXR

File indexing completed on 2024-04-06 12:22:31

```0001 #ifndef Grid1D_H
0002 #define Grid1D_H
0003 #include <cmath>
0004 #include <algorithm>
0005 #include "FWCore/Utilities/interface/Visibility.h"
0006
0007 class dso_internal Grid1D {
0008 public:
0009   typedef float Scalar;
0010   //  typedef double Scalar;
0011
0012   Grid1D() {}
0013
0014   Grid1D(Scalar lower, Scalar upper, int nodes) : lower_(lower), upper_(upper), edges_(nodes - 2) {
0015     stepinv_ = (nodes - 1) / (upper - lower);
0016   }
0017
0018   Scalar step() const { return 1. / stepinv_; }
0019   Scalar lower() const { return lower_; }
0020   Scalar upper() const { return upper_; }
0021   int nodes() const { return edges_ + 2; }
0022   int cells() const { return edges_ + 1; }
0023
0024   Scalar node(int i) const { return i * step() + lower(); }
0025
0026   bool inRange(int i) const { return i >= 0 && i <= edges_; }
0027
0028   // return index and fractional part...
0029   int index(Scalar a, Scalar& f) const {
0030     Scalar b;
0031     f = modff((a - lower()) * stepinv_, &b);
0032     return b;
0033   }
0034
0035   // move index and fraction in range..
0036   void normalize(int& ind, Scalar& f) const {
0037     if (ind < 0) {
0038       f -= ind;
0039       ind = 0;
0040     } else if (ind > edges_) {
0041       f += ind - edges_;
0042       ind = edges_;
0043     }
0044   }
0045
0046   Scalar closestNode(Scalar a) const {
0047     Scalar b = (a - lower()) / step();
0048     Scalar c = floor(b);
0049     Scalar tmp = (b - c < 0.5) ? std::max(c, 0.f) : std::min(c + 1.f, static_cast<Scalar>(nodes() - 1));
0050     return tmp * step() + lower();
0051   }
0052
0053   /// returns valid index, or -1 if the value is outside range +/- one cell.
0054   int index(Scalar a) const {
0055     int ind = static_cast<int>((a - lower()) / step());
0056     // FIXME: this causes an exception to be thrown later. Should be tested
0057     // more carefully before release
0058     //  if (ind < -1 || ind > cells()) {
0059     //     std::cout << "**** ind = " << ind << " cells: " << cells() << std::endl;
0060     //    return -1;
0061     //  }
0062     return std::max(0, std::min(cells() - 1, ind));
0063   }
0064
0065 private:
0066   Scalar stepinv_;
0067   Scalar lower_;
0068   Scalar upper_;
0069   int edges_;  // number of lower edges = nodes-2...
0070 };
0071
0072 #endif```