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