MINT2
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
SplineGenerator Class Reference

#include <SplineGenerator.h>

Classes

struct  BinInfo
 

Public Member Functions

 SplineGenerator (TRandom3 *rndm, const TSpline3 &spline)
 
double integral () const
 
double mean () const
 
double gen_random () const
 
double integral (double xmin, double xmax)
 
const TSpline3 & spline () const
 

Private Member Functions

double partial_integral (int i, double x)
 
double integral (int i, double xmin, double xmax)
 
std::pair< double, double > turning_points (int i) const
 
double mean_part_integral (int i, double x)
 
double mean (int i, double xmin, double xmax)
 

Private Attributes

TRandom3 * m_rndm
 
TSpline3 m_spline
 
double m_integral
 
double m_boxintegral
 
double m_mean
 
std::vector< BinInfom_bins
 

Detailed Description

Definition at line 9 of file SplineGenerator.h.

Constructor & Destructor Documentation

◆ SplineGenerator()

SplineGenerator::SplineGenerator ( TRandom3 *  rndm,
const TSpline3 &  spline 
)

Definition at line 7 of file SplineGenerator.cpp.

7  :
8  m_rndm(rndm),
10  m_integral(0.),
11  m_boxintegral(0.),
12  m_mean(0.),
13  m_bins(spline.GetNp())
14 {
15  BinInfo ibin ;
16  double xmin(0.), a(0.), b(0.), c(0.), d(0.) ;
17  int i = m_spline.GetNp() - 1 ;
18  // Get info on the highest knot in the spline.
19  m_spline.GetCoeff(i, xmin, a, b, c, d) ;
20  ibin.xmin = xmin ;
21  ibin.xmax = 1e30 ;
22  ibin.integral = 0. ;
23  ibin.ymin = 0. ;
24  ibin.ymax = 1e30 ;
25  m_bins[i] = ibin ;
26  --i ;
27  // Loop backwards over knots, so we can use the x value of the knot above
28  // to set the range.
29  for( ; i >= 0 ; --i){
30  m_spline.GetCoeff(i, xmin, a, b, c, d) ;
31  ibin.xmin = xmin ;
32  ibin.xmax = m_bins[i+1].xmin ;
33  // cout << "ibin " << i << " xmin " << ibin.xmin << " xmax " << ibin.xmax << endl ;
34  // cout << "a " << a << " b " << b << " c " << c << " d " << d << endl ;
35  ibin.integral = integral(i, ibin.xmin, ibin.xmax) ;
36  // cout << "integral " << ibin.integral << endl ;
37  m_integral += ibin.integral ;
38  pair<double, double> tps = turning_points(i) ;
39  // cout << "tps.first " << tps.first << " tps.second " << tps.second << endl ;
40  tps.first = max(min(tps.first, ibin.xmax), ibin.xmin) ;
41  tps.second = max(min(tps.second, ibin.xmax), ibin.xmin) ;
42  // cout << "tps.first " << tps.first << " tps.second " << tps.second << endl ;
43  ibin.ymax = max(
44  max(
45  max(m_spline.Eval(ibin.xmin),
46  m_spline.Eval(ibin.xmax)),
47  m_spline.Eval(tps.first)),
48  m_spline.Eval(tps.second)) ;
49  ibin.ymin = min(
50  min(
51  min(m_spline.Eval(ibin.xmin),
52  m_spline.Eval(ibin.xmax)),
53  m_spline.Eval(tps.first)),
54  m_spline.Eval(tps.second)) ;
55  if(ibin.ymin < 0.){
56  cerr << "SplineGenerator ERROR: ymin < 0. (" << ibin.ymin << ") for bin " << i << endl ;
57  }
58  ibin.boxintegral = ibin.ymax * (ibin.xmax - ibin.xmin) ;
59  // cout << "ymax " << ibin.ymax << " ymin " << ibin.ymin << " boxintegral " << ibin.boxintegral << endl ;
60  m_boxintegral += ibin.boxintegral ;
61  m_bins[i] = ibin ;
62  m_mean += mean(i, ibin.xmin, ibin.xmax) ;
63  }
64  if(m_integral != 0.)
65  m_mean /= m_integral ;
66 }
double mean() const
double integral() const
std::pair< double, double > turning_points(int i) const
std::vector< BinInfo > m_bins
const TSpline3 & spline() const
TRandom3 * m_rndm

Member Function Documentation

◆ gen_random()

double SplineGenerator::gen_random ( ) const

Definition at line 79 of file SplineGenerator.cpp.

79  {
80  while(true){
81  double boxsel = m_rndm->Rndm() * m_boxintegral ;
82  double boxsum(0.) ;
83  vector<BinInfo>::const_iterator ibin = m_bins.begin() ;
84  for(; ibin != m_bins.end() ; ++ibin){
85  boxsum += ibin->boxintegral ;
86  if(boxsum >= boxsel)
87  break ;
88  }
89  double x = m_rndm->Rndm() * (ibin->xmax - ibin->xmin) + ibin->xmin ;
90  if(m_rndm->Rndm() * ibin->ymax < m_spline.Eval(x))
91  return x ;
92  }
93 }
std::vector< BinInfo > m_bins
TRandom3 * m_rndm

