Jpp  18.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRunCalibration.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <algorithm>
4 
5 #include "dbclient/KM3NeTDBClient.h"
6 
7 #include "JDB/JDB.hh"
8 #include "JDB/JDBToolkit.hh"
9 #include "JDB/JSelector.hh"
11 #include "JDB/JCalibration.hh"
12 #include "JDB/JRunCalibration.hh"
13 #include "JDB/JSonSupportkit.hh"
14 
15 #include "JSon/JSon.hh"
16 #include "JSon/JSupport.hh"
17 
18 #include "JDetector/JDetector.hh"
20 
21 #include "JLang/JException.hh"
22 #include "JLang/JPredicate.hh"
23 #include "JLang/JManip.hh"
24 
25 #include "JSupport/JMeta.hh"
26 
27 #include "Jeep/JParser.hh"
28 #include "Jeep/JMessage.hh"
29 
30 namespace {
31 
32  using namespace JPP;
33 
34  static const char* const RUN_t = "RUN";
35 
36  /**
37  * Auxiliary data structure for run calibrations.
38  */
39  struct calibration_type :
40  public std::vector<APIV2::JRunCalibration_t>
41  {
42  /**
43  * Get calibration identifier.
44  *
45  * \param type calibration type
46  * \return calibration idientifier
47  */
48  std::string operator[](const std::string& type) const
49  {
50  using namespace std;
51 
52  const_iterator p = find_if(this->begin(), this->end(), make_predicate(&APIV2::JRunCalibration_t::CalibrationType, type));
53 
54  if (p != this->end())
55  return p->CalibrationId;
56  else
57  THROW(JException, "No calibration type " << type << endl);
58  }
59  };
60 
61 }
62 
63 
64 /**
65  * \file
66  *
67  * Auxiliary program to download official detector.
68  * \author mdejong
69  */
70 int main(int argc, char **argv)
71 {
72  using namespace std;
73  using namespace JPP;
74 
75  JServer server;
76  string usr;
77  string pwd;
78  string cookie;
79  string detid;
80  int run;
81  int ranking;
82  string variant;
83  string outputFile;
84  vector<string> format;
85  int debug;
86 
87  try {
88 
89  JParser<> zap("Auxiliary program to download official detector.");
90 
91  zap['s'] = make_field(server) = getServernames();
92  zap['u'] = make_field(usr) = "";
93  zap['!'] = make_field(pwd) = "";
94  zap['C'] = make_field(cookie) = "";
95  zap['D'] = make_field(detid);
96  zap['r'] = make_field(run);
97  zap['R'] = make_field(ranking, "ranking (1 is best)") = 1;
98  zap['V'] = make_field(variant, "detector version") = getDetectorVersions<string>();
99  zap['o'] = make_field(outputFile, "detector file or JSon files (must then contain wildcard \'" << FILENAME_WILD_CARD << "\')") = "";
100  zap['F'] = make_field(format, "column names: "
101  << RUN_t << ' '
102  << PMT_T0_CALIBRATION_t << ' '
105  << ACOUSTIC_T0_CALIBRATION_t << ' '
106  << COMPASS_CALIBRATION_t << ' '
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  try {
117 
118  JDB::reset(usr, pwd, cookie);
119 
120  detid = getDetector<string>(detid);
121 
122  json js;
123 
125 
126  DEBUG(setw(2) << js << endl);
127 
128  if (!is_valid(js)) {
129  FATAL("JSon error code not okay." << endl);
130  }
131 
132  if (!js.contains(Data_t) || js[Data_t].empty()) {
133  FATAL("JSon output does not contain data." << endl);
134  }
135 
136  const calibration_type calibration = js[Data_t].get<calibration_type>();
137 
138  if (debug >= debug_t) {
139  for (const auto& element : calibration) {
140  cout << setw(24) << left << element.CalibrationType << ' ' << right << element.CalibrationId << endl;
141  }
142  }
143 
144  if (outputFile != "") {
145 
146  if (!hasWildCard(outputFile)) {
147 
149 
150  *(JDB::get()->DetX)(detid.c_str(),
151  calibration[PMT_T0_CALIBRATION_t] .c_str(),
152  calibration[DOM_POSITION_CALIBRATION_t].c_str(),
153  calibration[DOM_ROTATION_CALIBRATION_t].c_str(),
154  calibration[ACOUSTIC_T0_CALIBRATION_t] .c_str(),
155  calibration[COMPASS_CALIBRATION_t] .c_str(),
156  calibration[STATUS_CALIBRATION_t] .c_str(),
157  getDetectorVersion(variant)) >> detector;
158 
159  detector.comment.add(JMeta(argc,argv));
160 
162  detector.comment.add(MAKE_STRING(i << "=" << calibration[i]));
163  }
164 
165  if (detector.setToLatestVersion()) {
166  NOTICE("Set detector version to " << detector.getVersion() << endl);
167  }
168 
169  store(outputFile, detector);
170 
171  } else {
172 
173  for (const auto& element : calibration) {
174 
175  json buffer;
176 
177  to_json(buffer, APIV2::JCalibration_t::getName(), getSelector<APIV2::JCalibration_t>(detid, element.CalibrationId));
178 
179  if (is_valid(buffer))
180  store(setWildCard(outputFile.c_str(), getCalibrationType.getNickname(element.CalibrationType)), buffer);
181  else
182  ERROR("Invalid JSon data " << setw(2) << buffer);
183  }
184  }
185  }
186 
187  if (!format.empty()) {
188 
189  for (vector<string>::const_iterator i = format.begin(); i != format.end(); ++i) {
190 
191  if (*i == RUN_t)
192  cout << ' ' << run;
193  else
194  cout << ' ' << calibration[*i];
195  }
196 
197  cout << endl;
198  }
199  }
200  catch(const exception& error) {
201  FATAL(error.what() << endl);
202  }
203 
204  return 0;
205 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Utility class to parse command line options.
Definition: JParser.hh:1514
General exception.
Definition: JException.hh:24
Exceptions.
static void reset()
Reset connection to database.
Definition: JDB.hh:244
debug
Definition: JMessage.hh:29
JPredicate< JResult_t T::*, JComparison::eq > make_predicate(JResult_t T::*member, const JResult_t value)
Helper method to create predicate for data member.
Definition: JPredicate.hh:128
int main(int argc, char *argv[])
Definition: Main.cc:15
static const std::string STATUS_CALIBRATION_t
(module|PMT) status
bool is_valid(const json &js)
Check validity of JSon data.
Detector data structure.
Definition: JDetector.hh:89
static const std::string DOM_ROTATION_CALIBRATION_t
optical module orientations
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
bool setToLatestVersion()
Set to latest version.
Definition: JDetector.hh:170
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Auxiliary data structure for correspondence between nick and full name of calibration types...
const std::string & getVersion() const
Get version.
string outputFile
Data structure for detector geometry and calibration.
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
Calibration.
Definition: JHead.hh:328
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
static const std::string PMT_T0_CALIBRATION_t
PMT time offsets.
static const std::string COMPASS_CALIBRATION_t
compass alignment (a.k.a. quaternion calibration)
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
ROOT I/O of application specific meta data.
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
then awk string
JSon definitions and auxiliaries.
General purpose messaging.
static const JGetDetectorVersion getDetectorVersion
Function object to map detector version to numerical value.
I/O manipulators.
static const std::string DOM_POSITION_CALIBRATION_t
(optical|base) module positions
#define FATAL(A)
Definition: JMessage.hh:67
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
const std::string & getNickname(const std::string &type) const
Get calibration type.
static const std::string Data_t
Utility class to parse command line options.
nlohmann::json json
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:106
Wrapper class for server name.
Definition: JDB.hh:50
static const char FILENAME_WILD_CARD
wild card character for file name substitution
Definition: JeepToolkit.hh:44
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:60
JComment & add(const std::string &comment)
Add comment.
Definition: JComment.hh:100
do set_variable DETECTOR_TXT $WORKDIR detector
void to_json(json &js, const std::string &query)
Get result set.
Definition: JDB.hh:461
static const std::string ACOUSTIC_T0_CALIBRATION_t
acoustic time offsets (piezo sensor or hydrophone)
Template definition for getting table specific selector.
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
bool hasWildCard(const std::string &file_name)
Check presence of wild card.
Definition: JeepToolkit.hh:53
static JDB & get()
Get connection to database.
Definition: JDB.hh:233