Jpp  16.0.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 <map>
6 #include <algorithm>
7 
8 #include "JDB/JDB.hh"
9 #include "JDB/JSelector.hh"
11 #include "JDB/JCLBMap.hh"
13 #include "JDB/JPBSSequence.hh"
14 #include "JDB/JProductRouter.hh"
15 #include "JDB/JDBToolkit.hh"
16 
17 #include "JDetector/JDetector.hh"
20 
21 #include "JLang/JComparator.hh"
22 #include "JLang/JPredicate.hh"
23 
24 #include "JSupport/JMeta.hh"
25 
26 #include "Jeep/JPrint.hh"
27 #include "Jeep/JParser.hh"
28 #include "Jeep/JMessage.hh"
29 
30 namespace {
31 
32  using JDATABASE::JUPI_t;
33 
34  /**
35  * Get delay time for acoustic sensor with given %UPI.
36  *
37  * \param upi %UPI
38  * \return delay time [ns]
39  */
40  inline double getDelayTime(const JUPI_t& upi)
41  {
42  using namespace std;
43  using namespace JPP;
44 
45  if (upi.getPBS() == PBS::HYDROPHONE)
46  return HYDROPHONE_DELAYTIME_US * 1.0e3;
47  else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V1") != string::npos)
48  return PIEZO_V1_DELAYTIME_US * 1.0e3;
49  else if (upi.getPBS() == PBS::ACOUSTIC_SENSOR && upi.getVariant().find("V2") != string::npos)
50  return PIEZO_V2_DELAYTIME_US * 1.0e3;
51  else
52  THROW(JValueOutOfRange, "Invalid UPI " << upi);
53  }
54 }
55 
56 
57 /**
58  * \file
59  *
60  * Auxiliary program to print detector from database.
61  * \author mdejong
62  */
63 int main(int argc, char **argv)
64 {
65  using namespace std;
66  using namespace JPP;
67 
68  JServer server;
69  string usr;
70  string pwd;
71  string cookie;
72  string id;
73  string tcalset;
74  string pcalset;
75  string rcalset;
76  string detectorFile;
77  string outputFile;
78  int run;
79  JCounter wizzard;
80  int debug;
81 
82  try {
83 
84  JParser<> zap("Auxiliary program to print detector from database.");
85 
86  zap['s'] = make_field(server) = getServernames();
87  zap['u'] = make_field(usr) = "";
88  zap['!'] = make_field(pwd) = "";
89  zap['C'] = make_field(cookie) = "";
90  zap['D'] = make_field(id, "detector identifier") = "";
91  zap['r'] = make_field(run, "run number") = -1;
92  zap['t'] = make_field(tcalset, "tcal: t0 (time offsets) calibration set from DB") = "";
93  zap['p'] = make_field(pcalset, "pcal: position (x,y,z) calibration set from DB") = "";
94  zap['q'] = make_field(rcalset, "rcal: orientation (quaterions) calibration set from DB") = "";
95  zap['a'] = make_field(detectorFile) = "";
96  zap['o'] = make_field(outputFile) = "";
97  zap['W'] = make_field(wizzard, "-W add base modules; -WW set time offsets of acoustic sensors");
98  zap['d'] = make_field(debug) = 2;
99 
100  zap(argc, argv);
101  }
102  catch(const exception &error) {
103  FATAL(error.what() << endl);
104  }
105 
106 
107  const int file = ((detectorFile != "" ? 1 : 0));
108  const int calset = ((tcalset.empty() ? 0 : 1) +
109  (pcalset.empty() ? 0 : 1) +
110  (rcalset.empty() ? 0 : 1));
111  const int daq = ((run != -1 ? 1 : 0));
112 
113  if (file != 0 && (file != 1)) { FATAL("Inconsistent specificaton of detector file."); }
114  if (calset != 0 && (calset != 3 || id == "")) { FATAL("Inconsistent specificaton of detector identifier, tcal, pcal and rcal calibration sets."); }
115  if (daq != 0 && (daq != 1 || id == "")) { FATAL("Inconsistent specificaton of detector identifier and run number."); }
116 
117  int np = 0;
118 
119  if (file == 1) { ++np; }
120  if (calset == 3) { ++np; }
121  if (daq == 1) { ++np; }
122 
123  if (np != 1) { FATAL("Specify detector file; OR detector identifier, tcal, pcal and rcal calibration set; OR detector identifier and run number."); }
124 
125 
126  try {
127  JDB::reset(usr, pwd, cookie);
128  }
129  catch(const exception& error) {
130  FATAL(error.what() << endl);
131  }
132 
134 
135  try {
136 
137  if (file == 1)
138  load(detectorFile, detector);
139  else if (calset == 3)
140  *(JDB::get()->DetX)(id.c_str(), tcalset.c_str(), pcalset.c_str(), rcalset.c_str()) >> detector;
141  else if (daq == 1)
142  *(JDB::get()->DetX)(id.c_str(), run) >> detector;
143  }
144  catch(const exception& error) {
145  FATAL(error.what() << endl);
146  }
147 
148  detector.comment.add(JMeta(argc,argv));
149 
150  if (detector.setToLatestVersion()) {
151  NOTICE("Set detector version to " << detector.getVersion() << endl);
152  }
153 
154  if (wizzard) {
155 
156  vector<JCLBMap> clbmap;
157 
158  try {
159 
160  JSelector selector = getSelector<JCLBMap>(getDetector(detector.getID()));
161 
162  selector.add(&JCLBMap::FLOORID, 0);
163 
164  ResultSet& rs = getResultSet(getTable<JCLBMap>(), selector);
165 
166  rs >> clbmap;
167 
168  rs.Close();
169  }
170  catch(const exception& error) {
171  FATAL(error.what() << endl);
172  }
173 
175 
176  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
177  zmap[module->getString()][module->getFloor()] = module->getPosition();
178  }
179 
180  for (vector<JCLBMap>::const_iterator i = clbmap.begin(); i != clbmap.end(); ++i) {
181 
182  if (i->FLOORID == 0) {
183 
184  JDetector::iterator p = find_if(detector.begin(), detector.end(), make_predicate(&JModule::getID, i->DOMID, JComparison::eq()));
185 
186  if (p == detector.end()) {
187 
188  JModule module(i->DOMID, JLocation(i->DUID, i->FLOORID));
189 
190  module.setPosition(JPosition3D(zmap[i->DUID].begin()->second.getX(),
191  zmap[i->DUID].begin()->second.getY(),
194  0.0));
195 
196  NOTICE("Adding module " << module << endl);
197 
198  detector.push_back(module);
199  }
200  }
201  }
202  }
203 
204  if (wizzard > 1) {
205 
206  try {
207 
208  NOTICE("Setting time offsets of acoustic sensors... " << flush);
209 
210  JDetectorIntegration_t detector_t;
211 
212  ResultSet& rs = getResultSet(getTable<JDetectorIntegration_t>());
213 
214  if (! (rs >> detector_t)) {
215  THROW(JDatabaseException, "Error reading " << getTable<JDetectorIntegration_t>());
216  }
217 
218  detector_t.configure(getDetector<string>(id));
219 
220  for (const JPBS_t& pbs : { PBS::ACOUSTIC_SENSOR, PBS::HYDROPHONE }) {
221 
222  const JProductRouter router(detector_t, getPBSSequences(pbs));
223 
225 
226  for (JDetectorIntegration_t::range_const_iterator i = range.first; i != range.second; ++i) {
227 
228  const JLocation_t location = router.getLocation(i->first);
229 
230  JDetector::iterator p = find_if(detector.begin(),
231  detector.end(),
232  make_predicate(&JModule::getLocation,
233  JLocation(location.string, (pbs != PBS::HYDROPHONE ? location.floor : 0)),
234  JComparison::eq()));
235 
236  if (p != detector.end())
237  p->setCalibration(getAverage(make_array(p->begin(), p->end(), &JModule::getT0), 0.0) - getDelayTime(i->first));
238  else
239  FATAL("No module for UPI " << i->first << endl);
240  }
241  }
242 
243  NOTICE("OK" << endl);
244  }
245  catch(const exception& error) {
246  FATAL(error.what() << endl);
247  }
248  }
249 
250  if (outputFile != "")
252  else
253  cout << detector;
254 }
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:1500
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.
then set_variable PMT_FILE set_variable DAQ_FILE set_variable OUTPUT_FILE set_variable DETECTOR else fatal Wrong number of arguments fi set_variable RUNBYRUN file
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
T get(const JHead &header)
Get object from header.
std::iterator_traits< T >::value_type getAverage(T __begin, T __end)
Get average.
Definition: JMath.hh:497
static const double PIEZO_V1_DELAYTIME_US
Piezo delay time [us].
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
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:99
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:224
Detector support kit.
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:1961
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
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
range_type find(const JUPI_t &upi) const
Find range of products with given UPI.
int debug
debug level
Definition: JSirene.cc:63
Auxiliary class to map UPI to location in detector.
General purpose messaging.
#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.
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:25
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
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.
void setPosition(const JVector3D &pos)
Set position.
Definition: JPosition3D.hh:152