Jpp  18.3.0-209-g56ce19a
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JDetectorDB.cc File Reference

Auxiliary program to download or update detector from database. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include "JDB/JDB.hh"
#include "JDB/JSelector.hh"
#include "JDB/JSelectorSupportkit.hh"
#include "JDB/JCLBMap.hh"
#include "JDB/JDetectorIntegration_t.hh"
#include "JDB/JPBSSequence.hh"
#include "JDB/JProductRouter.hh"
#include "JDB/JDBToolkit.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JDetectorSupportkit.hh"
#include "JDetector/JDetectorCalibration.hh"
#include "JLang/JEquation_t.hh"
#include "JLang/JComparator.hh"
#include "JLang/JPredicate.hh"
#include "JLang/JVectorize.hh"
#include "JSupport/JMeta.hh"
#include "JSon/JSupport.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to download or update detector from database.

Author
mdejong

Definition in file JDetectorDB.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 74 of file JDetectorDB.cc.

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() << make_array(getCalibrationType.begin(),
105  getCalibrationType.end(),
106  &JCalibrationType::nick_name)) = JPARSER::initialised();
107  zap['a'] = make_field(detectorFile) = "";
108  zap['o'] = make_field(outputFile) = "";
109  zap['V'] = make_field(variant, "detector version") = getDetectorVersions<string>(), "";
110  zap['W'] = make_field(wizzard, "-W add base modules; -WW set time offsets of acoustic sensors");
111  zap['p'] = make_field(precision) = 1.0e-5;
112  zap['d'] = make_field(debug) = 2;
113 
114  zap(argc, argv);
115  }
116  catch(const exception &error) {
117  FATAL(error.what() << endl);
118  }
119 
120 
122 
123  for (vector<JEquation_t>::const_iterator i = calset.begin(); i != calset.end(); ++i) {
124 
125  if (getCalibrationType.hasNickname(i->getKey()))
126  FATAL("Invalid calibration set \"" << i->getKey() << "\"" << endl);
127  else
128  calibration[i->getKey()] = i->getValue();
129  }
130 
131  if (calibration.size() != 0u &&
132  calibration.size() != 3u &&
133  calibration.size() != 6u) {
134  FATAL("Inconsistent specificaton of calibration sets, either {}, {t,p,r}cal or {t,p,r,a,c,s}cal.");
135  }
136 
137  const bool file = (detectorFile != "");
138  const bool tpr = (calibration.count(TCAL) != 0 &&
139  calibration.count(PCAL) != 0 &&
140  calibration.count(RCAL) != 0 &&
141  calibration.count(ACAL) == 0 &&
142  calibration.count(CCAL) == 0 &&
143  calibration.count(SCAL) == 0);
144  const bool tpracs = (calibration.count(TCAL) != 0 &&
145  calibration.count(PCAL) != 0 &&
146  calibration.count(RCAL) != 0 &&
147  calibration.count(ACAL) != 0 &&
148  calibration.count(CCAL) != 0 &&
149  calibration.count(SCAL) != 0);
150  const bool daq = (run != -1);
151 
152  if (file && id != "") { FATAL("Inconsistent specificaton of detector file and detector identifier."); }
153  if (file && tpr) { FATAL("Inconsistent specificaton of detector file and {t,p,r}cal calibration sets."); }
154  if (file && tpracs) { FATAL("Inconsistent specificaton of detector file and {t,p,r,a,c,s}cal calibration sets."); }
155  if (file && daq) { FATAL("Inconsistent specificaton of detector file and run number."); }
156 
157  if (tpr && id == "") { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and no detector identifier."); }
158  if (tpr && variant != "") { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and variant."); }
159  if (tpr && daq) { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and run number."); }
160 
161  if (tpracs && id == "") { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and no detector identifier."); }
162  if (tpracs && variant == "") { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and no variant."); }
163  if (tpracs && daq) { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and run number."); }
164 
165  if (daq && id == "") { FATAL("Inconsistent specificaton of run number and no detector identifier."); }
166 
167  if (wizzard &&
168  variant != "" &&
169  getDetectorVersion(variant) < JDetectorVersion::V4) {
170  FATAL("Inconsistent detector variant and option -W.");
171  }
172 
173  try {
174  JDB::reset(usr, pwd, cookie);
175  }
176  catch(const exception& error) {
177  FATAL(error.what() << endl);
178  }
179 
181 
182  try {
183 
184  if (file)
185  load(detectorFile, detector);
186  else if (tpr)
187  *(JDB::get()->DetX)(id.c_str(),
188  calibration[TCAL].c_str(),
189  calibration[PCAL].c_str(),
190  calibration[RCAL].c_str()) >> detector;
191  else if (tpracs)
192  *(JDB::get()->DetX)(id.c_str(),
193  calibration[TCAL].c_str(),
194  calibration[PCAL].c_str(),
195  calibration[RCAL].c_str(),
196  calibration[ACAL].c_str(),
197  calibration[CCAL].c_str(),
198  calibration[SCAL].c_str(),
199  getDetectorVersion(variant)) >> detector;
200  else if (daq && variant == "")
201  *(JDB::get()->DetX)(id.c_str(), run) >> detector;
202  else if (daq && variant != "")
203  *(JDB::get()->DetX)(id.c_str(), run, getDetectorVersion(variant)) >> detector;
204  else
205  FATAL("Invalid options.");
206  }
207  catch(const exception& error) {
208  FATAL(error.what() << endl);
209  }
210 
211 
212  detector.comment.add(JMeta(argc,argv));
213 
214  if (detector.setToLatestVersion()) {
215  NOTICE("Set detector version to " << detector.getVersion() << endl);
216  }
217 
218 
219  if (wizzard) {
220 
221  vector<JCLBMap> clbmap;
222 
223  try {
224 
225  JSelector selector = getSelector<JCLBMap>(getDetector(detector.getID()));
226 
227  selector.add(&JCLBMap::FLOORID, 0);
228 
229  ResultSet& rs = getResultSet(getTable<JCLBMap>(), selector);
230 
231  rs >> clbmap;
232 
233  rs.Close();
234  }
235  catch(const exception& error) {
236  FATAL(error.what() << endl);
237  }
238 
240 
241  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
242  zmap[module->getString()][module->getFloor()] = module->getPosition();
243  }
244 
245  for (vector<JCLBMap>::const_iterator i = clbmap.begin(); i != clbmap.end(); ++i) {
246 
247  if (i->FLOORID == 0) {
248 
249  JDetector::iterator p = find_if(detector.begin(), detector.end(), make_predicate(&JModule::getID, i->DOMID, JComparison::eq()));
250 
251  if (p == detector.end()) {
252 
253  if (zmap[i->DUID].count(1) != 0) {
254 
255  JModule module(i->DOMID, JLocation(i->DUID, i->FLOORID));
256 
257  module.setPosition(JPosition3D(zmap[i->DUID][1].getX(),
258  zmap[i->DUID][1].getY(),
261  0.0));
262 
263  NOTICE("Adding module " << module << endl);
264 
265  detector.push_back(module);
266 
267  } else {
268 
269  ERROR("Missing floor in string " << FILL(4,'0') << i->DUID << FILL() << endl);
270  }
271 
272  } else if (zmap[i->DUID].count(1) != 0) {
273 
274  if (fabs(p->getX() - zmap[i->DUID][1].getX()) > precision ||
275  fabs(p->getY() - zmap[i->DUID][1].getY()) > precision) {
276 
277  NOTICE("Offset module " << getLabel(p->getLocation()) << " (x,y) position "
278  << SCIENTIFIC(12,3) << (p->getX() - zmap[i->DUID][1].getX()) << ' '
279  << SCIENTIFIC(12,3) << (p->getY() - zmap[i->DUID][1].getY()) << endl);
280 
281  p->setPosition(JPosition3D(zmap[i->DUID][1].getX(),
282  zmap[i->DUID][1].getY(),
283  p->getZ()));
284  }
285 
286  } else {
287 
288  FATAL("Missing floor in string " << setw(4) << i->DUID << endl);
289  }
290  }
291  }
292  }
293 
294  if (wizzard > 1) {
295 
296  try {
297 
298  JDetectorIntegration_t detector_t;
299 
300  ResultSet& rs = getResultSet(getTable<JDetectorIntegration_t>());
301 
302  if (! (rs >> detector_t)) {
303  THROW(JDatabaseException, "Error reading " << getTable<JDetectorIntegration_t>());
304  }
305 
306  detector_t.configure(getDetector(detector.getID()));
307 
308  for (const JPBS_t& pbs : { PBS::ACOUSTIC_SENSOR, PBS::HYDROPHONE }) {
309 
310  const JProductRouter router(detector_t, getPBSSequences(pbs));
311 
313 
314  for (JDetectorIntegration_t::range_const_iterator i = range.first; i != range.second; ++i) {
315 
316  const JLocation_t location = router.getLocation(i->first);
317 
318  JDetector::iterator p = find_if(detector.begin(),
319  detector.end(),
320  make_predicate(&JModule::getLocation,
321  JLocation(location.string, (pbs != PBS::HYDROPHONE ? location.floor : 0)),
322  JComparison::eq()));
323 
324  if (p != detector.end()) {
325 
326  try {
327 
328  const double t0 = getAverage(make_array(p->begin(), p->end(), &JPMT::getT0), 0.0) - getDelayTime(i->first);
329 
330  if (t0 != p->getT0()) {
331 
332  NOTICE("Set module time calibration " << getLabel(p->getLocation()) << ' ' << setw(10) << p->getID() << ' ' << setw(28) << left << i->first << right << ' ' << FIXED(12,3) << t0 << endl);
333 
334  p->setCalibration(t0);
335  }
336  }
337  catch(const exception& error) {
338  if (i->first.getVariant() != NOPIEZO) {
339  ERROR(location << ' ' << error.what() << endl);
340  }
341  }
342 
343  } else {
344 
345  ERROR("No module for UPI " << i->first << " at " << location << endl);
346  }
347  }
348  }
349  }
350  catch(const exception& error) {
351  FATAL(error.what() << endl);
352  }
353  }
354 
355  if (outputFile != "")
357  else
358  cout << detector;
359 }
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
bool hasNickname(const std::string &type) const
Has calibration type.
Utility class to parse command line options.
Definition: JParser.hh:1711
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
static JGetPBSSequences getPBSSequences
Function object to get PBS sequences as a function of PBS.
Data structure for a composite optical module.
Definition: JModule.hh:67
static const double ARCA_TBARZ_M
ORCA T-bar position relative to seabed [m].
Acoustic counter.
bool isARCADetector(const JDetectorHeader &header)
Check if given detector header is compatible with tat of ARCA.
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:682
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:712
std::iterator_traits< T >::value_type getAverage(T __begin, T __end)
Get average.
Definition: JMath.hh:494
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:84
Auxiliary data structure for correspondence between nick and full name of calibration types...
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
Auxiliary class for specifying selection of database data.
string outputFile
JDetectorsHelper & getDetector()
Auxiliary function for helper object initialisation.
Definition: JDBToolkit.hh:378
Calibration.
Definition: JHead.hh:328
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
Detector file.
Definition: JHead.hh:226
Logical location of module.
Definition: JLocation.hh:37
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:2158
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
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
#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 module orientations
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 reset(T &value)
Reset value.
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
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:436
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:106
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:50
Product breakdown structure (PBS).
Definition: JPBS_t.hh:27
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
do set_variable DETECTOR_TXT $WORKDIR detector
static const std::string PCAL
(optical|base) module positions
double u[N+1]
Definition: JPolint.hh:865
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:486
int string
position in detector
Definition: JLocation_t.hh:105
Template definition for getting table specific selector.
int debug
debug level