CMS 3D CMS Logo

Classes | Public Member Functions | Private Attributes

PhysicsTools::Spline Class Reference

A simple class for cubic splines. More...

#include <Spline.h>

List of all members.

Classes

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

Public Member Functions

double deriv (double x) const
 compute the derivate at x coordinate x
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 ()
 Spline (const Spline &orig)
 Spline (unsigned int n, const double *vals)
 construct spline from n y coordinates in array vals
 ~Spline ()

Private Attributes

double area
unsigned int n
Segmentsegments

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 54 of file Spline.cc.

               : n(0), segments(0), area(0.0)
{}
PhysicsTools::Spline::Spline ( const Spline orig)

Definition at line 57 of file Spline.cc.

References n, and segments.

                                 : n(orig.n), area(orig.area)
{
        segments = new Segment[n];
        std::memcpy(segments, orig.segments, sizeof(Segment) * n);
}
PhysicsTools::Spline::Spline ( unsigned int  n,
const double *  vals 
)

construct spline from n y coordinates in array vals

Definition at line 63 of file Spline.cc.

                                                  :
        n(0), segments(0), area(0.0)
{ set(n_, vals); }
PhysicsTools::Spline::~Spline ( )

Definition at line 117 of file Spline.cc.

References segments.

{
        delete[] segments;
}

Member Function Documentation

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

compute the derivate at x coordinate x

Definition at line 145 of file Spline.cc.

References PhysicsTools::Spline::Segment::deriv(), n, segments, and pileupDistInMC::total.

{
        if (x < 0.0 || x > 1.0)
                return 0.0;
        else if (x == 0.0)
                return segments[0].deriv(0.0);
        else if (x == 1.0)
                return segments[n - 1].deriv(1.0);

        double total;
        double rest = std::modf(x * n, &total);

        return segments[(unsigned int)total].deriv(rest);
}
double PhysicsTools::Spline::eval ( double  x) const

compute y coordinate at x coordinate x

Definition at line 132 of file Spline.cc.

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

{
        if (x <= 0.0)
                return segments[0].eval(0.0);
        if (x >= 1.0)
                return segments[n - 1].eval(1.0);

        double total;
        double rest = std::modf(x * n, &total);

        return segments[(unsigned int)total].eval(rest);
}
double PhysicsTools::Spline::getArea ( ) const [inline]

total area (integral between 0 and 1) under curve

Definition at line 50 of file Spline.h.

References area.

{ return area; }
double PhysicsTools::Spline::integral ( double  x) const

compute integral under curve between 0 and x

Definition at line 160 of file Spline.cc.

References area, alignCSCRings::e, n, segments, and pileupDistInMC::total.

{
        if (x <= 0.0)
                return 0.0;
        if (x >= 1.0)
                return 1.0;

        if (area < 1.0e-9)
                return 0.0;

        double total;
        double rest = std::modf(x * n, &total);

        return segments[(unsigned int)total].integral(rest) / area;
}
unsigned int PhysicsTools::Spline::numberOfEntries ( ) const [inline]

return the number of entries

Definition at line 53 of file Spline.h.

{ return n + 1; }
Spline & PhysicsTools::Spline::operator= ( const Spline orig)

Definition at line 122 of file Spline.cc.

References area, n, and segments.

{
        delete[] segments;
        n = orig.n;
        segments = new Segment[n];
        std::memcpy(segments, orig.segments, sizeof(Segment) * n);
        area = orig.area;
        return *this;
}
void PhysicsTools::Spline::set ( unsigned int  n,
const double *  vals 
)

initialize spline from n y coordinates in array vals

Definition at line 67 of file Spline.cc.

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

{
        n = n_ - 1;
        area = 0.0;

        delete[] segments;
        segments = new Segment[n];

        if (n == 1) {
                Segment *seg = &segments[0];
                seg->coeffs[0] = vals[0];
                seg->coeffs[1] = vals[1] - vals[0];
                seg->coeffs[2] = 0.0;
                seg->coeffs[3] = 0.0;
                seg->area = 0.0;
                area = seg->integral(1.0);
                return;
        }

        double m0, m1;
        Segment *seg = &segments[0];
        m0 = 0.0, m1 = 0.5 * (vals[2] - vals[0]);
        seg->coeffs[0] = vals[0];
        seg->coeffs[1] = -2.0 * vals[0] + 2.0 * vals[1] - m1;
        seg->coeffs[2] = vals[0] - vals[1] + m1;
        seg->coeffs[3] = 0.0;
        seg->area = 0.0;
        area = seg->integral(1.0);
        m0 = m1;
        seg++, vals++;

        for(unsigned int i = 1; i < n - 1; i++, seg++, vals++) {
                m1 = 0.5 * (vals[2] - vals[0]);
                seg->coeffs[0] = vals[0];
                seg->coeffs[1] = m0;
                seg->coeffs[2] = -3.0 * vals[0] - 2.0 * m0 + 3.0 * vals[1] - m1;
                seg->coeffs[3] = 2.0 * vals[0] + m0 - 2.0 * vals[1] + m1;
                seg->area = area;
                area = seg->integral(1.0);
                m0 = m1;
        }

        seg->coeffs[0] = vals[0];
        seg->coeffs[1] = m0;
        seg->coeffs[2] = - vals[0] - m0 + vals[1];
        seg->coeffs[3] = 0.0;
        seg->area = area;
        area = seg->integral(1.0);
}

Member Data Documentation

double PhysicsTools::Spline::area [private]

Definition at line 68 of file Spline.h.

Referenced by getArea(), integral(), PhysicsTools::Spline::Segment::integral(), operator=(), and set().

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

Definition at line 66 of file Spline.h.

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

Definition at line 67 of file Spline.h.

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