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)
41  : StorableMultivariateFunctor(), table_(table), deg_(degree) {
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 
61  unsigned minDim() const override { return table_.dim(); };
62 
63  double operator()(const double* point, unsigned dim) const override;
64 
66  inline unsigned interpolationDegree() const { return deg_; }
67 
69  void setInterpolationDegree(const unsigned deg);
70 
72 
73  inline Table& interpolator() { return table_; }
74  inline const Table& interpolator() const { return table_; }
76 
78 
79  inline ArrayND<Numeric>& table() { return const_cast<ArrayND<Numeric>&>(table_.binContents()); }
80 
81  inline const ArrayND<Numeric>& table() const { return table_.binContents(); }
83 
85  inline void setConverter(const Converter& conv) { conv_ = conv; }
86 
88  // Method related to "geners" I/O
89  gs::ClassId classId() const override { return gs::ClassId(*this); }
90  bool write(std::ostream& of) const override;
92 
93  // I/O methods needed for reading
94  static inline const char* classname();
95  static inline unsigned version() { return 1; }
96  static StorableHistoNDFunctor* read(const gs::ClassId& id, std::istream& in);
97 
98  protected:
99  bool isEqual(const StorableMultivariateFunctor& other) const override {
100  // Note the use of static_cast rather than dynamic_cast below.
101  // static_cast works faster and it is guaranteed to succeed here.
102  const StorableHistoNDFunctor& r = static_cast<const StorableHistoNDFunctor&>(other);
103  return table_ == r.table_ && deg_ == r.deg_ && this->description() == other.description();
104  }
105 
106  private:
107  StorableHistoNDFunctor() = delete;
108 
109  Table table_;
110  unsigned deg_;
111  Converter conv_;
112 
113  static void validateInterDegree(unsigned deg, bool isUniform);
114  };
115 } // namespace npstat
116 
118 
119 #include "Alignment/Geners/interface/binaryIO.hh"
120 #include "Alignment/Geners/interface/CPP11_auto_ptr.hh"
121 #include "Alignment/Geners/interface/IOException.hh"
122 
124 
125 namespace npstat {
126  template <class Numeric, class Axis, class Converter>
127  double StorableHistoNDFunctor<Numeric, Axis, Converter>::operator()(const double* point, const unsigned dim) const {
128  return conv_(interpolateHistoND(table_, point, dim, deg_));
129  }
130 
131  template <class Numeric, class Axis, class Converter>
133  static const std::string myClass(gs::template_class_name<Numeric, Axis>("npstat::StorableHistoNDFunctor"));
134  return myClass.c_str();
135  }
136 
137  template <class Numeric, class Axis, class Converter>
139  gs::write_pod(of, this->description());
140  gs::write_pod(of, deg_);
141  return table_.classId().write(of) && table_.write(of);
142  }
143 
144  template <class Numeric, class Axis, class Converter>
146  const gs::ClassId& id, std::istream& in) {
147  static const gs::ClassId current(gs::ClassId::makeId<StorableHistoNDFunctor<Numeric, Axis> >());
148  current.ensureSameId(id);
149 
150  std::string descr;
151  gs::read_pod(in, &descr);
152  unsigned deg;
153  gs::read_pod(in, &deg);
154  gs::ClassId tabid(in, 1);
155  if (in.fail())
156  throw gs::IOReadFailure(
157  "In npstat::StorableHistoNDFunctor::read: "
158  "input stream failure");
159  CPP11_auto_ptr<Table> tab(Table::read(tabid, in));
160  return new StorableHistoNDFunctor(*tab, deg, descr);
161  }
162 
163  template <class Numeric, class Axis, class Converter>
166  deg_ = deg;
167  }
168 
169  template <class Numeric, class Axis, class Converter>
171  const bool isUniform) {
172  const bool ok = isUniform ? (deg == 0 || deg == 1 || deg == 3) : (deg == 0 || deg == 1);
173  if (!ok)
175  "In npstat::StorableHistoNDFunctor::validateInterDegree: "
176  "unsupported interpolation degree");
177  }
178 } // namespace npstat
179 
180 #endif // NPSTAT_STORABLEHISTONDFUNCTOR_HH_
bool write(std::ostream &of) const
Definition: HistoND.h:3490
static HepMC::IO_HEPEVT conv
unsigned minDim() const override
Interface definition for storable multivariate functors.
StorableHistoNDFunctor(const StorableHistoNDFunctor< Num2, Axis, Conv2 > &tab)
Arbitrary-dimensional histogram template.
static void validateInterDegree(unsigned deg, bool isUniform)
gs::ClassId classId() const
Definition: HistoND.h:921
const ArrayND< Numeric > & binContents() const
Definition: HistoND.h:179
Exceptions for the npstat namespace.
gs::ClassId classId() const override
bool write(std::ostream &of) const override
bool isUniformlyBinned() const
Definition: HistoND.h:1511
static StorableHistoNDFunctor * read(const gs::ClassId &id, std::istream &in)
Interface definitions and concrete simple functors for a variety of functor-based calculations...
double operator()(const double *point, unsigned dim) const override
static HistoND * read(const gs::ClassId &id, std::istream &in)
Definition: HistoND.h:3501
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)
bool isEqual(const StorableMultivariateFunctor &other) const override
StorableHistoNDFunctor(const HistoND< Num2, Axis > &table, const unsigned degree=1)
const ArrayND< Numeric > & table() const
void setInterpolationDegree(const unsigned deg)
Interpolate histogram contents.
unsigned dim() const
Definition: HistoND.h:170
*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)