CMS 3D CMS Logo

StorableHistoNDFunctor.h
Go to the documentation of this file.
1 #ifndef NPSTAT_STORABLEHISTONDFUNCTOR_HH_
2 #define NPSTAT_STORABLEHISTONDFUNCTOR_HH_
3 
17 
19 
20 namespace npstat {
25  template <class Numeric, class Axis = HistoAxis, class Converter = Same<Numeric> >
27  template <typename Num2, typename Axis2, typename Conv2>
28  friend class StorableHistoNDFunctor;
29 
30  public:
32 
34 
39  template <class Num2>
40  inline StorableHistoNDFunctor(const HistoND<Num2, Axis>& table, const unsigned degree = 1)
42  validateInterDegree(degree, table.isUniformlyBinned());
43  }
44 
45  template <class Num2>
46  inline StorableHistoNDFunctor(const HistoND<Num2, Axis>& table, const unsigned degree, const std::string& descr)
47  : StorableMultivariateFunctor(descr), table_(table), deg_(degree) {
48  validateInterDegree(degree, table.isUniformlyBinned());
49  }
51 
53  template <class Num2, class Conv2>
56  table_(tab.table_, Same<Num2>(), tab.title().c_str(), tab.accumulatedDataLabel().c_str()),
57  deg_(tab.deg_) {}
58 
60  StorableHistoNDFunctor() = delete;
61 
63 
64  unsigned minDim() const override { return table_.dim(); };
65 
66  double operator()(const double* point, unsigned dim) const override;
67 
69  inline unsigned interpolationDegree() const { return deg_; }
70 
72  void setInterpolationDegree(const unsigned deg);
73 
75 
76  inline Table& interpolator() { return table_; }
77  inline const Table& interpolator() const { return table_; }
79 
81 
82  inline ArrayND<Numeric>& table() { return const_cast<ArrayND<Numeric>&>(table_.binContents()); }
83 
84  inline const ArrayND<Numeric>& table() const { return table_.binContents(); }
86 
88  inline void setConverter(const Converter& conv) { conv_ = conv; }
89 
91  // Method related to "geners" I/O
92  gs::ClassId classId() const override { return gs::ClassId(*this); }
93  bool write(std::ostream& of) const override;
95 
96  // I/O methods needed for reading
97  static inline const char* classname();
98  static inline unsigned version() { return 1; }
99  static StorableHistoNDFunctor* read(const gs::ClassId& id, std::istream& in);
100 
101  protected:
102  bool isEqual(const StorableMultivariateFunctor& other) const override {
103  // Note the use of static_cast rather than dynamic_cast below.
104  // static_cast works faster and it is guaranteed to succeed here.
105  const StorableHistoNDFunctor& r = static_cast<const StorableHistoNDFunctor&>(other);
106  return table_ == r.table_ && deg_ == r.deg_ && this->description() == other.description();
107  }
108 
109  private:
111  unsigned deg_;
112  Converter conv_;
113 
114  static void validateInterDegree(unsigned deg, bool isUniform);
115  };
116 } // namespace npstat
117 
119 
120 #include "Alignment/Geners/interface/binaryIO.hh"
121 #include "Alignment/Geners/interface/CPP11_auto_ptr.hh"
122 #include "Alignment/Geners/interface/IOException.hh"
123 
125 
126 namespace npstat {
127  template <class Numeric, class Axis, class Converter>
128  double StorableHistoNDFunctor<Numeric, Axis, Converter>::operator()(const double* point, const unsigned dim) const {
129  return conv_(interpolateHistoND(table_, point, dim, deg_));
130  }
131 
132  template <class Numeric, class Axis, class Converter>
134  static const std::string myClass(gs::template_class_name<Numeric, Axis>("npstat::StorableHistoNDFunctor"));
135  return myClass.c_str();
136  }
137 
138  template <class Numeric, class Axis, class Converter>
140  gs::write_pod(of, this->description());
141  gs::write_pod(of, deg_);
142  return table_.classId().write(of) && table_.write(of);
143  }
144 
145  template <class Numeric, class Axis, class Converter>
147  const gs::ClassId& id, std::istream& in) {
148  static const gs::ClassId current(gs::ClassId::makeId<StorableHistoNDFunctor<Numeric, Axis> >());
149  current.ensureSameId(id);
150 
151  std::string descr;
152  gs::read_pod(in, &descr);
153  unsigned deg;
154  gs::read_pod(in, &deg);
155  gs::ClassId tabid(in, 1);
156  if (in.fail())
157  throw gs::IOReadFailure(
158  "In npstat::StorableHistoNDFunctor::read: "
159  "input stream failure");
160  CPP11_auto_ptr<Table> tab(Table::read(tabid, in));
161  return new StorableHistoNDFunctor(*tab, deg, descr);
162  }
163 
164  template <class Numeric, class Axis, class Converter>
166  validateInterDegree(deg, table_.isUniformlyBinned());
167  deg_ = deg;
168  }
169 
170  template <class Numeric, class Axis, class Converter>
172  const bool isUniform) {
173  const bool ok = isUniform ? (deg == 0 || deg == 1 || deg == 3) : (deg == 0 || deg == 1);
174  if (!ok)
176  "In npstat::StorableHistoNDFunctor::validateInterDegree: "
177  "unsupported interpolation degree");
178  }
179 } // namespace npstat
180 
181 #endif // NPSTAT_STORABLEHISTONDFUNCTOR_HH_
unsigned dim() const
Definition: HistoND.h:173
Interface definition for storable multivariate functors.
StorableHistoNDFunctor(const StorableHistoNDFunctor< Num2, Axis, Conv2 > &tab)
bool write(std::ostream &of) const override
Arbitrary-dimensional histogram template.
static void validateInterDegree(unsigned deg, bool isUniform)
Exceptions for the npstat namespace.
bool isEqual(const StorableMultivariateFunctor &other) const override
const ArrayND< Numeric > & binContents() const
Definition: HistoND.h:182
static StorableHistoNDFunctor * read(const gs::ClassId &id, std::istream &in)
Interface definitions and concrete simple functors for a variety of functor-based calculations...
const ArrayND< Numeric > & table() const
EPOS::IO_EPOS conv
double operator()(const double *point, unsigned dim) const override
gs::ClassId classId() const override
StorableHistoNDFunctor(const HistoND< Num2, Axis > &table, const unsigned degree, const std::string &descr)
Float interpolateHistoND(const HistoND< Float, Axis > &histo, const double *coords, unsigned coordsDim, unsigned interpolationDegree)
StorableHistoNDFunctor(const HistoND< Num2, Axis > &table, const unsigned degree=1)
unsigned minDim() const override
void setInterpolationDegree(const unsigned deg)
Interpolate histogram contents.
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
void setConverter(const Converter &conv)