CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/JetMETCorrections/InterpolationTables/interface/StorableHistoNDFunctor.h

Go to the documentation of this file.
00001 #ifndef NPSTAT_STORABLEHISTONDFUNCTOR_HH_
00002 #define NPSTAT_STORABLEHISTONDFUNCTOR_HH_
00003 
00015 #include "JetMETCorrections/InterpolationTables/interface/SimpleFunctors.h"
00016 #include "JetMETCorrections/InterpolationTables/interface/StorableMultivariateFunctor.h"
00017 
00018 #include "JetMETCorrections/InterpolationTables/interface/HistoND.h"
00019 
00020 namespace npstat {
00025     template
00026     <
00027         class Numeric,
00028         class Axis = HistoAxis,
00029         class Converter = Same<Numeric>
00030     >
00031     class StorableHistoNDFunctor : public StorableMultivariateFunctor
00032     {
00033         template <typename Num2, typename Axis2, typename Conv2>
00034         friend class StorableHistoNDFunctor;
00035 
00036     public:
00037         typedef HistoND<Numeric,Axis> Table;
00038 
00040 
00045         template <class Num2>
00046         inline StorableHistoNDFunctor(
00047             const HistoND<Num2,Axis>& table, const unsigned degree=1)
00048             : StorableMultivariateFunctor(), table_(table), deg_(degree)
00049             {validateInterDegree(degree, table.isUniformlyBinned());}
00050 
00051         template <class Num2>
00052         inline StorableHistoNDFunctor(
00053             const HistoND<Num2,Axis>& table,
00054             const unsigned degree,
00055             const std::string& descr)
00056             : StorableMultivariateFunctor(descr), table_(table), deg_(degree)
00057             {validateInterDegree(degree, table.isUniformlyBinned());}
00059 
00061         template <class Num2, class Conv2>
00062         inline StorableHistoNDFunctor(
00063             const StorableHistoNDFunctor<Num2,Axis,Conv2>& tab)
00064             : StorableMultivariateFunctor(tab.description()),
00065               table_(tab.table_, Same<Num2>(), tab.title().c_str(),
00066                      tab.accumulatedDataLabel().c_str()), deg_(tab.deg_) {}
00067 
00068         virtual ~StorableHistoNDFunctor() {}
00069 
00070         virtual unsigned minDim() const {return table_.dim();};
00071 
00072         virtual double operator()(const double* point, unsigned dim) const;
00073 
00075         inline unsigned interpolationDegree() const {return deg_;}
00076 
00078         void setInterpolationDegree(const unsigned deg);
00079 
00081 
00082         inline Table& interpolator() {return table_;}
00083         inline const Table& interpolator() const {return table_;}
00085 
00087 
00088         inline ArrayND<Numeric>& table()
00089             {return const_cast<ArrayND<Numeric>&>(table_.binContents());}
00090 
00091         inline const ArrayND<Numeric>& table() const
00092             {return table_.binContents();}
00094 
00096         inline void setConverter(const Converter& conv) {conv_ = conv;}
00097 
00099         // Method related to "geners" I/O
00100         virtual gs::ClassId classId() const {return gs::ClassId(*this);}
00101         virtual bool write(std::ostream& of) const;
00103 
00104         // I/O methods needed for reading
00105         static inline const char* classname();
00106         static inline unsigned version() {return 1;}
00107         static StorableHistoNDFunctor* read(
00108             const gs::ClassId& id, std::istream& in);
00109 
00110     protected:
00111         virtual bool isEqual(const StorableMultivariateFunctor& other) const
00112         {
00113             // Note the use of static_cast rather than dynamic_cast below.
00114             // static_cast works faster and it is guaranteed to succeed here.
00115             const StorableHistoNDFunctor& r = 
00116                 static_cast<const StorableHistoNDFunctor&>(other);
00117             return table_ == r.table_ && deg_ == r.deg_ &&
00118                    this->description() == other.description();
00119         }
00120 
00121     private:
00122         StorableHistoNDFunctor();
00123 
00124         Table table_;
00125         unsigned deg_;
00126         Converter conv_;
00127 
00128         static void validateInterDegree(unsigned deg, bool isUniform);
00129     };
00130 }
00131 
00132 #include "JetMETCorrections/InterpolationTables/interface/NpstatException.h"
00133 
00134 #include "Alignment/Geners/interface/binaryIO.hh"
00135 #include "Alignment/Geners/interface/CPP11_auto_ptr.hh"
00136 #include "Alignment/Geners/interface/IOException.hh"
00137 
00138 #include "JetMETCorrections/InterpolationTables/interface/interpolateHistoND.h"
00139 
00140 namespace npstat {
00141     template <class Numeric, class Axis, class Converter>
00142     double StorableHistoNDFunctor<Numeric,Axis,Converter>::operator()(
00143         const double* point, const unsigned dim) const
00144     {
00145         return conv_(interpolateHistoND(table_, point, dim, deg_));
00146     }
00147 
00148     template <class Numeric, class Axis, class Converter>
00149     const char* StorableHistoNDFunctor<Numeric,Axis,Converter>::classname()
00150     {
00151         static const std::string myClass(gs::template_class_name<Numeric,Axis>(
00152                                             "npstat::StorableHistoNDFunctor"));
00153         return myClass.c_str();
00154     }
00155 
00156     template <class Numeric, class Axis, class Converter>
00157     bool StorableHistoNDFunctor<Numeric,Axis,Converter>::write(std::ostream& of) const
00158     {
00159         gs::write_pod(of, this->description());
00160         gs::write_pod(of, deg_);
00161         return table_.classId().write(of) && table_.write(of);
00162     }
00163 
00164     template <class Numeric, class Axis, class Converter>
00165     StorableHistoNDFunctor<Numeric,Axis,Converter>* 
00166     StorableHistoNDFunctor<Numeric,Axis,Converter>::read(
00167         const gs::ClassId& id, std::istream& in)
00168     {
00169         static const gs::ClassId current(
00170             gs::ClassId::makeId<StorableHistoNDFunctor<Numeric,Axis> >());
00171         current.ensureSameId(id);
00172 
00173         std::string descr;
00174         gs::read_pod(in, &descr);
00175         unsigned deg;
00176         gs::read_pod(in, &deg);
00177         gs::ClassId tabid(in, 1);
00178         if (in.fail()) throw gs::IOReadFailure(
00179             "In npstat::StorableHistoNDFunctor::read: "
00180             "input stream failure");
00181         CPP11_auto_ptr<Table> tab(Table::read(tabid, in));
00182         return new StorableHistoNDFunctor(*tab, deg, descr);
00183     }
00184 
00185     template <class Numeric, class Axis, class Converter>
00186     inline void StorableHistoNDFunctor<Numeric,Axis,Converter>::setInterpolationDegree(
00187         const unsigned deg) 
00188     {
00189         validateInterDegree(deg, table_.isUniformlyBinned());
00190         deg_ = deg;
00191     }
00192 
00193     template <class Numeric, class Axis, class Converter>
00194     inline void StorableHistoNDFunctor<Numeric,Axis,Converter>::validateInterDegree(
00195         const unsigned deg, const bool isUniform)
00196     {
00197         const bool ok = isUniform ? (deg == 0 || deg == 1 || deg == 3) :
00198                                     (deg == 0 || deg == 1);
00199         if (!ok)
00200             throw npstat::NpstatInvalidArgument(
00201                 "In npstat::StorableHistoNDFunctor::validateInterDegree: "
00202                 "unsupported interpolation degree");
00203     }
00204 }
00205 
00206 
00207 #endif // NPSTAT_STORABLEHISTONDFUNCTOR_HH_
00208