Jpp  17.1.1
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 
9 #include "JDB/JDB.hh"
10 #include "JDB/JSelector.hh"
12 #include "JDB/JCLBMap.hh"
14 #include "JDB/JPBSSequence.hh"
15 #include "JDB/JProductRouter.hh"
16 #include "JDB/JDBToolkit.hh"
17 
18 #include "JDetector/JDetector.hh"
22 
23 #include "JLang/JEquation_t.hh"
24 #include "JLang/JComparator.hh"
25 #include "JLang/JPredicate.hh"
26 
27 #include "JSupport/JMeta.hh"
28 
29 #include "Jeep/JPrint.hh"
30 #include "Jeep/JParser.hh"
31 #include "Jeep/JMessage.hh"
32 
33 namespace {
34 
35  using namespace JPP;
36 
37  /**
38  * Get delay time for acoustic sensor with given %UPI.
39  *
40  * \param upi %UPI
41  * \return delay time [ns]
42  */
43  inline double getDelayTime(const JUPI_t& upi)
44  {
45  using namespace std;
46  using namespace JPP;
47 
48  if (upi.getPBS() == PBS::HYDROPHONE)
49  return HYDROPHONE_DELAYTIME_US * 1.0e3;
50  else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V1") != string::npos)
51  return PIEZO_V1_DELAYTIME_US * 1.0e3;
52  else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V2") != string::npos)
53  return PIEZO_V2_DELAYTIME_US * 1.0e3;
54  else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V3") != string::npos)
55  return PIEZO_V3_DELAYTIME_US * 1.0e3;
56  else
57  THROW(JValueOutOfRange, "Invalid UPI " << upi);
58  }
59 
60  /**
61  * List of valid keys.
62  */
63  static const std::set<std::string> keys = { TCAL, PCAL, RCAL, ACAL, CCAL, SCAL };
64 }
65 
66 
67 /**
68  * \file
69  *
70  * Auxiliary program to download detector from database.
71  * \author mdejong
72  */
73 int main(int argc, char **argv)
74 {
75  using namespace std;
76  using namespace JPP;
77 
78  JServer server;
79  string usr;
80  string pwd;
81  string cookie;
82  string id;
83  vector<JEquation_t> calset;
84  string detectorFile;
85  string outputFile;
86  int run;
87  string variant;
88  JCounter wizzard;
89  int debug;
90 
91  try {
92 
93  JParser<> zap("Auxiliary program to download detector from database.");
94 
95  zap['s'] = make_field(server) = getServernames();
96  zap['u'] = make_field(usr) = "";
97  zap['!'] = make_field(pwd) = "";
98  zap['C'] = make_field(cookie) = "";
99  zap['D'] = make_field(id, "detector identifier") = "";
100  zap['r'] = make_field(run, "run number") = -1;
101  zap['@'] = make_field(calset, "calibration sets \"<key> = <value>[; <key> = <value>]\"" << endl
102  << "possible keys: " << JEEPZ() << keys) = JPARSER::initialised();
103  zap['a'] = make_field(detectorFile) = "";
104  zap['o'] = make_field(outputFile) = "";
105  zap['V'] = make_field(variant, "detector version") = getDetectorVersions<string>(), "";
106  zap['W'] = make_field(wizzard, "-W add base modules; -WW set time offsets of acoustic sensors");
107  zap['d'] = make_field(debug) = 2;
108 
109  zap(argc, argv);
110  }
111  catch(const exception &error) {
112  FATAL(error.what() << endl);
113  }
114 
115 
116  map<string, string> calibration;
117 
118  for (vector<JEquation_t>::const_iterator i = calset.begin(); i != calset.end(); ++i) {
119 
120  if (keys.count(i->getKey()) == 0)
121  FATAL("Invalid calibration set \"" << i->getKey() << "\"" << endl);
122  else
123  calibration[i->getKey()] = i->getValue();
124  }
125 
126  if (calibration.size() != 0u &&
127  calibration.size() != 3u &&
128  calibration.size() != 6u) {
129  FATAL("Inconsistent specificaton of calibration sets, either {}, {t,p,r}cal or {t,p,r,a,c,s}cal.");
130  }
131 
132  const bool file = (detectorFile != "");
133  const bool tpr = (calibration.count(TCAL) != 0 &&
134  calibration.count(PCAL) != 0 &&
135  calibration.count(RCAL) != 0 &&
136  calibration.count(ACAL) == 0 &&
137  calibration.count(CCAL) == 0 &&
138  calibration.count(SCAL) == 0);
139  const bool tpracs = (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 daq = (run != -1);
146 
147  if (file && id != "") { FATAL("Inconsistent specificaton of detector file and detector identifier."); }
148  if (file && tpr) { FATAL("Inconsistent specificaton of detector file and {t,p,r}cal calibration sets."); }
149  if (file && tpracs) { FATAL("Inconsistent specificaton of detector file and {t,p,r,a,c,s}cal calibration sets."); }
150  if (file && daq) { FATAL("Inconsistent specificaton of detector file and run number."); }
151 
152  if (tpr && id == "") { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and no detector identifier."); }
153  if (tpr && variant != "") { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and variant."); }
154  if (tpr && daq) { FATAL("Inconsistent specificaton of {t,p,r}cal calibration sets and run number."); }
155 
156  if (tpracs && id == "") { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and no detector identifier."); }
157  if (tpracs && variant == "") { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and no variant."); }
158  if (tpracs && daq) { FATAL("Inconsistent specificaton of {t,p,r,a,c,s}cal calibration sets and run number."); }
159 
160  if (daq && id == "") { FATAL("Inconsistent specificaton of run number and no detector identifier."); }
161 
162  if (wizzard &&
163  variant != "" &&
165  FATAL("Inconsistent detector variant and option -W.");
166  }
167 
168  try {
169  JDB::reset(usr, pwd, cookie);
170  }
171  catch(const exception& error) {
172  FATAL(error.what() << endl);
173  }
174 
176 
177  try {
178 
179  if (file)
180  load(detectorFile, detector);
181  else if (tpr)
182  *(JDB::get()->DetX)(id.c_str(),
183  calibration[TCAL].c_str(),
184  calibration[PCAL].c_str(),
185  calibration[RCAL].c_str()) >> detector;
186  else if (tpracs)
187  *(JDB::get()->DetX)(id.c_str(),
188  calibration[TCAL].c_str(),
189  calibration[PCAL].c_str(),
190  calibration[RCAL].c_str(),
191  calibration[ACAL].c_str(),
192  calibration[CCAL].c_str(),
193  calibration[SCAL].c_str(),
194  getDetectorVersion(variant)) >> detector;
195  else if (daq && variant == "")
196  *(JDB::get()->DetX)(id.c_str(), run) >> detector;
197  else if (daq && variant != "")
198  *(JDB::get()->DetX)(id.c_str(), run, getDetectorVersion(variant)) >> detector;
199  else
200  FATAL("Invalid options.");
201  }
202  catch(const exception& error) {
203  FATAL(error.what() << endl);
204  }
205 
206 
207  detector.comment.add(JMeta(argc,argv));
208 
209  if (detector.setToLatestVersion()) {
210  NOTICE("Set detector version to " << detector.getVersion() << endl);
211  }
212 
213 
214  if (wizzard) {
215 
216  vector<JCLBMap> clbmap;
217 
218  try {
219 
220  JSelector selector = getSelector<JCLBMap>(getDetector(detector.getID()));
221 
222  selector.add(&JCLBMap::FLOORID, 0);
223 
224  ResultSet& rs = getResultSet(getTable<JCLBMap>(), selector);
225 
226  rs >> clbmap;
227 
228  rs.Close();
229  }
230  catch(const exception& error) {
231  FATAL(error.what() << endl);
232  }
233 
235 
236  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
237  zmap[module->getString()][module->getFloor()] = module->getPosition();
238  }
239 
240  for (vector<JCLBMap>::const_iterator i = clbmap.begin(); i != clbmap.end(); ++i) {
241 
242  if (i->FLOORID == 0) {
243 
244  JDetector::iterator p = find_if(detector.begin(), detector.end(), make_predicate(&JModule::getID, i->DOMID, JComparison::eq()));
245 
246  if (p == detector.end()) {
247 
248  JModule module(i->DOMID, JLocation(i->DUID, i->FLOORID));
249 
250  module.setPosition(JPosition3D(zmap[i->DUID].begin()->second.getX(),
251  zmap[i->DUID].begin()->second.getY(),
254  0.0));
255 
256  NOTICE("Adding module " << module << endl);
257 
258  detector.push_back(module);
259 
260  } else if (zmap[i->DUID].count(1) != 0) {
261 
262  p->setPosition(JPosition3D(zmap[i->DUID][1].getX(),
263  zmap[i->DUID][1].getY(),
264  p->getZ()));
265 
266  NOTICE("Setting module " << *p << endl);
267  }
268  }
269  }
270  }
271 
272  if (wizzard > 1) {
273 
274  try {
275 
276  NOTICE("Setting time offsets of acoustic sensors... " << flush);
277 
278  JDetectorIntegration_t detector_t;
279 
280  ResultSet& rs = getResultSet(getTable<JDetectorIntegration_t>());
281 
282  if (! (rs >> detector_t)) {
283  THROW(JDatabaseException, "Error reading " << getTable<JDetectorIntegration_t>());
284  }
285 
286  detector_t.configure(getDetector<string>(id));
287 
288  for (const JPBS_t& pbs : { PBS::ACOUSTIC_SENSOR, PBS::HYDROPHONE }) {
289 
290  const JProductRouter router(detector_t, getPBSSequences(pbs));
291 
293 
294  for (JDetectorIntegration_t::range_const_iterator i = range.first; i != range.second; ++i) {
295 
296  const JLocation_t location = router.getLocation(i->first);
297 
298  JDetector::iterator p = find_if(detector.begin(),
299  detector.end(),
301  JLocation(location.string, (pbs != PBS::HYDROPHONE ? location.floor : 0)),
302  JComparison::eq()));
303 
304  if (p != detector.end())
305  p->setCalibration(getAverage(make_array(p->begin(), p->end(), &JModule::getT0), 0.0) - getDelayTime(i->first));
306  else
307  ERROR("No module for UPI " << i->first << endl);
308  }
309  }
310 
311  NOTICE("OK" << endl);
312  }
313  catch(const exception& error) {
314  FATAL(error.what() << endl);
315  }
316  }
317 
318  if (outputFile != "")
320  else
321  cout << detector;
322 }
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 const std::string RCAL
(optical|base) module orientations
static void reset()
Reset connection to database.
Definition: JDB.hh:236
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].
Database exception.
Definition: JException.hh:666
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:497
static const double PIEZO_V3_DELAYTIME_US
Piezo delay time [us].
static const double PIEZO_V1_DELAYTIME_US
Piezo delay time [us].
static const std::string TCAL
PMT time offsets.
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
static const std::string CCAL
compass alignment (a.k.a. quaternion calibration)
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:365
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].
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
static const std::string SCAL
PMT status.
#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
static const std::string PCAL
(optical|base) module positions
static const std::string ACAL
acoustic time offsets (piezo sensor or hydrophone)
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.
General purpose messaging.
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 JPBS_t HYDROPHONE(4, 5)
PBS of hydrophone
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:432
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:98
Wrapper class for server name.
Definition: JDB.hh:42
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
double u[N+1]
Definition: JPolint.hh:776
int string
position in detector
Definition: JLocation_t.hh:105
void configure(const std::string &detid)
Configure detector integration for given detector identifier.
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:225