Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TSpline.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/hist:$Id$
0002 // Author: Federico Carminati   28/02/2000
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2022, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TSpline
0013 #define ROOT_TSpline
0014 
0015 #ifdef R__LESS_INCLUDES
0016 #include "TNamed.h"
0017 #include "TAttLine.h"
0018 #include "TAttFill.h"
0019 #include "TAttMarker.h"
0020 #else
0021 #include "TGraph.h"
0022 #endif
0023 
0024 class TH1;
0025 class TH1F;
0026 class TF1;
0027 class TGraph;
0028 
0029 class TSpline : public TNamed, public TAttLine,
0030                 public TAttFill, public TAttMarker
0031 {
0032 protected:
0033    Double_t fDelta = -1.;      ///< Distance between equidistant knots
0034    Double_t fXmin = 0.;        ///< Minimum value of abscissa
0035    Double_t fXmax = 0.;        ///< Maximum value of abscissa
0036    Int_t fNp = 0;              ///< Number of knots
0037    Bool_t fKstep = kFALSE;     ///< True of equidistant knots
0038    TH1F *fHistogram = nullptr; ///< Temporary histogram
0039    TGraph *fGraph = nullptr;   ///< Graph for drawing the knots
0040    Int_t fNpx = 100;           ///< Number of points used for graphical representation
0041 
0042    TSpline(const TSpline &);
0043    TSpline &operator=(const TSpline &);
0044    virtual void BuildCoeff() = 0;
0045 
0046 public:
0047    TSpline() {}
0048    TSpline(const char *title, Double_t delta, Double_t xmin, Double_t xmax, Int_t np, Bool_t step)
0049       : TNamed("Spline", title), TAttFill(0, 1), fDelta(delta), fXmin(xmin), fXmax(xmax), fNp(np), fKstep(step)
0050    {
0051    }
0052    ~TSpline() override;
0053 
0054    virtual void     GetKnot(Int_t i, Double_t &x, Double_t &y) const =0;
0055    Int_t    DistancetoPrimitive(Int_t px, Int_t py) override;
0056    void     Draw(Option_t *option="") override;
0057    void     ExecuteEvent(Int_t event, Int_t px, Int_t py) override;
0058    virtual Double_t GetDelta() const {return fDelta;}
0059    TH1F            *GetHistogram() const {return fHistogram;}
0060    virtual Int_t    GetNp()    const {return fNp;}
0061    virtual Int_t    GetNpx()   const {return fNpx;}
0062    virtual Double_t GetXmin()  const {return fXmin;}
0063    virtual Double_t GetXmax()  const {return fXmax;}
0064    void     Paint(Option_t *option="") override;
0065    virtual Double_t Eval(Double_t x) const=0;
0066    void     SaveAs(const char * /*filename*/ = "",Option_t * /*option*/ = "") const override {}
0067    void             SetNpx(Int_t n) {fNpx=n;}
0068 
0069    ClassDefOverride(TSpline,2) // Spline base class
0070 };
0071 
0072 
0073 //______________________________________________________________________________
0074 class TSplinePoly : public TObject
0075 {
0076 protected:
0077    Double_t fX = 0.;     ///< Abscissa
0078    Double_t fY = 0.;     ///< Constant term
0079 
0080 public:
0081    TSplinePoly() {}
0082    TSplinePoly(Double_t x, Double_t y) : fX(x), fY(y) {}
0083    TSplinePoly(TSplinePoly const &other) : TObject(other), fX(0), fY(0) { CopyPoly(other); }
0084 
0085    TSplinePoly &operator=(TSplinePoly const &other);
0086 
0087    Double_t &X() { return fX; }
0088    Double_t &Y() { return fY; }
0089    void GetKnot(Double_t &x, Double_t &y) const
0090    {
0091       x = fX;
0092       y = fY;
0093    }
0094 
0095    virtual Double_t Eval(Double_t) const { return fY; }
0096 
0097 private:
0098    void CopyPoly(TSplinePoly const &other);
0099 
0100    ClassDefOverride(TSplinePoly,2) // Spline polynomial terms
0101 };
0102 
0103 //______________________________________________________________________________
0104 class TSplinePoly3 : public TSplinePoly
0105 {
0106 private:
0107    Double_t fB = 0.; ///< First order expansion coefficient :  fB*1! is the first derivative at x
0108    Double_t fC = 0.; ///< Second order expansion coefficient : fC*2! is the second derivative at x
0109    Double_t fD = 0.; ///< Third order expansion coefficient :  fD*3! is the third derivative at x
0110 
0111 public:
0112    TSplinePoly3() {}
0113    TSplinePoly3(Double_t x, Double_t y, Double_t b, Double_t c, Double_t d) : TSplinePoly(x, y), fB(b), fC(c), fD(d) {}
0114    TSplinePoly3(TSplinePoly3 const &other) : TSplinePoly(other) { CopyPoly(other); }
0115 
0116    TSplinePoly3 &operator=(TSplinePoly3 const &other);
0117 
0118    Double_t &B() { return fB; }
0119    Double_t &C() { return fC; }
0120    Double_t &D() { return fD; }
0121    Double_t Eval(Double_t x) const override
0122    {
0123       Double_t dx = x - fX;
0124       return (fY + dx * (fB + dx * (fC + dx * fD)));
0125    }
0126    Double_t Derivative(Double_t x) const
0127    {
0128       Double_t dx = x - fX;
0129       return (fB + dx * (2 * fC + 3 * fD * dx));
0130    }
0131 
0132 private:
0133    void CopyPoly(TSplinePoly3 const &other);
0134 
0135    ClassDefOverride(TSplinePoly3,1)  // Third spline polynomial terms
0136 };
0137 
0138 //______________________________________________________________________________
0139 class TSplinePoly5 : public TSplinePoly
0140 {
0141 private:
0142    Double_t fB = 0.; ///< First order expansion coefficient :  fB*1! is the first derivative at x
0143    Double_t fC = 0.; ///< Second order expansion coefficient : fC*2! is the second derivative at x
0144    Double_t fD = 0.; ///< Third order expansion coefficient :  fD*3! is the third derivative at x
0145    Double_t fE = 0.; ///< Fourth order expansion coefficient : fE*4! is the fourth derivative at x
0146    Double_t fF = 0.; ///< Fifth order expansion coefficient :  fF*5! is the fifth derivative at x
0147 
0148 public:
0149    TSplinePoly5() {}
0150    TSplinePoly5(Double_t x, Double_t y, Double_t b, Double_t c, Double_t d, Double_t e, Double_t f)
0151       : TSplinePoly(x, y), fB(b), fC(c), fD(d), fE(e), fF(f)
0152    {
0153    }
0154    TSplinePoly5(TSplinePoly5 const &other) : TSplinePoly(other) { CopyPoly(other); }
0155 
0156    TSplinePoly5 &operator=(TSplinePoly5 const &other);
0157 
0158    Double_t &B() { return fB; }
0159    Double_t &C() { return fC; }
0160    Double_t &D() { return fD; }
0161    Double_t &E() { return fE; }
0162    Double_t &F() { return fF; }
0163    Double_t Eval(Double_t x) const override
0164    {
0165       Double_t dx=x-fX;
0166       return (fY+dx*(fB+dx*(fC+dx*(fD+dx*(fE+dx*fF)))));
0167    }
0168    Double_t Derivative(Double_t x) const{
0169       Double_t dx=x-fX;
0170       return (fB+dx*(2*fC+dx*(3*fD+dx*(4*fE+dx*(5*fF)))));
0171    }
0172 
0173 private:
0174    void CopyPoly(TSplinePoly5 const &other);
0175 
0176    ClassDefOverride(TSplinePoly5,1)  // Quintic spline polynomial terms
0177 };
0178 
0179 
0180 //______________________________________________________________________________
0181 class TSpline3 : public TSpline
0182 {
0183 protected:
0184    TSplinePoly3 *fPoly = nullptr; ///<[fNp] Array of polynomial terms
0185    Double_t fValBeg = 0.;         ///< Initial value of first or second derivative
0186    Double_t fValEnd = 0.;         ///< End value of first or second derivative
0187    Int_t fBegCond = -1;           ///< 0=no beg cond, 1=first derivative, 2=second derivative
0188    Int_t fEndCond = -1;           ///< 0=no end cond, 1=first derivative, 2=second derivative
0189 
0190    void   BuildCoeff() override;
0191    void   SetCond(const char *opt);
0192 
0193 public:
0194    TSpline3() {}
0195    TSpline3(const char *title,
0196             Double_t x[], Double_t y[], Int_t n, const char *opt=nullptr,
0197             Double_t valbeg=0, Double_t valend=0);
0198    TSpline3(const char *title,
0199             Double_t xmin, Double_t xmax,
0200             Double_t y[], Int_t n, const char *opt=nullptr,
0201             Double_t valbeg=0, Double_t valend=0);
0202    TSpline3(const char *title,
0203             Double_t x[], const TF1 *func, Int_t n, const char *opt=nullptr,
0204             Double_t valbeg=0, Double_t valend=0);
0205    TSpline3(const char *title,
0206             Double_t xmin, Double_t xmax,
0207             const TF1 *func, Int_t n, const char *opt=nullptr,
0208             Double_t valbeg=0, Double_t valend=0);
0209    TSpline3(const char *title,
0210             const TGraph *g, const char *opt=nullptr,
0211             Double_t valbeg=0, Double_t valend=0);
0212    TSpline3(const TH1 *h, const char *opt=nullptr,
0213             Double_t valbeg=0, Double_t valend=0);
0214    TSpline3(const TSpline3&);
0215    TSpline3& operator=(const TSpline3&);
0216    Int_t    FindX(Double_t x) const;
0217    Double_t Eval(Double_t x) const override;
0218    Double_t Derivative(Double_t x) const;
0219    ~TSpline3() override {if (fPoly) delete [] fPoly;}
0220    void GetCoeff(Int_t i, Double_t &x, Double_t &y, Double_t &b,
0221                  Double_t &c, Double_t &d) const
0222       {x=fPoly[i].X();y=fPoly[i].Y();
0223       b=fPoly[i].B();c=fPoly[i].C();d=fPoly[i].D();}
0224    void GetKnot(Int_t i, Double_t &x, Double_t &y) const override
0225       {x=fPoly[i].X(); y=fPoly[i].Y();}
0226     void     SaveAs(const char *filename="",Option_t *option="") const override;
0227     void     SavePrimitive(std::ostream &out, Option_t *option = "") override;
0228    virtual  void     SetPoint(Int_t i, Double_t x, Double_t y);
0229    virtual  void     SetPointCoeff(Int_t i, Double_t b, Double_t c, Double_t d);
0230    static void Test();
0231 
0232    ClassDefOverride(TSpline3,2)  // Class to create third natural splines
0233 };
0234 
0235 
0236 //______________________________________________________________________________
0237 class TSpline5 : public TSpline
0238 {
0239 protected:
0240    TSplinePoly5  *fPoly = nullptr;     ///<[fNp] Array of polynomial terms
0241 
0242    void BuildCoeff() override;
0243    void BoundaryConditions(const char *opt, Int_t &beg, Int_t &end,
0244                            const char *&cb1, const char *&ce1, const char *&cb2,
0245                            const char *&ce2);
0246    void SetBoundaries(Double_t b1, Double_t e1, Double_t b2, Double_t e2,
0247                       const char *cb1, const char *ce1, const char *cb2,
0248                       const char *ce2);
0249 public:
0250    TSpline5() {}
0251    TSpline5(const char *title,
0252             Double_t x[], Double_t y[], Int_t n,
0253             const char *opt=nullptr, Double_t b1=0, Double_t e1=0,
0254             Double_t b2=0, Double_t e2=0);
0255    TSpline5(const char *title,
0256             Double_t xmin, Double_t xmax,
0257             Double_t y[], Int_t n,
0258             const char *opt=nullptr, Double_t b1=0, Double_t e1=0,
0259             Double_t b2=0, Double_t e2=0);
0260    TSpline5(const char *title,
0261             Double_t x[], const TF1 *func, Int_t n,
0262             const char *opt=nullptr, Double_t b1=0, Double_t e1=0,
0263             Double_t b2=0, Double_t e2=0);
0264    TSpline5(const char *title,
0265             Double_t xmin, Double_t xmax,
0266             const TF1 *func, Int_t n,
0267             const char *opt=nullptr, Double_t b1=0, Double_t e1=0,
0268             Double_t b2=0, Double_t e2=0);
0269    TSpline5(const char *title,
0270             const TGraph *g,
0271             const char *opt=nullptr, Double_t b1=0, Double_t e1=0,
0272             Double_t b2=0, Double_t e2=0);
0273    TSpline5(const TH1 *h,
0274             const char *opt=nullptr, Double_t b1=0, Double_t e1=0,
0275             Double_t b2=0, Double_t e2=0);
0276    TSpline5(const TSpline5&);
0277    TSpline5& operator=(const TSpline5&);
0278    Int_t    FindX(Double_t x) const;
0279    Double_t Eval(Double_t x) const override;
0280    Double_t Derivative(Double_t x) const;
0281    ~TSpline5() override {if (fPoly) delete [] fPoly;}
0282    void GetCoeff(Int_t i, Double_t &x, Double_t &y, Double_t &b,
0283                  Double_t &c, Double_t &d, Double_t &e, Double_t &f) const
0284       {x=fPoly[i].X();y=fPoly[i].Y();b=fPoly[i].B();
0285       c=fPoly[i].C();d=fPoly[i].D();
0286       e=fPoly[i].E();f=fPoly[i].F();}
0287    void GetKnot(Int_t i, Double_t &x, Double_t &y) const override
0288       {x=fPoly[i].X(); y=fPoly[i].Y();}
0289     void     SaveAs(const char *filename="",Option_t *option="") const override;
0290     void     SavePrimitive(std::ostream &out, Option_t *option = "") override;
0291    virtual  void     SetPoint(Int_t i, Double_t x, Double_t y);
0292    virtual  void     SetPointCoeff(Int_t i, Double_t b, Double_t c, Double_t d,
0293                                    Double_t e, Double_t f);
0294    static void Test();
0295 
0296    ClassDefOverride(TSpline5,2) // Class to create quintic natural splines
0297 };
0298 
0299 #endif