◆ integral() [1/3]

double SplineGenerator::integral ( ) const

Definition at line 69 of file SplineGenerator.cpp.

69  {
70  return m_integral ;
71 }

◆ integral() [2/3]

double SplineGenerator::integral ( double  xmin,
double  xmax 
)

Definition at line 96 of file SplineGenerator.cpp.

96  {
97  int istart = m_spline.FindX(xmin) ;
98  int iend = m_spline.FindX(xmax) ;
99  double _integral = integral(istart, xmin, m_bins[istart].xmax)
100  + integral(iend, m_bins[iend].xmin, xmax) ;
101  for(int ibin = istart + 1 ; ibin != iend ; ++ibin)
102  _integral += m_bins[ibin].integral ;
103  return _integral ;
104 }
double integral() const
std::vector< BinInfo > m_bins

◆ integral() [3/3]

double SplineGenerator::integral ( int  i,
double  xmin,
double  xmax 
)
private

Definition at line 120 of file SplineGenerator.cpp.

120  {
121  return partial_integral(i, xmax) - partial_integral(i, xmin) ;
122 }
double partial_integral(int i, double x)

◆ mean() [1/2]

double SplineGenerator::mean ( ) const

Definition at line 74 of file SplineGenerator.cpp.

74  {
75  return m_mean ;
76 }

◆ mean() [2/2]

double SplineGenerator::mean ( int  i,
double  xmin,
double  xmax 
)
private

Definition at line 149 of file SplineGenerator.cpp.

149  {
150  return mean_part_integral(i, xmax) - mean_part_integral(i, xmin) ;
151 }
double mean_part_integral(int i, double x)

◆ mean_part_integral()

double SplineGenerator::mean_part_integral ( int  i,
double  x 
)
private

Definition at line 140 of file SplineGenerator.cpp.

140  {
141  double xmin(0.), a(0.), b(0.), c(0.), d(0.) ;
142  m_spline.GetCoeff(i, xmin, a, b, c, d) ;
143  double xshift = x - xmin ;
144  return x * xshift * (a + xshift * (b/2. + xshift * (c/3. + xshift * d/4.)))
145  - xshift * xshift * (a/2. + xshift * (b/6. + xshift * (c/12. + xshift * d/20.))) ;
146 }

◆ partial_integral()

double SplineGenerator::partial_integral ( int  i,
double  x 
)
private

Definition at line 112 of file SplineGenerator.cpp.

112  {
113  double xmin(0.), a(0.), b(0.), c(0.), d(0.) ;
114  m_spline.GetCoeff(i, xmin, a, b, c, d) ;
115  x -= xmin ;
116  return x * (a + x * (b/2. + x * (c/3. + x * d/4.))) ;
117 }

◆ spline()

const TSpline3 & SplineGenerator::spline ( ) const

Definition at line 107 of file SplineGenerator.cpp.

107  {
108  return m_spline ;
109 }

◆ turning_points()

pair< double, double > SplineGenerator::turning_points ( int  i) const
private

Definition at line 125 of file SplineGenerator.cpp.

125  {
126  // Note the reversal of coefficient labelling, as the standard
127  // quadratic formula is a x^2 + b x + c while TSplinePoly
128  // uses a + b x + c x^2 + d x^3.
129  double xmin(0.), d(0.), c(0.), b(0.), a(0.) ;
130  m_spline.GetCoeff(i, xmin, d, c, b, a) ;
131  b *= 2. ;
132  a *= 3. ;
133  double arg = b*b - 4. * a * c ;
134  if(arg < 0.)
135  return pair<double, double>(xmin-1e30, xmin-1e30) ;
136  return pair<double, double>(xmin + (-b - sqrt(arg))/2./a, xmin + (-b + sqrt(arg))/2./a) ;
137 }

Member Data Documentation

◆ m_bins

std::vector<BinInfo> SplineGenerator::m_bins
private

Definition at line 25 of file SplineGenerator.h.

◆ m_boxintegral

double SplineGenerator::m_boxintegral
private

Definition at line 23 of file SplineGenerator.h.

◆ m_integral

double SplineGenerator::m_integral
private

Definition at line 22 of file SplineGenerator.h.

◆ m_mean

double SplineGenerator::m_mean
private

Definition at line 24 of file SplineGenerator.h.

◆ m_rndm

TRandom3* SplineGenerator::m_rndm
private

Definition at line 20 of file SplineGenerator.h.

◆ m_spline

TSpline3 SplineGenerator::m_spline
mutableprivate

Definition at line 21 of file SplineGenerator.h.


The documentation for this class was generated from the following files: