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
Functions
JConstructDetector.cc File Reference

Auxiliary program to compose detector from separate calibrations. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <map>
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorCalibration.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JDetectorAddressMapToolkit.hh"
#include "JDetector/JModuleRouter.hh"
#include "JDetector/JPMTRouter.hh"
#include "JSupport/JMeta.hh"
#include "JSon/JSon.hh"
#include "Jeep/JeepToolkit.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 compose detector from separate calibrations.

Author
mdejong

Definition in file JConstructDetector.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 30 of file JConstructDetector.cc.

31 {
32  using namespace std;
33  using namespace JPP;
34 
35  string detectorFile;
36  vector<string> inputFile;
37  string outputFile;
38  int debug;
39 
40  try {
41 
42  JParser<> zap("Auxiliary program to compose detector from separate calibrations.");
43 
44  zap['a'] = make_field(detectorFile, "detector file");
45  zap['f'] = make_field(inputFile, "detector calibration files in JSON format "\
46  "(wild card \'" << FILENAME_WILD_CARD << "\' will be replaced by corresponding calibration set)");
47  zap['o'] = make_field(outputFile, "detector file") = "";
48  zap['d'] = make_field(debug) = 1;
49 
50  zap(argc, argv);
51  }
52  catch(const exception &error) {
53  FATAL(error.what() << endl);
54  }
55 
56 
58 
59  try {
60  load(detectorFile, detector);
61  }
62  catch(const JException& error) {
63  FATAL(error);
64  }
65 
66  if (!hasDetectorAddressMap(detector.getID())) {
67  FATAL("No detector address map for detector identier " << detector.getID() << endl);
68  }
69 
70  const JDetectorAddressMap& demo = getDetectorAddressMap(detector.getID());
71 
72  const JModuleRouter moduleRouter(detector);
73  const JPMTRouter pmtRouter (detector);
74 
75 
76  enum {
77  UNDEFINED = 0,
78  DEFINED = 1
79  };
80 
81  struct status_type {
82 
83  status_type() :
84  value(UNDEFINED)
85  {}
86 
87  operator int() const { return value; }
88 
89  status_type& operator=(const int value)
90  {
91  this->value = value;
92 
93  return *this;
94  }
95 
96  int value;
97  };
98 
99  typedef map<int, status_type> map_type;
100 
101  map_type tcal;
102  map_type pcal;
103  map_type rcal;
104  map_type acal;
105  map_type ccal;
106  map_type scal[2];
107 
108 
109  for (const auto& file_name : inputFile) {
110 
111  vector<string> buffer;
112 
113  if (hasWildCard(file_name))
114  buffer = { setWildCard(file_name, TCAL),
115  setWildCard(file_name, PCAL),
116  setWildCard(file_name, RCAL),
117  setWildCard(file_name, ACAL),
118  setWildCard(file_name, CCAL),
119  setWildCard(file_name, SCAL) };
120  else
121  buffer = { file_name };
122 
123  for (const auto& file_name : buffer) {
124 
125  const JSon js(file_name);
126 
127  json::const_iterator data;
128 
129  if (is_valid(js) && (data = js.find(Data_t)) != js.end()) {
130 
131  detector.comment.add(MAKE_STRING("calibration=" << (js.contains(Comment_t) ? js[Comment_t] : "?")));
132 
133  for (size_t i = 0; i != data->size(); ++i) {
134 
135  if (data->at(i).contains(DetID_t)) {
136  if (data->at(i)[DetID_t].get<int>() != detector.getID()) {
137  FATAL("Detector identifier mismatch " << data->at(i)[DetID_t].get<int>() << " != " << detector.getID() << endl);
138  }
139  }
140 
141  if (data->at(i).contains(PMTT0s_t)) {
142 
143  for (const auto& element : data->at(i)[PMTT0s_t].get<JPMTCalibration>()) {
144 
145  if (pmtRouter.hasPMT(element.getID())) {
146 
147  JPMT& pmt = detector.getPMT(pmtRouter.getAddress(element.getID()));
148 
149  pmt.setCalibration(element.getCalibration());
150 
151  tcal[pmt.getID()] = DEFINED;
152  }
153  }
154  }
155 
156  if (data->at(i).contains(DOMPositions_t)) {
157 
158  for (const auto& element : data->at(i)[DOMPositions_t].get<JModulePosition>()) {
159 
160  if (moduleRouter.hasModule(element.getID())) {
161 
162  JModule& module = detector.getModule(moduleRouter.getAddress(element.getID()));
163 
164  module.add(element.getPosition()); // position of reference module may not exactly be at origin
165 
166  pcal[module.getID()] = DEFINED;
167  }
168  }
169  }
170 
171  if (data->at(i).contains(BasePositions_t)) {
172 
173  for (const auto& element : data->at(i)[BasePositions_t].get<JModulePosition>()) {
174 
175  if (moduleRouter.hasModule(element.getID())) {
176 
177  JModule& module = detector.getModule(moduleRouter.getAddress(element.getID()));
178 
179  module.add(element.getPosition());
180 
181  pcal[module.getID()] = DEFINED;
182  }
183  }
184  }
185 
186  if (data->at(i).contains(DOMRotations_t)) {
187 
188  for (const auto& element : data->at(i)[DOMRotations_t].get<JModuleRotation>()) {
189 
190  if (moduleRouter.hasModule(element.getID())) {
191 
192  JModule& module = detector.getModule(moduleRouter.getAddress(element.getID()));
193 
194  const JModule buffer = getModule(demo.get(module.getID()), module.getID());
195 
196  if (module.size() != buffer.size()) {
197  FATAL("Module size " << module.size() << " != " << buffer.size() << endl);
198  }
199 
200  const JPosition3D center = module.getCenter();
201 
202  for (size_t i = 0; i != module.size(); ++i) {
203  module.getPMT(i).setAxis(buffer.getPMT(i).getAxis());
204  }
205 
206  module.rotate(element.getQuaternion());
207 
208  module.setPosition(module.getCenter());
209  module.set(center);
210 
211  rcal[module.getID()] = DEFINED;
212  }
213  }
214  }
215 
216  if (data->at(i).contains(DOMAcousticT0_t)) {
217 
218  for (const auto& element : data->at(i)[DOMAcousticT0_t].get<JModuleCalibration>()) {
219 
220  if (moduleRouter.hasModule(element.getID())) {
221 
222  JModule& module = detector.getModule(moduleRouter.getAddress(element.getID()));
223 
224  module.setCalibration(element.getCalibration());
225 
226  acal[module.getID()] = DEFINED;
227  }
228  }
229  }
230 
231  if (data->at(i).contains(BaseAcousticT0_t)) {
232 
233  for (const auto& element : data->at(i)[BaseAcousticT0_t].get<JModuleCalibration>()) {
234 
235  if (moduleRouter.hasModule(element.getID())) {
236 
237  JModule& module = detector.getModule(moduleRouter.getAddress(element.getID()));
238 
239  module.setCalibration(element.getCalibration());
240 
241  acal[module.getID()] = DEFINED;
242  }
243  }
244  }
245 
246  if (data->at(i).contains(DOMCompassRotations_t)) {
247 
248  for (const auto& element : data->at(i)[DOMCompassRotations_t].get<JCompassRotation>()) {
249 
250  if (moduleRouter.hasModule(element.getID())) {
251 
252  JModule& module = detector.getModule(moduleRouter.getAddress(element.getID()));
253 
254  module.setQuaternion(element.getQuaternion());
255 
256  ccal[module.getID()] = DEFINED;
257  }
258  }
259  }
260 
261  if (data->at(i).contains(BaseCompassRotations_t)) {
262 
263  for (const auto& element : data->at(i)[BaseCompassRotations_t].get<JCompassRotation>()) {
264 
265  if (moduleRouter.hasModule(element.getID())) {
266 
267  JModule& module = detector.getModule(moduleRouter.getAddress(element.getID()));
268 
269  module.setQuaternion(element.getQuaternion());
270 
271  ccal[module.getID()] = DEFINED;
272  }
273  }
274  }
275 
276  if (data->at(i).contains(PMTStatusInfo_t)) {
277 
278  for (const auto& element : data->at(i)[PMTStatusInfo_t].get<JPMTStatus>()) {
279 
280  if (pmtRouter.hasPMT(element.getID())) {
281 
282  JPMT& pmt = detector.getPMT(pmtRouter.getAddress(element.getID()));
283 
284  pmt.setStatus(element.getStatus());
285 
286  scal[0][pmt.getID()] = DEFINED;
287  }
288  }
289  }
290 
291  if (data->at(i).contains(DOMStatusInfo_t)) {
292 
293  for (const auto& element : data->at(i)[DOMStatusInfo_t].get<JModuleStatus>()) {
294 
295  if (moduleRouter.hasModule(element.getID())) {
296 
297  JModule& module = detector.getModule(moduleRouter.getAddress(element.getID()));
298 
299  module.setStatus(element.getStatus());
300 
301  scal[1][module.getID()] = DEFINED;
302  }
303  }
304  }
305 
306  if (data->at(i).contains(BaseStatusInfo_t)) {
307 
308  for (const auto& element : data->at(i)[BaseStatusInfo_t].get<JModuleStatus>()) {
309 
310  if (moduleRouter.hasModule(element.getID())) {
311 
312  JModule& module = detector.getModule(moduleRouter.getAddress(element.getID()));
313 
314  module.setStatus(element.getStatus());
315 
316  scal[1][module.getID()] = DEFINED;
317  }
318  }
319  }
320  }
321  }
322  }
323  }
324 
325  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
326 
327  if (pcal [module->getID()] != DEFINED) { ERROR("Module " << setw(10) << module->getID() << ' ' << getLabel(module->getLocation()) << " no position calibration." << endl); }
328  if (rcal [module->getID()] != DEFINED &&
329  module->getFloor() != 0) { ERROR("Module " << setw(10) << module->getID() << ' ' << getLabel(module->getLocation()) << " no rotation calibration." << endl); }
330  if (acal [module->getID()] != DEFINED) { ERROR("Module " << setw(10) << module->getID() << ' ' << getLabel(module->getLocation()) << " no acoustics calibration." << endl); }
331  if (ccal [module->getID()] != DEFINED) { ERROR("Module " << setw(10) << module->getID() << ' ' << getLabel(module->getLocation()) << " no compass calibration." << endl); }
332  if (scal[1][module->getID()] != DEFINED) { ERROR("Module " << setw(10) << module->getID() << ' ' << getLabel(module->getLocation()) << " no status calibration." << endl); }
333 
334  for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
335  if (tcal [pmt->getID()] != DEFINED) { ERROR("PMT " << setw(8) << pmt->getID() << " no time calibration." << endl); }
336  if (scal[0][pmt->getID()] != DEFINED) { ERROR("PMT " << setw(8) << pmt->getID() << " no status calibration." << endl); }
337  }
338  }
339 
340 
341  detector.comment.add(JMeta(argc,argv));
342 
343  try {
344  if (outputFile != "")
346  else
347  cout << detector << endl;
348  }
349  catch(const JException& error) {
350  FATAL(error);
351  }
352 
353  return 0;
354 }
Router for direct addressing of PMT data in detector data structure.
Definition: JPMTRouter.hh:35
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
General exception.
Definition: JException.hh:23
static const std::string DOMStatusInfo_t
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
Data structure for a composite optical module.
Definition: JModule.hh:68
static const std::string ACAL
acoustic time offsets (piezo sensor or hydrophone)
clean eval JPrintDetector a $DETECTOR O IDENTIFIER for CALSET in tcal rcal pcal ccal acal scal
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)
bool is_valid(const json &js)
Check validity of JSon data.
Detector data structure.
Definition: JDetector.hh:89
static const std::string PMTT0s_t
Router for direct addressing of module data in detector data structure.
bool hasDetectorAddressMap(const int id)
Check if detector address map is available.
void setAxis(const JAxis3D &axis)
Set axis.
Definition: JAxis3D.hh:109
Lookup table for PMT addresses in detector.
string outputFile
static const std::string BaseStatusInfo_t
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
static const std::string TCAL
PMT time offsets.
static const std::string Comment_t
Detector file.
Definition: JHead.hh:226
void setQuaternion(const JQuaternion3D &quaternion)
Set quaternion.
const JLocation & getLocation() const
Get location.
Definition: JLocation.hh:69
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
int getID() const
Get identifier.
Definition: JObjectID.hh:50
Data structure for PMT geometry, calibration and status.
Definition: JPMT.hh:43
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
#define ERROR(A)
Definition: JMessage.hh:66
static const std::string DOMPositions_t
JVector3D getCenter() const
Get center of module based on crossing point of PMT axes.
Definition: JModule.hh:215
JDetectorAddressMap & getDetectorAddressMap()
Get detector address map.
static const std::string RCAL
(optical|base) module orientations
const JPMT & getPMT(const int index) const
Get PMT.
Definition: JModule.hh:173
void rotate(const JRotation3D &R)
Rotate module.
Definition: JModule.hh:315
static const std::string DOMCompassRotations_t
void setStatus(const JStatus &status)
Set status.
Definition: JStatus.hh:97
#define FATAL(A)
Definition: JMessage.hh:67
std::string setWildCard(const std::string &file_name, const std::string &value)
Get file name by setting wild card to given value.
Definition: JeepToolkit.hh:66
static const std::string Data_t
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
static const std::string SCAL
(module|PMT) status
void setCalibration(const JCalibration &cal)
Set calibration.
static const std::string BasePositions_t
static const std::string DOMRotations_t
const JAxis3D & getAxis() const
Get axis.
Definition: JAxis3D.hh:98
static const char FILENAME_WILD_CARD
wild card character for file name substitution
Definition: JeepToolkit.hh:44
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
static const std::string BaseCompassRotations_t
Auxiliary class to load json data from file.
Definition: JSon.hh:61
do set_variable DETECTOR_TXT $WORKDIR detector
static const std::string PMTStatusInfo_t
static const std::string PCAL
(optical|base) module positions
static const std::string DetID_t
JModule & set(const JVector3D &pos)
Set position.
Definition: JModule.hh:408
static const std::string DOMAcousticT0_t
const JModule & getModule(const JDetector &detector, const JModuleLocation &location)
find module with a given string and floor number
int debug
debug level
static const std::string BaseAcousticT0_t
JModule & add(const JVector3D &pos)
Add position.
Definition: JModule.hh:420
void setPosition(const JVector3D &pos)
Set position.
Definition: JPosition3D.hh:152
bool hasWildCard(const std::string &file_name)
Check presence of wild card.
Definition: JeepToolkit.hh:53