CMS 3D CMS Logo

PhysicsTools::Spline Class Reference

A simple class for cubic splines. More...

#include <PhysicsTools/MVAComputer/interface/Spline.h>

List of all members.

Public Member Functions

double eval (double x) const
 compute y coordinate at x coordinate x
double getArea () const
 total area (integral between 0 and 1) under curve
double integral (double x) const
 compute integral under curve between 0 and x
unsigned int numberOfEntries () const
 return the number of entries
Splineoperator= (const Spline &orig)
void set (unsigned int n, const double *vals)
 initialize spline from n y coordinates in array vals
 Spline (unsigned int n, const double *vals)
 construct spline from n y coordinates in array vals
 Spline (const Spline &orig)
 Spline ()
 ~Spline ()

Private Attributes

double area
unsigned int n
Segmentsegments

Classes

struct  Segment
 internal class describing a "segment" (between two x points) More...


Detailed Description

A simple class for cubic splines.

This class implements cubic splines for n equidistant points in x between 0 and 1. It is constructed from an array of n y coordinates and can compute the interpolated y coordinate for a given x.

Definition at line 26 of file Spline.h.


Constructor & Destructor Documentation

PhysicsTools::Spline::Spline (  ) 

Definition at line 44 of file Spline.cc.

00044                : n(0), segments(0), area(0.0)
00045 {}

PhysicsTools::Spline::Spline ( const Spline orig  ) 

Definition at line 47 of file Spline.cc.

References n, and segments.

00047                                  : n(orig.n), area(orig.area)
00048 {
00049         segments = new Segment[n];
00050         std::memcpy(segments, orig.segments, sizeof(Segment) * n);
00051 }

PhysicsTools::Spline::Spline ( unsigned int  n,
const double *  vals 
)

construct spline from n y coordinates in array vals

Definition at line 53 of file Spline.cc.

00053                                                   :
00054         n(0), segments(0), area(0.0)
00055 { set(n_, vals); }

PhysicsTools::Spline::~Spline (  ) 

Definition at line 107 of file Spline.cc.

References segments.

00108 {
00109         delete[] segments;
00110 }


Member Function Documentation

double PhysicsTools::Spline::eval ( double  x  )  const

compute y coordinate at x coordinate x

Definition at line 122 of file Spline.cc.

References PhysicsTools::Spline::Segment::eval(), int, n, and segments.

00123 {
00124         if (x <= 0.0)
00125                 return segments[0].eval(0.0);
00126         if (x >= 1.0)
00127                 return segments[n - 1].eval(1.0);
00128 
00129         double total;
00130         double rest = std::modf(x * n, &total);
00131 
00132         return segments[(unsigned int)total].eval(rest);
00133 }

double PhysicsTools::Spline::getArea (  )  const [inline]

total area (integral between 0 and 1) under curve

Definition at line 47 of file Spline.h.

References area.

00047 { return area; }

double PhysicsTools::Spline::integral ( double  x  )  const

compute integral under curve between 0 and x

Definition at line 135 of file Spline.cc.

References area, e, int, n, and segments.

00136 {
00137         if (x <= 0.0)
00138                 return 0.0;
00139         if (x >= 1.0)
00140                 return 1.0;
00141 
00142         if (area < 1.0e-9)
00143                 return 0.0;
00144 
00145         double total;
00146         double rest = std::modf(x * n, &total);
00147 
00148         return segments[(unsigned int)total].integral(rest) / area;
00149 }

unsigned int PhysicsTools::Spline::numberOfEntries (  )  const [inline]

return the number of entries

Definition at line 50 of file Spline.h.

00050 { return n + 1; }

Spline & PhysicsTools::Spline::operator= ( const Spline orig  ) 

Definition at line 112 of file Spline.cc.

References area, n, and segments.

00113 {
00114         delete[] segments;
00115         n = orig.n;
00116         segments = new Segment[n];
00117         std::memcpy(segments, orig.segments, sizeof(Segment) * n);
00118         area = orig.area;
00119         return *this;
00120 }

void PhysicsTools::Spline::set ( unsigned int  n,
const double *  vals 
)

initialize spline from n y coordinates in array vals

Definition at line 57 of file Spline.cc.

References PhysicsTools::Spline::Segment::area, area, PhysicsTools::Spline::Segment::coeffs, i, PhysicsTools::Spline::Segment::integral(), m1, n, seg, and segments.

00058 {
00059         n = n_ - 1;
00060         area = 0.0;
00061 
00062         delete[] segments;
00063         segments = new Segment[n];
00064 
00065         if (n == 1) {
00066                 Segment *seg = &segments[0];
00067                 seg->coeffs[0] = vals[0];
00068                 seg->coeffs[1] = vals[1] - vals[0];
00069                 seg->coeffs[2] = 0.0;
00070                 seg->coeffs[3] = 0.0;
00071                 seg->area = 0.0;
00072                 area = seg->integral(1.0);
00073                 return;
00074         }
00075 
00076         double m0, m1;
00077         Segment *seg = &segments[0];
00078         m0 = 0.0, m1 = 0.5 * (vals[2] - vals[0]);
00079         seg->coeffs[0] = vals[0];
00080         seg->coeffs[1] = -2.0 * vals[0] + 2.0 * vals[1] - m1;
00081         seg->coeffs[2] = vals[0] - vals[1] + m1;
00082         seg->coeffs[3] = 0.0;
00083         seg->area = 0.0;
00084         area = seg->integral(1.0);
00085         m0 = m1;
00086         seg++, vals++;
00087 
00088         for(unsigned int i = 1; i < n - 1; i++, seg++, vals++) {
00089                 m1 = 0.5 * (vals[2] - vals[0]);
00090                 seg->coeffs[0] = vals[0];
00091                 seg->coeffs[1] = m0;
00092                 seg->coeffs[2] = -3.0 * vals[0] - 2.0 * m0 + 3.0 * vals[1] - m1;
00093                 seg->coeffs[3] = 2.0 * vals[0] + m0 - 2.0 * vals[1] + m1;
00094                 seg->area = area;
00095                 area = seg->integral(1.0);
00096                 m0 = m1;
00097         }
00098 
00099         seg->coeffs[0] = vals[0];
00100         seg->coeffs[1] = m0;
00101         seg->coeffs[2] = - vals[0] - m0 + vals[1];
00102         seg->coeffs[3] = 0.0;
00103         seg->area = area;
00104         area = seg->integral(1.0);
00105 }


Member Data Documentation

double PhysicsTools::Spline::area [private]

Definition at line 64 of file Spline.h.

Referenced by getArea(), integral(), operator=(), and set().

unsigned int PhysicsTools::Spline::n [private]

Definition at line 62 of file Spline.h.

Referenced by eval(), integral(), operator=(), set(), and Spline().

Segment* PhysicsTools::Spline::segments [private]

Definition at line 63 of file Spline.h.

Referenced by eval(), integral(), operator=(), set(), Spline(), and ~Spline().


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:50:14 2009 for CMSSW by  doxygen 1.5.4