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