Jpp  18.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
14 #include "JDetector/JDetector.hh"
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 
31 namespace {
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  */
73 int 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_WILD_CARD << "\' 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 
118  JDetector detector;
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 JDetectorAddressMap& demo = getDetectorAddressMap(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 = getModule(demo.get(module->getID()), 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  FATAL("Module " << setw(10) << module->getID() << ' ' << module->getLocation() << " incompatible with reference." << endl);
202  }
203  }
204  }
205 
206  const json header = { { UserId_t, userid },
207  { TypeId_t, json() },
208  { LocationId_t, locid },
209  { DetOID_t, detoid },
210  { StartTime_t, JDateAndTime(true).toString() },
211  { EndTime_t, JDateAndTime(true).toString() },
212  getJSon(ValidFrom_t, validity.getLowerLimit()),
213  getJSon(ValidThrough_t, validity.getUpperLimit()) };
214 
215  const json error = { {Message_t, "" },
216  {Code_t, OK_t },
217  {Arguments_t, json::array() } };
218 
219 
220  json js;
221 
222  js[Comment_t] = json(detector.comment);
223  js[Error_t] = json(error);
224 
225  if (hasWildCard(outputFile.c_str()) || outputFile.find(TCAL) != string::npos) {
226 
228 
229  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
230  for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
231  data.push_back(JPMTCalibration_t(pmt->getID(), getCalibration(JCalibration(), *pmt)));
232  }
233  }
234 
235  js[Data_t][0] = json(header);
236  js[Data_t][0][PMTT0s_t] = json(data);
237 
239  }
240 
241  if (hasWildCard(outputFile) || outputFile.find(PCAL) != string::npos) {
242 
244 
245  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
246 
247  const JModule& buffer = getModule(demo.get(module->getID()), module->getID(), module->getLocation());
248 
249  if (buffer.getFloor() == 0)
250  data[0].push_back(JModulePosition_t(module->getID(), getPosition(buffer, *module)));
251  else
252  data[1].push_back(JModulePosition_t(module->getID(), getPosition(buffer, *module)));
253  }
254 
255  js[Data_t][0] = json(header);
256  js[Data_t][0][BasePositions_t] = json(data[0]);
257  js[Data_t][0][DOMPositions_t] = json(data[1]);
258 
260  }
261 
262  if (hasWildCard(outputFile) || outputFile.find(RCAL) != string::npos) {
263 
265 
266  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
267 
268  const JModule& buffer = getModule(demo.get(module->getID()), module->getID(), module->getLocation());
269 
270  if (module->getFloor() != 0) {
271  data.push_back(JModuleRotation_t(module->getID(), getRotation(buffer, *module)));
272  }
273  }
274 
275  js[Data_t][0] = json(header);
276  js[Data_t][0][DOMRotations_t] = json(data);
277 
279  }
280 
281  if (hasWildCard(outputFile) || outputFile.find(ACAL) != string::npos) {
282 
284 
285  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
286 
287  if (module->getFloor() == 0)
288  data[0].push_back(JModuleCalibration_t(module->getID(), module->getT0()));
289  else
290  data[1].push_back(JModuleCalibration_t(module->getID(), module->getT0()));
291  }
292 
293  js[Data_t][0] = json(header);
294  js[Data_t][0][BaseAcousticT0_t] = json(data[0]);
295  js[Data_t][0][DOMAcousticT0_t] = json(data[1]);
296 
298  }
299 
300  if (hasWildCard(outputFile) || outputFile.find(CCAL) != string::npos) {
301 
303 
304  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
305  if (module->getFloor() == 0)
306  data[0].push_back(JCompassRotation_t(module->getID(), module->getQuaternion()));
307  else
308  data[1].push_back(JCompassRotation_t(module->getID(), module->getQuaternion()));
309  }
310 
311  js[Data_t][0] = json(header);
312  js[Data_t][0][BaseCompassRotations_t] = json(data[0]);
313  js[Data_t][0][DOMCompassRotations_t] = json(data[1]);
314 
316  }
317 
318  if (hasWildCard(outputFile) || outputFile.find(SCAL) != string::npos) {
319 
320  js[Data_t][0] = json(header);
321 
322  {
324 
325  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
326  for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
327  data.push_back(JPMTStatus_t(pmt->getID(), pmt->getStatus()));
328  }
329  }
330 
331  js[Data_t][0][PMTStatusInfo_t ] = json(data);
332  }
333  {
334  JModuleStatus data[2];
335 
336  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
337  if (module->getFloor() == 0)
338  data[0].push_back(JModuleStatus_t(module->getID(), module->getStatus()));
339  else
340  data[1].push_back(JModuleStatus_t(module->getID(), module->getStatus()));
341  }
342 
343  js[Data_t][0][BaseStatusInfo_t] = json(data[0]);
344  js[Data_t][0][DOMStatusInfo_t] = json(data[1]);
345  }
346 
348  }
349 
350  return 0;
351 }
static const std::string Arguments_t
Utility class to parse command line options.
Definition: JParser.hh:1514
int main(int argc, char *argv[])
Definition: Main.cc:15
static const std::string DOMStatusInfo_t
static const std::string Code_t
static const std::string UserId_t
static const std::string EndTime_t
static const std::string ACAL
acoustic time offsets (piezo sensor or hydrophone)
static const std::string CCAL
compass alignment (a.k.a. quaternion calibration)
std::string toString() const
Get ASCII formatted date and time.
static const std::string PMTT0s_t
bool hasDetectorAddressMap(const int id)
Check if detector address map is available.
static const std::string Error_t
T get(const JHead &header)
Get object from header.
std::vector< JModuleRotation_t > JModuleRotation
Module rotation.
static JRotation getRotation
Function object to get rotation matrix to go from first to second module.
JCalibration getCalibration(const JCalibration &first, const JCalibration &second)
Get calibration to go from first to second calibration.
string outputFile
Data structure for detector geometry and calibration.
static const std::string BaseStatusInfo_t
std::vector< JCompassRotation_t > JCompassRotation
Compass rotation.
static const std::string ValidFrom_t
static const std::string TCAL
PMT time offsets.
static const std::string Comment_t
Detector specific mapping between logical positions and readout channels of PMTs in optical modules...
static const std::string ValidThrough_t
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:54
Auxiliary methods for handling file names, type names and environment.
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
JPosition3D getPosition(const Vec &pos)
Get position.
static const std::string StartTime_t
then awk string
static const std::string DOMPositions_t
Auxiliary data structure for streaming of STL containers.
Definition: JPrint.hh:65
JSon definitions and auxiliaries.
JDetectorAddressMap & getDetectorAddressMap()
Get detector address map.
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
static const std::string RCAL
optical module orientations
General purpose messaging.
static const std::string DOMCompassRotations_t
static const std::string Message_t
#define FATAL(A)
Definition: JMessage.hh:67
static const std::string DetOID_t
std::string setWildCard(const std::string &file_name, const std::string &value)
Get file name by setting wild card to given value.
Definition: JeepToolkit.hh:66
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
std::vector< JModuleStatus_t > JModuleStatus
Module status.
void reset(T &value)
Reset value.
then JCookie sh JDataQuality D $DETECTOR_ID R
Definition: JDataQuality.sh:41
static const std::string Data_t
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Auxiliary class to define a range between two values.
Auxiliary class for date and time.
Definition: JDateAndTime.hh:78
Utility class to parse command line options.
static const std::string SCAL
(module|PMT) status
nlohmann::json json
std::vector< JPMTStatus_t > JPMTStatus
PMT status.
static const std::string BasePositions_t
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:436
static const std::string DOMRotations_t
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:106
static const char FILENAME_WILD_CARD
wild card character for file name substitution
Definition: JeepToolkit.hh:44
static const std::string LocationId_t
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:57
static const std::string BaseCompassRotations_t
std::vector< JModulePosition_t > JModulePosition
Module position.
do set_variable DETECTOR_TXT $WORKDIR detector
static const std::string PMTStatusInfo_t
static const std::string PCAL
(optical|base) module positions
JSON::getCalibrationType getCalibrationType
static const std::string DOMAcousticT0_t
std::vector< JPMTCalibration_t > JPMTCalibration
PMT time calibration.
static const std::string TypeId_t
const JModule & getModule(const JDetector &detector, const JModuleLocation &location)
find module with a given string and floor number
int debug
debug level
static const std::string BaseAcousticT0_t
std::vector< JModuleCalibration_t > JModuleCalibration
Module time calibration.
static const std::string OK_t
bool hasWildCard(const std::string &file_name)
Check presence of wild card.
Definition: JeepToolkit.hh:53