Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JTransformableFunction.cc
Go to the documentation of this file.
1
2#include <string>
3#include <iostream>
4#include <iomanip>
5
6#include "TMath.h"
7#include "TRandom.h"
8
12#include "JTools/JQuantile.hh"
13
14#include "Jeep/JPrint.hh"
15#include "Jeep/JParser.hh"
16#include "Jeep/JMessage.hh"
17
18
19namespace {
20
21 using namespace JPP;
22
23 /**
24 * Test function.
25 *
26 * \param x x value
27 * \param y y value
28 * \return function value
29 */
30 inline Double_t f2(const Double_t x,
31 const Double_t y)
32 {
33 return TMath::Gaus(y, 0.0, x, kTRUE);
34 }
35
36
37 /**
38 * Derivative of test function.
39 *
40 * \param x x value
41 * \param y y value
42 * \return derivative value
43 */
44 inline Double_t g2(const Double_t x,
45 const Double_t y)
46 {
47 return TMath::Gaus(y, 0.0, x, kTRUE) * -y/x;
48 }
49
50
51 /**
52 * Integral of test function.
53 *
54 * \param x x value
55 * \param y y value
56 * \return integral value
57 */
58 inline Double_t G2(const Double_t x,
59 const Double_t y)
60 {
61 return 0.5 * (1.0 + TMath::Erf(sqrt(0.5)*y/x));
62 }
63
64
66
67 typedef typename function_type::transformer_type transformer_type;
68 typedef typename function_type::result_type result_type;
69
70
71 /**
72 * Transformer.
73 */
74 struct JTransformer_t :
75 public transformer_type
76 {
77 typedef typename transformer_type::clone_type clone_type;
78 typedef typename transformer_type::argument_type argument_type;
79 typedef typename transformer_type::const_array_type const_array_type;
80
81
82 /**
83 * Evaluate y as a function of x.
84 *
85 * \param buffer x value
86 * \param xn y value
87 * \return y value
88 */
89 virtual argument_type putXn(const_array_type& buffer, const argument_type xn) const
90 {
91 return xn * buffer[0];
92 }
93
94
95 /**
96 * Evaluate y as a function of x.
97 *
98 * \param buffer x value
99 * \param xn y value
100 * \return y value
101 */
102 virtual argument_type getXn(const_array_type& buffer, const argument_type xn) const
103 {
104 return xn / buffer[0];
105 }
106
107
108 /**
109 * Weight function.
110 *
111 * \param buffer x0 - xn-1 values
112 * \return weight
113 */
114 virtual double getWeight(const_array_type& buffer) const
115 {
116 return 1.0;
117 }
118
119
120 /**
121 * Read from input.
122 *
123 * \param in reader
124 * \return reader
125 */
126 virtual JReader& read(JReader& in)
127 {
128 return in;
129 }
130
131
132 /**
133 * Write to output.
134 *
135 * \param out writer
136 * \return writer
137 */
138 virtual JWriter& write(JWriter& out) const
139 {
140 return out;
141 }
142
143
144 /**
145 * Get clone of this object.
146 *
147 * \return pointer to newly created object
148 */
149 virtual clone_type clone() const
150 {
151 return new JTransformer_t(*this);
152 }
153 };
154}
155
156
157/**
158 * \file
159 *
160 * Example program to test transformable function.
161 * \author mdejong
162 */
163int main(int argc, char **argv)
164{
165 using namespace std;
166 using namespace JPP;
167
168 int numberOfEvents;
169 int numberOfBins;
170 bool transform;
171 int debug;
172
173 try {
174
175 JParser<> zap("Example program to test transformable function.");
176
177 zap['n'] = make_field(numberOfEvents);
178 zap['N'] = make_field(numberOfBins) = 35;
179 zap['U'] = make_field(transform);
180 zap['d'] = make_field(debug) = 3;
181
182 zap(argc, argv);
183 }
184 catch(const exception &error) {
185 FATAL(error.what() << endl);
186 }
187
188 if (numberOfEvents <= 0) {
189 FATAL("No events." << endl);
190 }
191
192
193 const double xmin = 0.1;
194 const double xmax = 1.0;
195 const double dx = (xmax - xmin) / 3;
196
197 function_type h2;
198
199 for (double x = xmin; x < xmax + 0.5*dx; x += dx) {
200
201 const double ymin = -5.0 * xmax;
202 const double ymax = +5.0 * xmax;
203 const double dy = (ymax - ymin) / (numberOfBins - 1);
204
205 for (double y = ymin; y < ymax + 0.5*dy; y += dy) {
206 h2[x][y] = f2(x,y);
207 }
208 }
209
210 h2.setExceptionHandler(new typename function_type::JDefaultResult(JMATH::zero));
211
212 if (transform) {
213 h2.transform(JTransformer_t());
214 }
215
216 h2.compile();
217
218
219 JQuantile Q[] = {
220 JQuantile("function"),
221 JQuantile("derivative"),
222 JQuantile("integral")
223 };
224
225 const double ymin = -5.0 * xmax;
226 const double ymax = +5.0 * xmax;
227
228 for (int i = 0; i != numberOfEvents; ++i) {
229
230 const double x = gRandom->Uniform(xmin, xmax);
231 const double y = gRandom->Uniform(ymin, ymax);
232
233 try {
234
235 const result_type value = h2(x,y);
236
237 const double f = f2(x,y);
238 const double fp = g2(x,y);
239 const double v = G2(x,y);
240
241 Q[0].put(value.f - f);
242 Q[1].put(value.fp - fp);
243 Q[2].put(value.v - v);
244 }
245 catch(const std::exception& error) {}
246 }
247
248
249 if (debug >= notice_t) {
250
251 cout << " ";
252 for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
253 cout << " " << setw(10) << Q[i].getTitle();
254 }
255 cout << endl;
256
257 cout << "mean ";
258 for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
259 cout << " " << SCIENTIFIC(10,2) << Q[i].getMean();
260 }
261 cout << endl;
262
263 cout << "RMS ";
264 for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
265 cout << " " << SCIENTIFIC(10,2) << Q[i].getSTDev();
266 }
267 cout << endl;
268 }
269}
Various implementations of functional maps.
General purpose messaging.
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
I/O formatting auxiliaries.
int numberOfBins
number of bins for average CDF integral of optical module
Definition JSirene.cc:73
int main(int argc, char **argv)
Interface for binary input.
Interface for binary output.
const std::string & getTitle() const
Get title.
Definition JTitle.hh:55
Utility class to parse command line options.
Definition JParser.hh:1698
Transformable multidimensional function.
bool write(const Vec &v, std::ostream &os)
Write a Vec(tor) to a stream.
Definition io_ascii.hh:155
double getWeight(T __begin, T __end)
Get total weight of data points.
std::istream & read(std::istream &in, JTestSummary &summary, const char delimiter=' ')
Read test summary.
@ notice_t
notice
Definition JMessage.hh:32
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 data structure for running average, standard deviation and quantiles.
Definition JQuantile.hh:46
double getSTDev() const
Get standard deviation.
Definition JQuantile.hh:281
void put(const double x, const double w=1.0)
Put value.
Definition JQuantile.hh:133
double getMean() const
Get mean value.
Definition JQuantile.hh:252
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488