Jpp master_rocky-44-g75b7c4f75
the software that should make you happy
Loading...
Searching...
No Matches
JToolsToolkit.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JTOOLSTOOLKIT__
2#define __JTOOLS__JTOOLSTOOLKIT__
3
4#include "JLang/JException.hh"
5#include "JLang/JBool.hh"
6#include "JMath/JZero.hh"
8#include "JTools/JElement.hh"
14
15
16/**
17 * \file
18 *
19 * This include file contains various recursive methods to operate on multi-dimensional collections.
20 * \author mdejong
21 */
22namespace JTOOLS {}
23namespace JPP { using namespace JTOOLS; }
24
25/**
26 * Auxiliary classes and methods for multi-dimensional interpolations and histograms.
27 */
28namespace JTOOLS {
29
33 using JLANG::JBool;
34
35
36 /**
37 * Conversion of data points to cumulative probability distribition (CDF).
38 *
39 * Note that the data type of the input container should be preserved
40 * so that the corresponding method <tt>integrate()</tt> is used.
41 *
42 * \param input collection
43 * \param output mappable collection
44 * \param eps minimal step size
45 * \return integral value
46 */
47 template<class JContainer_t,
48 class JKey_t,
49 class JValue_t>
50 inline typename JContainer_t::ordinate_type
51 makeCDF(const JContainer_t& input,
53 const typename JContainer_t::ordinate_type eps = JMATH::zero)
54 {
55 typedef typename JContainer_t::ordinate_type ordinate_type;
56 typedef typename JContainer_t::abscissa_type abscissa_type;
58 typedef JCollection<element_type> buffer_type;
59
60
61 if (input.getSize() > 1) {
62
63 buffer_type buffer;
64
65 const ordinate_type V = integrate(input, buffer);
66
67 if (V == ordinate_type(JMATH::zero)) {
68 THROW(JDivisionByZero, "Method makeCDF(): integral equals zero.");
69 }
70
71 output.clear();
72
73 typename buffer_type::const_iterator i = buffer.begin();
74
75 for ( ; i != buffer.end() && i->getY() <= 0.5 * eps * V; ++i) {}
76
77 if (i != buffer.end()) {
78
79 // force first point at (0, ymin)
80
81 JKey_t x = 0.0;
82 JValue_t y = i->getX();
83
84 output.put(x, y);
85
86 JKey_t xmax = x;
87 JValue_t ymax = y;
88
89 for (++i; i != buffer.end(); ++i) {
90
91 x = i->getY() / V;
92 y = i->getX();
93
94 if (x > xmax) {
95
96 ymax = y;
97
98 if (x > xmax + eps) {
99
100 output.put(x, y);
101
102 xmax = x;
103 }
104 }
105 }
106
107 // force last point at (1, ymax)
108
109 x = 1.0;
110 y = ymax;
111
112 output.put(x,y);
113
114 } else {
115 THROW(JDivisionByZero, "Method makeCDF(): no remaining data.");
116 }
117
118 return V;
119
120 } else {
121 THROW(JEmptyCollection, "Method makeCDF(): not sufficient input data.");
122 }
123 }
124
125
126 /**
127 * Get integral of input data points.
128 *
129 * \param input input data
130 * \return integral value
131 */
132 template<class JContainer_t>
133 inline typename JContainer_t::ordinate_type getIntegral(const JContainer_t& input)
134 {
136
137 return integrate(input, garbage);
138 }
139
140
141 /**
142 * Auxiliary method to get integral of input data points.
143 *
144 * \param input input data
145 * \return integral value
146 */
147 template<class JFunction_t,
148 template<class, class, class> class JMap_t,
149 class JTail_t>
150 inline typename JFunction_t::ordinate_type
151 getIntegral(const JMultiMap<typename JFunction_t::abscissa_type, JFunction_t, JMapList<JMap_t, JTail_t>, typename JFunction_t::distance_type>& input)
152 {
153 typedef typename JFunction_t::abscissa_type abscissa_type;
154 typedef typename JFunction_t::ordinate_type ordinate_type;
155 typedef typename JFunction_t::distance_type distance_type;
156 typedef typename JFunctionalMap<JMap_t>::template function_type<abscissa_type,
157 ordinate_type,
158 ordinate_type,
159 distance_type> buffer_type;
160 typedef JMultiMap<abscissa_type, JFunction_t, JMapList<JMap_t, JTail_t>, distance_type> multimap_type;
161
162 static buffer_type buffer;
163
164 buffer.configure(input);
165
166 typename buffer_type::iterator out = buffer.begin();
167
168 for (typename multimap_type::const_iterator in = input.begin(); in != input.end(); ++in, ++out) {
169 out->getY() = getIntegral(in->getY());
170 }
171
172 buffer.compile();
173
174 return getIntegral(buffer);
175 }
176
177
178 /**
179 * Get integral of input data points.
180 *
181 * \param input input data
182 * \return integral value
183 */
184 template<class JFunction_t,
185 template<class, class, class> class JMap_t,
186 class JTail_t>
187 inline typename JFunction_t::ordinate_type
188 getIntegral(const JMultiFunction<JFunction_t, JMapList<JMap_t, JTail_t>, typename JFunction_t::distance_type>& input)
189 {
190 typedef typename JFunction_t::abscissa_type abscissa_type;
191 typedef typename JFunction_t::ordinate_type ordinate_type;
192 typedef typename JFunction_t::distance_type distance_type;
193 typedef typename JFunctionalMap<JMap_t>::template function_type<abscissa_type,
194 ordinate_type,
195 ordinate_type,
196 distance_type> buffer_type;
197 typedef JMultiFunction<JFunction_t, JMapList<JMap_t, JTail_t>, distance_type> multifunction_type;
198
199 buffer_type buffer;
200
201 for (typename multifunction_type::const_iterator i = input.begin(); i != input.end(); ++i) {
202 buffer.put(i->getX(), getIntegral(i->getY()));
203 }
204
205 buffer.compile();
206
207 return getIntegral(buffer);
208 }
209
210
211 /**
212 * Reset value.
213 *
214 * The value is set to (the equivalent of) zero, see method JMATH::getZero.
215 *
216 * \param value value
217 */
218 template<class T>
219 inline void reset(T& value)
220 {
221 value = JMATH::getZero<T>();
222 }
223
224
225 /**
226 * Recursive reset of collection.
227 *
228 * \param collection collection
229 */
230 template<class JElement_t, class JDistance_t>
232 {
233 typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
234
235 for (iterator i = collection.begin(); i != collection.end(); ++i) {
236 reset(i->getY());
237 }
238 }
239
240
241 /**
242 * Copy of input to output.
243 *
244 * The output value is set to the input value.
245 *
246 * \param input input
247 * \param output output
248 */
249 template<class T>
250 inline void copy(const T& input, T& output)
251 {
252 output = input;
253 }
254
255
256 /**
257 * Recursive copy of input collection to output collection.
258 *
259 * \param input input
260 * \param output output
261 */
262 template<class JElement_t, class JDistance_t, class JKey_t, class JValue_t>
264 {
265 typedef typename JCollection<JElement_t, JDistance_t>::const_iterator const_iterator;
266
267 output.clear();
268
269 for (const_iterator i = input.begin(); i != input.end(); ++i) {
270 copy(i->getY(), output.get(i->getX()));
271 }
272 }
273
274
275 /**
276 * Configuration of value.
277 *
278 * This is a fall-back method; it does nothing.
279 *
280 * \param value value
281 * \param bounds bounds
282 * \param option false
283 */
284 template<class T, class JAbscissa_t>
285 inline void configure(const T& value, const JAbstractCollection<JAbscissa_t>& bounds, JBool<false> option)
286 {}
287
288
289 /**
290 * Recursive configuration of collection.
291 *
292 * \param collection collection
293 * \param bounds bounds
294 * \param option true
295 */
296 template<class JElement_t, class JDistance_t>
299 JBool<true> option = JBool<true>())
300 {
301 typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
302 typedef typename JCollection<JElement_t, JDistance_t>::ordinate_type ordinate_type;
303
304 collection.configure(bounds);
305
306 for (iterator i = collection.begin(); i != collection.end(); ++i) {
308 }
309 }
310
311
312 /**
313 * Accumulation of value.
314 *
315 * This is a fall-back method; it does nothing.
316 *
317 * \param value value
318 * \param option false
319 */
320 template<class T>
321 inline void accumulate(T& value, JBool<false> option)
322 {}
323
324
325 /**
326 * Recursive accumulation of collection.
327 *
328 * \param collection collection
329 * \param option true
330 */
331 template<class JElement_t, class JDistance_t>
333 {
334 typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
335 typedef typename JCollection<JElement_t, JDistance_t>::ordinate_type ordinate_type;
336
337 for (iterator i = collection.begin(); i != collection.end(); ++i) {
339 }
340
341 if (collection.getSize() > 1) {
342
343 for (iterator j = collection.begin(), i = j++; j != collection.end(); ++i, ++j) {
344 j->getY() += i->getY();
345 }
346
347 reset(collection.begin()->getY());
348 }
349 }
350}
351
352#endif
General purpose class for a collection of sorted elements.
The elements in a collection are sorted according to their abscissa values and a given distance opera...
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition of zero value for any class.
Exception for division by zero.
Exception for an empty collection.
General exception.
Definition JException.hh:24
General purpose class for collection of elements, see: <a href="JTools.PDF";>Collection of elements....
Definition JSet.hh:22
void configure(const JAbstractCollection< abscissa_type > &bounds)
Configure collection.
JElement_t::ordinate_type ordinate_type
virtual int getSize() const override
Get number of elements.
container_type::iterator iterator
const ordinate_type & getY(int index) const
Get ordinate value.
container_type::const_iterator const_iterator
Multidimensional interpolation method.
Multidimensional map.
Definition JMultiMap.hh:52
T getZero()
Get zero value for a given data type.
Definition JZero.hh:26
static const JZero zero
Function object to assign zero value.
Definition JZero.hh:105
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
void copy(const T &input, T &output)
Copy of input to output.
JContainer_t::ordinate_type makeCDF(const JContainer_t &input, JMappableCollection< JKey_t, JValue_t > &output, const typename JContainer_t::ordinate_type eps=JMATH::zero)
Conversion of data points to cumulative probability distribition (CDF).
void accumulate(T &value, JBool< false > option)
Accumulation of value.
JElement_t::ordinate_type integrate(const JCollection< JElement_t, JDistance_t > &input, typename JMappable< JElement_t >::map_type &output)
Conversion of data points to integral values.
JContainer_t::ordinate_type getIntegral(const JContainer_t &input)
Get integral of input data points.
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
int j
Definition JPolint.hh:792
void reset(T &value)
Reset value.
Auxiliary template class for type bool.
Definition JBool.hh:21
Abstract interface for abscissa values of a collection of elements.
Auxiliary class to check whether given template is a collection, i.e. has a defined type collection_t...
Definition JAssembler.hh:24
2D Element.
Definition JPolint.hh:1131
Auxiliary class to define corresponding one-dimensional function interpolator function_type.
Map list.
Definition JMapList.hh:25
Template interface definition for associative collection of elements.
void put(typename JClass< key_type > ::argument_type key, typename JClass< mapped_type >::argument_type value)
Put pair-wise element (key,value) into collection.
virtual void clear()=0
Clear.
virtual const mapped_type & get(typename JClass< key_type >::argument_type key) const =0
Get mapped value.