CMS 3D CMS Logo

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

float deriv (float x) const
 compute the derivate at x coordinate x More...
 
float eval (float x) const
 compute y coordinate at x coordinate x More...
 
float getArea () const
 total area (integral between 0 and 1) under curve More...
 
float integral (float 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

float 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 25 of file Spline.h.

Constructor & Destructor Documentation

◆ Spline() [1/3]

PhysicsTools::Spline::Spline ( )

Definition at line 58 of file Spline.cc.

58 : n(0), segments(nullptr), area(0.0) {}

◆ Spline() [2/3]

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

Definition at line 60 of file Spline.cc.

60  : n(orig.n), area(orig.area) {
61  segments = new Segment[n];
62  std::memcpy(segments, orig.segments, sizeof(Segment) * n);
63  }

References n, and segments.

◆ Spline() [3/3]

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

construct spline from n y coordinates in array vals

Definition at line 65 of file Spline.cc.

65 : n(0), segments(nullptr), area(0.0) { set(n_, vals); }

References set().

◆ ~Spline()

PhysicsTools::Spline::~Spline ( )

Definition at line 116 of file Spline.cc.

116 { delete[] segments; }

References segments.

Member Function Documentation

◆ deriv()

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

compute the derivate at x coordinate x

Definition at line 139 of file Spline.cc.

139  {
140  if (x < 0.0 || x > 1.0)
141  return 0.0;
142  else if (x == 0.0)
143  return segments[0].deriv(0.0);
144  else if (x == 1.0)
145  return segments[n - 1].deriv(1.0);
146 
147  float total;
148  float rest = std::modf(x * n, &total);
149 
150  return segments[(unsigned int)total].deriv(rest);
151  }

References PhysicsTools::Spline::Segment::deriv(), createfilelist::int, n, mergeAndRegister::rest, segments, and dqmMemoryStats::total.

◆ eval()

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

compute y coordinate at x coordinate x

Definition at line 127 of file Spline.cc.

127  {
128  if (x <= 0.0)
129  return segments[0].eval(0.0);
130  if (x >= 1.0)
131  return segments[n - 1].eval(1.0);
132 
133  float total;
134  float rest = std::modf(x * n, &total);
135 
136  return segments[(unsigned int)total].eval(rest);
137  }

References PhysicsTools::Spline::Segment::eval(), createfilelist::int, n, mergeAndRegister::rest, segments, and dqmMemoryStats::total.

◆ getArea()

float PhysicsTools::Spline::getArea ( ) const
inline

total area (integral between 0 and 1) under curve

Definition at line 49 of file Spline.h.

49 { return area; }

References area.

◆ integral()

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

compute integral under curve between 0 and x

Definition at line 153 of file Spline.cc.

153  {
154  if (x <= 0.0)
155  return 0.0;
156  if (x >= 1.0)
157  return 1.0;
158 
159  if (area < 1.0e-9)
160  return 0.0;
161 
162  float total;
163  float rest = std::modf(x * n, &total);
164 
165  return segments[(unsigned int)total].integral(rest) / area;
166  }

References area, MillePedeFileConverter_cfg::e, createfilelist::int, n, mergeAndRegister::rest, segments, and dqmMemoryStats::total.

◆ numberOfEntries()

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

return the number of entries

Definition at line 52 of file Spline.h.

52 { return n + 1; }

References n.

◆ operator=()

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

Definition at line 118 of file Spline.cc.

118  {
119  delete[] segments;
120  n = orig.n;
121  segments = new Segment[n];
122  std::memcpy(segments, orig.segments, sizeof(Segment) * n);
123  area = orig.area;
124  return *this;
125  }

References area, n, and segments.

◆ set()

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.

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

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

Referenced by Spline().

Member Data Documentation

◆ area

float PhysicsTools::Spline::area
private

Definition at line 67 of file Spline.h.

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

◆ n

unsigned int PhysicsTools::Spline::n
private

Definition at line 65 of file Spline.h.

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

◆ segments

Segment* PhysicsTools::Spline::segments
private

Definition at line 66 of file Spline.h.

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

mps_fire.i
i
Definition: mps_fire.py:355
PhysicsTools::Spline::area
float area
Definition: Spline.h:67
PhysicsTools::Spline::Segment::coeffs
float coeffs[4]
Definition: Spline.h:57
DDAxes::x
PhysicsTools::Spline::eval
float eval(float x) const
compute y coordinate at x coordinate x
Definition: Spline.cc:127
PhysicsTools::Spline::Segment::deriv
float deriv(float x) const
Definition: Spline.cc:34
PhysicsTools::Spline::integral
float integral(float x) const
compute integral under curve between 0 and x
Definition: Spline.cc:153
PhysicsTools::Spline::segments
Segment * segments
Definition: Spline.h:66
createfilelist.int
int
Definition: createfilelist.py:10
PhysicsTools::Spline::set
void set(unsigned int n, const double *vals)
initialize spline from n y coordinates in array vals
Definition: Spline.cc:67
mergeAndRegister.rest
rest
Definition: mergeAndRegister.py:121
PhysicsTools::Spline::deriv
float deriv(float x) const
compute the derivate at x coordinate x
Definition: Spline.cc:139
PhysicsTools::Spline::n
unsigned int n
Definition: Spline.h:65
dqmMemoryStats.total
total
Definition: dqmMemoryStats.py:152
PhysicsTools::Spline::Segment::eval
float eval(float x) const
Definition: Spline.cc:21
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37