Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TSpline.h
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Federico Carminati 28/02/2000
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TSpline
13 #define ROOT_TSpline
14 
15 #include "TGraph.h"
16 
17 class TH1;
18 class TF1;
19 
20 class TSpline : public TNamed, public TAttLine,
21  public TAttFill, public TAttMarker
22 {
23 protected:
24  Double_t fDelta; // Distance between equidistant knots
25  Double_t fXmin; // Minimum value of abscissa
26  Double_t fXmax; // Maximum value of abscissa
27  Int_t fNp; // Number of knots
28  Bool_t fKstep; // True of equidistant knots
29  TH1F *fHistogram; // Temporary histogram
30  TGraph *fGraph; // Graph for drawing the knots
31  Int_t fNpx; // Number of points used for graphical representation
32 
33  TSpline(const TSpline&);
34  TSpline& operator=(const TSpline&);
35  virtual void BuildCoeff()=0;
36 
37 public:
38  TSpline() : fDelta(-1), fXmin(0), fXmax(0),
39  fNp(0), fKstep(kFALSE), fHistogram(0), fGraph(0), fNpx(100) {}
40  TSpline(const char *title, Double_t delta, Double_t xmin,
41  Double_t xmax, Int_t np, Bool_t step) :
42  TNamed("Spline",title), TAttFill(0,1),
43  fDelta(delta), fXmin(xmin),
44  fXmax(xmax), fNp(np), fKstep(step),
45  fHistogram(0), fGraph(0), fNpx(100) {}
46  virtual ~TSpline();
47 
48  virtual void GetKnot(Int_t i, Double_t &x, Double_t &y) const =0;
49  virtual Int_t DistancetoPrimitive(Int_t px, Int_t py);
50  virtual void Draw(Option_t *option="");
51  virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py);
52  virtual Double_t GetDelta() const {return fDelta;}
53  TH1F *GetHistogram() const {return fHistogram;}
54  virtual Int_t GetNp() const {return fNp;}
55  virtual Int_t GetNpx() const {return fNpx;}
56  virtual Double_t GetXmin() const {return fXmin;}
57  virtual Double_t GetXmax() const {return fXmax;}
58  virtual void Paint(Option_t *option="");
59  virtual Double_t Eval(Double_t x) const=0;
60  virtual void SaveAs(const char * /*filename*/,Option_t * /*option*/) const {;}
61  void SetNpx(Int_t n) {fNpx=n;}
62 
63  ClassDef (TSpline,2) // Spline base class
64 };
65 
66 
67 //______________________________________________________________________________
68 class TSplinePoly : public TObject
69 {
70 protected:
71  Double_t fX; // abscissa
72  Double_t fY; // constant term
73 
74 public:
75  TSplinePoly() :
76  fX(0), fY(0) {}
77  TSplinePoly(Double_t x, Double_t y) :
78  fX(x), fY(y) {}
79  TSplinePoly(TSplinePoly const &other);
80  TSplinePoly &operator=(TSplinePoly const &other);
81 
82  Double_t &X() {return fX;}
83  Double_t &Y() {return fY;}
84  void GetKnot(Double_t &x, Double_t &y) const {x=fX; y=fY;}
85 
86  virtual Double_t Eval(Double_t) const {return fY;}
87 
88  private:
89  void CopyPoly(TSplinePoly const &other);
90 
91  ClassDef(TSplinePoly,2) // Spline polynomial terms
92 };
93 
94 inline TSplinePoly::TSplinePoly(TSplinePoly const &other)
95 :
96  TObject(other), fX(0), fY(0)
97 {
98  CopyPoly(other);
99 }
100 
101 
102 //______________________________________________________________________________
103 class TSplinePoly3 : public TSplinePoly
104 {
105 private:
106  Double_t fB; // first order expansion coefficient : fB*1! is the first derivative at x
107  Double_t fC; // second order expansion coefficient : fC*2! is the second derivative at x
108  Double_t fD; // third order expansion coefficient : fD*3! is the third derivative at x
109 
110 public:
111  TSplinePoly3() :
112  fB(0), fC(0), fD(0) {}
113  TSplinePoly3(Double_t x, Double_t y, Double_t b, Double_t c, Double_t d) :
114  TSplinePoly(x,y), fB(b), fC(c), fD(d) {}
115  TSplinePoly3(TSplinePoly3 const &other);
116  TSplinePoly3 &operator=(TSplinePoly3 const &other);
117 
118  Double_t &B() {return fB;}
119  Double_t &C() {return fC;}
120  Double_t &D() {return fD;}
121  Double_t Eval(Double_t x) const {
122  Double_t dx=x-fX;
123  return (fY+dx*(fB+dx*(fC+dx*fD)));
124  }
125  Double_t Derivative(Double_t x) const {
126  Double_t dx=x-fX;
127  return (fB+dx*(2*fC+3*fD*dx));
128  }
129 
130 private:
131  void CopyPoly(TSplinePoly3 const &other);
132 
133  ClassDef(TSplinePoly3,1) // Third spline polynomial terms
134 };
135 
136 inline TSplinePoly3::TSplinePoly3(TSplinePoly3 const &other)
137  :
138  TSplinePoly(other), fB(0), fC(0), fD(0)
139 {
140  CopyPoly(other);
141 }
142 
143 //______________________________________________________________________________
144 class TSplinePoly5 : public TSplinePoly
145 {
146 private:
147  Double_t fB; // first order expansion coefficient : fB*1! is the first derivative at x
148  Double_t fC; // second order expansion coefficient : fC*2! is the second derivative at x
149  Double_t fD; // third order expansion coefficient : fD*3! is the third derivative at x
150  Double_t fE; // fourth order expansion coefficient : fE*4! is the fourth derivative at x
151  Double_t fF; // fifth order expansion coefficient : fF*5! is the fifth derivative at x
152 
153 public:
154  TSplinePoly5() :
155  fB(0), fC(0), fD(0), fE(0), fF(0) {}
156  TSplinePoly5(Double_t x, Double_t y, Double_t b, Double_t c,
157  Double_t d, Double_t e, Double_t f) :
158  TSplinePoly(x,y), fB(b), fC(c), fD(d), fE(e), fF(f) {}
159  TSplinePoly5(TSplinePoly5 const &other);
160  TSplinePoly5 &operator=(TSplinePoly5 const &other);
161 
162  Double_t &B() {return fB;}
163  Double_t &C() {return fC;}
164  Double_t &D() {return fD;}
165  Double_t &E() {return fE;}
166  Double_t &F() {return fF;}
167  Double_t Eval(Double_t x) const {
168  Double_t dx=x-fX;
169  return (fY+dx*(fB+dx*(fC+dx*(fD+dx*(fE+dx*fF)))));
170  }
171  Double_t Derivative(Double_t x) const{
172  Double_t dx=x-fX;
173  return (fB+dx*(2*fC+dx*(3*fD+dx*(4*fE+dx*(5*fF)))));
174  }
175 
176 private:
177  void CopyPoly(TSplinePoly5 const &other);
178 
179  ClassDef(TSplinePoly5,1) // Quintic spline polynomial terms
180 };
181 
182 inline TSplinePoly5::TSplinePoly5(TSplinePoly5 const &other)
183 :
184  TSplinePoly(other), fB(0), fC(0), fD(0), fE(0), fF(0)
185 {
186  CopyPoly(other);
187 }
188 
189 
190 //______________________________________________________________________________
191 class TSpline3 : public TSpline
192 {
193 protected:
194  TSplinePoly3 *fPoly; //[fNp] Array of polynomial terms
195  Double_t fValBeg; // Initial value of first or second derivative
196  Double_t fValEnd; // End value of first or second derivative
197  Int_t fBegCond; // 0=no beg cond, 1=first derivative, 2=second derivative
198  Int_t fEndCond; // 0=no end cond, 1=first derivative, 2=second derivative
199 
200  void BuildCoeff();
201  void SetCond(const char *opt);
202 
203 public:
204  TSpline3() : TSpline() , fPoly(0), fValBeg(0), fValEnd(0),
205  fBegCond(-1), fEndCond(-1) {}
206  TSpline3(const char *title,
207  Double_t x[], Double_t y[], Int_t n, const char *opt=0,
208  Double_t valbeg=0, Double_t valend=0);
209  TSpline3(const char *title,
210  Double_t xmin, Double_t xmax,
211  Double_t y[], Int_t n, const char *opt=0,
212  Double_t valbeg=0, Double_t valend=0);
213  TSpline3(const char *title,
214  Double_t x[], const TF1 *func, Int_t n, const char *opt=0,
215  Double_t valbeg=0, Double_t valend=0);
216  TSpline3(const char *title,
217  Double_t xmin, Double_t xmax,
218  const TF1 *func, Int_t n, const char *opt=0,
219  Double_t valbeg=0, Double_t valend=0);
220  TSpline3(const char *title,
221  const TGraph *g, const char *opt=0,
222  Double_t valbeg=0, Double_t valend=0);
223  TSpline3(const TH1 *h, const char *opt=0,
224  Double_t valbeg=0, Double_t valend=0);
225  TSpline3(const TSpline3&);
226  TSpline3& operator=(const TSpline3&);
227  Int_t FindX(Double_t x) const;
228  Double_t Eval(Double_t x) const;
229  Double_t Derivative(Double_t x) const;
230  virtual ~TSpline3() {if (fPoly) delete [] fPoly;}
231  void GetCoeff(Int_t i, Double_t &x, Double_t &y, Double_t &b,
232  Double_t &c, Double_t &d) {x=fPoly[i].X();y=fPoly[i].Y();
233  b=fPoly[i].B();c=fPoly[i].C();d=fPoly[i].D();}
234  void GetKnot(Int_t i, Double_t &x, Double_t &y) const
235  {x=fPoly[i].X(); y=fPoly[i].Y();}
236  virtual void SaveAs(const char *filename,Option_t *option="") const;
237  virtual void SavePrimitive(std::ostream &out, Option_t *option = "");
238  virtual void SetPoint(Int_t i, Double_t x, Double_t y);
239  virtual void SetPointCoeff(Int_t i, Double_t b, Double_t c, Double_t d);
240  static void Test();
241 
242  ClassDef (TSpline3,2) // Class to create third natural splines
243 };
244 
245 
246 //______________________________________________________________________________
247 class TSpline5 : public TSpline
248 {
249 protected:
250  TSplinePoly5 *fPoly; //[fNp] Array of polynomial terms
251 
252  void BuildCoeff();
253  void BoundaryConditions(const char *opt, Int_t &beg, Int_t &end,
254  const char *&cb1, const char *&ce1, const char *&cb2,
255  const char *&ce2);
256  void SetBoundaries(Double_t b1, Double_t e1, Double_t b2, Double_t e2,
257  const char *cb1, const char *ce1, const char *cb2,
258  const char *ce2);
259 public:
260  TSpline5() : TSpline() , fPoly(0) {}
261  TSpline5(const char *title,
262  Double_t x[], Double_t y[], Int_t n,
263  const char *opt=0, Double_t b1=0, Double_t e1=0,
264  Double_t b2=0, Double_t e2=0);
265  TSpline5(const char *title,
266  Double_t xmin, Double_t xmax,
267  Double_t y[], Int_t n,
268  const char *opt=0, Double_t b1=0, Double_t e1=0,
269  Double_t b2=0, Double_t e2=0);
270  TSpline5(const char *title,
271  Double_t x[], const TF1 *func, Int_t n,
272  const char *opt=0, Double_t b1=0, Double_t e1=0,
273  Double_t b2=0, Double_t e2=0);
274  TSpline5(const char *title,
275  Double_t xmin, Double_t xmax,
276  const TF1 *func, Int_t n,
277  const char *opt=0, Double_t b1=0, Double_t e1=0,
278  Double_t b2=0, Double_t e2=0);
279  TSpline5(const char *title,
280  const TGraph *g,
281  const char *opt=0, Double_t b1=0, Double_t e1=0,
282  Double_t b2=0, Double_t e2=0);
283  TSpline5(const TH1 *h,
284  const char *opt=0, Double_t b1=0, Double_t e1=0,
285  Double_t b2=0, Double_t e2=0);
286  TSpline5(const TSpline5&);
287  TSpline5& operator=(const TSpline5&);
288  Int_t FindX(Double_t x) const;
289  Double_t Eval(Double_t x) const;
290  Double_t Derivative(Double_t x) const;
291  virtual ~TSpline5() {if (fPoly) delete [] fPoly;}
292  void GetCoeff(Int_t i, Double_t &x, Double_t &y, Double_t &b,
293  Double_t &c, Double_t &d, Double_t &e, Double_t &f)
294  {x=fPoly[i].X();y=fPoly[i].Y();b=fPoly[i].B();
295  c=fPoly[i].C();d=fPoly[i].D();
296  e=fPoly[i].E();f=fPoly[i].F();}
297  void GetKnot(Int_t i, Double_t &x, Double_t &y) const
298  {x=fPoly[i].X(); y=fPoly[i].Y();}
299  virtual void SaveAs(const char *filename,Option_t *option="") const;
300  virtual void SavePrimitive(std::ostream &out, Option_t *option = "");
301  virtual void SetPoint(Int_t i, Double_t x, Double_t y);
302  virtual void SetPointCoeff(Int_t i, Double_t b, Double_t c, Double_t d,
303  Double_t e, Double_t f);
304  static void Test();
305 
306  ClassDef (TSpline5,2) // Class to create quintic natural splines
307 };
308 
309 #endif