npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
MultivariateFunctorScanner.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_MULTIVARIATEFUNCTORSCANNER_HH_
2 #define NPSTAT_MULTIVARIATEFUNCTORSCANNER_HH_
3 
4 /*!
5 // \file MultivariateFunctorScanner.hh
6 //
7 // \brief Adapts any AbsMultivariateFunctor for use with ArrayND method
8 // "functorFill"
9 //
10 // Author: I. Volobouev
11 //
12 // July 2012
13 */
14 
15 #include <vector>
16 #include <cassert>
17 #include <stdexcept>
18 
20 
21 namespace npstat {
22  /**
23  // This class adapts an object derived from AbsMultivariateFunctor
24  // so that it can be used with ArrayND method "functorFill" and such
25  */
26  template<class IndexMapper>
28  {
29  public:
30  /**
31  // A mapper for each coordinate in the "maps" argument will
32  // convert the array index into a proper argument for the scanned
33  // density.
34  //
35  // This functor will NOT make copies of either "fcn" or "maps"
36  // parameters. These parameters will be used by reference only
37  // (aliased). It is up to the user of this class to ensure proper
38  // lifetime of these objects.
39  */
41  const std::vector<IndexMapper>& maps)
42  : fcn_(fcn), mapping_(maps), buf_(fcn.minDim()), dim_(fcn.minDim())
43  {
44  if (!(dim_ && dim_ == maps.size())) throw std::invalid_argument(
45  "In npstat::MultivariateFunctorScanner constructor: "
46  "incompatible arguments");
47  if (dim_ != fcn.maxDim()) throw std::invalid_argument(
48  "In npstat::MultivariateFunctorScanner constructor: "
49  "functors of variable dimensionality are not supported");
50  }
51 
52  /** Calculate the functor value for the given array indices */
53  inline double operator()(
54  const unsigned* index, const unsigned indexLen) const
55  {
56  if (dim_ != indexLen) throw std::invalid_argument(
57  "In npstat::MultivariateFunctorScanner::operator(): "
58  "incompatible input point dimensionality");
59  assert(index);
60  double* x = &buf_[0];
61  for (unsigned i=0; i<dim_; ++i)
62  x[i] = mapping_[i](index[i]);
63  return fcn_(x, dim_);
64  }
65 
66  private:
68 
69  const AbsMultivariateFunctor& fcn_;
70  const std::vector<IndexMapper>& mapping_;
71  mutable std::vector<double> buf_;
72  unsigned dim_;
73  };
74 }
75 
76 #endif // NPSTAT_MULTIVARIATEFUNCTORSCANNER_HH_
Interface definition for multidimensional functors.
Definition: MultivariateFunctorScanner.hh:28
double operator()(const unsigned *index, const unsigned indexLen) const
Definition: MultivariateFunctorScanner.hh:53
MultivariateFunctorScanner(const AbsMultivariateFunctor &fcn, const std::vector< IndexMapper > &maps)
Definition: MultivariateFunctorScanner.hh:40
Definition: AbsArrayProjector.hh:14
Definition: AbsMultivariateFunctor.hh:19
virtual unsigned maxDim() const
Definition: AbsMultivariateFunctor.hh:32