File indexing completed on 2024-09-07 04:35:39
0001 #ifndef CondFormats_HcalObjects_PiecewiseScalingPolynomial_h_
0002 #define CondFormats_HcalObjects_PiecewiseScalingPolynomial_h_
0003
0004 #include "FWCore/Utilities/interface/Exception.h"
0005
0006 #include "boost/serialization/vector.hpp"
0007 #include "boost/serialization/version.hpp"
0008
0009 #include <vector>
0010
0011 class PiecewiseScalingPolynomial {
0012 public:
0013 inline PiecewiseScalingPolynomial() {}
0014
0015 PiecewiseScalingPolynomial(const std::vector<std::vector<double> >& coeffs, const std::vector<double>& limits);
0016
0017 inline double operator()(const double x) const {
0018 double scale(0.0);
0019 if (x > 0.0) {
0020 const unsigned nLimits(limits_.size());
0021 if (nLimits) {
0022 const double* limits(&limits_[0]);
0023 unsigned which(0U);
0024 for (; which < nLimits; ++which)
0025 if (x < limits[which])
0026 break;
0027 const std::vector<double>& c(coeffs_[which]);
0028 const double* a = &c[0];
0029 for (int deg = c.size() - 1; deg >= 0; --deg) {
0030 scale *= x;
0031 scale += a[deg];
0032 }
0033 }
0034 }
0035 return scale * x;
0036 }
0037
0038 inline bool operator==(const PiecewiseScalingPolynomial& r) const {
0039 return coeffs_ == r.coeffs_ && limits_ == r.limits_;
0040 }
0041
0042 inline bool operator!=(const PiecewiseScalingPolynomial& r) const { return !(*this == r); }
0043
0044 private:
0045 bool validate() const;
0046
0047 std::vector<std::vector<double> > coeffs_;
0048 std::vector<double> limits_;
0049
0050 friend class boost::serialization::access;
0051
0052 template <class Archive>
0053 inline void save(Archive& ar, const unsigned ) const {
0054 if (!validate())
0055 throw cms::Exception("In PiecewiseScalingPolynomial::save: invalid data");
0056 ar & coeffs_ & limits_;
0057 }
0058
0059 template <class Archive>
0060 inline void load(Archive& ar, const unsigned ) {
0061 ar & coeffs_ & limits_;
0062 if (!validate())
0063 throw cms::Exception("In PiecewiseScalingPolynomial::load: invalid data");
0064 }
0065
0066 BOOST_SERIALIZATION_SPLIT_MEMBER()
0067 };
0068
0069 BOOST_CLASS_VERSION(PiecewiseScalingPolynomial, 1)
0070
0071 #endif