npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
coordAndWeight.hh
1 #ifndef NPSTAT_COORDANDWEIGHT_HH_
2 #define NPSTAT_COORDANDWEIGHT_HH_
3 
4 //======================================================================
5 // coordAndWeight.hh
6 //
7 // This is an internal header which is subject to change without notice.
8 // Application code should never call the functions declared/defined in
9 // this header directly.
10 //
11 // Author: I. Volobouev
12 //
13 // May 2022
14 //======================================================================
15 
16 #include <vector>
17 #include <utility>
18 #include <cassert>
19 #include <stdexcept>
20 
21 namespace npstat {
22  namespace Private {
23  template<typename Numeric>
24  inline void coordAndWeight(const Numeric& r,
25  double* x, double* w)
26  {
27  *x = r;
28  *w = 1.0;
29  }
30 
31  template<typename Num1, typename Num2>
32  inline void coordAndWeight(const std::pair<Num1, Num2>& r,
33  double* x, double* w)
34  {
35  *x = r.first;
36  *w = r.second;
37  }
38 
39  template<typename Numeric>
41  {
42  inline WeightedCoord(const double x, const double /* w */)
43  : value(static_cast<Numeric>(x)) {}
44  Numeric value;
45  };
46 
47  template<typename Num1, typename Num2>
48  struct WeightedCoord<std::pair<Num1, Num2> >
49  {
50  inline WeightedCoord(const double x, const double w)
51  : value(x, w) {}
52  std::pair<Num1, Num2> value;
53  };
54 
55  template<typename Numeric>
56  struct SampleWeight
57  {
58  inline SampleWeight(const std::vector<Numeric>& vec)
59  : value(vec.size()) {}
60  double value;
61  };
62 
63  template<typename Num1, typename Num2>
64  struct SampleWeight<std::pair<Num1, Num2> >
65  {
66  inline SampleWeight(const std::vector<std::pair<Num1, Num2> >& vec)
67  {
68  long double sum = 0.0L;
69  const unsigned long sz = vec.size();
70  for (unsigned long i=0; i<sz; ++i)
71  sum += static_cast<long double>(vec[i].second);
72  value = sum;
73  }
74  double value;
75  };
76 
77  template<typename Numeric>
79  {
80  inline SampleSquaredWeight(const std::vector<Numeric>& vec)
81  : value(vec.size()) {}
82  double value;
83  };
84 
85  template<typename Num1, typename Num2>
86  struct SampleSquaredWeight<std::pair<Num1, Num2> >
87  {
88  inline SampleSquaredWeight(const std::vector<std::pair<Num1, Num2> >& vec)
89  {
90  long double sum = 0.0L;
91  const unsigned long sz = vec.size();
92  for (unsigned long i=0; i<sz; ++i)
93  {
94  const long double w = static_cast<long double>(vec[i].second);
95  sum += w*w;
96  }
97  value = sum;
98  }
99  double value;
100  };
101 
102  template<typename Numeric>
104  {
105  inline static void set(Numeric* /* arr */, const double* /* weights */,
106  const unsigned long /* nWeights */)
107  {
108  throw std::runtime_error("In npstat::Private::SetPairSeconds::set: "
109  "array to set is not of pair type");
110  }
111  };
112 
113  template<typename Num1, typename Num2>
114  struct SetPairSeconds<std::pair<Num1, Num2> >
115  {
116  inline static void set(std::pair<Num1,Num2>* arr, const double* weights,
117  const unsigned long nWeights)
118  {
119  if (nWeights)
120  {
121  assert(arr);
122  assert(weights);
123  for (unsigned long i=0; i<nWeights; ++i, ++arr)
124  arr->second = *weights++;
125  }
126  }
127  };
128  }
129 }
130 
131 #endif // NPSTAT_COORDANDWEIGHT_HH_
Definition: AbsArrayProjector.hh:14
Definition: coordAndWeight.hh:79
Definition: coordAndWeight.hh:57
Definition: coordAndWeight.hh:104
Definition: coordAndWeight.hh:41