Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JWatershed.cc
Go to the documentation of this file.
1
2#include <string>
3#include <iostream>
4#include <iomanip>
5#include <cmath>
6#include <vector>
7#include <map>
8#include <algorithm>
9
10#include "TRandom3.h"
11
12#include "JMath/JConstants.hh"
13#include "JMath/JWatershed.hh"
14
15#include "JLang/JManip.hh"
16
17#include "Jeep/JParser.hh"
18#include "Jeep/JMessage.hh"
19
20
21namespace {
22
23 /**
24 * Type definition of data point.
25 */
26 typedef double data_type;
27
28
29 /**
30 * Test function.
31 *
32 * \param x data point
33 * \return value
34 */
35 inline double getValue(const data_type x)
36 {
37 const double y = sin(x);
38
39 return y*y;
40 }
41
42
43 /**
44 * Distance between data points.
45 *
46 * \param first first data point
47 * \param second second data point
48 * \return distance
49 */
50 inline double getDistance(const data_type first, const data_type second)
51 {
52 return fabs(first - second);
53 }
54
55
56 /**
57 * Helper data structure.
58 */
59 struct ps {
60
61 ps(data_type x)
62 {
63 this->x = x;
64 this->l = (int) (x/JMATH::PI);
65 this->y = getValue(x);
66 }
67
69 int l;
70 double y;
71
72 friend inline std::ostream& operator<<(std::ostream& out, const ps& object)
73 {
74 using namespace std;
75
76 return out << "[" << setw(2) << object.l << "] (" << FIXED(6,3) << object.x << "," << FIXED(5,3) << object.y << ")";
77 }
78 };
79}
80
81
82/**
83 * \file
84 *
85 * Example program to test JGEOMETRY3D::JRotation3D class.
86 * \author mdejong
87 */
88int main(int argc, char**argv)
89{
90 using namespace std;
91 using namespace JPP;
92
93 int numberOfEvents;
95 UInt_t seed;
96 double Y;
97 int debug;
98
99 try {
100
101 JParser<> zap("Example program to test watershed algorithm.");
102
103 zap['n'] = make_field(numberOfEvents) = 1;
104 zap['N'] = make_field(numberOfPoints) = 10;
105 zap['S'] = make_field(seed) = 0;
106 zap['Y'] = make_field(Y) = 0.0;
107 zap['d'] = make_field(debug) = 3;
108
109 zap(argc, argv);
110 }
111 catch(const exception &error) {
112 FATAL(error.what() << endl);
113 }
114
115
116 gRandom->SetSeed(seed);
117
118
119 const int M = 10; // number of maxima
120 const int N = numeric_limits<int>::max();
121
122 for (int count = 0; count != numberOfEvents; ++count) {
123
125
126 for (int i = 0; i != numberOfPoints; ++i) {
127 data.push_back(gRandom->Uniform(0.0, M * PI));
128 }
129
130 if (debug >= debug_t) {
131 for (const data_type& i : data) {
132 cout << ps(i) << endl;
133 }
134 }
135
136 vector<data_type>::iterator __end = watershed(data.begin(), data.end(), getValue, getDistance, Y, N);
137
138 DEBUG("number of soultions " << distance(data.begin(),__end) << endl);
139
140 ASSERT(distance(data.begin(), __end) > 0 && distance(data.begin(), __end) <= N, "number of solutions");
141
142 if (debug >= debug_t) {
143
144 vector<data_type> buffer = data;
145
146 vector<data_type>::iterator p1 = buffer.begin();
147
148 for (vector<data_type>::iterator i = data.begin(); i != __end; ++i) {
149
150 cout << ps(*i) << " | ";
151
152 auto compare = [i](const data_type first, const data_type second)
153 {
154 return getDistance(*i,first) < getDistance(*i,second);
155 };
156
157 sort(p1, buffer.end(), compare);
158
159 double y = getValue(*p1);
160
161 ++p1;
162
163 if (p1 != buffer.end()) {
164
165 for (vector<data_type>::iterator p2 = next(p1); p2 != buffer.end() &&
166 (getValue(*p1) <= y + Y ||
167 getValue(*p2) <= y);
168 ++p1, ++p2) {
169
170 cout << ' ' << ps(*p1) << ' ' << FIXED(6,3) << getDistance(*i,*p1);
171
172 y = getValue(*p1);
173 }
174 }
175
176 cout << endl;
177 }
178 }
179
180 if (distance(data.begin(), __end) > 1) {
181
182 {
183 auto compare = [](const data_type first, const data_type second)
184 {
185 return getValue(first) > getValue(second);
186 };
187
188 ASSERT(is_sorted(data.begin(), __end, compare), "test if maxima are sorted");
189 }
190 {
191 for (vector<data_type>::iterator p = data.begin(); p != __end; ++p) {
192 for (vector<data_type>::iterator q = p; ++q != __end; ) {
193
194 const ps p1(*p);
195 const ps p2(*q);
196
197 if (p1.l == p2.l) { ASSERT(p2.y <= p1.y, "test if maxima " << p1 << " and " << p2 << " are not compatible"); }
198 }
199 }
200 }
201 {
204
205 for (vector<data_type>::iterator i = data.begin(); i != __end; ++i) {
206 m1[ps(*i).l].push_back(*i);
207 }
208
209 for (vector<data_type>::iterator i = __end; i != data.end(); ++i) {
210 m2[ps(*i).l].push_back(*i);
211 }
212
213 auto compare = [](const data_type first, const data_type second)
214 {
215 return getValue(first) > getValue(second);
216 };
217
218 for (auto& i : m1) {
219 ASSERT(is_sorted(i.second.begin(), i.second.end(), compare), "test if maxima are sorted in bin " << i.first);
220 }
221 for (auto& i : m2) {
222 sort(i.second.begin(), i.second.end(), compare);
223 }
224
225 for (const auto& i : m1) {
226
227 const ps p1(i.second[0]);
228
229 if (m2.count(p1.l)) {
230
231 const ps p2(m2[p1.l][0]);
232
233 ASSERT(p1.y >= p2.y, "test maximum in " << p1.l << " " << p1 << " " << p2);
234 }
235 }
236 }
237 }
238 }
239
240 return 0;
241}
TPaveText * p1
I/O manipulators.
Mathematical constants.
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:74
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2107
int numberOfPoints
Definition JResultPDF.cc:22
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Utility class to parse command line options.
Definition JParser.hh:1664
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition JScale.hh:47
static const double PI
Mathematical constants.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
std::vector< event_type > data_type
Definition JPerth.cc:105
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Data structure for measured coincidence rates of all pairs of PMTs in optical module.
Definition JFitK40.hh:110