Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JSonDetector.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <iomanip>
4#include <fstream>
5
6#include "JDB/JDB.hh"
7#include "JDB/JSelector.hh"
9#include "JDB/JDBToolkit.hh"
10#include "JDB/JDetectors.hh"
11#include "JDB/JPersons.hh"
12#include "JDB/JRuns.hh"
13
18
19#include "JTools/JRange.hh"
20
21#include "JSon/JSon.hh"
22#include "JSon/JSupport.hh"
23
24#include "JLang/JVectorize.hh"
25
26#include "Jeep/JeepToolkit.hh"
27#include "Jeep/JPrint.hh"
28#include "Jeep/JParser.hh"
29#include "Jeep/JMessage.hh"
30
31namespace {
32
33 using JSON::json;
34
35 /**
36 * Invalid UTC.
37 */
38 static const time_t UTC_INVALID_S = -1;
39
40 /**
41 * Invalid run.
42 */
43 static const int RUN_INVALID = -1;
44
45 /**
46 * Get JSon object for given key and UTC time.
47 *
48 * \param key key
49 * \param utc_s UTC time [s]
50 * \return JSon object
51 */
52 inline json getJSon(const std::string& key, const time_t utc_s)
53 {
54 if (utc_s != UTC_INVALID_S)
55 return json({key, JPP::JDateAndTime(utc_s, true).toString() });
56 else
57 return json({key, json() });
58 }
59}
60
61
62/**
63 * \file
64 *
65 * Auxiliary program to decompose detector to separate calibrations.
66 *
67 * Note that the validity range is defined in the following order.
68 * -# option <tt>-V "<UTC min> <UTC max>"</tt>;
69 * -# option <tt>-r "<first run> <last run>"</tt>;
70 *
71 * \author mdejong
72 */
73int main(int argc, char **argv)
74{
75 using namespace std;
76 using namespace JPP;
77
78 typedef JRange<time_t> JRange_t;
79
80 JServer server;
81 string usr;
82 string pwd;
83 string cookie;
84 string detectorFile;
85 string outputFile;
86 JRange_t validity;
87 JRange_t range;
88 double precision;
89 int debug;
90
91 try {
92
93 JParser<> zap("Auxiliary program to decompose detector to separate calibrations.");
94
95 zap['s'] = make_field(server) = getServernames();
96 zap['u'] = make_field(usr) = "";
97 zap['!'] = make_field(pwd) = "";
98 zap['C'] = make_field(cookie) = "";
99 zap['a'] = make_field(detectorFile, "detector file");
100 zap['o'] = make_field(outputFile, "detector calibrations file in JSON format."\
101 "\nFile name should contain wild card \'" << FILENAME_WILDCARD << "\' or"\
102 "\none of the key words: " << JEEPZ() << make_array(getCalibrationType.begin(),
103 getCalibrationType.end(),
104 &JCalibrationType::nick_name));
105 zap['V'] = make_field(validity, "validity range UTC [s], e.g. \"<UTC min> <UTC max>\"") = JRange_t(UTC_INVALID_S, UTC_INVALID_S);
106 zap['r'] = make_field(range, "run range e.g. \"<first run> <last run>\"") = JRange_t(RUN_INVALID, RUN_INVALID);
107 zap['p'] = make_field(precision, "precision for match with reference module") = 1.0e-5;
108 zap['d'] = make_field(debug) = 1;
109
110 zap(argc, argv);
111 }
112 catch(const exception &error) {
113 FATAL(error.what() << endl);
114 }
115
116
117
119
120 try {
121 load(detectorFile, detector);
122 }
123 catch(const JException& error) {
124 FATAL(error);
125 }
126
127 if (!hasDetectorAddressMap(detector.getID())) {
128 FATAL("No detector address map for detector identier " << detector.getID() << endl);
129 }
130
131 const JDetectorBuilder& demo = getDetectorBuilder(detector.getID());
132
133 string userid = "";
134 string detoid = "";
135 string locid = "";
136
137 try {
138
139 JDB::reset(usr, pwd, cookie);
140
141 {
142 ResultSet& rs = getResultSet(JPersons::getName(), getSelector<JPersons>(JDB::get()->User()));
143
144 for (JPersons object; rs >> object; ) {
145 userid = object.OID;
146 }
147
148 rs.Close();
149 }
150 {
151 ResultSet& rs = getResultSet(JDetectors::getName(), getSelector<JDetectors>(detector.getID()));
152
153 for (JDetectors object; rs >> object; ) {
154 locid = object.LOCATIONID;
155 detoid = object.OID;
156 }
157
158 rs.Close();
159 }
160
161 if (range.getLowerLimit() != RUN_INVALID && validity.getLowerLimit() == UTC_INVALID_S) {
162
163 ResultSet& rs = getResultSet(JRuns::getName(), getSelector<JRuns>(detector.getID(), range.getLowerLimit()));
164
165 for (JRuns object; rs >> object; ) {
166 validity.setLowerLimit(object.UNIXJOBSTART / 1000);
167 }
168
169 rs.Close();
170 }
171
172 if (range.getUpperLimit() != RUN_INVALID && validity.getUpperLimit() == UTC_INVALID_S) {
173
174 ResultSet& rs = getResultSet(JRuns::getName(), getSelector<JRuns>(detector.getID(), range.getUpperLimit()));
175
176 for (JRuns object; rs >> object; ) {
177 validity.setUpperLimit(object.UNIXJOBEND / 1000);
178 }
179
180 rs.Close();
181 }
182 }
183 catch(const exception& error) {
184 FATAL(error.what() << endl);
185 }
186
187
188 // Test compatibility of modules with reference(s).
189
190 for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
191
192 if (module->getFloor() != 0) {
193
194 JModule buffer = demo.getModule(module->getID(), module->getLocation());
195
196 const JRotation3D R = getRotation(buffer, *module);
197
198 buffer.rotate(R);
199
200 if (!JModule::compare(buffer, *module, precision)) {
201
202 for (size_t i = 0; i != module->size(); ++i) {
203 DEBUG("PMT " << setw(2) << i << ' '
204 << (*module)[i].getDirection() << ' '
205 << buffer[i].getDirection() << ' '
206 << FIXED(7,3) << acos(getDot((*module)[i].getDirection(), buffer[i].getDirection())) * 180.0 / PI << " [deg]" << endl);
207 }
208
209 FATAL("Module " << setw(10) << module->getID() << ' ' << module->getLocation() << " incompatible with reference." << endl);
210 }
211 }
212 }
213
214 const json header = { { UserId_t, userid },
215 { TypeId_t, json() },
216 { LocationId_t, locid },
217 { DetOID_t, detoid },
218 { StartTime_t, JDateAndTime(true).toString() },
219 { EndTime_t, JDateAndTime(true).toString() },
220 getJSon(ValidFrom_t, validity.getLowerLimit()),
221 getJSon(ValidThrough_t, validity.getUpperLimit()) };
222
223 const json error = { {Message_t, "" },
224 {Code_t, OK_t },
225 {Arguments_t, json::array() } };
226
227
228 json js;
229
230 js[Comment_t] = json(detector.comment);
231 js[Error_t] = json(error);
232
233 if (hasWildCard(outputFile.c_str()) || outputFile.find(TCAL) != string::npos) {
234
235 JPMTCalibration data;
236
237 for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
238 for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
239 data.push_back(JPMTCalibration_t(pmt->getID(), getCalibration(JCalibration(), *pmt)));
240 }
241 }
242
243 js[Data_t][0] = json(header);
244 js[Data_t][0][PMTT0s_t] = json(data);
245
246 store(hasWildCard(outputFile) ? setWildCard(outputFile, TCAL) : outputFile, js);
247 }
248
249 if (hasWildCard(outputFile) || outputFile.find(PCAL) != string::npos) {
250
251 JModulePosition data[2];
252
253 for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
254
255 const JModule& buffer = demo.getModule(module->getID(), module->getLocation());
256
257 if (buffer.getFloor() == 0)
258 data[0].push_back(JModulePosition_t(module->getID(), getPosition(buffer, *module)));
259 else
260 data[1].push_back(JModulePosition_t(module->getID(), getPosition(buffer, *module)));
261 }
262
263 js[Data_t][0] = json(header);
264 js[Data_t][0][BasePositions_t] = json(data[0]);
265 js[Data_t][0][DOMPositions_t] = json(data[1]);
266
267 store(hasWildCard(outputFile) ? setWildCard(outputFile, PCAL) : outputFile, js);
268 }
269
270 if (hasWildCard(outputFile) || outputFile.find(RCAL) != string::npos) {
271
272 JModuleRotation data;
273
274 for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
275
276 const JModule& buffer = demo.getModule(module->getID(), module->getLocation());
277
278 if (module->getFloor() != 0) {
279 data.push_back(JModuleRotation_t(module->getID(), getRotation(buffer, *module)));
280 }
281 }
282
283 js[Data_t][0] = json(header);
284 js[Data_t][0][DOMRotations_t] = json(data);
285
286 store(hasWildCard(outputFile) ? setWildCard(outputFile, RCAL) : outputFile, js);
287 }
288
289 if (hasWildCard(outputFile) || outputFile.find(ACAL) != string::npos) {
290
291 JModuleCalibration data[2];
292
293 for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
294
295 if (module->getFloor() == 0)
296 data[0].push_back(JModuleCalibration_t(module->getID(), module->getT0()));
297 else
298 data[1].push_back(JModuleCalibration_t(module->getID(), module->getT0()));
299 }
300
301 js[Data_t][0] = json(header);
302 js[Data_t][0][BaseAcousticT0_t] = json(data[0]);
303 js[Data_t][0][DOMAcousticT0_t] = json(data[1]);
304
305 store(hasWildCard(outputFile) ? setWildCard(outputFile, ACAL) : outputFile, js);
306 }
307
308 if (hasWildCard(outputFile) || outputFile.find(CCAL) != string::npos) {
309
310 JCompassRotation data[2];
311
312 for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
313 if (module->getFloor() == 0)
314 data[0].push_back(JCompassRotation_t(module->getID(), module->getQuaternion()));
315 else
316 data[1].push_back(JCompassRotation_t(module->getID(), module->getQuaternion()));
317 }
318
319 js[Data_t][0] = json(header);
320 js[Data_t][0][BaseCompassRotations_t] = json(data[0]);
321 js[Data_t][0][DOMCompassRotations_t] = json(data[1]);
322
323 store(hasWildCard(outputFile) ? setWildCard(outputFile, CCAL) : outputFile, js);
324 }
325
326 if (hasWildCard(outputFile) || outputFile.find(SCAL) != string::npos) {
327
328 js[Data_t][0] = json(header);
329
330 {
331 JPMTStatus data;
332
333 for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
334 for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
335 data.push_back(JPMTStatus_t(pmt->getID(), pmt->getStatus()));
336 }
337 }
338
339 js[Data_t][0][PMTStatusInfo_t ] = json(data);
340 }
341 {
342 JModuleStatus data[2];
343
344 for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
345 if (module->getFloor() == 0)
346 data[0].push_back(JModuleStatus_t(module->getID(), module->getStatus()));
347 else
348 data[1].push_back(JModuleStatus_t(module->getID(), module->getStatus()));
349 }
350
351 js[Data_t][0][BaseStatusInfo_t] = json(data[0]);
352 js[Data_t][0][DOMStatusInfo_t] = json(data[1]);
353 }
354
355 store(hasWildCard(outputFile) ? setWildCard(outputFile, SCAL) : outputFile, js);
356 }
357
358 return 0;
359}
string outputFile
Detector support kit.
Data structure for detector geometry and calibration.
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
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.
Auxiliary class to define a range between two values.
int main(int argc, char **argv)
JSon definitions and auxiliaries.
nlohmann::json json
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
Auxiliary methods for handling file names, type names and environment.
Data structure for time calibration.
Detector data structure.
Definition JDetector.hh:96
int getFloor() const
Get floor number.
Definition JLocation.hh:146
Data structure for a composite optical module.
Definition JModule.hh:75
void rotate(const JRotation3D &R)
Rotate module.
Definition JModule.hh:314
General exception.
Definition JException.hh:24
Utility class to parse command line options.
Definition JParser.hh:1698
Range of values.
Definition JRange.hh:42
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Detector file.
Definition JHead.hh:227
Wrapper class for server name.
Definition JDB.hh:54
Template definition for getting table specific selector.
Auxiliary data structure for compass rotation.
Auxiliary interface for building detector.
const JModule & getModule(const int id=-1, const JLocation &location=JLocation()) const
Get module.
Auxiliary data structure for module time calibration.
Auxiliary data structure for module position.
Auxiliary data structure for module rotation.
Auxiliary data structure for module status.
Auxiliary data structure for PMT time calibration.
Auxiliary data structure for PMT status.
Auxiliary data structure for streaming of STL containers.
Auxiliary data structure for correspondence between nick and full name of calibration types.
Auxiliary class for date and time.
std::string toString() const
Get ASCII formatted date and time.