Jpp  18.6.0-rc.1
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) << endl
107  << "possible values: <calibration identifier> or \"?\"") = JPARSER::initialised();
108  zap['a'] = make_field(detectorFile) = "";
109  zap['o'] = make_field(outputFile) = "";
110  zap['V'] = make_field(variant, "detector version") = getDetectorVersions<string>(), "";
111  zap['W'] = make_field(wizzard, "-W add base modules; -WW set time offsets of acoustic sensors");
112  zap['p'] = make_field(precision) = 1.0e-5;
113  zap['d'] = make_field(debug) = 2;
114 
115  zap(argc, argv);
116  }
117  catch(const exception &error) {
118  FATAL(error.what() << endl);
119  }
120 
121 
123 
124  for (vector<JEquation_t>::const_iterator i = calset.begin(); i != calset.end(); ++i) {
125 
126  if (getCalibrationType.hasNickname(i->getKey()))
127  FATAL("Invalid calibration set \"" << i->getKey() << "\"" << endl);
128  else
129  calibration[i->getKey()] = i->getValue();
130  }
131 
132  if (calibration.size() != 0u &&
133  calibration.size() != 3u &&
134  calibration.size() != 6u) {
135  FATAL("Inconsistent specificaton of calibration sets, either {}, {t,p,r}cal or {t,p,r,a,c,s}cal.");
136  }
137 
138  const bool file = (detectorFile != "");
139  const bool tpr = (calibration.count(TCAL) != 0 &&
140  calibration.count(PCAL) != 0 &&
141  calibration.count(RCAL) != 0 &&
142  calibration.count(ACAL) == 0 &&
143  calibration.count(CCAL) == 0 &&
144  calibration.count(SCAL) == 0);
145  const bool tpracs = (calibration.count(TCAL) != 0 &&
146  calibration.count(PCAL) != 0 &&
147  calibration.count(RCAL) != 0 &&
148  calibration.count(ACAL) != 0 &&
149  calibration.count(CCAL) != 0 &&
150  calibration.count(SCAL) != 0);
151  const bool daq = (run != -1);
152 
153  if (file && id != "") { FATAL("Inconsistent specificaton of detector file and detector identifier."); }
154  if (file && tpr) { FATAL("Inconsistent specificaton of detector file and {t,p,r}cal calibration sets."); }
155  if (file && tpracs) { FATAL("Inconsistent specificaton of detector file and {t,p,r,a,c,s}cal calibration sets."); }
156  if (file && daq) { FATAL("Inconsistent specificaton of detector file and run number."); }
157 
158  if (tpr && id == "") { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and no detector identifier."); }
159  if (tpr && variant != "") { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and variant."); }
160  if (tpr && daq) { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and run number."); }
161 
162  if (tpracs && id == "") { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and no detector identifier."); }
163  if (tpracs && variant == "") { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and no variant."); }
164  if (tpracs && daq) { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and run number."); }
165 
166  if (daq && id == "") { FATAL("Inconsistent specificaton of run number and no detector identifier."); }
167 
168  if (wizzard &&
169  variant != "" &&
170  getDetectorVersion(variant) < JDetectorVersion::V4) {
171  FATAL("Inconsistent detector variant and option -W.");
172  }
173 
174  try {
175  JDB::reset(usr, pwd, cookie);
176  }
177  catch(const exception& error) {
178  FATAL(error.what() << endl);
179  }
180 
182 
183  try {
184 
185  if (file)
186  load(detectorFile, detector);
187  else if (tpr)
188  *(JDB::get()->DetX)(id.c_str(),
189  calibration[TCAL] != "?" ? calibration[TCAL].c_str() : NULL,
190  calibration[PCAL] != "?" ? calibration[PCAL].c_str() : NULL,
191  calibration[RCAL] != "?" ? calibration[RCAL].c_str() : NULL) >> detector;
192  else if (tpracs)
193  *(JDB::get()->DetX)(id.c_str(),
194  calibration[TCAL] != "?" ? calibration[TCAL].c_str() : NULL,
195  calibration[PCAL] != "?" ? calibration[PCAL].c_str() : NULL,
196  calibration[RCAL] != "?" ? calibration[RCAL].c_str() : NULL,
197  calibration[ACAL] != "?" ? calibration[ACAL].c_str() : NULL,
198  calibration[CCAL] != "?" ? calibration[CCAL].c_str() : NULL,
199  calibration[SCAL] != "?" ? calibration[SCAL].c_str() : NULL,
200  getDetectorVersion(variant)) >> detector;
201  else if (daq && variant == "")
202  *(JDB::get()->DetX)(id.c_str(), run) >> detector;
203  else if (daq && variant != "")
204  *(JDB::get()->DetX)(id.c_str(), run, getDetectorVersion(variant)) >> detector;
205  else
206  FATAL("Invalid options.");
207  }
208  catch(const exception& error) {
209  FATAL(error.what() << endl);
210  }
211 
212 
213  detector.comment.add(JMeta(argc,argv));
214 
215  if (detector.setToLatestVersion()) {
216  NOTICE("Set detector version to " << detector.getVersion() << endl);
217  }
218 
219 
220  if (wizzard) {
221 
222  vector<JCLBMap> clbmap;
223 
224  try {
225 
226  JSelector selector = getSelector<JCLBMap>(getDetector(detector.getID()));
227 
228  selector.add(&JCLBMap::FLOORID, 0);
229 
230  ResultSet& rs = getResultSet(getTable<JCLBMap>(), selector);
231 
232  rs >> clbmap;
233 
234  rs.Close();
235  }
236  catch(const exception& error) {
237  FATAL(error.what() << endl);
238  }
239 
241 
242  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
243  zmap[module->getString()][module->getFloor()] = module->getPosition();
244  }
245 
246  for (vector<JCLBMap>::const_iterator i = clbmap.begin(); i != clbmap.end(); ++i) {
247 
248  if (i->FLOORID == 0) {
249 
250  JDetector::iterator p = find_if(detector.begin(), detector.end(), make_predicate(&JModule::getID, i->DOMID, JComparison::eq()));
251 
252  if (p == detector.end()) {
253 
254  if (zmap[i->DUID].count(1) != 0) {
255 
256  JModule module(i->DOMID, JLocation(i->DUID, i->FLOORID));
257 
258  module.setPosition(JPosition3D(zmap[i->DUID][1].getX(),
259  zmap[i->DUID][1].getY(),
262  0.0));
263 
264  NOTICE("Adding module " << module << endl);
265 
266  detector.push_back(module);
267 
268  } else {
269 
270  ERROR("Missing floor in string " << FILL(4,'0') << i->DUID << FILL() << endl);
271  }
272 
273  } else if (zmap[i->DUID].count(1) != 0) {
274 
275  if (fabs(p->getX() - zmap[i->DUID][1].getX()) > precision ||
276  fabs(p->getY() - zmap[i->DUID][1].getY()) > precision) {
277 
278  NOTICE("Offset module " << getLabel(p->getLocation()) << " (x,y) position "
279  << SCIENTIFIC(12,3) << (p->getX() - zmap[i->DUID][1].getX()) << ' '
280  << SCIENTIFIC(12,3) << (p->getY() - zmap[i->DUID][1].getY()) << endl);
281 
282  p->setPosition(JPosition3D(zmap[i->DUID][1].getX(),
283  zmap[i->DUID][1].getY(),
284  p->getZ()));
285  }
286 
287  } else {
288 
289  FATAL("Missing floor in string " << setw(4) << i->DUID << endl);
290  }
291  }
292  }
293  }
294 
295  if (wizzard > 1) {
296 
297  try {
298 
299  JDetectorIntegration_t detector_t;
300 
301  ResultSet& rs = getResultSet(getTable<JDetectorIntegration_t>());
302 
303  if (! (rs >> detector_t)) {
304  THROW(JDatabaseException, "Error reading " << getTable<JDetectorIntegration_t>());
305  }
306 
307  detector_t.configure(getDetector(detector.getID()));
308 
309  for (const JPBS_t& pbs : { PBS::ACOUSTIC_SENSOR, PBS::HYDROPHONE }) {
310 
311  const JProductRouter router(detector_t, getPBSSequences(pbs));
312 
314 
315  for (JDetectorIntegration_t::range_const_iterator i = range.first; i != range.second; ++i) {
316 
317  const JLocation_t location = router.getLocation(i->first);
318 
319  JDetector::iterator p = find_if(detector.begin(),
320  detector.end(),
321  make_predicate(&JModule::getLocation,
322  JLocation(location.string, (pbs != PBS::HYDROPHONE ? location.floor : 0)),
323  JComparison::eq()));
324 
325  if (p != detector.end()) {
326 
327  try {
328 
329  const double t0 = getAverage(make_array(p->begin(), p->end(), &JPMT::getT0), 0.0) - getDelayTime(i->first);
330 
331  if (t0 != p->getT0()) {
332 
333  NOTICE("Set module time calibration " << getLabel(p->getLocation()) << ' ' << setw(10) << p->getID() << ' ' << setw(28) << left << i->first << right << ' ' << FIXED(12,3) << t0 << endl);
334 
335  p->setCalibration(t0);
336  }
337  }
338  catch(const exception& error) {
339  if (i->first.getVariant() != NOPIEZO) {
340  ERROR(location << ' ' << error.what() << endl);
341  }
342  }
343 
344  } else {
345 
346  ERROR("No module for UPI " << i->first << " at " << location << endl);
347  }
348  }
349  }
350  }
351  catch(const exception& error) {
352  FATAL(error.what() << endl);
353  }
354  }
355 
356  if (outputFile != "")
358  else
359  cout << detector;
360 }
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