CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Classes | Public Member Functions | Private Attributes
PhysicsTools::Spline Class Reference

A simple class for cubic splines. More...

#include <Spline.h>

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

54  : n(0), segments(0), area(0.0)
55 {}
unsigned int n
Definition: Spline.h:66
Segment * segments
Definition: Spline.h:67
PhysicsTools::Spline::Spline ( const Spline orig)

Definition at line 57 of file Spline.cc.

References n, and segments.

57  : n(orig.n), area(orig.area)
58 {
59  segments = new Segment[n];
60  std::memcpy(segments, orig.segments, sizeof(Segment) * n);
61 }
unsigned int n
Definition: Spline.h:66
Segment * segments
Definition: Spline.h:67
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.

References set().

63  :
64  n(0), segments(0), area(0.0)
65 { set(n_, vals); }
unsigned int n
Definition: Spline.h:66
void set(unsigned int n, const double *vals)
initialize spline from n y coordinates in array vals
Definition: Spline.cc:67
Segment * segments
Definition: Spline.h:67
PhysicsTools::Spline::~Spline ( )

Definition at line 117 of file Spline.cc.

References segments.

118 {
119  delete[] segments;
120 }
Segment * segments
Definition: Spline.h:67

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.

146 {
147  if (x < 0.0 || x > 1.0)
148  return 0.0;
149  else if (x == 0.0)
150  return segments[0].deriv(0.0);
151  else if (x == 1.0)
152  return segments[n - 1].deriv(1.0);
153 
154  double total;
155  double rest = std::modf(x * n, &total);
156 
157  return segments[(unsigned int)total].deriv(rest);
158 }
unsigned int n
Definition: Spline.h:66
double deriv(double x) const
compute the derivate at x coordinate x
Definition: Spline.cc:145
double deriv(double x) const
Definition: Spline.cc:33
Definition: DDAxes.h:10
Segment * segments
Definition: Spline.h:67
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.

133 {
134  if (x <= 0.0)
135  return segments[0].eval(0.0);
136  if (x >= 1.0)
137  return segments[n - 1].eval(1.0);
138 
139  double total;
140  double rest = std::modf(x * n, &total);
141 
142  return segments[(unsigned int)total].eval(rest);
143 }
unsigned int n
Definition: Spline.h:66
double eval(double x) const
Definition: Spline.cc:22
double eval(double x) const
compute y coordinate at x coordinate x
Definition: Spline.cc:132
Definition: DDAxes.h:10
Segment * segments
Definition: Spline.h:67
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.

50 { 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.

161 {
162  if (x <= 0.0)
163  return 0.0;
164  if (x >= 1.0)
165  return 1.0;
166 
167  if (area < 1.0e-9)
168  return 0.0;
169 
170  double total;
171  double rest = std::modf(x * n, &total);
172 
173  return segments[(unsigned int)total].integral(rest) / area;
174 }
unsigned int n
Definition: Spline.h:66
double integral(double x) const
compute integral under curve between 0 and x
Definition: Spline.cc:160
Definition: DDAxes.h:10
Segment * segments
Definition: Spline.h:67
unsigned int PhysicsTools::Spline::numberOfEntries ( ) const
inline

return the number of entries

Definition at line 53 of file Spline.h.

53 { return n + 1; }
unsigned int n
Definition: Spline.h:66
Spline & PhysicsTools::Spline::operator= ( const Spline orig)

Definition at line 122 of file Spline.cc.

References area, n, and segments.

123 {
124  delete[] segments;
125  n = orig.n;
126  segments = new Segment[n];
127  std::memcpy(segments, orig.segments, sizeof(Segment) * n);
128  area = orig.area;
129  return *this;
130 }
unsigned int n
Definition: Spline.h:66
Segment * segments
Definition: Spline.h:67
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.

Referenced by betterConfigParser.BetterConfigParser::getGeneral(), and Spline().

68 {
69  n = n_ - 1;
70  area = 0.0;
71 
72  delete[] segments;
73  segments = new Segment[n];
74 
75  if (n == 1) {
76  Segment *seg = &segments[0];
77  seg->coeffs[0] = vals[0];
78  seg->coeffs[1] = vals[1] - vals[0];
79  seg->coeffs[2] = 0.0;
80  seg->coeffs[3] = 0.0;
81  seg->area = 0.0;
82  area = seg->integral(1.0);
83  return;
84  }
85 
86  double m0, m1;
87  Segment *seg = &segments[0];
88  m0 = 0.0, m1 = 0.5 * (vals[2] - vals[0]);
89  seg->coeffs[0] = vals[0];
90  seg->coeffs[1] = -2.0 * vals[0] + 2.0 * vals[1] - m1;
91  seg->coeffs[2] = vals[0] - vals[1] + m1;
92  seg->coeffs[3] = 0.0;
93  seg->area = 0.0;
94  area = seg->integral(1.0);
95  m0 = m1;
96  seg++, vals++;
97 
98  for(unsigned int i = 1; i < n - 1; i++, seg++, vals++) {
99  m1 = 0.5 * (vals[2] - vals[0]);
100  seg->coeffs[0] = vals[0];
101  seg->coeffs[1] = m0;
102  seg->coeffs[2] = -3.0 * vals[0] - 2.0 * m0 + 3.0 * vals[1] - m1;
103  seg->coeffs[3] = 2.0 * vals[0] + m0 - 2.0 * vals[1] + m1;
104  seg->area = area;
105  area = seg->integral(1.0);
106  m0 = m1;
107  }
108 
109  seg->coeffs[0] = vals[0];
110  seg->coeffs[1] = m0;
111  seg->coeffs[2] = - vals[0] - m0 + vals[1];
112  seg->coeffs[3] = 0.0;
113  seg->area = area;
114  area = seg->integral(1.0);
115 }
int i
Definition: DBlmapReader.cc:9
unsigned int n
Definition: Spline.h:66
Segment * segments
Definition: Spline.h:67

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().

Segment* PhysicsTools::Spline::segments
private

Definition at line 67 of file Spline.h.

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