Jpp  test_elongated_shower_pde
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDetectorCalibration.hh
Go to the documentation of this file.
1 #ifndef __JDETECTOR__JDETECTORCALIBRATION__
2 #define __JDETECTOR__JDETECTORCALIBRATION__
3 
4 #include <string>
5 #include <vector>
6 
7 #include "JSon/JSon.hh"
8 
9 #include "JDB/JPBS_t.hh"
10 #include "JDB/JUPI_t.hh"
11 
12 #include "JLang/JObjectID.hh"
13 #include "JLang/JException.hh"
14 
15 #include "Jeep/JPrint.hh"
16 
18 
21 
22 
23 /**
24  * \author mdejong
25  */
26 
27 namespace JDETECTOR {}
28 namespace JPP { using namespace JDETECTOR; }
29 
30 /**
31  * Auxiliary classes and methods for detector calibration.
32  */
33 namespace JDETECTOR {
34 
35  using JSON::json;
36 
37  using JLANG::JObjectID;
39 
40  using JDATABASE::JUPI_t;
41  using JDATABASE::JPBS_t;
42 
45 
46 
47  static const std::string TCAL = "tcal"; //!< PMT time offsets
48  static const std::string PCAL = "pcal"; //!< (optical|base) module positions
49  static const std::string RCAL = "rcal"; //!< (optical|base) module orientations
50  static const std::string ACAL = "acal"; //!< acoustic time offsets (piezo sensor or hydrophone)
51  static const std::string CCAL = "ccal"; //!< compass alignment (a.k.a.\ quaternion calibration)
52  static const std::string SCAL = "scal"; //!< PMT status
53 
54 
55  /**
56  * Detector calibration key words for JSON I/O.
57  */
58 
59  // Meta data
60  static const std::string User_t = "User";
61  static const std::string Location_t = "Location";
62  static const std::string Time_t = "Time";
63  static const std::string Start_t = "Start";
64  static const std::string End_t = "End";
65  static const std::string Comment_t = "Comment";
66  static const std::string Encoding_t = "Encoding";
67  static const std::string Input_t = "Inputs";
68  static const std::string Error_t = "Error";
69  static const std::string Message_t = "Message";
70  static const std::string Code_t = "Code";
71  static const std::string Provenance_t = "Provenance";
72  static const std::string Info_t = "Info";
73  static const std::string Configuration_t = "Configuration";
74  static const std::string Arguments_t = "Arguments";
75  static const std::string Inputs_t = "Inputs";
76  static const std::string UUID_t = "UUID";
77  static const std::string APIVersion_t = "APIVersion";
78 
79  // Type specifiers
80  static const std::string Type_t = "Type";
81  static const std::string Test_t = "Test";
82  static const std::string Tests_t = "Tests";
83  static const std::string Data_t = "Data";
84  static const std::string URL_t = "URL";
85  static const std::string Name_t = "Name";
86  static const std::string Unit_t = "Unit";
87  static const std::string Values_t = "Values";
88  static const std::string Parameters_t = "Parameters";
89  static const std::string RunNumber_t = "RUN_NUMBER";
90 
91  // Calibration types
92  static const std::string PMTT0s_t = "PMTT0s";
93  static const std::string PMTSupplyVoltage_t = "PMT_Supply_Voltage";
94  static const std::string PMTThreshold_t = "PMT_Threshold";
95  static const std::string DOMPositions_t = "DOMPositions";
96  static const std::string DOMRotations_t = "DOMRotations";
97  static const std::string PMTGain_t = "PMT_Gain";
98 
99  // Identifiers
100  static const std::string UPI_t = "UPI";
101  static const std::string PBS_t = "PBS";
102  static const std::string Variant_t = "Variant";
103  static const std::string PMTSerial_t = "PMTSerial";
104  static const std::string DOMSerial_t = "DOMSerial";
105  static const std::string DOMId_t = "DOMId";
106 
107  // Variable names
108  static const std::string T0_t = "T0";
109  static const std::string PX_t = "PX";
110  static const std::string PY_t = "PY";
111  static const std::string PZ_t = "PZ";
112  static const std::string Q0_t = "Q0";
113  static const std::string QX_t = "QX";
114  static const std::string QY_t = "QY";
115  static const std::string QZ_t = "QZ";
116 
117  // Units
118  static const std::string Unitless_t = "-";
119  static const std::string Volt_t = "V";
120 
121  // Status
122  static const std::string Result_t = "Result";
123  static const std::string OK_t = "OK";
124  static const std::string Fail_t = "FAIL";
125 
126 
127  /**
128  * Check validity of JSon data.
129  *
130  * \param js JSon data
131  * \return true if valid; else false
132  */
133  inline bool is_valid(const json& js)
134  {
135  return js.contains(Result_t) && js[Result_t] == OK_t;
136  }
137 
138 
139  /**
140  * Cast single value to array of strings, conform the DB-format.
141  *
142  * \param value value
143  * \return std::vector containing value casted to string
144  */
145  template <class T>
147  {
148  using namespace std;
149 
150  return vector<string>{ to_string(value) };
151  }
152 
153 
154  /**
155  * Retrieve value from json array of strings.
156  *
157  * \param string_value_array array of string-casted values
158  * \return double-casted value
159  */
160  inline double retrieve_value(std::vector<std::string> string_value_array)
161  {
162  return std::stod(string_value_array[0]);
163  }
164 
165 
166  /**
167  * Auxiliary data structure for PMT time calibration.
168  */
170  public JObjectID,
171  public JCalibration
172  {
173  /**
174  * Default constructor.
175  */
177  {}
178 
179 
180  /**
181  * Constructor.
182  *
183  * \param id PMT identifier
184  * \param calibration PMT time calibration
185  */
187  const JCalibration& calibration) :
188  JObjectID (id),
189  JCalibration(calibration)
190  {}
191  };
192 
193 
194  /**
195  * Convert PMT time calibration to JSon.
196  *
197  * \param js json
198  * \param object PMT time calibration
199  */
200  inline void to_json(json& js, const JPMTCalibration_t& object)
201  {
202  js = json{ { PMTSerial_t, object.getID() },
203  { T0_t, object.getT0() } };
204  }
205 
206 
207  /**
208  * Convert JSon to PMT time calibration.
209  *
210  * \param js json
211  * \param object PMT time calibration
212  */
213  inline void from_json(const json& js, JPMTCalibration_t& object)
214  {
215  object.setID(js.at(PMTSerial_t).get<int>());
216 
217  object.setT0(js.at(T0_t).get<double>());
218  }
219 
220 
221  /**
222  * Data structure for PMT high-voltage calibration.
223  */
225  public JUPI_t
226  {
227  /**
228  * Default constructor.
229  */
231  JUPI_t (),
232  result (Fail_t),
233  supplyVoltage(0.0),
234  runNumbers (std::vector<int>(0)),
235  PMTgain (0.0)
236  {}
237 
238 
239  /**
240  * Constructor.
241  *
242  * \param upi %UPI
243  * \param result result
244  * \param hv HV
245  * \param runNumberList run numbers
246  * \param gain gain
247  */
249  const std::string& result,
250  const double hv,
251  const std::vector<int> runNumberList = std::vector<int>(0),
252  const double gain = 1.0) :
253  JUPI_t (upi),
254  result (result),
255  supplyVoltage(hv),
256  runNumbers (runNumberList),
257  PMTgain (gain)
258  {}
259 
260 
261  /**
262  * Get HV-tuning database test type.
263  *
264  * \return HV-tuning database test type
265  */
266  static int getVersion()
267  {
268  return get_version();
269  }
270 
271 
272  /**
273  * Set HV-tuning database test type.
274  *
275  * \param version HV-tuning database test type
276  */
277  static void setVersion(const int version)
278  {
279  get_version() = version;
280  }
281 
282 
283  std::string result;
286  double PMTgain;
287 
288 
289  private:
290 
291  /**
292  * Get reference to HV-tuning database test type.
293  *
294  * \return HV-tuning database test type
295  */
296  static int& get_version()
297  {
298  static int version = 3;
299 
300  return version;
301  }
302  };
303 
304 
305  /**
306  * Convert PMT high-voltage calibration to JSon.
307  *
308  * \param js json2
309  * \param object PMT high-voltage calibration
310  */
311  inline void to_json(json& js, const JHVCalibration_t& object)
312  {
313  using namespace std;
314 
315  ostringstream os;
316 
317  os << object.getUPI();
318 
319  js[UPI_t] = os.str();
320  js[Test_t + Result_t] = object.result;
322  { Unit_t, Volt_t },
323  { Values_t, get_string_array(object.supplyVoltage) } };
324 
325  static const int version = JHVCalibration_t::getVersion();
326 
327  if (version < 2) {
328 
329  js[Test_t + Parameters_t][2] = json{ { Name_t, PMTGain_t },
330  { Unit_t, Unitless_t },
331  { Values_t, get_string_array(object.PMTgain) } };
332  }
333 
334 
335  if (version < 3) {
336 
337  vector<string> runNumberList;
338 
339  for (vector<int>::const_iterator i = object.runNumbers.cbegin(); i != object.runNumbers.cend(); ++i) {
340  runNumberList.push_back(to_string(*i));
341  }
342 
343  js[Test_t + Parameters_t][1] = json{ { Name_t, RunNumber_t },
344  { Unit_t, Unitless_t },
345  { Values_t, runNumberList } };
346  }
347  }
348 
349 
350  /**
351  * Convert JSon to PMT high-voltage calibration.
352  *
353  * \param js json
354  * \param object PMT high-voltage calibration
355  */
356  inline void from_json(const json& js, JHVCalibration_t& object)
357  {
358  using namespace std;
359 
360  stringstream is(js.at(UPI_t).get<string>());
361 
362  is >> static_cast<JUPI_t&>(object);
363 
364  json parameters = js.at(Test_t + Parameters_t);
365 
366  if (JHVCalibration_t::getVersion() > 0 && parameters.size() > 0) {
367 
368  object.result = js.at(Test_t + Result_t).get<string>();
369  object.supplyVoltage = retrieve_value(parameters[0].at(Values_t).get<vector<string>>());
370 
371  if (JHVCalibration_t::getVersion() < 3 && parameters.size() > 1) {
372 
373  vector<string> runNumberList = parameters[1].at(Values_t).get<vector<string>>();
374 
375  for (vector<string>::const_iterator i = runNumberList.begin(); i != runNumberList.end(); ++i) {
376  object.runNumbers.push_back(stoi(*i));
377  }
378  }
379 
380  if (JHVCalibration_t::getVersion() < 2 && parameters.size() > 2) {
381  object.PMTgain = retrieve_value(parameters[2].at(Values_t).get<vector<string>>());
382  }
383 
384  } else {
385 
386  THROW(JValueOutOfRange, "JHVCalibration_t::from_json(): No " << MAKE_STRING(Test_t + Parameters_t) << " found." << endl);
387  }
388  }
389 
390 
391  /**
392  * Data structure for PMT threshold calibration.
393  */
395  public JUPI_t
396  {
397  /**
398  * Default constructor.
399  */
401  {}
402 
403  /**
404  * Constructor.
405  *
406  * \param upi %UPI
407  * \param result result
408  * \param threshold threshold
409  * \param runNumbers run numbers
410  */
412  const std::string& result,
413  const double threshold,
414  const std::vector<int>& runNumbers = std::vector<int>(0)) :
415  JUPI_t (upi),
416  result (result),
417  threshold (threshold),
418  runNumberList(runNumbers)
419  {}
420 
421  std::string result;
422  double threshold;
424  };
425 
426 
427  /**
428  * Convert PMT threshold calibration to JSon.
429  *
430  * \param js json
431  * \param object PMT threshold calibration
432  */
433  inline void to_json(json& js, const JPMTThresholdCalibration_t& object)
434  {
435  std::ostringstream os;
436 
437  os << object.getUPI();
438 
439  js[UPI_t] = os.str();
440  js[Test_t + Result_t] = object.result;
441  js[Test_t + Parameters_t][0] = json{ { Name_t, PMTThreshold_t },
442  { Unit_t, Unitless_t },
443  { Values_t, get_string_array(object.threshold) } };
444  js[Test_t + Parameters_t][2] = json{ { Name_t, RunNumber_t },
445  { Unit_t, Unitless_t },
446  { Values_t, object.runNumberList } };
447  }
448 
449 
450  /**
451  * Convert JSon to PMT threshold calibration.
452  *
453  * \param js json
454  * \param object PMT threshold calibration
455  */
456  inline void from_json(const json& js, JPMTThresholdCalibration_t& object)
457  {
458  using namespace std;
459 
460  stringstream is(js.at(UPI_t).get<string>());
461 
462  is >> static_cast<JUPI_t&>(object);
463 
464  object.result = js.at(Test_t + Result_t).get<string>();
465  object.threshold = retrieve_value(js.at(Test_t + Parameters_t)[0].at(Values_t).get<vector<string>>());
466  object.runNumberList = js.at(Test_t + Parameters_t)[2].at(Values_t).get<vector<int>>();
467  }
468 
469 
470  /**
471  * Auxiliary data structure for module position.
472  */
474  public JObjectID,
475  public JPosition3D
476  {
477  /**
478  * Default constructor.
479  */
481  {}
482 
483 
484  /**
485  * Constructor.
486  *
487  * \param id module identifier
488  * \param position module position
489  */
491  const JPosition3D& position) :
492  JObjectID (id),
493  JPosition3D(position)
494  {}
495  };
496 
497 
498  /**
499  * Convert module position to JSon.
500  *
501  * \param js json
502  * \param object module position
503  */
504  inline void to_json(json& js, const JModulePosition_t& object)
505  {
506  js = json{ { DOMId_t, object.getID() },
507  { PX_t, object.getX() },
508  { PY_t, object.getY() },
509  { PZ_t, object.getZ() } };
510  }
511 
512 
513  /**
514  * Convert JSon to module position.
515  *
516  * \param js json
517  * \param object module position
518  */
519  inline void from_json(const json& js, JModulePosition_t& object)
520  {
521  object.setID(js.at(DOMId_t).get<int>());
522 
523  object.setPosition(JPosition3D(js.at(PX_t).get<double>(),
524  js.at(PY_t).get<double>(),
525  js.at(PZ_t).get<double>()));
526  }
527 
528  /**
529  * Auxiliary data structure for module rotation.
530  */
532  public JObjectID,
533  public JQuaternion3D
534  {
535  /**
536  * Default constructor.
537  */
539  {}
540 
541 
542  /**
543  * Constructor.
544  *
545  * \param id module identifier
546  * \param rotation module rotation
547  */
549  const JQuaternion3D& rotation) :
550  JObjectID (id),
551  JQuaternion3D(rotation)
552  {}
553  };
554 
555 
556  /**
557  * Convert module rotation to JSon.
558  *
559  * \param js json
560  * \param object module rotation
561  */
562  inline void to_json(json& js, const JModuleRotation_t& object)
563  {
564  js = json{ { DOMId_t, object.getID() },
565  { Q0_t, object.getA() },
566  { QX_t, object.getB() },
567  { QY_t, object.getC() },
568  { QZ_t, object.getD() } };
569  }
570 
571 
572  /**
573  * Convert JSon to module rotation.
574  *
575  * \param js json
576  * \param object module rotation
577  */
578  inline void from_json(const json& js, JModuleRotation_t& object)
579  {
580  object.setID(js.at(DOMId_t).get<int>());
581 
582  object.setQuaternion(JQuaternion3D(js.at(Q0_t).get<double>(),
583  js.at(QX_t).get<double>(),
584  js.at(QY_t).get<double>(),
585  js.at(QZ_t).get<double>()));
586  }
587 
588 
589  typedef std::vector<JHVCalibration_t> JHVCalibration; //!< PMT high voltage calibration
591  typedef std::vector<JPMTCalibration_t> JPMTCalibration; //!< PMT time calibration
592  typedef std::vector<JModulePosition_t> JModulePosition; //!< Module position
593  typedef std::vector<JModuleRotation_t> JModuleRotation; //!< Module rotation
594 }
595 
596 #endif
static const std::string DOMSerial_t
static const std::string UUID_t
Exceptions.
static const std::string PMTGain_t
static const std::string RCAL
(optical|base) module orientations
void to_json(json &js, const JPMTCalibration_t &object)
Convert PMT time calibration to JSon.
static const std::string PZ_t
Data structure for PMT threshold calibration.
static const std::string Name_t
static void setVersion(const int version)
Set HV-tuning database test type.
static const std::string PMTSupplyVoltage_t
static const std::string Q0_t
JPMTCalibration_t()
Default constructor.
Time calibration (including definition of sign of time offset).
static const std::string Unit_t
static const std::string Time_t
static const std::string UPI_t
static const std::string Tests_t
static const std::string DOMPositions_t
static const std::string OK_t
JModuleRotation_t(const JObjectID &id, const JQuaternion3D &rotation)
Constructor.
static const std::string DOMId_t
JHVCalibration_t()
Default constructor.
std::vector< JPMTThresholdCalibration_t > JPMTThresholdCalibration
PMT threshold calibration.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
std::vector< JModuleRotation_t > JModuleRotation
Module rotation.
static const std::string TCAL
PMT time offsets.
static const std::string QZ_t
static const std::string Variant_t
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
static const std::string PX_t
static const std::string Location_t
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
static const std::string CCAL
compass alignment (a.k.a. quaternion calibration)
Data structure for time calibration.
static int & get_version()
Get reference to HV-tuning database test type.
double retrieve_value(std::vector< std::string > string_value_array)
Retrieve value from json array of strings.
JPMTCalibration_t(const JObjectID &id, const JCalibration &calibration)
Constructor.
is
Definition: JDAQCHSM.chsm:167
static const std::string Encoding_t
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
static const std::string Inputs_t
static const std::string Provenance_t
static const std::string Result_t
static const std::string Arguments_t
static const std::string Volt_t
static const std::string Configuration_t
I/O formatting auxiliaries.
void from_json(const json &js, JPMTCalibration_t &object)
Convert JSon to PMT time calibration.
static const std::string T0_t
static const std::string SCAL
PMT status.
std::vector< JHVCalibration_t > JHVCalibration
PMT high voltage calibration.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
static const std::string DOMRotations_t
static const std::string PCAL
(optical|base) module positions
bool is_valid(const json &js)
Check validity of JSon data.
static const std::string ACAL
acoustic time offsets (piezo sensor or hydrophone)
static const std::string PMTThreshold_t
static const std::string Code_t
static const std::string Test_t
static const std::string Info_t
static const std::string PMTSerial_t
static const std::string Message_t
JPMTThresholdCalibration_t(const JUPI_t &upi, const std::string &result, const double threshold, const std::vector< int > &runNumbers=std::vector< int >(0))
Constructor.
static const std::string RunNumber_t
JModulePosition_t(const JObjectID &id, const JPosition3D &position)
Constructor.
static const std::string Comment_t
Data structure for unit quaternion in three dimensions.
static const std::string Values_t
static const std::string PY_t
then $JPP_DIR examples JDetector JToT o $OUTPUT_FILE n N $NPE P gain
Definition: JToT.sh:47
JModulePosition_t()
Default constructor.
JHVCalibration_t(const JUPI_t &upi, const std::string &result, const double hv, const std::vector< int > runNumberList=std::vector< int >(0), const double gain=1.0)
Constructor.
static const std::string PBS_t
Auxiliary class for object identification.
Definition: JObjectID.hh:22
Data structure for PMT high-voltage calibration.
nlohmann::json json
static const std::string Type_t
std::string to_string(const T &value)
Convert value to string.
static const std::string APIVersion_t
Auxiliary data structure for module rotation.
JModuleRotation_t()
Default constructor.
Product breakdown structure (PBS).
Definition: JPBS_t.hh:27
static const std::string Input_t
static const std::string User_t
Detector calibration key words for JSON I/O.
Auxiliary data structure for module position.
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
static const std::string PMTT0s_t
std::vector< JModulePosition_t > JModulePosition
Module position.
static const std::string Error_t
static const std::string URL_t
static const std::string Parameters_t
static const std::string Data_t
static const std::string End_t
static const std::string Start_t
Auxiliary data structure for PMT time calibration.
static const std::string QY_t
static int getVersion()
Get HV-tuning database test type.
version
Definition: JCalibratePMT.sh:7
static const std::string Unitless_t
std::vector< JPMTCalibration_t > JPMTCalibration
PMT time calibration.
std::vector< std::string > get_string_array(T value)
Cast single value to array of strings, conform the DB-format.
static const std::string QX_t
static const std::string Fail_t