diff --git a/inc/Lau1DCubicSpline.hh b/inc/Lau1DCubicSpline.hh index e9a258f..71704d9 100644 --- a/inc/Lau1DCubicSpline.hh +++ b/inc/Lau1DCubicSpline.hh @@ -1,302 +1,302 @@ /* Copyright 2015 University of Warwick Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /* Laura++ package authors: John Back Paul Harrison Thomas Latham */ /*! \file Lau1DCubicSpline.hh \brief File containing declaration of Lau1DCubicSpline class. */ /*! \class Lau1DCubicSpline \brief Class for defining a 1D cubic spline based on a set of knots. Class for defining a 1D cubic spline based on a set of knots. Interpolation between the knots is performed either by one of two types of cubic spline (standard or Akima) or by linear interpolation. The splines are defined by a piecewise cubic function which, between knots i and i+1, has the form f_i(x) = (1 - t)*y_i + t*y_i+1 + t*(1 - t)(a*(1 - t) + b*t) where t = (x - x_i)/(x_i+1 - x_i), a = k_i *(x_i+1 - x_i) - (y_i+1 - y_i), b = -k_i+1*(x_i+1 - x_i) + (y_i+1 - y_i) and k_i is (by construction) the first derivative at knot i. f(x) and f'(x) are continuous at the internal knots by construction. For the standard splines, f''(x) is required to be continuous at all internal knots placing n-2 constraints on the n parameters, k_i. The final two constraints are set by the boundary conditions. At each boundary, the function may be: (i) Clamped : f'(x) = C at the last knot (ii) Natural : f''(x) = 0 at the last knot (iii) Not a knot : f'''(x) continuous at the second last knot The algorithms used in these splines can be found on: http://en.wikipedia.org/wiki/Spline_interpolation#Algorithm_to_find_the_interpolating_cubic_spline http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm For the Akima splines, the values of k_i are determined from the slopes of the four nearest segments (a_i-1, a_i, a_i+1 and a_i+2) as k_i = ( | a_i+2 - a_i+1 | a_i + | a_i - a_i-1 | a_i+1 ) / ( | a_i+2 - a_i+1 | + | a_i - a_i-1 | ) and as k_i = ( a_i + a_i+1 ) / 2 in the special case a_i-1 == a_i != a_i+1 == a_i+2. Boundary conditions are specified by the relations a_2 - a_1 = a_1 - a_0 = a_0 - a_-1 and a_n-1 - a_n-2 = a_n - a_n-1 = a_n+1 - a_n The algorithms used in these splines can be found in: J.ACM vol. 17 no. 4 pp 589-602 */ #ifndef LAU_1DCUBICSPLINE #define LAU_1DCUBICSPLINE #include #include #include #include #include "Rtypes.h" #include "TF1.h" #include "TFitResultPtr.h" #include "LauPrint.hh" class TH1; class LauAbsRValue; class LauParameter; class Lau1DCubicSpline final { public: //! Define the allowed interpolation types enum class SplineType { StandardSpline, /*!< standard cubic splines with f''(x) continuous at all internal knots */ AkimaSpline, /*!< Akima cubic splines with f'(x) at each knot defined locally by the positions of only five knots */ LinearInterpolation /*! Linear interpolation between each pair of knots */ }; //! Define the allowed boundary condition types /*! These are only supported by standard splines */ enum class BoundaryType { Clamped, /*!< clamped boundary - f'(x) = C */ Natural, /*!< natural boundary - f''(x) = 0 */ NotAKnot /*!< 'not a knot' boundary - f'''(x) continuous at second last knot */ }; //! Constructor /*! \param [in] xs the x-values of the knots \param [in] ys the y-values of the knots \param [in] type the type of spline (Standard, Akima, Linear) \param [in] leftBound the left-hand boundary condition \param [in] rightBound the right-hand boundary condition \param [in] dydx0 the gradient at the left-hand boundary of a clamped spline \param [in] dydxn the gradient at the right-hand boundary of a clamped spline */ Lau1DCubicSpline(const std::vector& xs, const std::vector& ys, const SplineType type = SplineType::StandardSpline, const BoundaryType leftBound = BoundaryType::NotAKnot, const BoundaryType rightBound = BoundaryType::NotAKnot, const Double_t dydx0 = 0.0, const Double_t dydxn = 0.0); //! Evaluate the function at given point /*! \param [in] x the x-coordinate \return the value of the spline at x */ Double_t evaluate(const Double_t x) const; //! Update the y-values of the knots /*! \param [in] ys the y-values of the knots */ void updateYValues(const std::vector& ys); //! Update the y-values of the knots /*! \param [in] ys the y-values of the knots */ void updateYValues(const std::vector& ys); //! Update the y-values of the knots /*! \param [in] ys the y-values of the knots */ void updateYValues(const std::vector& ys); //! Update the type of interpolation to perform /*! \param [in] type the type of interpolation */ void updateType(const SplineType type); //! Update the boundary conditions for the spline /*! \param [in] leftBound the left-hand boundary condition \param [in] rightBound the right-hand boundary condition \param [in] dydx0 the gradient at the left-hand boundary of a clamped spline \param [in] dydxn the gradient at the right-hand boundary of a clamped spline */ void updateBoundaryConditions(const BoundaryType leftBound, const BoundaryType rightBound, const Double_t dydx0 = 0.0, const Double_t dydxn = 0.0); //! Get the number of knots std::size_t getnKnots() const {return nKnots_;} //! Get y values const std::vector& getYValues() const {return y_;} //! Get x values const std::vector& getXValues() const {return x_;} //! Get the coefficients of a given spline segment in the form a + bx + cx^2 + dx^3 /*! \param [in] segIndex refers to the index of the knot at the left end of the segment (segIndex = 0 gets the coefficients of the the segment between x_0 and x_1) \param [in] normalise if true, the coefficients returned will be normalised by the integral of the complete spline (defaults to false) \return coefficients {a, b, c, d} */ std::array getCoefficients(const std::size_t segIndex, const bool normalise = false) const; //! Get the integral over all the spline segments Double_t integral() const; //! Make a TF1 showing the spline with its current knot values /*! \param [in] normalise whether or not you want the spline normalised \return 1D function object */ TF1* makeTF1(const bool normalise = false) const; //! Fit the the normalisation of the spline to a TH1 /*! \param [in] hist the histogram to be fitted \param [in] printLevel the level of printout desired from fit \return a TF1 fit to the histogram */ TF1* normaliseToTH1(TH1* hist, const LauOutputLevel printLevel = LauOutputLevel::Standard) const; //! Fit the spline to a TH1 /*! \param [in] hist the histogram to be fitted \param [in] printLevel the level of printout desired from fit \param [in] doWL If true do a weighted likelihood fit, else chi2 \return a shared-ownership smart pointer to the fit results */ TFitResultPtr fitToTH1(TH1* hist, const LauOutputLevel printLevel = LauOutputLevel::Standard, const bool doWL = false); - //! Set spline variables to those read from a json file - /* - \param[in] fileName the name of the file from which the JSON should be read - \param[in] splineName the (optional) name of the entry in the JSON from which the spline should be read - \return the newly constructed spline - */ - static std::unique_ptr readFromJson(const TString& fileName, const TString& splineName = ""); - - //! Write the spline to a json file using nlohmann json + //! Write the spline to a json file /*! \param[in] fileName the name of the file to which the JSON should be written \param[in] splineName the (optional) name of the entry in the JSON to which the spline should be written \param[in] append if true, append the spline to the existing JSON within the file - if using this option it is then mandatory to provide splineName */ void writeToJson(const TString& fileName, const TString& splineName = "", const bool append = false) const; + //! Construct a new Lau1DCubicSpline object based on values read from a json file + /*! + \param[in] fileName the name of the file from which the JSON should be read + \param[in] splineName the (optional) name of the entry in the JSON from which the spline should be read + \return the newly constructed spline + */ + static std::unique_ptr readFromJson(const TString& fileName, const TString& splineName = ""); + private: //! Default constructor - only for use in readFromJson Lau1DCubicSpline() = default; //! Initialise the class void init(); //! Calculate the first derivative at each knot void calcDerivatives(); //! Calculate the first derivatives according to the standard method void calcDerivativesStandard(); //! Calculate the first derivatives according to the Akima method void calcDerivativesAkima(); //! The number of knots in the spline std::size_t nKnots_{0}; //! The x-value at each knot std::vector x_; //! The y-value at each knot std::vector y_; //! The first derivative at each knot std::vector dydx_; //! The 'a' coefficients used to determine the derivatives std::vector a_; //! The 'b' coefficients used to determine the derivatives std::vector b_; //! The 'c' coefficients used to determine the derivatives std::vector c_; //! The 'd' coefficients used to determine the derivatives std::vector d_; //! The type of interpolation to be performed SplineType type_{SplineType::StandardSpline}; //! The left-hand boundary condition on the spline BoundaryType leftBound_{BoundaryType::NotAKnot}; //! The right-hand boundary condition on the spline BoundaryType rightBound_{BoundaryType::NotAKnot}; //! The gradient at the left boundary for a clamped spline Double_t dydx0_{0.0}; //! The gradient at the right boundary for a clamped spline Double_t dydxn_{0.0}; - // JSON serialisation stuff - - // map SplineType values to JSON as strings - NLOHMANN_JSON_SERIALIZE_ENUM( SplineType, { - {SplineType::StandardSpline, "StandardSpline"}, - {SplineType::AkimaSpline, "AkimaSpline"}, - {SplineType::LinearInterpolation, "LinearInterpolation"}, - }) - - // map BoundaryType values to JSON as strings - NLOHMANN_JSON_SERIALIZE_ENUM( BoundaryType, { - {BoundaryType::Clamped, "Clamped"}, - {BoundaryType::Natural, "Natural"}, - {BoundaryType::NotAKnot, "NotAKnot"}, - }) - - // enable serialisation of this class + // enable JSON serialisation of this class NLOHMANN_DEFINE_TYPE_INTRUSIVE(Lau1DCubicSpline, nKnots_, x_, y_, type_, leftBound_, rightBound_, dydx0_, dydxn_) ClassDef(Lau1DCubicSpline, 0) // Class for defining a 1D cubic spline }; +//! \cond DOXYGEN_IGNORE +// map Lau1DCubicSpline::SplineType values to JSON as strings +NLOHMANN_JSON_SERIALIZE_ENUM( Lau1DCubicSpline::SplineType, { + {Lau1DCubicSpline::SplineType::StandardSpline, "StandardSpline"}, + {Lau1DCubicSpline::SplineType::AkimaSpline, "AkimaSpline"}, + {Lau1DCubicSpline::SplineType::LinearInterpolation, "LinearInterpolation"}, + }) + +// map Lau1DCubicSpline::BoundaryType values to JSON as strings +NLOHMANN_JSON_SERIALIZE_ENUM( Lau1DCubicSpline::BoundaryType, { + {Lau1DCubicSpline::BoundaryType::Clamped, "Clamped"}, + {Lau1DCubicSpline::BoundaryType::Natural, "Natural"}, + {Lau1DCubicSpline::BoundaryType::NotAKnot, "NotAKnot"}, + }) +//! \endcond DOXYGEN_IGNORE + #endif