Jpp  17.3.1
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"
21 
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  static const char* const RUN_t = "RUN";
33 }
34 
35 
36 /**
37  * \file
38  *
39  * Auxiliary program to download official detector.
40  * \author mdejong
41  */
42 int main(int argc, char **argv)
43 {
44  using namespace std;
45  using namespace JPP;
46 
47  JServer server;
48  string usr;
49  string pwd;
50  string cookie;
51  string detid;
52  int run;
53  int ranking;
54  string variant;
55  string outputFile;
56  vector<string> format;
57  int debug;
58 
59  try {
60 
61  JParser<> zap("Auxiliary program to download official detector.");
62 
63  zap['s'] = make_field(server) = getServernames();
64  zap['u'] = make_field(usr) = "";
65  zap['!'] = make_field(pwd) = "";
66  zap['C'] = make_field(cookie) = "";
67  zap['D'] = make_field(detid);
68  zap['r'] = make_field(run);
69  zap['R'] = make_field(ranking, "ranking (1 is best)") = 1;
70  zap['V'] = make_field(variant, "detector version") = getDetectorVersions<string>();
71  zap['o'] = make_field(outputFile, "detector file or JSon files (must then contain wildcard \'" << FILENAME_WILD_CARD << "\')") = "";
72  zap['F'] = make_field(format, "column names: "
73  << RUN_t << ' '
74  << PMT_T0_CALIBRATION_t << ' '
78  << COMPASS_CALIBRATION_t << ' '
80  zap['d'] = make_field(debug) = 1;
81 
82  zap(argc, argv);
83  }
84  catch(const exception &error) {
85  FATAL(error.what() << endl);
86  }
87 
88  try {
89 
90  JDB::reset(usr, pwd, cookie);
91 
92  detid = getDetector<string>(detid);
93 
94  json js;
95 
97 
98  DEBUG(setw(2) << js << endl);
99 
100  if (!is_valid(js)) {
101  FATAL("JSon error code not okay." << endl);
102  }
103 
104  if (!js.contains(Data_t) || js[Data_t].empty()) {
105  FATAL("JSon outout does not contain data." << endl);
106  }
107 
108  struct calibration_type :
109  public vector<JRunCalibration_t>
110  {
111  /**
112  * Get calibration identifier.
113  *
114  * \param type calibration type
115  * \return calibration idientifier
116  */
117  std::string operator[](const std::string& type) const
118  {
119  const_iterator p = find_if(this->begin(), this->end(), make_predicate(&JRunCalibration_t::CalibrationType, type));
120 
121  if (p != this->end())
122  return p->CalibrationId;
123  else
124  FATAL("No calibration type " << type << endl);
125  }
126  };
127 
128  const calibration_type calibration = js[Data_t].get<calibration_type>();
129 
130  if (debug >= debug_t) {
131  for (const auto& element : calibration) {
132  cout << setw(24) << left << element.CalibrationType << ' ' << right << element.CalibrationId << endl;
133  }
134  }
135 
136  if (outputFile != "") {
137 
138  if (!hasWildCard(outputFile)) {
139 
141 
142  *(JDB::get()->DetX)(detid.c_str(),
143  calibration[PMT_T0_CALIBRATION_t] .c_str(),
144  calibration[DOM_POSITION_CALIBRATION_t].c_str(),
145  calibration[DOM_ROTATION_CALIBRATION_t].c_str(),
146  calibration[ACOUSTIC_T0_CALIBRATION_t] .c_str(),
147  calibration[COMPASS_CALIBRATION_t] .c_str(),
148  calibration[STATUS_CALIBRATION_t] .c_str(),
149  getDetectorVersion(variant)) >> detector;
150 
151  detector.comment.add(JMeta(argc,argv));
152 
154  detector.comment.add(MAKE_STRING(i << "=" << calibration[i]));
155  }
156 
157  if (detector.setToLatestVersion()) {
158  NOTICE("Set detector version to " << detector.getVersion() << endl);
159  }
160 
161  store(outputFile, detector);
162 
163  } else {
164 
165  for (const auto& element : calibration) {
166 
167  json buffer;
168 
169  to_json(buffer, JCalibration_t::getName(), getSelector<JCalibration_t>(detid, element.CalibrationId));
170 
171  if (is_valid(buffer))
172  store(setWildCard(outputFile.c_str(), element.CalibrationType), buffer);
173  else
174  ERROR("Invalid JSon data " << setw(2) << buffer);
175  }
176  }
177  }
178 
179  if (!format.empty()) {
180 
181  for (vector<string>::const_iterator i = format.begin(); i != format.end(); ++i) {
182 
183  if (*i == RUN_t)
184  cout << ' ' << run;
185  else
186  cout << ' ' << calibration[*i];
187  }
188 
189  cout << endl;
190  }
191  }
192  catch(const exception& error) {
193  FATAL(error.what() << endl);
194  }
195 
196  return 0;
197 }
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:1517
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|base) module orientations
bool setToLatestVersion()
Set to latest version.
Definition: JDetector.hh:170
T get(const JHead &header)
Get object from header.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
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
Detector support kit.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
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
void reset(T &value)
Reset value.
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:101
Wrapper class for server name.
Definition: JDB.hh:45
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:57
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:456
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