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