Jpp test-rotations-new
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"
11#include "JTools/JRange.hh"
15
16
17/**
18 * \file
19 *
20 * This include file contains various recursive methods to operate on multi-dimensional collections.
21 * \author mdejong
22 */
23namespace JTOOLS {}
24namespace JPP { using namespace JTOOLS; }
25
26/**
27 * Auxiliary classes and methods for multi-dimensional interpolations and histograms.
28 */
29namespace JTOOLS {
30
34 using JLANG::JBool;
35
36
37 /**
38 * Conversion of data points to cumulative probability distribition (CDF).
39 *
40 * Note that the data type of the input container should be preserved
41 * so that the corresponding method <tt>integrate()</tt> is used.
42 *
43 * \param input collection
44 * \param output mappable collection
45 * \param eps minimal step size
46 * \return integral value
47 */
48 template<class JContainer_t,
49 class JKey_t,
50 class JValue_t>
51 inline typename JContainer_t::ordinate_type
52 makeCDF(const JContainer_t& input,
54 const typename JContainer_t::ordinate_type eps = JMATH::zero)
55 {
56 typedef typename JContainer_t::ordinate_type ordinate_type;
57 typedef typename JContainer_t::abscissa_type abscissa_type;
59 typedef JCollection<element_type> buffer_type;
60
61
62 if (input.getSize() > 1) {
63
64 buffer_type buffer;
65
66 const ordinate_type V = integrate(input, buffer);
67
68 if (V == ordinate_type(JMATH::zero)) {
69 THROW(JDivisionByZero, "Method makeCDF(): integral equals zero.");
70 }
71
72 output.clear();
73
74 typename buffer_type::const_iterator i = buffer.begin();
75
76 for ( ; i != buffer.end() && i->getY() <= 0.5 * eps * V; ++i) {}
77
78 if (i != buffer.end()) {
79
80 // force first point at (0, ymin)
81
82 JKey_t x = 0.0;
83 JValue_t y = i->getX();
84
85 output.put(x, y);
86
87 JKey_t xmax = x;
88 JValue_t ymax = y;
89
90 for (++i; i != buffer.end(); ++i) {
91
92 x = i->getY() / V;
93 y = i->getX();
94
95 if (x > xmax) {
96
97 ymax = y;
98
99 if (x > xmax + eps) {
100
101 output.put(x, y);
102
103 xmax = x;
104 }
105 }
106 }
107
108 // force last point at (1, ymax)
109
110 x = 1.0;
111 y = ymax;
112
113 output.put(x,y);
114
115 } else {
116 THROW(JDivisionByZero, "Method makeCDF(): no remaining data.");
117 }
118
119 return V;
120
121 } else {
122 THROW(JEmptyCollection, "Method makeCDF(): not sufficient input data.");
123 }
124 }
125
126
127 /**
128 * Get integral of input data points.
129 *
130 * \param input input data
131 * \return integral value
132 */
133 template<class JContainer_t>
134 inline typename JContainer_t::ordinate_type getIntegral(const JContainer_t& input)
135 {
137
138 return integrate(input, garbage);
139 }
140
141
142 /**
143 * Get integral of input data points.
144 *
145 * \param input input data
146 * \param range range
147 * \return integral value
148 */
149 template<class JContainer_t>
150 inline typename JContainer_t::ordinate_type getIntegral(const JContainer_t& input,
152 {
153 JContainer_t output;
154
155 integrate(input, output);
156
157 if (!output.empty())
158 return ((output.in_range(range.getUpperLimit()) ? get_value(output(range.getUpperLimit())) : output.rbegin()->getY()) -
159 (output.in_range(range.getLowerLimit()) ? get_value(output(range.getLowerLimit())) : output. begin()->getY()));
160 else
161 return 0.0;
162 }
163
164
165 /**
166 * Auxiliary method to get integral of input data points.
167 *
168 * \param input input data
169 * \return integral value
170 */
171 template<class JFunction_t,
172 template<class, class, class> class JMap_t,
173 class JTail_t>
174 inline typename JFunction_t::ordinate_type
175 getIntegral(const JMultiMap<typename JFunction_t::abscissa_type, JFunction_t, JMapList<JMap_t, JTail_t>, typename JFunction_t::distance_type>& input)
176 {
177 typedef typename JFunction_t::abscissa_type abscissa_type;
178 typedef typename JFunction_t::ordinate_type ordinate_type;
179 typedef typename JFunction_t::distance_type distance_type;
180 typedef typename JFunctionalMap<JMap_t>::template function_type<abscissa_type,
181 ordinate_type,
182 ordinate_type,
183 distance_type> buffer_type;
184 typedef JMultiMap<abscissa_type, JFunction_t, JMapList<JMap_t, JTail_t>, distance_type> multimap_type;
185
186 static buffer_type buffer;
187
188 buffer.configure(input);
189
190 typename buffer_type::iterator out = buffer.begin();
191
192 for (typename multimap_type::const_iterator in = input.begin(); in != input.end(); ++in, ++out) {
193 out->getY() = getIntegral(in->getY());
194 }
195
196 buffer.compile();
197
198 return getIntegral(buffer);
199 }
200
201
202 /**
203 * Get integral of input data points.
204 *
205 * \param input input data
206 * \return integral value
207 */
208 template<class JFunction_t,
209 template<class, class, class> class JMap_t,
210 class JTail_t>
211 inline typename JFunction_t::ordinate_type
212 getIntegral(const JMultiFunction<JFunction_t, JMapList<JMap_t, JTail_t>, typename JFunction_t::distance_type>& input)
213 {
214 typedef typename JFunction_t::abscissa_type abscissa_type;
215 typedef typename JFunction_t::ordinate_type ordinate_type;
216 typedef typename JFunction_t::distance_type distance_type;
217 typedef typename JFunctionalMap<JMap_t>::template function_type<abscissa_type,
218 ordinate_type,
219 ordinate_type,
220 distance_type> buffer_type;
221 typedef JMultiFunction<JFunction_t, JMapList<JMap_t, JTail_t>, distance_type> multifunction_type;
222
223 buffer_type buffer;
224
225 for (typename multifunction_type::const_iterator i = input.begin(); i != input.end(); ++i) {
226 buffer.put(i->getX(), getIntegral(i->getY()));
227 }
228
229 buffer.compile();
230
231 return getIntegral(buffer);
232 }
233
234
235 /**
236 * Reset value.
237 *
238 * The value is set to (the equivalent of) zero, see method JMATH::getZero.
239 *
240 * \param value value
241 */
242 template<class T>
243 inline void reset(T& value)
244 {
245 value = JMATH::getZero<T>();
246 }
247
248
249 /**
250 * Recursive reset of collection.
251 *
252 * \param collection collection
253 */
254 template<class JElement_t, class JDistance_t>
256 {
257 typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
258
259 for (iterator i = collection.begin(); i != collection.end(); ++i) {
260 reset(i->getY());
261 }
262 }
263
264
265 /**
266 * Copy of input to output.
267 *
268 * The output value is set to the input value.
269 *
270 * \param input input
271 * \param output output
272 */
273 template<class T>
274 inline void copy(const T& input, T& output)
275 {
276 output = input;
277 }
278
279
280 /**
281 * Recursive copy of input collection to output collection.
282 *
283 * \param input input
284 * \param output output
285 */
286 template<class JElement_t, class JDistance_t, class JKey_t, class JValue_t>
288 {
289 typedef typename JCollection<JElement_t, JDistance_t>::const_iterator const_iterator;
290
291 output.clear();
292
293 for (const_iterator i = input.begin(); i != input.end(); ++i) {
294 copy(i->getY(), output.get(i->getX()));
295 }
296 }
297
298
299 /**
300 * Configuration of value.
301 *
302 * This is a fall-back method; it does nothing.
303 *
304 * \param value value
305 * \param bounds bounds
306 * \param option false
307 */
308 template<class T, class JAbscissa_t>
309 inline void configure(const T& value, const JAbstractCollection<JAbscissa_t>& bounds, JBool<false> option)
310 {}
311
312
313 /**
314 * Recursive configuration of collection.
315 *
316 * \param collection collection
317 * \param bounds bounds
318 * \param option true
319 */
320 template<class JElement_t, class JDistance_t>
323 JBool<true> option = JBool<true>())
324 {
325 typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
326 typedef typename JCollection<JElement_t, JDistance_t>::ordinate_type ordinate_type;
327
328 collection.configure(bounds);
329
330 for (iterator i = collection.begin(); i != collection.end(); ++i) {
332 }
333 }
334
335
336 /**
337 * Accumulation of value.
338 *
339 * This is a fall-back method; it does nothing.
340 *
341 * \param value value
342 * \param option false
343 */
344 template<class T>
345 inline void accumulate(T& value, JBool<false> option)
346 {}
347
348
349 /**
350 * Recursive accumulation of collection.
351 *
352 * \param collection collection
353 * \param option true
354 */
355 template<class JElement_t, class JDistance_t>
357 {
358 typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
359 typedef typename JCollection<JElement_t, JDistance_t>::ordinate_type ordinate_type;
360
361 for (iterator i = collection.begin(); i != collection.end(); ++i) {
363 }
364
365 if (collection.getSize() > 1) {
366
367 for (iterator j = collection.begin(), i = j++; j != collection.end(); ++i, ++j) {
368 j->getY() += i->getY();
369 }
370
371 reset(collection.begin()->getY());
372 }
373 }
374}
375
376#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.
Auxiliary class to define a range between two values.
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
Range of values.
Definition JRange.hh:42
T getLowerLimit() const
Get lower limit.
Definition JRange.hh:202
T getUpperLimit() const
Get upper limit.
Definition JRange.hh:213
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:801
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.