Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-30 02:33:16

0001 #ifndef NPSTAT_STORABLEINTERPOLATIONFUNCTOR_HH_
0002 #define NPSTAT_STORABLEINTERPOLATIONFUNCTOR_HH_
0003 
0004 /*!
0005 // \file StorableInterpolationFunctor.h
0006 //
0007 // \brief Storable multivariate functor represented by a multilinear
0008 //        interpolation/extrapolation table
0009 //
0010 // Author: I. Volobouev
0011 //
0012 // July 2012
0013 */
0014 
0015 #include "JetMETCorrections/InterpolationTables/interface/StorableMultivariateFunctor.h"
0016 #include "JetMETCorrections/InterpolationTables/interface/LinInterpolatedTableND.h"
0017 #include "JetMETCorrections/InterpolationTables/interface/SimpleFunctors.h"
0018 
0019 namespace npstat {
0020   /**
0021     // This class adapts LinInterpolatedTableND template to the
0022     // StorableMultivariateFunctor interface
0023     */
0024   template <class Numeric, class Axis = UniformAxis, class Converter = Same<Numeric> >
0025   class StorableInterpolationFunctor : public StorableMultivariateFunctor {
0026     template <typename Num2, typename Axis2, typename Conv2>
0027     friend class StorableInterpolationFunctor;
0028 
0029   public:
0030     typedef LinInterpolatedTableND<Numeric, Axis> Table;
0031 
0032     //@{
0033     /** Constructor from a pre-existing table */
0034     template <class Num2>
0035     inline StorableInterpolationFunctor(const LinInterpolatedTableND<Num2, Axis>& table)
0036         : StorableMultivariateFunctor(), table_(table) {}
0037 
0038     template <class Num2>
0039     inline StorableInterpolationFunctor(const LinInterpolatedTableND<Num2, Axis>& table, const std::string& descr)
0040         : StorableMultivariateFunctor(descr), table_(table) {}
0041     //@}
0042 
0043     /** Converting copy constructor */
0044     template <class Num2, class Conv2>
0045     inline StorableInterpolationFunctor(const StorableInterpolationFunctor<Num2, Axis, Conv2>& tab)
0046         : StorableMultivariateFunctor(tab.description()), table_(tab.table_) {}
0047 
0048     //@{
0049     /**
0050         // Constructor which builds the table in place.
0051         // It basically passses its arguments to the
0052         // corresponding constructor of LinInterpolatedTableND.
0053         */
0054     inline StorableInterpolationFunctor(const std::vector<Axis>& axes,
0055                                         const std::vector<std::pair<bool, bool> >& interpolationType,
0056                                         const char* functionLabel = nullptr)
0057         : StorableMultivariateFunctor(), table_(axes, interpolationType, functionLabel) {}
0058 
0059     inline StorableInterpolationFunctor(const Axis& xAxis, bool leftX, bool rightX, const char* functionLabel = nullptr)
0060         : StorableMultivariateFunctor(), table_(xAxis, leftX, rightX, functionLabel) {}
0061 
0062     inline StorableInterpolationFunctor(const Axis& xAxis,
0063                                         bool leftX,
0064                                         bool rightX,
0065                                         const Axis& yAxis,
0066                                         bool leftY,
0067                                         bool rightY,
0068                                         const char* functionLabel = nullptr)
0069         : StorableMultivariateFunctor(), table_(xAxis, leftX, rightX, yAxis, leftY, rightY, functionLabel) {}
0070 
0071     inline StorableInterpolationFunctor(const Axis& xAxis,
0072                                         bool leftX,
0073                                         bool rightX,
0074                                         const Axis& yAxis,
0075                                         bool leftY,
0076                                         bool rightY,
0077                                         const Axis& zAxis,
0078                                         bool leftZ,
0079                                         bool rightZ,
0080                                         const char* functionLabel = nullptr)
0081         : StorableMultivariateFunctor(),
0082           table_(xAxis, leftX, rightX, yAxis, leftY, rightY, zAxis, leftZ, rightZ, functionLabel) {}
0083 
0084     inline StorableInterpolationFunctor(const Axis& xAxis,
0085                                         bool leftX,
0086                                         bool rightX,
0087                                         const Axis& yAxis,
0088                                         bool leftY,
0089                                         bool rightY,
0090                                         const Axis& zAxis,
0091                                         bool leftZ,
0092                                         bool rightZ,
0093                                         const Axis& tAxis,
0094                                         bool leftT,
0095                                         bool rightT,
0096                                         const char* functionLabel = nullptr)
0097         : StorableMultivariateFunctor(),
0098           table_(
0099               xAxis, leftX, rightX, yAxis, leftY, rightY, zAxis, leftZ, rightZ, tAxis, leftT, rightT, functionLabel) {}
0100 
0101     inline StorableInterpolationFunctor(const Axis& xAxis,
0102                                         bool leftX,
0103                                         bool rightX,
0104                                         const Axis& yAxis,
0105                                         bool leftY,
0106                                         bool rightY,
0107                                         const Axis& zAxis,
0108                                         bool leftZ,
0109                                         bool rightZ,
0110                                         const Axis& tAxis,
0111                                         bool leftT,
0112                                         bool rightT,
0113                                         const Axis& vAxis,
0114                                         bool leftV,
0115                                         bool rightV,
0116                                         const char* functionLabel = nullptr)
0117         : StorableMultivariateFunctor(),
0118           table_(xAxis,
0119                  leftX,
0120                  rightX,
0121                  yAxis,
0122                  leftY,
0123                  rightY,
0124                  zAxis,
0125                  leftZ,
0126                  rightZ,
0127                  tAxis,
0128                  leftT,
0129                  rightT,
0130                  vAxis,
0131                  leftV,
0132                  rightV,
0133                  functionLabel) {}
0134     //@}
0135 
0136     /** Default constructor not implemented **/
0137     StorableInterpolationFunctor() = delete;
0138 
0139     ~StorableInterpolationFunctor() override {}
0140 
0141     unsigned minDim() const override { return table_.dim(); };
0142 
0143     double operator()(const double* point, unsigned dim) const override { return conv_(table_(point, dim)); }
0144 
0145     //@{
0146     /** Retrieve the underlying LinInterpolatedTableND object */
0147     inline Table& interpolator() { return table_; }
0148     inline const Table& interpolator() const { return table_; }
0149     //@}
0150 
0151     //@{
0152     /** Retrieve the tabulated data */
0153     inline ArrayND<Numeric>& table() { return table_.table(); }
0154     inline const ArrayND<Numeric>& table() const { return table_.table(); }
0155     //@}
0156 
0157     /** Change the coordinate converter */
0158     inline void setConverter(const Converter& conv) { conv_ = conv; }
0159 
0160     //@{
0161     // Method related to "geners" I/O
0162     gs::ClassId classId() const override { return gs::ClassId(*this); }
0163     bool write(std::ostream& of) const override;
0164     //@}
0165 
0166     // I/O methods needed for reading
0167     static inline const char* classname();
0168     static inline unsigned version() { return 1; }
0169     static StorableInterpolationFunctor* read(const gs::ClassId& id, std::istream& in);
0170 
0171   protected:
0172     bool isEqual(const StorableMultivariateFunctor& other) const override {
0173       // Note the use of static_cast rather than dynamic_cast below.
0174       // static_cast works faster and it is guaranteed to succeed here.
0175       const StorableInterpolationFunctor& r = static_cast<const StorableInterpolationFunctor&>(other);
0176       return table_ == r.table_ && this->description() == other.description();
0177     }
0178 
0179   private:
0180     Table table_;
0181     Converter conv_;
0182   };
0183 }  // namespace npstat
0184 
0185 #include "Alignment/Geners/interface/binaryIO.hh"
0186 #include "Alignment/Geners/interface/CPP11_auto_ptr.hh"
0187 #include "Alignment/Geners/interface/IOException.hh"
0188 
0189 namespace npstat {
0190   template <typename Numeric, class Axis, class Converter>
0191   const char* StorableInterpolationFunctor<Numeric, Axis, Converter>::classname() {
0192     static const std::string myClass(gs::template_class_name<Numeric, Axis>("npstat::StorableInterpolationFunctor"));
0193     return myClass.c_str();
0194   }
0195 
0196   template <typename Numeric, class Axis, class Converter>
0197   bool StorableInterpolationFunctor<Numeric, Axis, Converter>::write(std::ostream& of) const {
0198     gs::write_pod(of, this->description());
0199     return table_.classId().write(of) && table_.write(of);
0200   }
0201 
0202   template <typename Numeric, class Axis, class Converter>
0203   StorableInterpolationFunctor<Numeric, Axis, Converter>* StorableInterpolationFunctor<Numeric, Axis, Converter>::read(
0204       const gs::ClassId& id, std::istream& in) {
0205     static const gs::ClassId current(gs::ClassId::makeId<StorableInterpolationFunctor<Numeric, Axis> >());
0206     current.ensureSameId(id);
0207 
0208     std::string descr;
0209     gs::read_pod(in, &descr);
0210     gs::ClassId tabid(in, 1);
0211     if (in.fail())
0212       throw gs::IOReadFailure(
0213           "In npstat::StorableInterpolationFunctor::read: "
0214           "input stream failure");
0215     CPP11_auto_ptr<Table> tab(Table::read(tabid, in));
0216     return new StorableInterpolationFunctor(*tab, descr);
0217   }
0218 }  // namespace npstat
0219 
0220 #endif  // NPSTAT_STORABLEINTERPOLATIONFUNCTOR_HH_