Jpp  17.3.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDetectorDB.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <set>
6 #include <map>
7 #include <algorithm>
8 #include <cmath>
9 
10 #include "JDB/JDB.hh"
11 #include "JDB/JSelector.hh"
13 #include "JDB/JCLBMap.hh"
15 #include "JDB/JPBSSequence.hh"
16 #include "JDB/JProductRouter.hh"
17 #include "JDB/JDBToolkit.hh"
18 
19 #include "JDetector/JDetector.hh"
23 
24 #include "JLang/JEquation_t.hh"
25 #include "JLang/JComparator.hh"
26 #include "JLang/JPredicate.hh"
27 
28 #include "JSupport/JMeta.hh"
29 
30 #include "Jeep/JPrint.hh"
31 #include "Jeep/JParser.hh"
32 #include "Jeep/JMessage.hh"
33 
34 namespace {
35 
36  using namespace JPP;
37 
38  /**
39  * Get delay time for acoustic sensor with given %UPI.
40  *
41  * \param upi %UPI
42  * \return delay time [ns]
43  */
44  inline double getDelayTime(const JUPI_t& upi)
45  {
46  using namespace std;
47  using namespace JPP;
48 
49  if (upi.getPBS() == PBS::HYDROPHONE)
50  return HYDROPHONE_DELAYTIME_US * 1.0e3;
51  else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V1") != string::npos)
52  return PIEZO_V1_DELAYTIME_US * 1.0e3;
53  else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V2") != string::npos)
54  return PIEZO_V2_DELAYTIME_US * 1.0e3;
55  else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V3") != string::npos)
56  return PIEZO_V3_DELAYTIME_US * 1.0e3;
57  else
58  THROW(JValueOutOfRange, "Invalid UPI " << upi);
59  }
60 
61  /**
62  * List of valid keys.
63  */
64  static const std::set<std::string> keys = { TCAL, PCAL, RCAL, ACAL, CCAL, SCAL };
65 }
66 
67 
68 /**
69  * \file
70  *
71  * Auxiliary program to download or update detector from database.
72  * \author mdejong
73  */
74 int main(int argc, char **argv)
75 {
76  using namespace std;
77  using namespace JPP;
78 
79  JServer server;
80  string usr;
81  string pwd;
82  string cookie;
83  string id;
84  vector<JEquation_t> calset;
85  string detectorFile;
86  string outputFile;
87  int run;
88  string variant;
89  JCounter wizzard;
90  double precision;
91  int debug;
92 
93  try {
94 
95  JParser<> zap("Auxiliary program to download or update detector from database.");
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['D'] = make_field(id, "detector identifier") = "";
102  zap['r'] = make_field(run, "run number") = -1;
103  zap['@'] = make_field(calset, "calibration sets \"<key> = <value>[; <key> = <value>]\"" << endl
104  << "possible keys: " << JEEPZ() << keys) = JPARSER::initialised();
105  zap['a'] = make_field(detectorFile) = "";
106  zap['o'] = make_field(outputFile) = "";
107  zap['V'] = make_field(variant, "detector version") = getDetectorVersions<string>(), "";
108  zap['W'] = make_field(wizzard, "-W add base modules; -WW set time offsets of acoustic sensors");
109  zap['p'] = make_field(precision) = 1.0e-5;
110  zap['d'] = make_field(debug) = 2;
111 
112  zap(argc, argv);
113  }
114  catch(const exception &error) {
115  FATAL(error.what() << endl);
116  }
117 
118 
119  map<string, string> calibration;
120 
121  for (vector<JEquation_t>::const_iterator i = calset.begin(); i != calset.end(); ++i) {
122 
123  if (keys.count(i->getKey()) == 0)
124  FATAL("Invalid calibration set \"" << i->getKey() << "\"" << endl);
125  else
126  calibration[i->getKey()] = i->getValue();
127  }
128 
129  if (calibration.size() != 0u &&
130  calibration.size() != 3u &&
131  calibration.size() != 6u) {
132  FATAL("Inconsistent specificaton of calibration sets, either {}, {t,p,r}cal or {t,p,r,a,c,s}cal.");
133  }
134 
135  const bool file = (detectorFile != "");
136  const bool tpr = (calibration.count(TCAL) != 0 &&
137  calibration.count(PCAL) != 0 &&
138  calibration.count(RCAL) != 0 &&
139  calibration.count(ACAL) == 0 &&
140  calibration.count(CCAL) == 0 &&
141  calibration.count(SCAL) == 0);
142  const bool tpracs = (calibration.count(TCAL) != 0 &&
143  calibration.count(PCAL) != 0 &&
144  calibration.count(RCAL) != 0 &&
145  calibration.count(ACAL) != 0 &&
146  calibration.count(CCAL) != 0 &&
147  calibration.count(SCAL) != 0);
148  const bool daq = (run != -1);
149 
150  if (file && id != "") { FATAL("Inconsistent specificaton of detector file and detector identifier."); }
151  if (file && tpr) { FATAL("Inconsistent specificaton of detector file and {t,p,r}cal calibration sets."); }
152  if (file && tpracs) { FATAL("Inconsistent specificaton of detector file and {t,p,r,a,c,s}cal calibration sets."); }
153  if (file && daq) { FATAL("Inconsistent specificaton of detector file and run number."); }
154 
155  if (tpr && id == "") { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and no detector identifier."); }
156  if (tpr && variant != "") { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and variant."); }
157  if (tpr && daq) { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and run number."); }
158 
159  if (tpracs && id == "") { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and no detector identifier."); }
160  if (tpracs && variant == "") { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and no variant."); }
161  if (tpracs && daq) { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and run number."); }
162 
163  if (daq && id == "") { FATAL("Inconsistent specificaton of run number and no detector identifier."); }
164 
165  if (wizzard &&
166  variant != "" &&
168  FATAL("Inconsistent detector variant and option -W.");
169  }
170 
171  try {
172  JDB::reset(usr, pwd, cookie);
173  }
174  catch(const exception& error) {
175  FATAL(error.what() << endl);
176  }
177 
179 
180  try {
181 
182  if (file)
183  load(detectorFile, detector);
184  else if (tpr)
185  *(JDB::get()->DetX)(id.c_str(),
186  calibration[TCAL].c_str(),
187  calibration[PCAL].c_str(),
188  calibration[RCAL].c_str()) >> detector;
189  else if (tpracs)
190  *(JDB::get()->DetX)(id.c_str(),
191  calibration[TCAL].c_str(),
192  calibration[PCAL].c_str(),
193  calibration[RCAL].c_str(),
194  calibration[ACAL].c_str(),
195  calibration[CCAL].c_str(),
196  calibration[SCAL].c_str(),
197  getDetectorVersion(variant)) >> detector;
198  else if (daq && variant == "")
199  *(JDB::get()->DetX)(id.c_str(), run) >> detector;
200  else if (daq && variant != "")
201  *(JDB::get()->DetX)(id.c_str(), run, getDetectorVersion(variant)) >> detector;
202  else
203  FATAL("Invalid options.");
204  }
205  catch(const exception& error) {
206  FATAL(error.what() << endl);
207  }
208 
209 
210  detector.comment.add(JMeta(argc,argv));
211 
212  if (detector.setToLatestVersion()) {
213  NOTICE("Set detector version to " << detector.getVersion() << endl);
214  }
215 
216 
217  if (wizzard) {
218 
219  vector<JCLBMap> clbmap;
220 
221  try {
222 
223  JSelector selector = getSelector<JCLBMap>(getDetector(detector.getID()));
224 
225  selector.add(&JCLBMap::FLOORID, 0);
226 
227  ResultSet& rs = getResultSet(getTable<JCLBMap>(), selector);
228 
229  rs >> clbmap;
230 
231  rs.Close();
232  }
233  catch(const exception& error) {
234  FATAL(error.what() << endl);
235  }
236 
238 
239  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
240  zmap[module->getString()][module->getFloor()] = module->getPosition();
241  }
242 
243  for (vector<JCLBMap>::const_iterator i = clbmap.begin(); i != clbmap.end(); ++i) {
244 
245  if (i->FLOORID == 0) {
246 
247  JDetector::iterator p = find_if(detector.begin(), detector.end(), make_predicate(&JModule::getID, i->DOMID, JComparison::eq()));
248 
249  if (p == detector.end()) {
250 
251  if (zmap[i->DUID].count(1) != 0) {
252 
253  JModule module(i->DOMID, JLocation(i->DUID, i->FLOORID));
254 
255  module.setPosition(JPosition3D(zmap[i->DUID][1].getX(),
256  zmap[i->DUID][1].getY(),
259  0.0));
260 
261  NOTICE("Adding module " << module << endl);
262 
263  detector.push_back(module);
264 
265  } else {
266 
267  FATAL("Missing floor in string " << FILL(4,'0') << i->DUID << FILL() << endl);
268  }
269 
270  } else if (zmap[i->DUID].count(1) != 0) {
271 
272  if (fabs(p->getX() - zmap[i->DUID][1].getX()) > precision ||
273  fabs(p->getY() - zmap[i->DUID][1].getY()) > precision) {
274 
275  NOTICE("Offset module " << getLabel(p->getLocation()) << " (x,y) position "
276  << SCIENTIFIC(12,3) << (p->getX() - zmap[i->DUID][1].getX()) << ' '
277  << SCIENTIFIC(12,3) << (p->getY() - zmap[i->DUID][1].getY()) << endl);
278 
279  p->setPosition(JPosition3D(zmap[i->DUID][1].getX(),
280  zmap[i->DUID][1].getY(),
281  p->getZ()));
282  }
283 
284  } else {
285 
286  FATAL("Missing floor in string " << setw(4) << i->DUID << endl);
287  }
288  }
289  }
290  }
291 
292  if (wizzard > 1) {
293 
294  try {
295 
296  JDetectorIntegration_t detector_t;
297 
298  ResultSet& rs = getResultSet(getTable<JDetectorIntegration_t>());
299 
300  if (! (rs >> detector_t)) {
301  THROW(JDatabaseException, "Error reading " << getTable<JDetectorIntegration_t>());
302  }
303 
304  detector_t.configure(getDetector(detector.getID()));
305 
306  for (const JPBS_t& pbs : { PBS::ACOUSTIC_SENSOR, PBS::HYDROPHONE }) {
307 
308  const JProductRouter router(detector_t, getPBSSequences(pbs));
309 
311 
312  for (JDetectorIntegration_t::range_const_iterator i = range.first; i != range.second; ++i) {
313 
314  const JLocation_t location = router.getLocation(i->first);
315 
316  JDetector::iterator p = find_if(detector.begin(),
317  detector.end(),
319  JLocation(location.string, (pbs != PBS::HYDROPHONE ? location.floor : 0)),
320  JComparison::eq()));
321 
322  if (p != detector.end()) {
323 
324  const double t0 = getAverage(make_array(p->begin(), p->end(), &JPMT::getT0), 0.0) - getDelayTime(i->first);
325 
326  if (t0 != p->getT0()) {
327 
328  NOTICE("Set module time calibration " << p->getLocation() << ' ' << setw(10) << p->getID() << ' ' << FIXED(12,3) << t0 << endl);
329 
330  p->setCalibration(t0);
331  }
332 
333  } else {
334 
335  ERROR("No module for UPI " << i->first << " at " << location << endl);
336  }
337  }
338  }
339  }
340  catch(const exception& error) {
341  FATAL(error.what() << endl);
342  }
343  }
344 
345  if (outputFile != "")
347  else
348  cout << detector;
349 }
bool isORCADetector(const JDetectorHeader &header)
Check if given detector header is compatible with that of ORCA.
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
static void reset()
Reset connection to database.
Definition: JDB.hh:239
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 JGetPBSSequences getPBSSequences
Function object to get PBS sequences as a function of PBS.
Data structure for a composite optical module.
Definition: JModule.hh:68
static const double ARCA_TBARZ_M
ORCA T-bar position relative to seabed [m].
Acoustic counter.
const std::string & getVariant() const
Get variant.
Definition: JUPI_t.hh:108
bool isARCADetector(const JDetectorHeader &header)
Check if given detector header is compatible with tat of ARCA.
static const double HYDROPHONE_DELAYTIME_US
Hydrophone delay time [us].
then usage $script[< detector identifier >< run range >]< QA/QCfile > nExample script to produce data quality plots nWhen a detector identifier and run range are data are downloaded from the database nand subsequently stored in the given QA QC file
Definition: JDataQuality.sh:19
static const std::string ACAL
acoustic time offsets (piezo sensor or hydrophone)
Database exception.
Definition: JException.hh:666
std::string getLabel(const JLocation &location)
Get module label for monitoring and other applications.
Definition: JLocation.hh:246
static const std::string CCAL
compass alignment (a.k.a. quaternion calibration)
Detector data structure.
Definition: JDetector.hh:89
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
std::iterator_traits< T >::value_type getAverage(T __begin, T __end)
Get average.
Definition: JMath.hh:494
static const double PIEZO_V3_DELAYTIME_US
Piezo delay time [us].
static const double PIEZO_V1_DELAYTIME_US
Piezo delay time [us].
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
Auxiliary class for specifying selection of database data.
string outputFile
Data structure for detector geometry and calibration.
static const double PIEZO_V2_DELAYTIME_US
Piezo delay time [us].
JDetectorsHelper & getDetector()
Auxiliary function for helper object initialisation.
Definition: JDBToolkit.hh:378
const JPBS_t & getPBS() const
Get PBS.
Definition: JPBS_t.hh:101
static const JPBS_t ACOUSTIC_SENSOR(3, 4, 3, 6, 2)
PBS of piezo sensor
static const double ORCA_TBARZ_M
ORCA T-bar position relative to seabed [m].
static const std::string TCAL
PMT time offsets.
int floor
position in string
Definition: JLocation_t.hh:106
map_type::const_iterator range_const_iterator
I/O formatting auxiliaries.
Detector file.
Definition: JHead.hh:226
Detector support kit.
Version with quaternion and time offset per module.
Logical location of module.
Definition: JLocation.hh:37
const JLocation & getLocation() const
Get location.
Definition: JLocation.hh:69
Auxiliary data structure for location of product in detector.
Definition: JLocation_t.hh:24
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
JLocation_t getLocation(const JUPI_t &upi) const
Get location of product with given UPI.
JSelector & add(const JSelector &selection)
Add selection.
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:54
int getID() const
Get identifier.
Definition: JObjectID.hh:50
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
range_type find(const JUPI_t &upi) const
Find range of products with given UPI.
Auxiliary data structure for streaming of STL containers.
Definition: JPrint.hh:65
Auxiliary class to map UPI to location in detector.
static const std::string RCAL
(optical|base) module orientations
General purpose messaging.
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
static const JGetDetectorVersion getDetectorVersion
Function object to map detector version to numerical value.
#define FATAL(A)
Definition: JMessage.hh:67
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Utility class to parse command line options.
static const std::string SCAL
(module|PMT) status
static const JPBS_t HYDROPHONE(4, 5)
PBS of hydrophone
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:431
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:101
void configure(const std::string &detid, const bool option=false)
Configure detector integration for given detector identifier.
Wrapper class for server name.
Definition: JDB.hh:45
Product breakdown structure (PBS).
Definition: JPBS_t.hh:27
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
do set_variable DETECTOR_TXT $WORKDIR detector
static const std::string PCAL
(optical|base) module positions
double u[N+1]
Definition: JPolint.hh:776
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
int string
position in detector
Definition: JLocation_t.hh:105
Template definition for getting table specific selector.
int debug
debug level
void setPosition(const JVector3D &pos)
Set position.
Definition: JPosition3D.hh:152
double getT0() const
Get time offset.
static JDB & get()
Get connection to database.
Definition: JDB.hh:228