Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JEditTripod.cc
Go to the documentation of this file.
1#include <iostream>
2#include <iomanip>
3#include <string>
4#include <vector>
5#include <set>
6#include <algorithm>
7
8#include "TRandom3.h"
9
10#include "JDetector/JTripod.hh"
11#include "JSupport/JMeta.hh"
12#include "JLang/JComparator.hh"
13#include "JLang/JComparison.hh"
14#include "Jeep/JContainer.hh"
15#include "Jeep/JPrint.hh"
16#include "Jeep/JParser.hh"
17#include "Jeep/JMessage.hh"
18
19
20namespace {
21
22 using namespace JPP;
23
24 /**
25 * Wild card for tripod identifier.
26 */
27 static const int WILDCARD = -1;
28
29 static const std::string set_t = "set"; //!< Set UTM position
30 static const std::string add_t = "add"; //!< Add UTM position
31 static const std::string sub_t = "sub"; //!< Subtract UTM position
32 static const std::string setx_t = "setx"; //!< Set East"
33 static const std::string sety_t = "sety"; //!< Set North"
34 static const std::string setz_t = "setz"; //!< Set depth
35 static const std::string addx_t = "addx"; //!< Add East"
36 static const std::string addy_t = "addy"; //!< Add North"
37 static const std::string addz_t = "addz"; //!< Add depth
38 static const std::string subx_t = "subx"; //!< Subtract East"
39 static const std::string suby_t = "suby"; //!< Subtract North"
40 static const std::string subz_t = "subz"; //!< Subtract depth
41
42 static const std::string rand_t = "rand"; //!< Random value(s)
43 static const std::string randset_t = rand_t + set_t; //!< Set UTM position
44 static const std::string randadd_t = rand_t + add_t; //!< Add UTM position
45 static const std::string randsub_t = rand_t + sub_t; //!< Subtract UTM position
46
47 /**
48 * Auxiliary class to apply tripod modifications.
49 */
50 struct JModifier {
51 /**
52 * Default constructor.
53 */
54 JModifier()
55 {}
56
57
58 /**
59 * Check validity.
60 *
61 * \return true if valid modifier; else false
62 */
63 bool is_valid() const
64 {
65 return (action != "" && !data.empty());
66 }
67
68 /**
69 * Apply modification to given tripod.
70 *
71 * \param tripod tripod
72 * \return true if valid action; else false
73 */
74 bool apply(JTripod& tripod) const
75 {
76 switch (data.size()) {
77
78 case 1:
79 return apply(tripod, action, data[0]);
80
81 case 3:
82 return apply(tripod, action, JUTMPosition(data[0], data[1], data[2]));
83
84 default:
85 return false;
86 }
87 }
88
89
90 /**
91 * Apply modification to given tripod.
92 *
93 * \param tripod tripod
94 * \param action action
95 * \param value value
96 * \return true if valid action; else false
97 */
98 static bool apply(JTripod& tripod, const std::string& action, const double value)
99 {
100 if (action == setx_t)
101 tripod.setUTMPosition(JUTMPosition(value, tripod.getUTMNorth(), tripod.getUTMZ()));
102 else if (action == addx_t)
103 tripod.add(JUTMPosition(value, 0.0, 0.0));
104 else if (action == subx_t)
105 tripod.sub(JUTMPosition(value, 0.0, 0.0));
106 else if (action == sety_t)
107 tripod.setUTMPosition(JUTMPosition(tripod.getUTMEast(), value, tripod.getUTMZ()));
108 else if (action == addy_t)
109 tripod.add(JUTMPosition(0.0, value, 0.0));
110 else if (action == suby_t)
111 tripod.sub(JUTMPosition(0.0, value, 0.0));
112 else if (action == setz_t)
113 tripod.setUTMPosition(JUTMPosition(tripod.getUTMEast(), tripod.getUTMNorth(), value));
114 else if (action == addz_t)
115 tripod.add(JUTMPosition(0.0, 0.0, value));
116 else if (action == subz_t)
117 tripod.sub(JUTMPosition(0.0, 0.0, value));
118 else
119 return false;
120
121 return true;
122 }
123
124
125 /**
126 * Apply modification to given tripod.
127 *
128 * \param tripod tripod
129 * \param action action
130 * \param pos position
131 * \return true if valid action; else false
132 */
133 static bool apply(JTripod& tripod, const std::string& action, const JUTMPosition& pos)
134 {
135 const JUTMPosition randpos(gRandom->Gaus(0.0, pos.getUTMEast()),
136 gRandom->Gaus(0.0, pos.getUTMNorth()),
137 gRandom->Gaus(0.0, pos.getUTMZ()));
138
139 if (action == set_t) // actions with fixed values
140 tripod.setUTMPosition(pos);
141 else if (action == add_t)
142 tripod.add(pos);
143 else if (action == sub_t)
144 tripod.sub(pos);
145 else if (action == randset_t) // actions with random values
146 tripod.setUTMPosition(randpos);
147 else if (action == randadd_t)
148 tripod.add(randpos);
149 else if (action == randsub_t)
150 tripod.sub(randpos);
151 else
152 return false;
153
154 return true;
155 }
156
157
158 /**
159 * Read modifier from input.
160 *
161 * \param in input stream
162 * \param modifier modifier
163 * \return input stream
164 */
165 friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
166 {
167 if (in >> modifier.id >> modifier.action) {
168
169 modifier.data.clear();
170
171 for (double x; in >> x; ) {
172 modifier.data.push_back(x);
173 }
174
175 in.clear(std::ios_base::eofbit);
176 }
177
178 return in;
179 }
180
181
182 /**
183 * Write modifier to output.
184 *
185 * \param out output stream
186 * \param modifier modifier
187 * \return output stream
188 */
189 friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
190 {
191 out << modifier.id;
192 out << ' ';
193 out << modifier.action;
194
195 for (std::vector<double>::const_iterator i = modifier.data.begin(); i != modifier.data.end(); ++i) {
196 out << ' ' << *i;
197 }
198
199 return out;
200 }
201
202
203 int id;
204 std::string action;
206 };
207}
208
209/**
210 * \file
211 *
212 * Auxiliary program to modify tripod configuration.
213 *
214 * Syntax:
215 * <pre>
216 * -T "<tripod identifier> (set|add|sub) East North depth"
217 * -T "<tripod identifier> (setx|addx|subx) East"
218 * -T "<tripod identifier> (sety|addy|suby) North"
219 * -T "<tripod identifier> (setz|addz|subz) depth"
220 * </pre>
221 *
222 * The options
223 * <pre>
224 * -A "<tripod identifier> East North depth"
225 * -r "<tripod identifier>"
226 * -k "<tripod identifier>"
227 * </pre>
228 * can be used to add, remove and keep a specific tripod, respectively.
229 *
230 * \author mdejong
231 */
232int main(int argc, char **argv)
233{
234 using namespace std;
235 using namespace JPP;
236
237 typedef JContainer< vector<JTripod> > container_type;
238
239 string inputFile;
240 string outputFile;
242 vector<JTripod> add;
243 set<int> rm;
244 set<int> keep;
245 bool squash;
246 int debug;
247
248 try {
249
250 JParser<> zap("Auxiliary program to modify tripod configuration.");
251
252 zap['f'] = make_field(inputFile, "tripod input file");
253 zap['o'] = make_field(outputFile, "tripod output file");
254 zap['T'] = make_field(mod, "tripod modifier") = JPARSER::initialised();
255 zap['A'] = make_field(add, "add tripod") = JPARSER::initialised();
256 zap['r'] = make_field(rm, "remove tripod[s]") = JPARSER::initialised();
257 zap['k'] = make_field(keep, "keep tripod[s]") = JPARSER::initialised();
258 zap['q'] = make_field(squash, "squash meta data");
259 zap['d'] = make_field(debug, "debug level") = 2;
260
261 zap(argc, argv);
262 }
263 catch(const exception &error) {
264 FATAL(error.what() << endl);
265 }
266
267 gRandom->SetSeed(0);
268
269
270 if (!rm.empty() && !keep.empty()) {
271 FATAL("Use either option -K or -D." << endl);
272 }
273
274 container_type data;
275
276 if (inputFile != "") {
277 try {
278 data.load(inputFile.c_str());
279 }
280 catch(const exception& error) {
281 //FATAL(error.what() << endl);
282 }
283 }
284
285 if (squash) {
286 data.comment.clear();
287 }
288
289 data.comment.add(JMeta(argc, argv));
290
291 for (vector<JTripod>::const_iterator i = add.begin(); i != add.end(); ++i) {
292 data.push_back(*i);
293 }
294
295 for (vector<JModifier>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
296
297 for (container_type::iterator target = data.begin(); target != data.end(); ++target) {
298
299 if (target->getID() == i->id || i->id == WILDCARD) {
300
301 DEBUG("Modifier" << ' '
302 << "(" << FILL(2,'0') << target->getID() << FILL() << ")" << ' '
303 << i->action << ' ' << JEEPZ() << i->data << endl);
304
305 if (!i->apply(*target)) {
306 ERROR("No valid action: " << *i << endl);
307 }
308 }
309 }
310 }
311
312 if (!rm.empty()) {
313 for (container_type::iterator target = data.begin(); target != data.end(); ) {
314 if (rm.count(target->getID()) != 0)
315 target = data.erase(target);
316 else
317 ++target;
318 }
319 }
320
321 if (!keep.empty()) {
322 for (container_type::iterator target = data.begin(); target != data.end(); ) {
323 if (keep.count(target->getID()) == 0)
324 target = data.erase(target);
325 else
326 ++target;
327 }
328 }
329
330 sort(data.begin(), data.end(), make_comparator(&JTripod::getID));
331
332 data.store(outputFile.c_str());
333}
Container I/O.
string outputFile
int main(int argc, char **argv)
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
ROOT I/O of application specific meta data.
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.
Data structure for tripod.
Data structure for tripod.
Definition JTripod.hh:35
Utility class to parse command line options.
Definition JParser.hh:1698
Data structure for UTM position.
double getUTMNorth() const
Get UTM north.
double getUTMEast() const
Get UTM east.
double getUTMZ() const
Get UTM Z.
JUTMPosition & add(const JUTMPosition &pos)
Add UTM position.
JUTMPosition & sub(const JUTMPosition &pos)
Subtract UTM position.
void setUTMPosition(const JUTMPosition &position)
Set UTM position.
std::istream & operator>>(std::istream &in, JAHRSCalibration &calibration)
Read AHRS calibration from input stream.
std::ostream & operator<<(std::ostream &out, const JAHRSCalibration &calibration)
Write AHRS calibration to output stream.
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
bool is_valid(const json &js)
Check validity of JSon data.
Auxiliary data structure for sequence of same character.
Definition JManip.hh:330
Target.
Definition JHead.hh:300
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition JContainer.hh:42
Auxiliary data structure for streaming of STL containers.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:68
Auxiliary class for ROOT I/O of application specific meta data.
Definition JMeta.hh:72